Skip to content

Commit cb19e33

Browse files
committed
Merge branch 'develop' of github.com:savvato-software/tribe-app-backend into develop
2 parents 1c70326 + 3b71ec1 commit cb19e33

29 files changed

+397
-342
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@
184184
<configuration>
185185
<excludes>
186186
<exclude>com/savvato/tribeapp/TribeAppApplication.class</exclude>
187+
<exclude>com/savvato/tribeapp/entities/**</exclude>
187188
<exclude>**/**/constants/**</exclude>
188189
</excludes>
189190

src/main/java/com/savvato/tribeapp/controllers/AttributesAPIController.java

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package com.savvato.tribeapp.controllers;
22

3-
import com.savvato.tribeapp.controllers.annotations.controllers.AttributesAPIController.*;
3+
import com.savvato.tribeapp.controllers.annotations.controllers.AttributesAPIController.ApplyPhraseToUser;
4+
import com.savvato.tribeapp.controllers.annotations.controllers.AttributesAPIController.DeletePhraseFromUser;
5+
import com.savvato.tribeapp.controllers.annotations.controllers.AttributesAPIController.GetAttributesForUser;
6+
import com.savvato.tribeapp.controllers.annotations.controllers.AttributesAPIController.GetUserPhrasesToBeReviewed;
47
import com.savvato.tribeapp.controllers.dto.AttributesRequest;
58
import com.savvato.tribeapp.dto.AttributeDTO;
6-
import com.savvato.tribeapp.dto.GenericMessageDTO;
9+
import com.savvato.tribeapp.dto.GenericResponseDTO;
710
import com.savvato.tribeapp.dto.ToBeReviewedDTO;
811
import com.savvato.tribeapp.entities.NotificationType;
912
import com.savvato.tribeapp.services.*;
@@ -27,6 +30,9 @@ public class AttributesAPIController {
2730

2831
@Autowired
2932
AttributesService attributesService;
33+
34+
@Autowired
35+
private GenericResponseService GenericResponseService;
3036

3137
@Autowired
3238
PhraseService phraseService;
@@ -43,19 +49,6 @@ public class AttributesAPIController {
4349
AttributesAPIController() {
4450
}
4551

46-
@GetNumberOfUsersWithAttribute
47-
@GetMapping("/total/{attributeId}")
48-
public ResponseEntity<GenericMessageDTO> getNumberOfUsersWithAttribute(
49-
@Parameter(description = "Attribute ID", example = "1") @PathVariable Long attributeId) {
50-
51-
Optional<Integer> numberOfUsers = attributesService.getNumberOfUsersWithAttribute(attributeId);
52-
53-
if (numberOfUsers.isPresent()) {
54-
String userCount = String.valueOf(numberOfUsers.get());
55-
return ResponseEntity.ok(GenericMessageDTO.builder().responseMessage(userCount).build());
56-
}
57-
return ResponseEntity.badRequest().build();
58-
}
5952

6053
@GetAttributesForUser
6154
@GetMapping("/{userId}")
@@ -76,25 +69,27 @@ public ResponseEntity<List<ToBeReviewedDTO>> getUserPhrasesToBeReviewed(
7669
List<ToBeReviewedDTO> rtn = reviewSubmittingUserService.getUserPhrasesToBeReviewed(userId);
7770
return ResponseEntity.status(HttpStatus.OK).body(rtn);
7871
}
79-
8072
@ApplyPhraseToUser
8173
@PostMapping
82-
public ResponseEntity<Boolean> applyPhraseToUser(@RequestBody @Valid AttributesRequest req) {
83-
if (phraseService.isPhraseValid(req.adverb, req.verb, req.preposition, req.noun)) {
84-
boolean isPhraseApplied =
85-
phraseService.applyPhraseToUser(
86-
req.userId, req.adverb, req.verb, req.preposition, req.noun);
87-
if (isPhraseApplied) {
88-
sendNotification(true, req.userId);
89-
return ResponseEntity.status(HttpStatus.OK).body(true);
90-
} else {
91-
sendNotification(false, req.userId);
92-
return ResponseEntity.status(HttpStatus.OK).body(false);
93-
}
74+
public ResponseEntity<GenericResponseDTO> applyPhraseToUser(@RequestBody @Valid AttributesRequest req) {
75+
if (phraseService.isPhraseValid(req.adverb, req.verb, req.preposition, req.noun)) {
76+
boolean isPhraseApplied =
77+
phraseService.applyPhraseToUser(
78+
req.userId, req.adverb, req.verb, req.preposition, req.noun);
79+
if (isPhraseApplied) {
80+
sendNotification(true, req.userId);
81+
GenericResponseDTO rtn = GenericResponseService.createDTO("true");
82+
return ResponseEntity.status(HttpStatus.OK).body(rtn);
9483
} else {
95-
sendNotification(false, req.userId);
96-
return ResponseEntity.status(HttpStatus.OK).body(false);
84+
sendNotification(false, req.userId);
85+
GenericResponseDTO rtn = GenericResponseService.createDTO("false");
86+
return ResponseEntity.status(HttpStatus.OK).body(rtn);
9787
}
88+
} else {
89+
sendNotification(false, req.userId);
90+
GenericResponseDTO rtn = GenericResponseService.createDTO("false");
91+
return ResponseEntity.status(HttpStatus.OK).body(rtn);
92+
}
9893
}
9994

10095
///api/attributes/?phraseId=xx&userId=xx

src/main/java/com/savvato/tribeapp/controllers/AuthAPIController.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import com.savvato.tribeapp.services.AuthServiceImpl;
88
import io.swagger.v3.oas.annotations.tags.Tag;
99
import javax.validation.Valid;
10+
11+
import org.springframework.beans.factory.annotation.Autowired;
1012
import org.springframework.http.HttpHeaders;
1113
import org.springframework.http.HttpStatus;
1214
import org.springframework.http.ResponseEntity;
@@ -25,6 +27,7 @@ public class AuthAPIController {
2527

2628
private final AuthenticationManager authenticationManager;
2729

30+
2831
public AuthAPIController(AuthenticationManager authenticationManager) {
2932
this.authenticationManager = authenticationManager;
3033
}

src/main/java/com/savvato/tribeapp/controllers/NotificationAPIController.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
import com.savvato.tribeapp.controllers.annotations.controllers.NotificationController.UpdateNotification;
55
import com.savvato.tribeapp.controllers.dto.NotificationRequest;
66
import com.savvato.tribeapp.dto.NotificationDTO;
7-
import com.savvato.tribeapp.dto.GenericMessageDTO;
7+
import com.savvato.tribeapp.dto.GenericResponseDTO;
8+
import com.savvato.tribeapp.services.GenericResponseService;
89
import com.savvato.tribeapp.services.NotificationService;
910
import io.swagger.v3.oas.annotations.Parameter;
1011
import io.swagger.v3.oas.annotations.tags.Tag;
@@ -24,30 +25,32 @@
2425
public class NotificationAPIController {
2526

2627
@Autowired private NotificationService notificationService;
28+
@Autowired private GenericResponseService GenericResponseService;
2729

2830
@UpdateNotification
2931
@PutMapping
30-
public ResponseEntity<GenericMessageDTO> updateNotification(@RequestBody @Valid NotificationRequest req) {
32+
public ResponseEntity<GenericResponseDTO> updateNotification(@RequestBody @Valid NotificationRequest req) {
3133
boolean isRead = notificationService.checkNotificationReadStatus(req.id);
3234
if (isRead) {
33-
GenericMessageDTO rtn = notificationService.createMessageDTO("Notification is already read");
35+
GenericResponseDTO rtn = GenericResponseService.createDTO("Notification is already read");
3436
return ResponseEntity.ok(rtn);
3537
} else {
3638
notificationService.updateNotificationReadStatus(req.id);
37-
GenericMessageDTO rtn = notificationService.createMessageDTO("Notification read status updated");
39+
GenericResponseDTO rtn = GenericResponseService.createDTO("Notification read status updated");
40+
3841
return ResponseEntity.ok(rtn);
3942
}
4043
}
4144

4245
@DeleteMapping("/{id}")
43-
public ResponseEntity<GenericMessageDTO> deleteNotification(@PathVariable Long id) {
46+
public ResponseEntity<GenericResponseDTO> deleteNotification(@PathVariable Long id) {
4447
boolean exists = notificationService.checkNotificationExists(id);
4548
if (exists) {
4649
notificationService.deleteNotification(id);
47-
GenericMessageDTO rtn = notificationService.createMessageDTO("Notification deleted");
50+
GenericResponseDTO rtn = GenericResponseService.createDTO("Notification deleted");
4851
return ResponseEntity.ok(rtn);
4952
} else {
50-
GenericMessageDTO rtn = notificationService.createMessageDTO("Bad Request");
53+
GenericResponseDTO rtn = GenericResponseService.createDTO("Bad Request");
5154
return ResponseEntity
5255
.status(HttpStatus.BAD_REQUEST)
5356
.body(rtn);

src/main/java/com/savvato/tribeapp/controllers/PermissionsAPIController.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
import com.savvato.tribeapp.controllers.annotations.controllers.PermissionsAPIController.*;
44
import com.savvato.tribeapp.controllers.dto.PermissionsRequest;
5+
import com.savvato.tribeapp.dto.GenericResponseDTO;
56
import com.savvato.tribeapp.dto.UserDTO;
67
import com.savvato.tribeapp.entities.UserRole;
8+
import com.savvato.tribeapp.services.GenericResponseService;
79
import com.savvato.tribeapp.services.UserRoleMapService;
810
import com.savvato.tribeapp.services.UserRoleService;
911
import com.savvato.tribeapp.services.UserService;
@@ -20,6 +22,8 @@
2022
public class PermissionsAPIController {
2123
@Autowired UserRoleMapService userRoleMapService;
2224

25+
@Autowired private GenericResponseService GenericResponseService;
26+
2327
@Autowired UserRoleService userRoleService;
2428

2529
@Autowired UserService userService;
@@ -69,22 +73,22 @@ public ResponseEntity<Iterable<UserRole>> getAllRoles() {
6973

7074
@AddPermissions
7175
@PostMapping
72-
public ResponseEntity<Boolean> addPermissions(@RequestBody @Valid PermissionsRequest request) {
73-
boolean rtn = userRoleMapService.addRolesToUser(request.id, request.permissions);
74-
75-
if (rtn) {
76-
return ResponseEntity.status(HttpStatus.OK).body(rtn);
76+
public ResponseEntity<GenericResponseDTO> addPermissions(@RequestBody @Valid PermissionsRequest request) {
77+
boolean val = userRoleMapService.addRolesToUser(request.id, request.permissions);
78+
GenericResponseDTO rtn = GenericResponseService.createDTO(val);
79+
if (val) {
80+
return ResponseEntity.status(HttpStatus.OK).body(rtn);
7781
} else {
78-
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(rtn);
82+
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(rtn);
7983
}
8084
}
8185

8286
@DeletePermissions
8387
@DeleteMapping
84-
public ResponseEntity<Boolean> deletePermissions(@RequestBody @Valid PermissionsRequest request) {
85-
boolean rtn = userRoleMapService.removeRolesFromUser(request.id, request.permissions);
86-
87-
if (rtn) {
88+
public ResponseEntity<GenericResponseDTO> deletePermissions(@RequestBody @Valid PermissionsRequest request) {
89+
boolean val = userRoleMapService.removeRolesFromUser(request.id, request.permissions);
90+
GenericResponseDTO rtn = GenericResponseService.createDTO(val);
91+
if (val) {
8892
return ResponseEntity.status(HttpStatus.OK).body(rtn);
8993
} else {
9094
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(rtn);

src/main/java/com/savvato/tribeapp/controllers/ProfileAPIController.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import com.savvato.tribeapp.controllers.annotations.controllers.ProfileAPIController.GetById;
44
import com.savvato.tribeapp.controllers.annotations.controllers.ProfileAPIController.Update;
55
import com.savvato.tribeapp.controllers.dto.ProfileRequest;
6+
import com.savvato.tribeapp.dto.GenericResponseDTO;
67
import com.savvato.tribeapp.dto.ProfileDTO;
8+
import com.savvato.tribeapp.services.GenericResponseService;
79
import com.savvato.tribeapp.services.ProfileService;
810
import io.swagger.v3.oas.annotations.Parameter;
911
import io.swagger.v3.oas.annotations.tags.Tag;
@@ -23,6 +25,8 @@ public class ProfileAPIController {
2325
@Autowired
2426
ProfileService profileService;
2527

28+
@Autowired private GenericResponseService GenericResponseService;
29+
2630
ProfileAPIController() {
2731

2832
}
@@ -42,10 +46,10 @@ public ResponseEntity<ProfileDTO> getById(@Parameter(description = "The profile
4246

4347
@Update
4448
@PutMapping
45-
public ResponseEntity<Boolean> update(@RequestBody @Valid ProfileRequest request) {
46-
boolean rtn = profileService.update(request.userId, request.name, request.email, request.phone);
47-
48-
if (rtn) {
49+
public ResponseEntity<GenericResponseDTO> update(@RequestBody @Valid ProfileRequest request) {
50+
boolean val = profileService.update(request.userId, request.name, request.email, request.phone);
51+
GenericResponseDTO rtn = GenericResponseService.createDTO(val);
52+
if (val) {
4953
return ResponseEntity.status(HttpStatus.OK).body(rtn);
5054
} else {
5155
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(rtn);

src/main/java/com/savvato/tribeapp/controllers/annotations/controllers/AttributesAPIController/GetNumberOfUsersWithAttribute.java

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/main/java/com/savvato/tribeapp/dto/AttributeDTO.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,7 @@
88
@Schema(description = "An attribute DTO")
99
@Builder
1010
public class AttributeDTO {
11-
public PhraseDTO phrase;
11+
public PhraseDTO phrase;
12+
@Schema(example = "1")
13+
public Integer userCount;
1214
}

src/main/java/com/savvato/tribeapp/dto/GenericMessageDTO.java

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.savvato.tribeapp.dto;
2+
3+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
4+
import lombok.Builder;
5+
6+
@Builder
7+
@JsonDeserialize(builder = GenericResponseDTO.GenericResponseDTOBuilder.class)
8+
public class GenericResponseDTO {
9+
public String responseMessage;
10+
11+
public Boolean booleanMessage;
12+
13+
public Iterable<String> iterableMessage;
14+
15+
16+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.savvato.tribeapp.dto.projections;
2+
3+
4+
public record PhraseWithUserCountDTO(Long id, Long adverbId, Long verbId, Long prepositionId, Long nounId,
5+
Long userCount) {
6+
7+
}

src/main/java/com/savvato/tribeapp/entities/Phrase.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package com.savvato.tribeapp.entities;
22

3-
import javax.persistence.Entity;
4-
import javax.persistence.GeneratedValue;
5-
import javax.persistence.GenerationType;
6-
import javax.persistence.Id;
3+
import lombok.AllArgsConstructor;
4+
import lombok.NoArgsConstructor;
5+
6+
import javax.persistence.*;
7+
import java.util.List;
78

89
@Entity
10+
@AllArgsConstructor
11+
@NoArgsConstructor
912
public class Phrase {
1013

1114
@Id
@@ -60,4 +63,13 @@ public Long getNounId() {
6063
public void setNounId(Long nounID) {
6164
this.nounId = nounID;
6265
}
66+
67+
@OneToMany
68+
@JoinTable(
69+
name = "user_phrase",
70+
joinColumns = {@JoinColumn(name = "phraseId")},
71+
inverseJoinColumns = {@JoinColumn(name = "userId")})
72+
private List<User> users;
73+
74+
6375
}

src/main/java/com/savvato/tribeapp/repositories/PhraseRepository.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.savvato.tribeapp.repositories;
22

33

4+
import com.savvato.tribeapp.dto.projections.PhraseWithUserCountDTO;
45
import com.savvato.tribeapp.entities.Phrase;
56
import org.springframework.data.jpa.repository.Query;
67
import org.springframework.data.repository.CrudRepository;
@@ -11,8 +12,10 @@
1112
@Repository
1213
public interface PhraseRepository extends CrudRepository<Phrase, Long> {
1314

14-
@Query(nativeQuery = true, value = "select * from phrase where id = ?")
15-
Optional<Phrase> findPhraseByPhraseId(Long Id);
15+
@Query("""
16+
SELECT new com.savvato.tribeapp.dto.projections.PhraseWithUserCountDTO(p.id, p.adverbId, p.verbId, p.prepositionId, p.nounId, COUNT(u.id) AS userCount) FROM Phrase p LEFT JOIN p.users u GROUP BY p HAVING p.id = :id
17+
""")
18+
Optional<PhraseWithUserCountDTO> findPhraseByPhraseId(Long id);
1619

1720
Optional<Phrase> findByAdverbIdAndVerbIdAndPrepositionIdAndNounId(Long AdverbId, Long VerbId, Long PrepositionId, Long NounId);
1821

src/main/java/com/savvato/tribeapp/repositories/UserPhraseRepository.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,4 @@
1313
public interface UserPhraseRepository extends CrudRepository<UserPhrase, UserPhraseId> {
1414
@Query(nativeQuery = true, value = "select phrase_id from user_phrase where user_id = ?")
1515
Optional<List<Long>> findPhraseIdsByUserId(Long Id);
16-
17-
@Query(nativeQuery = true, value = "select COUNT(*) FROM user_phrase where attribute_id = ?")
18-
Integer countUsersWithAttribute(Long attributeId);
1916
}

src/main/java/com/savvato/tribeapp/services/AttributesService.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,5 @@
88
public interface AttributesService {
99

1010
Optional<List<AttributeDTO>> getAttributesByUserId(Long id);
11-
12-
Optional<Integer> getNumberOfUsersWithAttribute(Long attributeId);
1311
}
1412

0 commit comments

Comments
 (0)