1
|
|
|
package unicon.matthews.oneroster.endpoint; |
2
|
|
|
|
3
|
|
|
import java.time.LocalDateTime; |
4
|
|
|
import java.time.ZoneId; |
5
|
|
|
import java.util.Collection; |
6
|
|
|
|
7
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
8
|
|
|
import org.springframework.http.HttpHeaders; |
9
|
|
|
import org.springframework.http.HttpStatus; |
10
|
|
|
import org.springframework.http.ResponseEntity; |
11
|
|
|
import org.springframework.web.bind.annotation.PathVariable; |
12
|
|
|
import org.springframework.web.bind.annotation.RequestBody; |
13
|
|
|
import org.springframework.web.bind.annotation.RequestMapping; |
14
|
|
|
import org.springframework.web.bind.annotation.RequestMethod; |
15
|
|
|
import org.springframework.web.bind.annotation.RequestParam; |
16
|
|
|
import org.springframework.web.bind.annotation.RestController; |
17
|
|
|
import org.springframework.web.servlet.support.ServletUriComponentsBuilder; |
18
|
|
|
|
19
|
|
|
import unicon.matthews.caliper.ClassEventStatistics; |
20
|
|
|
import unicon.matthews.caliper.Event; |
21
|
|
|
import unicon.matthews.caliper.service.EventService; |
22
|
|
|
import unicon.matthews.entity.ClassMapping; |
23
|
|
|
import unicon.matthews.entity.MongoClassMappingRepository; |
24
|
|
|
import unicon.matthews.oneroster.Class; |
25
|
|
|
import unicon.matthews.oneroster.Enrollment; |
26
|
|
|
import unicon.matthews.oneroster.LineItem; |
27
|
|
|
import unicon.matthews.oneroster.Result; |
28
|
|
|
import unicon.matthews.oneroster.exception.EnrollmentNotFoundException; |
29
|
|
|
import unicon.matthews.oneroster.exception.LineItemNotFoundException; |
30
|
|
|
import unicon.matthews.oneroster.exception.ResultNotFoundException; |
31
|
|
|
import unicon.matthews.oneroster.service.ClassService; |
32
|
|
|
import unicon.matthews.oneroster.service.EnrollmentService; |
33
|
|
|
import unicon.matthews.oneroster.service.LineItemService; |
34
|
|
|
import unicon.matthews.oneroster.service.ResultService; |
35
|
|
|
import unicon.matthews.oneroster.service.repository.MongoClass; |
36
|
|
|
import unicon.matthews.security.auth.JwtAuthenticationToken; |
37
|
|
|
import unicon.matthews.security.model.UserContext; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @author ggilbert |
41
|
|
|
* @author xchopin <[email protected]> |
42
|
|
|
*/ |
43
|
|
|
@RestController |
44
|
|
|
@RequestMapping("/api/classes") |
45
|
|
|
public class ClassController { |
46
|
|
|
|
47
|
|
|
private LineItemService lineItemService; |
48
|
|
|
private EnrollmentService enrollmentService; |
49
|
|
|
private EventService eventService; |
50
|
|
|
private ClassService classService; |
51
|
|
|
private ResultService resultService; |
52
|
|
|
private MongoClassMappingRepository mongoClassMappingRepository; |
53
|
|
|
|
54
|
|
|
@Autowired |
55
|
|
|
public ClassController(LineItemService lineItemService, |
56
|
|
|
EnrollmentService enrollmentService, |
57
|
|
|
EventService eventService, |
58
|
|
|
ClassService classService, |
59
|
|
|
ResultService resultService, |
60
|
|
|
MongoClassMappingRepository mongoClassMappingRepository) { |
61
|
|
|
this.lineItemService = lineItemService; |
62
|
|
|
this.enrollmentService = enrollmentService; |
63
|
|
|
this.eventService = eventService; |
64
|
|
|
this.classService = classService; |
65
|
|
|
this.resultService = resultService; |
66
|
|
|
this.mongoClassMappingRepository = mongoClassMappingRepository; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
@RequestMapping(value = "/{classId:.+}", method = RequestMethod.GET) |
70
|
|
|
public Class getClass(JwtAuthenticationToken token, @PathVariable final String classId) throws LineItemNotFoundException { |
71
|
|
|
UserContext userContext = (UserContext) token.getPrincipal(); |
72
|
|
|
return classService.findBySourcedId(userContext.getTenantId(), userContext.getOrgId(), classId); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
@RequestMapping(value = "/{classId:.+}/events/stats", method = RequestMethod.GET) |
76
|
|
|
public ClassEventStatistics getEventStatisticsForClass(JwtAuthenticationToken token, @PathVariable final String classId, |
77
|
|
|
@RequestParam(name="studentsOnly",required=false,defaultValue="true") String studentsOnly) { |
78
|
|
|
UserContext userContext = (UserContext) token.getPrincipal(); |
79
|
|
|
|
80
|
|
|
return eventService.getEventStatisticsForClass(userContext.getTenantId(), userContext.getOrgId(), classId, Boolean.valueOf(studentsOnly)); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
@RequestMapping(value = "/{classId}/events/user/{userId:.+}", method = RequestMethod.GET) |
84
|
|
|
public Collection<Event> getEventForClassAndUser(JwtAuthenticationToken token, @PathVariable final String classId, @PathVariable final String userId) { |
85
|
|
|
UserContext userContext = (UserContext) token.getPrincipal(); |
86
|
|
|
return eventService.getEventsForClassAndUser(userContext.getTenantId(), userContext.getOrgId(), classId, userId); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
@RequestMapping(value = "/{classId}/results/user/{userId}", method = RequestMethod.GET) |
90
|
|
|
public Collection<Result> getResultsForClassAndUser(JwtAuthenticationToken token, @PathVariable final String classId, @PathVariable final String userId) { |
91
|
|
|
UserContext userContext = (UserContext) token.getPrincipal(); |
92
|
|
|
return resultService.getResultsForClassAndUser(userContext.getTenantId(), userContext.getOrgId(), classId, userId); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
@RequestMapping(value = "/{classId}/lineitems", method = RequestMethod.GET) |
96
|
|
|
public Collection<LineItem> getLineItemsForClass(JwtAuthenticationToken token, @PathVariable final String classId) throws LineItemNotFoundException { |
97
|
|
|
UserContext userContext = (UserContext) token.getPrincipal(); |
98
|
|
|
return lineItemService.getLineItemsForClass(userContext.getTenantId(), userContext.getOrgId(), classId); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** Retrieves the result of lineitem of class |
102
|
|
|
* @param token |
103
|
|
|
* @param lineitemId |
104
|
|
|
* @return Result |
105
|
|
|
* @throws ResultNotFoundException |
106
|
|
|
*/ |
107
|
|
|
@RequestMapping(value = "/{classId:.+}/lineitems/{lineitemId}/results", method = RequestMethod.GET) |
108
|
|
|
public Result getLineItemsResults(JwtAuthenticationToken token, @PathVariable final String lineitemId) throws ResultNotFoundException { |
109
|
|
|
UserContext userContext = (UserContext) token.getPrincipal(); |
110
|
|
|
return resultService.getResultsForlineItem(userContext.getTenantId(), userContext.getOrgId(), lineitemId); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
View Code Duplication |
@RequestMapping(value= "/{classId:.+}/lineitems", method = RequestMethod.POST) |
|
|
|
|
114
|
|
|
public ResponseEntity<?> postLineItem(JwtAuthenticationToken token, @RequestBody LineItem lineItem, @RequestParam(value="check", required=false) Boolean check) { |
|
|
|
|
115
|
|
|
UserContext userContext = (UserContext) token.getPrincipal(); |
116
|
|
|
LineItem savedLineItem = this.lineItemService.save(userContext.getTenantId(), userContext.getOrgId(), lineItem, (check == null) ? true : check); |
117
|
|
|
HttpHeaders httpHeaders = new HttpHeaders(); |
118
|
|
|
httpHeaders.setLocation(ServletUriComponentsBuilder |
119
|
|
|
.fromCurrentRequest().path("/{id}") |
120
|
|
|
.buildAndExpand(savedLineItem.getSourcedId()).toUri()); |
121
|
|
|
return new ResponseEntity<>(savedLineItem, httpHeaders, HttpStatus.CREATED); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
@RequestMapping(value = "/{classId:.+}/results", method = RequestMethod.GET) |
125
|
|
|
public Collection<Result> getResultsForClass(JwtAuthenticationToken token, @PathVariable final String classId) throws LineItemNotFoundException, ResultNotFoundException { |
126
|
|
|
UserContext userContext = (UserContext) token.getPrincipal(); |
127
|
|
|
return resultService.getResultsForClass(userContext.getTenantId(), userContext.getOrgId(), classId); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
View Code Duplication |
@RequestMapping(value= "/{classId:.+}/results", method = RequestMethod.POST) |
|
|
|
|
131
|
|
|
public ResponseEntity<?> postResult(JwtAuthenticationToken token, @PathVariable final String classId, @RequestBody Result result, @RequestParam(value="check", required=false) Boolean check) { |
|
|
|
|
132
|
|
|
UserContext userContext = (UserContext) token.getPrincipal(); |
133
|
|
|
Result savedResult = this.resultService.save(userContext.getTenantId(), userContext.getOrgId(), classId, result, (check == null) ? true : check); |
134
|
|
|
HttpHeaders httpHeaders = new HttpHeaders(); |
135
|
|
|
httpHeaders.setLocation(ServletUriComponentsBuilder.fromCurrentRequest() |
136
|
|
|
.path("/{id}") |
137
|
|
|
.buildAndExpand(savedResult.getSourcedId()) |
138
|
|
|
.toUri()); |
139
|
|
|
|
140
|
|
|
return new ResponseEntity<>(savedResult, httpHeaders, HttpStatus.CREATED); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
View Code Duplication |
@RequestMapping(value= "/{classId:.+}/enrollments", method = RequestMethod.POST) |
|
|
|
|
144
|
|
|
public ResponseEntity<?> postEnrollment(JwtAuthenticationToken token, @PathVariable final String classId, @RequestBody Enrollment enrollment, @RequestParam(value="check", required=false) Boolean check) { |
|
|
|
|
145
|
|
|
UserContext userContext = (UserContext) token.getPrincipal(); |
146
|
|
|
Enrollment savedEnrollment = enrollmentService.save(userContext.getTenantId(), userContext.getOrgId(), classId, enrollment, (check == null) ? true : check); |
147
|
|
|
HttpHeaders httpHeaders = new HttpHeaders(); |
148
|
|
|
httpHeaders.setLocation(ServletUriComponentsBuilder |
149
|
|
|
.fromCurrentRequest().path("/{id}") |
150
|
|
|
.buildAndExpand(savedEnrollment.getSourcedId()).toUri()); |
151
|
|
|
return new ResponseEntity<>(savedEnrollment, httpHeaders, HttpStatus.CREATED); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
@RequestMapping(value = "/{classId:.+}/enrollments", method = RequestMethod.GET) |
155
|
|
|
public Collection<Enrollment> getEnrollmentsForClass(JwtAuthenticationToken token, @PathVariable final String classId) throws EnrollmentNotFoundException { |
156
|
|
|
UserContext userContext = (UserContext) token.getPrincipal(); |
157
|
|
|
return enrollmentService.findEnrollmentsForClass(userContext.getTenantId(), userContext.getOrgId(), classId); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
@RequestMapping(value = "/mapping/{externalClassId}", method = RequestMethod.GET) |
161
|
|
|
public ClassMapping getClassMapping(JwtAuthenticationToken token, @PathVariable("externalClassId") final String externalClassId) { |
162
|
|
|
UserContext userContext = (UserContext) token.getPrincipal(); |
163
|
|
|
return mongoClassMappingRepository.findByTenantIdAndOrganizationIdAndClassExternalId(userContext.getTenantId(), userContext.getOrgId(), externalClassId); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
@RequestMapping(value= "/mapping", method = RequestMethod.POST) |
167
|
|
|
public ResponseEntity<?> postClassMapping(JwtAuthenticationToken token, @RequestBody ClassMapping cm) { |
|
|
|
|
168
|
|
|
UserContext userContext = (UserContext) token.getPrincipal(); |
169
|
|
|
|
170
|
|
|
ClassMapping classMapping = null; |
171
|
|
|
|
172
|
|
|
ClassMapping existingClassMapping = mongoClassMappingRepository |
173
|
|
|
.findByTenantIdAndOrganizationIdAndClassExternalId(userContext.getTenantId(), userContext.getOrgId(), cm.getClassExternalId()); |
174
|
|
|
|
175
|
|
|
if (existingClassMapping == null) { |
176
|
|
|
classMapping |
177
|
|
|
= new ClassMapping.Builder() |
178
|
|
|
.withClassExternalId(cm.getClassExternalId()) |
179
|
|
|
.withClassSourcedId(cm.getClassSourcedId()) |
180
|
|
|
.withDateLastModified(LocalDateTime.now(ZoneId.of("UTC"))) |
181
|
|
|
.withOrganizationId(userContext.getOrgId()) |
182
|
|
|
.withTenantId(userContext.getTenantId()) |
183
|
|
|
.build(); |
184
|
|
|
|
185
|
|
|
ClassMapping saved = mongoClassMappingRepository.save(classMapping); |
186
|
|
|
|
187
|
|
|
return new ResponseEntity<>(saved, null, HttpStatus.CREATED); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
return new ResponseEntity<>(existingClassMapping, null, HttpStatus.NOT_MODIFIED); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
@RequestMapping(method = RequestMethod.POST) |
194
|
|
|
public ResponseEntity<?> postClass(JwtAuthenticationToken token, @RequestBody Class klass) { |
|
|
|
|
195
|
|
|
UserContext userContext = (UserContext) token.getPrincipal(); |
196
|
|
|
Class saved = classService.save(userContext.getTenantId(), userContext.getOrgId(), klass); |
197
|
|
|
HttpHeaders httpHeaders = new HttpHeaders(); |
198
|
|
|
httpHeaders.setLocation(ServletUriComponentsBuilder |
199
|
|
|
.fromCurrentRequest().path("/{id}") |
200
|
|
|
.buildAndExpand(saved.getSourcedId()).toUri()); |
201
|
|
|
return new ResponseEntity<>(saved, httpHeaders, HttpStatus.CREATED); |
202
|
|
|
|
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
@RequestMapping(method = RequestMethod.GET) |
206
|
|
|
public Collection<MongoClass> getClass(JwtAuthenticationToken token) { |
207
|
|
|
UserContext userContext = (UserContext) token.getPrincipal(); |
208
|
|
|
return classService.findAll(userContext.getTenantId(), userContext.getOrgId()); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
//@RequestMapping(value= "/{classId}", method = RequestMethod.PUT) |
212
|
|
|
|
213
|
|
|
public ResponseEntity<?> putClass(JwtAuthenticationToken token, @PathVariable("classId") final String classId, @RequestBody Class klass) { |
|
|
|
|
214
|
|
|
UserContext userContext = (UserContext) token.getPrincipal(); |
|
|
|
|
215
|
|
|
return null; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
} |
219
|
|
|
|