Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build api to approve/reject skill request #144

Merged
merged 2 commits into from
Aug 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions skill-tree/src/main/java/com/RDS/skilltree/apis/SkillsApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

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;
Expand Down Expand Up @@ -38,6 +40,16 @@ public ResponseEntity<SkillRequestsDto> getAllRequests() {
return ResponseEntity.ok(skillService.getAllRequests());
}

@PostMapping("/requests/{skillId}/action")
@AuthorizedRoles({UserRoleEnum.SUPERUSER})
public ResponseEntity<GenericResponse<String>> 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<SkillViewModel> create(@Valid @RequestBody CreateSkillViewModel skill) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
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;
Expand All @@ -11,4 +13,7 @@ public interface SkillService {
SkillViewModel create(CreateSkillViewModel skill);

SkillRequestsDto getAllRequests();

GenericResponse<String> approveRejectSkillRequest(
Integer skillId, String endorseId, UserSkillStatusEnum action);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -12,6 +13,7 @@
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;
Expand All @@ -21,13 +23,16 @@
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;

@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;
Expand Down Expand Up @@ -116,6 +121,24 @@ public SkillViewModel create(CreateSkillViewModel skill) {
return SkillViewModel.toViewModel(skillRepository.saveAndFlush(newSkill));
}

@Override
public GenericResponse<String> approveRejectSkillRequest(
Integer skillId, String endorseId, UserSkillStatusEnum action) {
List<UserSkills> 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);
Expand Down
Loading