|
1 | 1 | package com.savvato.tribeapp.controllers;
|
2 | 2 |
|
3 | 3 | import com.savvato.tribeapp.controllers.annotations.controllers.AttributesAPIController.ApplyPhraseToUser;
|
| 4 | +import com.savvato.tribeapp.controllers.annotations.controllers.AttributesAPIController.DeletePhraseFromUser; |
4 | 5 | import com.savvato.tribeapp.controllers.annotations.controllers.AttributesAPIController.GetAttributesForUser;
|
| 6 | +import com.savvato.tribeapp.controllers.annotations.controllers.AttributesAPIController.GetUserPhrasesToBeReviewed; |
5 | 7 | import com.savvato.tribeapp.controllers.dto.AttributesRequest;
|
6 | 8 | import com.savvato.tribeapp.dto.AttributeDTO;
|
| 9 | +import com.savvato.tribeapp.dto.ToBeReviewedDTO; |
7 | 10 | import com.savvato.tribeapp.entities.NotificationType;
|
8 |
| -import com.savvato.tribeapp.services.AttributesService; |
9 |
| -import com.savvato.tribeapp.services.NotificationService; |
10 |
| -import com.savvato.tribeapp.services.PhraseService; |
| 11 | +import com.savvato.tribeapp.services.*; |
11 | 12 | import io.swagger.v3.oas.annotations.Parameter;
|
12 | 13 | import io.swagger.v3.oas.annotations.tags.Tag;
|
| 14 | +import java.util.List; |
| 15 | +import java.util.Optional; |
| 16 | +import javax.validation.Valid; |
13 | 17 | import org.springframework.beans.factory.annotation.Autowired;
|
14 | 18 | import org.springframework.http.HttpStatus;
|
15 | 19 | import org.springframework.http.ResponseEntity;
|
16 | 20 | import org.springframework.web.bind.annotation.*;
|
17 | 21 |
|
18 |
| -import javax.validation.Valid; |
19 |
| -import java.util.List; |
20 |
| -import java.util.Optional; |
21 |
| - |
22 | 22 | @RestController
|
23 | 23 | @RequestMapping("/api/attributes")
|
24 | 24 | @Tag(
|
25 |
| - name = "attributes", |
26 |
| - description = "Everything about attributes, e.g. \"plays chess competitively\"") |
| 25 | + name = "attributes", |
| 26 | + description = "Everything about attributes, e.g. \"plays chess competitively\"") |
27 | 27 | public class AttributesAPIController {
|
28 | 28 |
|
29 |
| - @Autowired |
30 |
| - AttributesService attributesService; |
| 29 | + @Autowired |
| 30 | + AttributesService attributesService; |
31 | 31 |
|
32 |
| - @Autowired |
33 |
| - PhraseService phraseService; |
| 32 | + @Autowired |
| 33 | + PhraseService phraseService; |
34 | 34 |
|
35 |
| - @Autowired |
36 |
| - NotificationService notificationService; |
| 35 | + @Autowired |
| 36 | + NotificationService notificationService; |
37 | 37 |
|
38 |
| - AttributesAPIController() { |
39 |
| - } |
| 38 | + @Autowired |
| 39 | + UserPhraseService userPhraseService; |
| 40 | + |
| 41 | + @Autowired |
| 42 | + ReviewSubmittingUserService reviewSubmittingUserService; |
| 43 | + |
| 44 | + AttributesAPIController() {} |
40 | 45 |
|
41 |
| - @GetAttributesForUser |
42 |
| - @GetMapping("/{userId}") |
43 |
| - public ResponseEntity<List<AttributeDTO>> getAttributesForUser( |
44 |
| - @Parameter(description = "User ID of user", example = "1") @PathVariable Long userId) { |
| 46 | + @GetAttributesForUser |
| 47 | + @GetMapping("/{userId}") |
| 48 | + public ResponseEntity<List<AttributeDTO>> getAttributesForUser( |
| 49 | + @Parameter(description = "User ID of user", example = "1") @PathVariable Long userId) { |
45 | 50 |
|
46 |
| - Optional<List<AttributeDTO>> opt = attributesService.getAttributesByUserId(userId); |
| 51 | + Optional<List<AttributeDTO>> opt = attributesService.getAttributesByUserId(userId); |
47 | 52 |
|
48 |
| - if (opt.isPresent()) return ResponseEntity.status(HttpStatus.OK).body(opt.get()); |
49 |
| - else return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null); |
| 53 | + if (opt.isPresent()) return ResponseEntity.status(HttpStatus.OK).body(opt.get()); |
| 54 | + else return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null); |
| 55 | + } |
| 56 | + |
| 57 | + @GetUserPhrasesToBeReviewed |
| 58 | + @GetMapping("/in-review/{userId}") |
| 59 | + public ResponseEntity<List<ToBeReviewedDTO>> getUserPhrasesToBeReviewed( |
| 60 | + @Parameter(description = "User ID of user", example = "1") |
| 61 | + @PathVariable Long userId) { |
| 62 | + List<ToBeReviewedDTO> rtn = reviewSubmittingUserService.getUserPhrasesToBeReviewed(userId); |
| 63 | + return ResponseEntity.status(HttpStatus.OK).body(rtn); |
50 | 64 | }
|
51 | 65 |
|
52 |
| - @ApplyPhraseToUser |
53 |
| - @PostMapping |
54 |
| - public ResponseEntity<Boolean> applyPhraseToUser(@RequestBody @Valid AttributesRequest req) { |
55 |
| - if (phraseService.isPhraseValid(req.adverb, req.verb, req.preposition, req.noun)) { |
56 |
| - boolean isPhraseApplied = |
57 |
| - phraseService.applyPhraseToUser( |
58 |
| - req.userId, req.adverb, req.verb, req.preposition, req.noun); |
59 |
| - if (isPhraseApplied) { |
60 |
| - sendNotification(true, req.userId); |
61 |
| - return ResponseEntity.status(HttpStatus.OK).body(true); |
62 |
| - } else { |
63 |
| - sendNotification(false, req.userId); |
64 |
| - return ResponseEntity.status(HttpStatus.OK).body(false); |
65 |
| - } |
66 |
| - } else { |
67 |
| - sendNotification(false, req.userId); |
68 |
| - return ResponseEntity.status(HttpStatus.OK).body(false); |
69 |
| - } |
| 66 | + @ApplyPhraseToUser |
| 67 | + @PostMapping |
| 68 | + public ResponseEntity<Boolean> applyPhraseToUser(@RequestBody @Valid AttributesRequest req) { |
| 69 | + if (phraseService.isPhraseValid(req.adverb, req.verb, req.preposition, req.noun)) { |
| 70 | + boolean isPhraseApplied = |
| 71 | + phraseService.applyPhraseToUser( |
| 72 | + req.userId, req.adverb, req.verb, req.preposition, req.noun); |
| 73 | + if (isPhraseApplied) { |
| 74 | + sendNotification(true, req.userId); |
| 75 | + return ResponseEntity.status(HttpStatus.OK).body(true); |
| 76 | + } else { |
| 77 | + sendNotification(false, req.userId); |
| 78 | + return ResponseEntity.status(HttpStatus.OK).body(false); |
| 79 | + } |
| 80 | + } else { |
| 81 | + sendNotification(false, req.userId); |
| 82 | + return ResponseEntity.status(HttpStatus.OK).body(false); |
70 | 83 | }
|
| 84 | + } |
| 85 | + |
| 86 | + ///api/attributes/?phraseId=xx&userId=xx |
| 87 | + @DeletePhraseFromUser |
| 88 | + @DeleteMapping |
| 89 | + public ResponseEntity deletePhraseFromUser(@Parameter(description = "Phrase ID of phrase", example = "1") @RequestParam("phraseId") Long phraseId, @Parameter(description = "User ID of user", example = "1") @RequestParam("userId") Long userId) { |
| 90 | + userPhraseService.deletePhraseFromUser(phraseId, userId); |
| 91 | + return ResponseEntity.ok().build(); |
| 92 | + } |
| 93 | + |
| 94 | + private void sendNotification(Boolean approved, Long userId) { |
| 95 | + if (approved) { |
| 96 | + notificationService.createNotification( |
| 97 | + NotificationType.ATTRIBUTE_REQUEST_APPROVED, |
| 98 | + userId, |
| 99 | + NotificationType.ATTRIBUTE_REQUEST_APPROVED.getName(), |
| 100 | + "Your attribute has been approved!"); |
71 | 101 |
|
72 |
| - private void sendNotification(Boolean approved, Long userId) { |
73 |
| - if (approved) { |
74 |
| - notificationService.createNotification( |
75 |
| - NotificationType.ATTRIBUTE_REQUEST_APPROVED, |
76 |
| - userId, |
77 |
| - NotificationType.ATTRIBUTE_REQUEST_APPROVED.getName(), |
78 |
| - "Your attribute has been approved!"); |
79 |
| - } else { |
80 |
| - notificationService.createNotification( |
81 |
| - NotificationType.ATTRIBUTE_REQUEST_REJECTED, |
82 |
| - userId, |
83 |
| - NotificationType.ATTRIBUTE_REQUEST_REJECTED.getName(), |
84 |
| - "Your attribute was rejected. This attribute is unsuitable and cannot be applied to users."); |
85 |
| - |
86 |
| - } |
| 102 | + } else { |
| 103 | + notificationService.createNotification( |
| 104 | + NotificationType.ATTRIBUTE_REQUEST_REJECTED, |
| 105 | + userId, |
| 106 | + NotificationType.ATTRIBUTE_REQUEST_REJECTED.getName(), |
| 107 | + "Your attribute was rejected. This attribute is unsuitable and cannot be applied to users."); |
87 | 108 | }
|
| 109 | + } |
88 | 110 | }
|
0 commit comments