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

create api to get all skill requests #137

Closed
wants to merge 39 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
872e5e6
create a model to store user_skill and add remove unused columns in u…
yesyash Jun 23, 2024
2495c34
create a model to store user_skill and add/remove unused columns in u…
yesyash Jun 24, 2024
2d73260
change id to integer
yesyash Jun 24, 2024
b1b20a0
check if a skill already exists before creating one, make `updated_at…
yesyash Jun 24, 2024
8063f40
remove unit and integration test and remove skill service & skill ser…
yesyash Jun 24, 2024
7f09890
add todo
yesyash Jun 24, 2024
8f17bbc
rename SkillType to SkillTypeEnum
yesyash Jun 24, 2024
76b71bf
set logging level debug in application-dev instead of application
yesyash Jun 24, 2024
711757d
add todo
yesyash Jun 24, 2024
ff0f665
remove endorsements list
yesyash Jun 24, 2024
dc03b6d
add reference to user table in skills modal
yesyash Jun 24, 2024
4f52d11
Build api to create a new endorsement
yesyash Jun 24, 2024
14424c4
create api to update an endorsement
yesyash Jun 25, 2024
7a48e9c
remove unused files
yesyash Jun 25, 2024
22eece3
remove unused code in endorsement controller
yesyash Jun 25, 2024
514ffde
chagne import order
yesyash Jun 25, 2024
15e5740
change the skills project structure to match the new one
yesyash Jun 25, 2024
c77efb4
rename exceptions folder to small case exceptions and create user not…
yesyash Jun 25, 2024
43ae5b5
create enums folder and move skill type enum to the folder
yesyash Jun 25, 2024
a2aabe7
rename Conifg to config, move generic response and jwtAuthenticationF…
yesyash Jun 25, 2024
f6d364c
add api to get all endorsements for a skill using skill id in skillsapi
yesyash Jun 26, 2024
9592f6f
remove skills package
yesyash Jun 26, 2024
169c212
fix build error
yesyash Jun 26, 2024
2cfa444
move api to create a endorsement to the new folder structure
yesyash Jun 26, 2024
0508fc1
add api to update endorsement in apis/endorsements api and remove old…
yesyash Jun 26, 2024
1418fc4
move health check api to the api folder & metric service inside the s…
yesyash Jun 26, 2024
be2e5bb
fix formatting
yesyash Jun 26, 2024
5c3b983
create annotation and aspect to handle authorized roles to an api
yesyash Jul 7, 2024
abd38a4
add authorizred roles annotation to skillsapi & endorsementsapi class
yesyash Jul 7, 2024
884e8cc
set authorized role for creating a skill to only superuser
yesyash Jul 7, 2024
8bc7c9f
fix formatting
yesyash Jul 7, 2024
7eb0bac
Merge branch 'develop' of github.com:Real-Dev-Squad/skill-tree-backen…
yesyash Jul 7, 2024
6e33319
create api to get all skill requests
yesyash Jul 13, 2024
93211a8
apply spotless
yesyash Jul 13, 2024
e3897b8
set endorsement id
yesyash Jul 13, 2024
2398419
run spotlesscheck
yesyash Jul 13, 2024
d904a0e
Merge branch 'develop' of github.com:Real-Dev-Squad/skill-tree-backen…
yesyash Jul 13, 2024
f3d9df8
remove comments
yesyash Jul 13, 2024
bc59405
add super user role check in skills/requests api
yesyash Jul 13, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
public class UserSkillsModel {
@Id
@GeneratedValue
@Column(name = "id", columnDefinition = "BINARY(16)")
@Column(name = "id")
private UUID id;

@JsonBackReference
Expand Down
17 changes: 9 additions & 8 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,18 +2,16 @@

import com.RDS.skilltree.User.UserRoleEnum;
import com.RDS.skilltree.annotations.AuthorizedRoles;
import com.RDS.skilltree.dtos.SkillRequestsDto;
import com.RDS.skilltree.services.EndorsementService;
import com.RDS.skilltree.services.SkillService;
import com.RDS.skilltree.viewmodels.CreateSkillViewModel;
import com.RDS.skilltree.viewmodels.EndorsementViewModel;
import com.RDS.skilltree.viewmodels.SkillViewModel;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Min;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

Expand All @@ -31,18 +29,21 @@ public ResponseEntity<List<SkillViewModel>> getAll() {
return ResponseEntity.ok(skillService.getAll());
}

@GetMapping("/requests")
@AuthorizedRoles({UserRoleEnum.SUPERUSER})
public ResponseEntity<SkillRequestsDto> getAllRequests() {
return ResponseEntity.ok(skillService.getAllRequests());
}

@PostMapping
@AuthorizedRoles({UserRoleEnum.SUPERUSER})
public ResponseEntity<SkillViewModel> create(@Valid @RequestBody CreateSkillViewModel skill) {
return ResponseEntity.ok(skillService.create(skill));
}

@GetMapping("/{id}/endorsements")
public ResponseEntity<Page<EndorsementViewModel>> getEndorsementsBySkillId(
@RequestParam(name = "offset", defaultValue = "0", required = false) @Min(0) int offset,
@RequestParam(name = "limit", defaultValue = "10", required = false) @Min(1) int limit,
public ResponseEntity<List<EndorsementViewModel>> getEndorsementsBySkillId(
@PathVariable(value = "id") Integer skillID) {
PageRequest pageRequest = PageRequest.of(offset, limit);
return ResponseEntity.ok(endorsementService.getAllEndorsementsBySkillId(skillID, pageRequest));
return ResponseEntity.ok(endorsementService.getAllEndorsementsBySkillId(skillID));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.RDS.skilltree.dtos;

import com.RDS.skilltree.viewmodels.SkillRequestViewModel;
import com.RDS.skilltree.viewmodels.UserViewModel;
import java.util.List;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
public class SkillRequestsDto {
private List<SkillRequestViewModel> data;
private List<UserViewModel> users;

public static SkillRequestsDto toDto(
List<SkillRequestViewModel> skillRequests, List<UserViewModel> users) {
SkillRequestsDto skillRequestsDto = new SkillRequestsDto();
skillRequestsDto.setData(skillRequests);
skillRequestsDto.setUsers(users);

return skillRequestsDto;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.RDS.skilltree.repositories;

import com.RDS.skilltree.models.Endorsement;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;

public interface EndorsementRepository extends JpaRepository<Endorsement, Integer> {
Page<Endorsement> findBySkillId(Integer skillId, Pageable pageable);
List<Endorsement> findBySkillId(Integer skillId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.RDS.skilltree.repositories;

import com.RDS.skilltree.User.UserSkillStatusEnum;
import com.RDS.skilltree.User.UserSkillsModel;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserSkillRepository extends JpaRepository<UserSkillsModel, Integer> {
List<UserSkillsModel> findByStatus(UserSkillStatusEnum status);

List<UserSkillsModel> findByUserIdAndSkillId(String userId, Integer skillId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
import com.RDS.skilltree.viewmodels.CreateEndorsementViewModel;
import com.RDS.skilltree.viewmodels.EndorsementViewModel;
import com.RDS.skilltree.viewmodels.UpdateEndorsementViewModel;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;

public interface EndorsementService {
Page<EndorsementViewModel> getAllEndorsementsBySkillId(Integer skillId, Pageable pageable);
List<EndorsementViewModel> getAllEndorsementsBySkillId(Integer skillId);

EndorsementViewModel create(CreateEndorsementViewModel endorsement);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.RDS.skilltree.User.UserModel;
import com.RDS.skilltree.User.UserRepository;
import com.RDS.skilltree.User.UserSkillStatusEnum;
import com.RDS.skilltree.User.UserSkillsModel;
import com.RDS.skilltree.exceptions.EndorsementNotFoundException;
import com.RDS.skilltree.exceptions.SelfEndorsementNotAllowedException;
import com.RDS.skilltree.exceptions.SkillNotFoundException;
Expand All @@ -10,14 +12,14 @@
import com.RDS.skilltree.models.Skill;
import com.RDS.skilltree.repositories.EndorsementRepository;
import com.RDS.skilltree.repositories.SkillRepository;
import com.RDS.skilltree.repositories.UserSkillRepository;
import com.RDS.skilltree.viewmodels.CreateEndorsementViewModel;
import com.RDS.skilltree.viewmodels.EndorsementViewModel;
import com.RDS.skilltree.viewmodels.UpdateEndorsementViewModel;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

@Service
Expand All @@ -26,12 +28,12 @@ public class EndorsementServiceImplementation implements EndorsementService {
private final UserRepository userRepository;
private final SkillRepository skillRepository;
private final EndorsementRepository endorsementRepository;
private final UserSkillRepository userSkillRepository;

@Override
public Page<EndorsementViewModel> getAllEndorsementsBySkillId(
Integer skillId, Pageable pageable) {
Page<Endorsement> endorsementPage = endorsementRepository.findBySkillId(skillId, pageable);
return endorsementPage.map(EndorsementViewModel::toViewModel);
public List<EndorsementViewModel> getAllEndorsementsBySkillId(Integer skillId) {
List<Endorsement> endorsements = endorsementRepository.findBySkillId(skillId);
return endorsements.stream().map(EndorsementViewModel::toViewModel).toList();
}

@Override
Expand Down Expand Up @@ -64,14 +66,27 @@ public EndorsementViewModel create(CreateEndorsementViewModel endorsementViewMod
throw new SkillNotFoundException(String.format("Skill id: %s not found", skillId));
}

List<UserSkillsModel> userSkillEntry =
userSkillRepository.findByUserIdAndSkillId(endorseId, skillId);
Endorsement endorsement = new Endorsement();

endorsement.setMessage(message);
endorsement.setSkill(skillDetails.get());
endorsement.setEndorse(endorseDetails.get());
endorsement.setEndorser(endorserDetails.get());

return EndorsementViewModel.toViewModel(endorsementRepository.save(endorsement));
if (userSkillEntry.isEmpty()) {
UserSkillsModel userSkillsModel = new UserSkillsModel();
userSkillsModel.setUser(endorseDetails.get());
userSkillsModel.setSkill(skillDetails.get());
userSkillsModel.setStatus(UserSkillStatusEnum.PENDING);

userSkillRepository.save(userSkillsModel);
}

Endorsement newEndorsement = endorsementRepository.save(endorsement);

return EndorsementViewModel.toViewModel(newEndorsement);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.RDS.skilltree.services;

import com.RDS.skilltree.dtos.SkillRequestsDto;
import com.RDS.skilltree.viewmodels.CreateSkillViewModel;
import com.RDS.skilltree.viewmodels.SkillViewModel;
import java.util.List;
Expand All @@ -8,4 +9,6 @@ public interface SkillService {
List<SkillViewModel> getAll();

SkillViewModel create(CreateSkillViewModel skill);

SkillRequestsDto getAllRequests();
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package com.RDS.skilltree.services;

import com.RDS.skilltree.User.JwtUserModel;
import com.RDS.skilltree.User.UserModel;
import com.RDS.skilltree.User.UserRepository;
import com.RDS.skilltree.User.*;
import com.RDS.skilltree.dtos.SkillRequestsDto;
import com.RDS.skilltree.exceptions.SkillAlreadyExistsException;
import com.RDS.skilltree.exceptions.UserNotFoundException;
import com.RDS.skilltree.models.Endorsement;
import com.RDS.skilltree.models.Skill;
import com.RDS.skilltree.repositories.EndorsementRepository;
import com.RDS.skilltree.repositories.SkillRepository;
import com.RDS.skilltree.repositories.UserSkillRepository;
import com.RDS.skilltree.viewmodels.CreateSkillViewModel;
import com.RDS.skilltree.viewmodels.SkillRequestViewModel;
import com.RDS.skilltree.viewmodels.SkillViewModel;
import java.util.List;
import java.util.Optional;
import com.RDS.skilltree.viewmodels.UserViewModel;
import java.util.*;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
Expand All @@ -22,6 +25,8 @@
public class SkillServiceImplementation implements SkillService {
private final SkillRepository skillRepository;
private final UserRepository userRepository;
private final UserSkillRepository userSkillRepository;
private final EndorsementRepository endorsementRepository;

@Override
public List<SkillViewModel> getAll() {
Expand All @@ -30,6 +35,38 @@ public List<SkillViewModel> getAll() {
.collect(Collectors.toList());
}

@Override
public SkillRequestsDto getAllRequests() {
List<UserSkillsModel> pendingSkills =
userSkillRepository.findByStatus(UserSkillStatusEnum.PENDING);
Set<UserViewModel> uniqueUsers = new HashSet<>();

List<SkillRequestViewModel> skillRequests =
pendingSkills.stream()
.map(
pendingSkill -> {
List<Endorsement> endorsements =
endorsementRepository.findBySkillId(pendingSkill.getSkill().getId());

uniqueUsers.add(
// TODO : call rds backend to get user details
UserViewModel.toViewModel(new UserModel(pendingSkill.getUser().getId(), "")));

endorsements.forEach(
endorsement -> {
UserModel endorser = endorsement.getEndorser();
// TODO : call rds backend to get user details
uniqueUsers.add(
UserViewModel.toViewModel(new UserModel(endorser.getId(), "")));
});

return SkillRequestViewModel.toViewModel(pendingSkill, endorsements);
})
.toList();

return SkillRequestsDto.toDto(skillRequests, new ArrayList<>(uniqueUsers));
}

@Override
public SkillViewModel create(CreateSkillViewModel skill) {
if (skillRepository.existsByName(skill.getName())) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.RDS.skilltree.viewmodels;

import com.RDS.skilltree.models.Endorsement;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
public class MinimalEndorsementViewModel {
private Integer id;
private String endorserId;
private String endorsementDate;
private String message;

public static MinimalEndorsementViewModel toViewModel(Endorsement endorsement) {
MinimalEndorsementViewModel endorsementViewModel = new MinimalEndorsementViewModel();

endorsementViewModel.setId(endorsement.getId());
endorsementViewModel.setMessage(endorsement.getMessage());
endorsementViewModel.setEndorsementDate(endorsement.getCreatedAt().toString());
endorsementViewModel.setEndorserId(endorsement.getEndorser().getId());

return endorsementViewModel;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.RDS.skilltree.viewmodels;

import com.RDS.skilltree.User.UserModel;
import com.RDS.skilltree.User.UserSkillsModel;
import com.RDS.skilltree.models.Endorsement;
import com.RDS.skilltree.models.Skill;
import java.util.List;
import java.util.stream.Collectors;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
public class SkillRequestViewModel {
private Integer id;
private String name;
private String endorseId;
private List<MinimalEndorsementViewModel> endorsements;

public static SkillRequestViewModel toViewModel(
UserSkillsModel userSkillsModel, List<Endorsement> endorsements) {
SkillRequestViewModel skillRequestViewModel = new SkillRequestViewModel();

Skill skill = userSkillsModel.getSkill();
UserModel user = userSkillsModel.getUser();

skillRequestViewModel.setId(skill.getId());
skillRequestViewModel.setName(skill.getName());
skillRequestViewModel.setEndorseId(user.getId());
skillRequestViewModel.setEndorsements(
endorsements.stream()
.map(MinimalEndorsementViewModel::toViewModel)
.collect(Collectors.toList()));

return skillRequestViewModel;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
@Getter
@Setter
public class UserViewModel {
private String id;
private String name;

public static UserViewModel toViewModel(UserModel user) {
Expand Down
Loading