@@ -16,6 +16,7 @@ namespace QCVOC.Api.Scans.Controller
16
16
using QCVOC . Api . Events . Data . Model ;
17
17
using QCVOC . Api . Scans . Data . DTO ;
18
18
using QCVOC . Api . Scans . Data . Model ;
19
+ using QCVOC . Api . Services . Data . Model ;
19
20
using QCVOC . Api . Veterans ;
20
21
using QCVOC . Api . Veterans . Data . Model ;
21
22
@@ -34,16 +35,19 @@ public class ScansController : Controller
34
35
/// <param name="scanRepository">The repository used for Scan data access.</param>
35
36
/// <param name="eventRepository">The repository used for Event data access.</param>
36
37
/// <param name="veteranRepository">The repository used for Veteran data access.</param>
37
- public ScansController ( ITripleKeyRepository < Scan > scanRepository , ISingleKeyRepository < Event > eventRepository , ISingleKeyRepository < Veteran > veteranRepository )
38
+ /// <param name="serviceRepository">The repository used for Service data access.</param>
39
+ public ScansController ( ITripleKeyRepository < Scan > scanRepository , ISingleKeyRepository < Event > eventRepository , ISingleKeyRepository < Veteran > veteranRepository , ISingleKeyRepository < Service > serviceRepository )
38
40
{
39
41
ScanRepository = scanRepository ;
40
42
EventRepository = eventRepository ;
41
43
VeteranRepository = veteranRepository ;
44
+ ServiceRepository = serviceRepository ;
42
45
}
43
46
44
47
private ITripleKeyRepository < Scan > ScanRepository { get ; set ; }
45
48
private ISingleKeyRepository < Event > EventRepository { get ; }
46
49
private ISingleKeyRepository < Veteran > VeteranRepository { get ; set ; }
50
+ private ISingleKeyRepository < Service > ServiceRepository { get ; set ; }
47
51
48
52
/// <summary>
49
53
/// Returns a list of Scans.
@@ -68,12 +72,12 @@ public IActionResult GetAll([FromQuery]ScanFilters filters)
68
72
/// </summary>
69
73
/// <param name="scan">The scan context.</param>
70
74
/// <returns>See attributes.</returns>
71
- /// <response code="200">The Veteran is already checked in .</response>
72
- /// <response code="201">The Veteran was checked in .</response>
75
+ /// <response code="200">The Scan has already been recorded .</response>
76
+ /// <response code="201">The Scan was recorded or updated .</response>
73
77
/// <response code="400">The specified Scan was invalid.</response>
74
78
/// <response code="401">Unauthorized.</response>
75
79
/// <response code="403">The Veteran has not checked in for the Event.</response>
76
- /// <response code="404">Either the specified Veteran or Event was invalid.</response>
80
+ /// <response code="404">The specified Veteran, Event or Service was invalid.</response>
77
81
/// <response code="500">The server encountered an error while processing the request.</response>
78
82
[ HttpPost ( "" ) ]
79
83
[ Authorize ]
@@ -95,7 +99,7 @@ public IActionResult Scan([FromBody]ScanRequest scan)
95
99
96
100
if ( @event == default ( Event ) )
97
101
{
98
- StatusCode ( 403 , "The specified Event is not found." ) ;
102
+ StatusCode ( 404 , "The specified Event could not be found." ) ;
99
103
}
100
104
101
105
var veteran = VeteranRepository
@@ -104,12 +108,47 @@ public IActionResult Scan([FromBody]ScanRequest scan)
104
108
105
109
if ( veteran == default ( Veteran ) )
106
110
{
107
- return StatusCode ( 403 , "The specified Card Id doesn't match an enrolled Veteran." ) ;
111
+ return StatusCode ( 404 , "The specified Card Id doesn't match an enrolled Veteran." ) ;
108
112
}
109
113
114
+ var scanRecord = new Scan ( )
115
+ {
116
+ EventId = ( Guid ) scan . EventId ,
117
+ VeteranId = veteran . Id ,
118
+ ServiceId = scan . ServiceId ,
119
+ PlusOne = scan . PlusOne ,
120
+ ScanById = User . GetId ( ) ,
121
+ ScanDate = DateTime . UtcNow ,
122
+ } ;
123
+
110
124
var previousScans = ScanRepository . GetAll ( new ScanFilters ( ) { EventId = scan . EventId , VeteranId = veteran . Id } ) ;
111
125
112
- if ( scan . ServiceId == Guid . Empty && ! previousScans . Where ( s => s . ServiceId == Guid . Empty ) . Any ( ) )
126
+ if ( scan . ServiceId == Guid . Empty )
127
+ {
128
+ var existingCheckIn = previousScans . Where ( s => s . ServiceId == Guid . Empty ) . SingleOrDefault ( ) ;
129
+
130
+ if ( existingCheckIn == default ( Scan ) )
131
+ {
132
+ return CreateScan ( scanRecord ) ;
133
+ }
134
+ else if ( existingCheckIn . PlusOne != scan . PlusOne )
135
+ {
136
+ return UpdateScan ( scanRecord ) ;
137
+ }
138
+ else
139
+ {
140
+ return StatusCode ( 200 , existingCheckIn ) ;
141
+ }
142
+ }
143
+
144
+ var service = ServiceRepository . Get ( scan . ServiceId ) ;
145
+
146
+ if ( service == default ( Service ) )
147
+ {
148
+ return StatusCode ( 404 , "The specified Service could not be found." ) ;
149
+ }
150
+
151
+ if ( ! previousScans . Where ( s => s . ServiceId == Guid . Empty ) . Any ( ) )
113
152
{
114
153
return StatusCode ( 403 , "The Veteran has not checked in for this Event." ) ;
115
154
}
@@ -119,25 +158,33 @@ public IActionResult Scan([FromBody]ScanRequest scan)
119
158
return StatusCode ( 200 , previousScans . Where ( s => s . ServiceId == scan . ServiceId ) . SingleOrDefault ( ) ) ;
120
159
}
121
160
122
- var scanRecord = new Scan ( )
123
- {
124
- EventId = ( Guid ) scan . EventId ,
125
- VeteranId = veteran . Id ,
126
- ServiceId = scan . ServiceId ,
127
- PlusOne = scan . PlusOne ,
128
- ScanById = User . GetId ( ) ,
129
- ScanDate = DateTime . UtcNow ,
130
- } ;
161
+ return CreateScan ( scanRecord ) ;
162
+ }
131
163
164
+ private IActionResult CreateScan ( Scan scan )
165
+ {
132
166
try
133
167
{
134
- var createdScan = ScanRepository . Create ( scanRecord ) ;
168
+ var createdScan = ScanRepository . Create ( scan ) ;
135
169
return StatusCode ( 201 , createdScan ) ;
136
170
}
137
171
catch ( Exception ex )
138
172
{
139
173
throw new Exception ( $ "Error processing Scan: { ex . Message } . See inner Exception for details.", ex ) ;
140
174
}
141
175
}
176
+
177
+ private IActionResult UpdateScan ( Scan scan )
178
+ {
179
+ try
180
+ {
181
+ var updatedScan = ScanRepository . Update ( scan ) ;
182
+ return StatusCode ( 201 , updatedScan ) ;
183
+ }
184
+ catch ( Exception ex )
185
+ {
186
+ throw new Exception ( $ "Error processing Scan: { ex . Message } . See inner Exception for details.", ex ) ;
187
+ }
188
+ }
142
189
}
143
190
}
0 commit comments