1
- using System ;
2
- using System . Collections . Generic ;
3
- using System . Linq ;
4
- using Microsoft . AspNetCore . Mvc ;
1
+ using Microsoft . AspNetCore . Mvc ;
5
2
using SchedulerApp . Models ;
6
3
7
4
namespace SchedulerApp . Controllers
@@ -28,9 +25,9 @@ public IEnumerable<WebAPIRecurring> Get([FromQuery] DateTime from, [FromQuery] D
28
25
29
26
// GET api/recurringevents/5
30
27
[ HttpGet ( "{id}" ) ]
31
- public WebAPIRecurring Get ( int id )
28
+ public SchedulerRecurringEvent ? Get ( int id )
32
29
{
33
- return ( WebAPIRecurring ) _context
30
+ return _context
34
31
. RecurringEvents
35
32
. Find ( id ) ;
36
33
}
@@ -46,7 +43,7 @@ public ObjectResult Post([FromForm] WebAPIRecurring apiEvent)
46
43
47
44
// delete a single occurrence from recurring series
48
45
var resultAction = "inserted" ;
49
- if ( newEvent . RecType == "none" )
46
+ if ( newEvent . RecType == "none" )
50
47
{
51
48
resultAction = "deleted" ;
52
49
}
@@ -63,13 +60,19 @@ public ObjectResult Post([FromForm] WebAPIRecurring apiEvent)
63
60
public ObjectResult Put ( int id , [ FromForm ] WebAPIRecurring apiEvent )
64
61
{
65
62
var updatedEvent = ( SchedulerRecurringEvent ) apiEvent ;
66
- var dbEveht = _context . RecurringEvents . Find ( id ) ;
67
- dbEveht . Name = updatedEvent . Name ;
68
- dbEveht . StartDate = updatedEvent . StartDate ;
69
- dbEveht . EndDate = updatedEvent . EndDate ;
70
- dbEveht . EventPID = updatedEvent . EventPID ;
71
- dbEveht . RecType = updatedEvent . RecType ;
72
- dbEveht . EventLength = updatedEvent . EventLength ;
63
+ var dbEvent = _context . RecurringEvents . Find ( id ) ;
64
+
65
+ if ( dbEvent == null )
66
+ {
67
+ return null ;
68
+ }
69
+
70
+ dbEvent . Name = updatedEvent . Name ;
71
+ dbEvent . StartDate = updatedEvent . StartDate ;
72
+ dbEvent . EndDate = updatedEvent . EndDate ;
73
+ dbEvent . EventPID = updatedEvent . EventPID ;
74
+ dbEvent . RecType = updatedEvent . RecType ;
75
+ dbEvent . EventLength = updatedEvent . EventLength ;
73
76
74
77
if ( ! string . IsNullOrEmpty ( updatedEvent . RecType ) && updatedEvent . RecType != "none" )
75
78
{
@@ -123,15 +126,15 @@ public ObjectResult DeleteEvent(int id)
123
126
_context . RecurringEvents . Remove ( e ) ;
124
127
}
125
128
126
-
129
+
127
130
_context . SaveChanges ( ) ;
128
131
}
129
-
132
+
130
133
return Ok ( new
131
134
{
132
135
action = "deleted"
133
136
} ) ;
134
137
}
135
-
138
+
136
139
}
137
140
}
0 commit comments