From 6fc49f71666c816f411d88d8abfcc1ff335dbe97 Mon Sep 17 00:00:00 2001 From: yash raj Date: Sun, 4 Aug 2024 18:18:23 +0530 Subject: [PATCH 1/2] build api to approve/reject skill request --- .../com/RDS/skilltree/apis/SkillsApi.java | 12 +++++++- .../dtos/SkillRequestActionRequestDto.java | 16 ++++++++++ .../com/RDS/skilltree/models/Endorsement.java | 2 +- .../RDS/skilltree/services/SkillService.java | 5 ++++ .../services/SkillServiceImplementation.java | 30 ++++++++++++++++--- 5 files changed, 59 insertions(+), 6 deletions(-) create mode 100644 skill-tree/src/main/java/com/RDS/skilltree/dtos/SkillRequestActionRequestDto.java diff --git a/skill-tree/src/main/java/com/RDS/skilltree/apis/SkillsApi.java b/skill-tree/src/main/java/com/RDS/skilltree/apis/SkillsApi.java index 3cbcf4a7..6925e582 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/apis/SkillsApi.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/apis/SkillsApi.java @@ -2,22 +2,25 @@ import com.RDS.skilltree.annotations.AuthorizedRoles; import com.RDS.skilltree.dtos.CreateEndorsementRequestDto; +import com.RDS.skilltree.dtos.SkillRequestActionRequestDto; import com.RDS.skilltree.dtos.SkillRequestsDto; import com.RDS.skilltree.enums.UserRoleEnum; import com.RDS.skilltree.services.EndorsementService; import com.RDS.skilltree.services.SkillService; +import com.RDS.skilltree.utils.GenericResponse; import com.RDS.skilltree.viewmodels.CreateEndorsementViewModel; import com.RDS.skilltree.viewmodels.CreateSkillViewModel; import com.RDS.skilltree.viewmodels.EndorsementViewModel; import com.RDS.skilltree.viewmodels.SkillViewModel; import jakarta.validation.Valid; -import java.util.List; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; +import java.util.List; + @Slf4j @RestController @RequiredArgsConstructor @@ -38,6 +41,13 @@ public ResponseEntity getAllRequests() { return ResponseEntity.ok(skillService.getAllRequests()); } + + @PostMapping("/requests/{skillId}/action") + @AuthorizedRoles({UserRoleEnum.SUPERUSER}) + public ResponseEntity> approveRejectSkillRequest(@PathVariable(value = "skillId") Integer skillId, @Valid @RequestBody SkillRequestActionRequestDto skillRequestAction) { + return ResponseEntity.ok(skillService.approveRejectSkillRequest(skillId, skillRequestAction.getEndorseId(), skillRequestAction.getAction())); + } + @PostMapping @AuthorizedRoles({UserRoleEnum.SUPERUSER}) public ResponseEntity create(@Valid @RequestBody CreateSkillViewModel skill) { diff --git a/skill-tree/src/main/java/com/RDS/skilltree/dtos/SkillRequestActionRequestDto.java b/skill-tree/src/main/java/com/RDS/skilltree/dtos/SkillRequestActionRequestDto.java new file mode 100644 index 00000000..66cf83e4 --- /dev/null +++ b/skill-tree/src/main/java/com/RDS/skilltree/dtos/SkillRequestActionRequestDto.java @@ -0,0 +1,16 @@ +package com.RDS.skilltree.dtos; + +import com.RDS.skilltree.enums.UserSkillStatusEnum; +import jakarta.validation.constraints.NotNull; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class SkillRequestActionRequestDto { + @NotNull(message = "user id cannot be null") + private String endorseId; + + @NotNull(message = "Action should not empty") + private UserSkillStatusEnum action; +} diff --git a/skill-tree/src/main/java/com/RDS/skilltree/models/Endorsement.java b/skill-tree/src/main/java/com/RDS/skilltree/models/Endorsement.java index 3f9494aa..935d75b9 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/models/Endorsement.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/models/Endorsement.java @@ -27,6 +27,6 @@ public class Endorsement extends TrackedProperties { @Column(name = "endorser_id", nullable = false) private String endorserId; - @Column(name = "message", nullable = false) + @Column(name = "message", nullable = false, columnDefinition = "TEXT") private String message; } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/services/SkillService.java b/skill-tree/src/main/java/com/RDS/skilltree/services/SkillService.java index e456ade1..867ae8c5 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/services/SkillService.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/services/SkillService.java @@ -1,8 +1,11 @@ package com.RDS.skilltree.services; import com.RDS.skilltree.dtos.SkillRequestsDto; +import com.RDS.skilltree.enums.UserSkillStatusEnum; +import com.RDS.skilltree.utils.GenericResponse; import com.RDS.skilltree.viewmodels.CreateSkillViewModel; import com.RDS.skilltree.viewmodels.SkillViewModel; + import java.util.List; public interface SkillService { @@ -11,4 +14,6 @@ public interface SkillService { SkillViewModel create(CreateSkillViewModel skill); SkillRequestsDto getAllRequests(); + + GenericResponse approveRejectSkillRequest(Integer skillId, String endorseId, UserSkillStatusEnum action); } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/services/SkillServiceImplementation.java b/skill-tree/src/main/java/com/RDS/skilltree/services/SkillServiceImplementation.java index 459ea75d..61cdd209 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/services/SkillServiceImplementation.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/services/SkillServiceImplementation.java @@ -3,6 +3,7 @@ import com.RDS.skilltree.dtos.RdsGetUserDetailsResDto; import com.RDS.skilltree.dtos.SkillRequestsDto; import com.RDS.skilltree.enums.UserSkillStatusEnum; +import com.RDS.skilltree.exceptions.NoEntityException; import com.RDS.skilltree.exceptions.SkillAlreadyExistsException; import com.RDS.skilltree.models.Endorsement; import com.RDS.skilltree.models.JwtUser; @@ -12,22 +13,27 @@ import com.RDS.skilltree.repositories.SkillRepository; import com.RDS.skilltree.repositories.UserSkillRepository; import com.RDS.skilltree.services.external.RdsService; +import com.RDS.skilltree.utils.GenericResponse; import com.RDS.skilltree.viewmodels.CreateSkillViewModel; import com.RDS.skilltree.viewmodels.SkillRequestViewModel; import com.RDS.skilltree.viewmodels.SkillViewModel; import com.RDS.skilltree.viewmodels.UserViewModel; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.BeanUtils; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Service; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + @Service @RequiredArgsConstructor public class SkillServiceImplementation implements SkillService { + private static final Logger log = LoggerFactory.getLogger(SkillServiceImplementation.class); private final RdsService rdsService; private final SkillRepository skillRepository; private final UserSkillRepository userSkillRepository; @@ -116,6 +122,22 @@ public SkillViewModel create(CreateSkillViewModel skill) { return SkillViewModel.toViewModel(skillRepository.saveAndFlush(newSkill)); } + @Override + public GenericResponse approveRejectSkillRequest(Integer skillId, String endorseId, UserSkillStatusEnum action) { + List existingSkillRequest = userSkillRepository.findByUserIdAndSkillId(endorseId, skillId); + + if (existingSkillRequest.isEmpty()) { + log.info("Skill request not found! endorseId: {} and skillId: {}", endorseId, skillId); + throw new NoEntityException("Skill request not found"); + } + + UserSkills updatedSkillRequest = existingSkillRequest.get(0); + updatedSkillRequest.setStatus(action); + + userSkillRepository.save(updatedSkillRequest); + return new GenericResponse<>("Skill {}", action.toString().toLowerCase()); + } + private Skill toEntity(CreateSkillViewModel viewModel) { Skill entity = new Skill(); BeanUtils.copyProperties(viewModel, entity); From 430fd298e90c9ea17d7e8f107639b24bd9dc5855 Mon Sep 17 00:00:00 2001 From: yash raj Date: Sun, 4 Aug 2024 18:19:44 +0530 Subject: [PATCH 2/2] fix formatting --- .../java/com/RDS/skilltree/apis/SkillsApi.java | 12 +++++++----- .../com/RDS/skilltree/services/SkillService.java | 4 ++-- .../services/SkillServiceImplementation.java | 15 ++++++++------- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/skill-tree/src/main/java/com/RDS/skilltree/apis/SkillsApi.java b/skill-tree/src/main/java/com/RDS/skilltree/apis/SkillsApi.java index 6925e582..28ba4828 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/apis/SkillsApi.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/apis/SkillsApi.java @@ -13,14 +13,13 @@ import com.RDS.skilltree.viewmodels.EndorsementViewModel; import com.RDS.skilltree.viewmodels.SkillViewModel; import jakarta.validation.Valid; +import java.util.List; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; -import java.util.List; - @Slf4j @RestController @RequiredArgsConstructor @@ -41,11 +40,14 @@ public ResponseEntity getAllRequests() { return ResponseEntity.ok(skillService.getAllRequests()); } - @PostMapping("/requests/{skillId}/action") @AuthorizedRoles({UserRoleEnum.SUPERUSER}) - public ResponseEntity> approveRejectSkillRequest(@PathVariable(value = "skillId") Integer skillId, @Valid @RequestBody SkillRequestActionRequestDto skillRequestAction) { - return ResponseEntity.ok(skillService.approveRejectSkillRequest(skillId, skillRequestAction.getEndorseId(), skillRequestAction.getAction())); + public ResponseEntity> approveRejectSkillRequest( + @PathVariable(value = "skillId") Integer skillId, + @Valid @RequestBody SkillRequestActionRequestDto skillRequestAction) { + return ResponseEntity.ok( + skillService.approveRejectSkillRequest( + skillId, skillRequestAction.getEndorseId(), skillRequestAction.getAction())); } @PostMapping diff --git a/skill-tree/src/main/java/com/RDS/skilltree/services/SkillService.java b/skill-tree/src/main/java/com/RDS/skilltree/services/SkillService.java index 867ae8c5..06439ee8 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/services/SkillService.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/services/SkillService.java @@ -5,7 +5,6 @@ import com.RDS.skilltree.utils.GenericResponse; import com.RDS.skilltree.viewmodels.CreateSkillViewModel; import com.RDS.skilltree.viewmodels.SkillViewModel; - import java.util.List; public interface SkillService { @@ -15,5 +14,6 @@ public interface SkillService { SkillRequestsDto getAllRequests(); - GenericResponse approveRejectSkillRequest(Integer skillId, String endorseId, UserSkillStatusEnum action); + GenericResponse approveRejectSkillRequest( + Integer skillId, String endorseId, UserSkillStatusEnum action); } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/services/SkillServiceImplementation.java b/skill-tree/src/main/java/com/RDS/skilltree/services/SkillServiceImplementation.java index 61cdd209..ee900f5a 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/services/SkillServiceImplementation.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/services/SkillServiceImplementation.java @@ -18,6 +18,10 @@ import com.RDS.skilltree.viewmodels.SkillRequestViewModel; import com.RDS.skilltree.viewmodels.SkillViewModel; import com.RDS.skilltree.viewmodels.UserViewModel; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -25,11 +29,6 @@ import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Service; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; - @Service @RequiredArgsConstructor public class SkillServiceImplementation implements SkillService { @@ -123,8 +122,10 @@ public SkillViewModel create(CreateSkillViewModel skill) { } @Override - public GenericResponse approveRejectSkillRequest(Integer skillId, String endorseId, UserSkillStatusEnum action) { - List existingSkillRequest = userSkillRepository.findByUserIdAndSkillId(endorseId, skillId); + public GenericResponse approveRejectSkillRequest( + Integer skillId, String endorseId, UserSkillStatusEnum action) { + List existingSkillRequest = + userSkillRepository.findByUserIdAndSkillId(endorseId, skillId); if (existingSkillRequest.isEmpty()) { log.info("Skill request not found! endorseId: {} and skillId: {}", endorseId, skillId);