From 872e5e62be274e80419b1513b7cc7e7e0f83d2ad Mon Sep 17 00:00:00 2001 From: yash raj Date: Sun, 23 Jun 2024 11:39:53 +0530 Subject: [PATCH 01/31] create a model to store user_skill and add remove unused columns in users and skills model --- .../com/RDS/skilltree/Skill/SkillDRO.java | 2 +- .../com/RDS/skilltree/Skill/SkillDTO.java | 6 +-- .../com/RDS/skilltree/Skill/SkillModel.java | 9 +---- .../com/RDS/skilltree/Skill/SkillType.java | 1 - .../java/com/RDS/skilltree/User/UserDRO.java | 15 -------- .../java/com/RDS/skilltree/User/UserDTO.java | 12 ++---- .../com/RDS/skilltree/User/UserModel.java | 24 +----------- .../RDS/skilltree/User/UserServiceImpl.java | 4 +- .../skilltree/User/UserSkillStatusEnum.java | 7 ++++ .../RDS/skilltree/User/UserSkillsModel.java | 37 +++++++++++++++++++ 10 files changed, 57 insertions(+), 60 deletions(-) create mode 100644 skill-tree/src/main/java/com/RDS/skilltree/User/UserSkillStatusEnum.java create mode 100644 skill-tree/src/main/java/com/RDS/skilltree/User/UserSkillsModel.java diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDRO.java b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDRO.java index 543c4ace..48930a00 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDRO.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDRO.java @@ -23,7 +23,7 @@ public static SkillModel toModel(SkillDRO skillDRO) { return SkillModel.builder() .name(skillDRO.getName()) .type(skillDRO.getType()) - .deleted(false) + .isDeleted(false) .build(); } } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDTO.java b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDTO.java index a6304e2b..d95e5d6e 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDTO.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDTO.java @@ -28,9 +28,9 @@ public static SkillDTO toDto(SkillModel skillModel) { public static SkillDTO getSkillsWithUsers(SkillModel skillModel) { Set users = new HashSet<>(); - if (skillModel.getUsers() != null) { - users = skillModel.getUsers().stream().map(UserDTO::toDTO).collect(Collectors.toSet()); - } +// if (skillModel.getUsers() != null) { +// users = skillModel.getUsers().stream().map(UserDTO::toDTO).collect(Collectors.toSet()); +// } return SkillDTO.builder() .id(skillModel.getId()) .name(skillModel.getName()) diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillModel.java b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillModel.java index 0074c602..97b633f4 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillModel.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillModel.java @@ -17,7 +17,7 @@ @Entity @Builder @Getter -@Table(name = "Skill") +@Table(name = "skills") public class SkillModel extends TrackedProperties { @Id @GeneratedValue @@ -32,10 +32,5 @@ public class SkillModel extends TrackedProperties { private SkillType type = SkillType.ATOMIC; @Column(name = "is_deleted", nullable = false) - private boolean deleted; - - @JsonBackReference - @JsonIgnore - @ManyToMany(mappedBy = "skills", cascade = CascadeType.ALL, fetch = FetchType.LAZY) - private Set users; + private boolean isDeleted; } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillType.java b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillType.java index 32500383..86780f0a 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillType.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillType.java @@ -2,5 +2,4 @@ public enum SkillType { ATOMIC, - DERIVED } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/User/UserDRO.java b/skill-tree/src/main/java/com/RDS/skilltree/User/UserDRO.java index c0141963..b0a468af 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/User/UserDRO.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/User/UserDRO.java @@ -25,10 +25,7 @@ public class UserDRO { public static UserModel toModel(UserDRO user) { return UserModel.builder() .rdsUserId(user.getRdsUserId()) - .firstName(user.getFirstName()) - .lastName(user.getLastName()) .role(user.getRole()) - .imageUrl(user.getImageUrl()) .build(); } @@ -36,9 +33,6 @@ public static UserDRO fromModel(UserModel user) { return UserDRO.builder() .rdsUserId(user.getRdsUserId()) .role(user.getRole()) - .firstName(user.getFirstName()) - .lastName(user.getLastName()) - .imageUrl(user.getImageUrl()) .build(); } @@ -46,15 +40,6 @@ public static UserModel compareAndUpdateModel(UserModel user, UserDRO userDRO) { if (userDRO.getRdsUserId() != null) { user.setRdsUserId(user.getRdsUserId()); } - if (userDRO.getFirstName() != null) { - user.setFirstName(user.getFirstName()); - } - if (userDRO.getLastName() != null) { - user.setLastName(user.getLastName()); - } - if (userDRO.getImageUrl() != null) { - user.setImageUrl(user.getImageUrl()); - } if (userDRO.getRole() != null) { user.setRole(user.getRole()); } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/User/UserDTO.java b/skill-tree/src/main/java/com/RDS/skilltree/User/UserDTO.java index fedccf7b..869cabab 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/User/UserDTO.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/User/UserDTO.java @@ -31,24 +31,18 @@ public static UserDTO toDTO(UserModel user) { return UserDTO.builder() .id(user.getId()) .rdsUserId(user.getRdsUserId()) - .firstName(user.getFirstName()) - .lastName(user.getLastName()) - .imageUrl(user.getImageUrl()) .role(user.getRole()) .build(); } public static UserDTO getUsersWithSkills(UserModel user) { - Set skills = - user.getSkills().stream().map(SkillDTO::toDto).collect(Collectors.toSet()); +// Set skills = [] +// user.getSkills().stream().map(SkillDTO::toDto).collect(Collectors.toSet()); return UserDTO.builder() .id(user.getId()) .rdsUserId(user.getRdsUserId()) - .firstName(user.getFirstName()) - .lastName(user.getLastName()) - .imageUrl(user.getImageUrl()) - .skills(skills) +// .skills(skills) .role(user.getRole()) .build(); } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/User/UserModel.java b/skill-tree/src/main/java/com/RDS/skilltree/User/UserModel.java index 42776846..e3505001 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/User/UserModel.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/User/UserModel.java @@ -1,12 +1,9 @@ package com.RDS.skilltree.User; -import com.RDS.skilltree.Skill.SkillModel; import com.RDS.skilltree.utils.TrackedProperties; -import com.fasterxml.jackson.annotation.JsonManagedReference; import com.fasterxml.jackson.databind.annotation.JsonSerialize; import jakarta.persistence.*; -import java.net.URL; -import java.util.Set; + import java.util.UUID; import lombok.*; @@ -17,7 +14,7 @@ @JsonSerialize @NoArgsConstructor @AllArgsConstructor -@Table(name = "Users") +@Table(name = "users") public class UserModel extends TrackedProperties { @Id @GeneratedValue @@ -27,24 +24,7 @@ public class UserModel extends TrackedProperties { @Column(name = "rds_user_id", unique = true) private String rdsUserId; - @Column(name = "first_name", nullable = false) - private String firstName; - - @Column(name = "last_name") - private String lastName; - - @Column(name = "image_url", nullable = false) - private URL imageUrl; - @Column(name = "user_role", nullable = false) @Enumerated(value = EnumType.STRING) private UserRole role = UserRole.USER; - - @JsonManagedReference - @ManyToMany(targetEntity = SkillModel.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER) - @JoinTable( - name = "user_skill", - joinColumns = @JoinColumn(name = "user_id"), - inverseJoinColumns = @JoinColumn(name = "skill_id")) - private Set skills; } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/User/UserServiceImpl.java b/skill-tree/src/main/java/com/RDS/skilltree/User/UserServiceImpl.java index c85f9d94..b62e4739 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/User/UserServiceImpl.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/User/UserServiceImpl.java @@ -55,8 +55,8 @@ public void addSkill(UUID skillId, UUID userId) { UserModel userModel = userOptional.get(); SkillModel skillModel = skillOptional.get(); - userModel.getSkills().add(skillModel); - skillModel.getUsers().add(userModel); +// userModel.getSkills().add(skillModel); +// skillModel.getUsers().add(userModel); userRepository.save(userModel); skillRepository.save(skillModel); diff --git a/skill-tree/src/main/java/com/RDS/skilltree/User/UserSkillStatusEnum.java b/skill-tree/src/main/java/com/RDS/skilltree/User/UserSkillStatusEnum.java new file mode 100644 index 00000000..79637df6 --- /dev/null +++ b/skill-tree/src/main/java/com/RDS/skilltree/User/UserSkillStatusEnum.java @@ -0,0 +1,7 @@ +package com.RDS.skilltree.User; + +public enum UserSkillStatusEnum { + APPROVED, + REJECTED, + PENDING +} diff --git a/skill-tree/src/main/java/com/RDS/skilltree/User/UserSkillsModel.java b/skill-tree/src/main/java/com/RDS/skilltree/User/UserSkillsModel.java new file mode 100644 index 00000000..481c8172 --- /dev/null +++ b/skill-tree/src/main/java/com/RDS/skilltree/User/UserSkillsModel.java @@ -0,0 +1,37 @@ +package com.RDS.skilltree.User; + +import com.RDS.skilltree.Skill.SkillModel; +import com.fasterxml.jackson.annotation.JsonBackReference; +import jakarta.persistence.*; + +import lombok.*; + +import java.util.UUID; + +@Entity +@Getter +@Setter +@Builder +@NoArgsConstructor +@AllArgsConstructor +@Table(name = "user_skills") +public class UserSkillsModel { + @Id + @GeneratedValue + @Column(name = "id", columnDefinition = "BINARY(16)") + private UUID id; + + @JsonBackReference + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "user_id", nullable = false) + private UserModel user; + + @JsonBackReference + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "skill_id", nullable = false) + private SkillModel skill; + + @Enumerated(EnumType.STRING) + @Column(name = "status", nullable = false) + private UserSkillStatusEnum status = UserSkillStatusEnum.PENDING; +} From 2495c3455ac47be43436425df1ce37b640326662 Mon Sep 17 00:00:00 2001 From: yash raj Date: Mon, 24 Jun 2024 09:14:58 +0530 Subject: [PATCH 02/31] create a model to store user_skill and add/remove unused columns in users and skills model --- .../UserAuthenticationToken.java | 13 +- .../RDS/skilltree/Config/SecurityConfig.java | 6 +- .../skilltree/Endorsement/EndorsementDRO.java | 4 +- .../skilltree/Endorsement/EndorsementDTO.java | 2 - .../Endorsement/EndorsementModel.java | 6 +- .../Endorsement/EndorsementRepository.java | 2 +- .../Endorsement/EndorsementServiceImpl.java | 21 ++- .../Skill/{SkillModel.java => Skill.java} | 21 ++- .../com/RDS/skilltree/Skill/SkillDRO.java | 7 +- .../com/RDS/skilltree/Skill/SkillDTO.java | 20 +-- .../RDS/skilltree/Skill/SkillRepository.java | 9 +- .../RDS/skilltree/Skill/SkillsController.java | 162 +++++++++++++----- .../skilltree/Skill/SkillsServiceImpl.java | 16 +- .../com/RDS/skilltree/User/JwtUserModel.java | 14 ++ .../java/com/RDS/skilltree/User/UserDRO.java | 12 +- .../java/com/RDS/skilltree/User/UserDTO.java | 7 +- .../com/RDS/skilltree/User/UserModel.java | 4 - .../User/{UserRole.java => UserRoleEnum.java} | 12 +- .../com/RDS/skilltree/User/UserService.java | 2 +- .../RDS/skilltree/User/UserServiceImpl.java | 16 +- .../RDS/skilltree/User/UserSkillsModel.java | 4 +- .../skilltree/utils/TrackedProperties.java | 11 +- .../EndorsementsIntegrationTests.java | 2 +- .../integration/SkillsIntegrationTests.java | 2 +- .../unit/EndorsementServiceTest.java | 95 +++++----- .../RDS/skilltree/unit/SkillsServiceTest.java | 38 ++-- 26 files changed, 300 insertions(+), 208 deletions(-) rename skill-tree/src/main/java/com/RDS/skilltree/Skill/{SkillModel.java => Skill.java} (68%) create mode 100644 skill-tree/src/main/java/com/RDS/skilltree/User/JwtUserModel.java rename skill-tree/src/main/java/com/RDS/skilltree/User/{UserRole.java => UserRoleEnum.java} (55%) diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Authentication/UserAuthenticationToken.java b/skill-tree/src/main/java/com/RDS/skilltree/Authentication/UserAuthenticationToken.java index e2efea72..5799bfc8 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Authentication/UserAuthenticationToken.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Authentication/UserAuthenticationToken.java @@ -1,7 +1,7 @@ package com.RDS.skilltree.Authentication; -import com.RDS.skilltree.User.UserModel; -import com.RDS.skilltree.User.UserRole; +import com.RDS.skilltree.User.JwtUserModel; +import com.RDS.skilltree.User.UserRoleEnum; import java.util.List; import javax.security.auth.Subject; import org.springframework.security.authentication.AbstractAuthenticationToken; @@ -9,11 +9,12 @@ public class UserAuthenticationToken extends AbstractAuthenticationToken { - private final UserModel user; + private final JwtUserModel user; public UserAuthenticationToken(String role, String rdsUserId) { - super(List.of(new SimpleGrantedAuthority(UserRole.fromString(role).name()))); - this.user = UserModel.builder().rdsUserId(rdsUserId).role(UserRole.fromString(role)).build(); + super(List.of(new SimpleGrantedAuthority(UserRoleEnum.fromString(role).name()))); + + this.user = new JwtUserModel(rdsUserId, UserRoleEnum.fromString(role)); setAuthenticated(true); } @@ -23,7 +24,7 @@ public Object getCredentials() { } @Override - public UserModel getPrincipal() { + public JwtUserModel getPrincipal() { return user; } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Config/SecurityConfig.java b/skill-tree/src/main/java/com/RDS/skilltree/Config/SecurityConfig.java index ac4fa3dc..24c0e28a 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Config/SecurityConfig.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Config/SecurityConfig.java @@ -3,7 +3,7 @@ import com.RDS.skilltree.Authentication.AuthEntryPoint; import com.RDS.skilltree.Authentication.CustomAccessDeniedHandler; import com.RDS.skilltree.Filters.JWTAuthenticationFilter; -import com.RDS.skilltree.User.UserRole; +import com.RDS.skilltree.User.UserRoleEnum; import java.util.Arrays; import java.util.List; import org.springframework.context.annotation.Bean; @@ -50,10 +50,10 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { && request.getQueryString().contains("dummyData=true")) .permitAll() .requestMatchers(HttpMethod.GET, "/v1/**") - .hasAnyAuthority(UserRole.getAllRoles()) // give read-only access to all + .hasAnyAuthority(UserRoleEnum.getAllRoles()) // give read-only access to all .requestMatchers("/v1/**") .hasAnyAuthority( - UserRole.USER.name(), UserRole.MEMBER.name(), UserRole.SUPERUSER.name()) + UserRoleEnum.USER.name(), UserRoleEnum.MEMBER.name(), UserRoleEnum.SUPERUSER.name()) .anyRequest() .authenticated()) .exceptionHandling( diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementDRO.java b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementDRO.java index d0910431..de152bab 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementDRO.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementDRO.java @@ -1,7 +1,9 @@ package com.RDS.skilltree.Endorsement; import jakarta.validation.constraints.NotNull; + import java.util.UUID; + import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; @@ -16,5 +18,5 @@ public class EndorsementDRO { private UUID endorseeId; @NotNull(message = "skill id cannot be null") - private UUID skillId; + private Integer skillId; } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementDTO.java b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementDTO.java index 3047e63f..e0698bb3 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementDTO.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementDTO.java @@ -29,8 +29,6 @@ public static EndorsementDTO toDto(EndorsementModel endorsementModel) { .build(); endorsementDTO.setCreatedAt(endorsementModel.getCreatedAt()); endorsementDTO.setUpdatedAt(endorsementModel.getUpdatedAt()); - endorsementDTO.setCreatedBy(endorsementModel.getCreatedBy()); - endorsementDTO.setUpdatedBy(endorsementModel.getUpdatedBy()); return endorsementDTO; } } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementModel.java b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementModel.java index 68a3faec..19691be9 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementModel.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementModel.java @@ -1,7 +1,7 @@ package com.RDS.skilltree.Endorsement; import com.RDS.skilltree.EndorsementList.EndorsementListModel; -import com.RDS.skilltree.Skill.SkillModel; +import com.RDS.skilltree.Skill.Skill; import com.RDS.skilltree.utils.TrackedProperties; import com.fasterxml.jackson.annotation.JsonManagedReference; import jakarta.persistence.*; @@ -25,9 +25,9 @@ public class EndorsementModel extends TrackedProperties { @Column(name = "endorsee_id") private UUID endorseeId; - @ManyToOne(targetEntity = SkillModel.class, cascade = CascadeType.ALL) + @ManyToOne(targetEntity = Skill.class, cascade = CascadeType.ALL) @JoinColumn(name = "skill_id", referencedColumnName = "id") - private SkillModel skill; + private Skill skill; @OneToMany(mappedBy = "endorsement") @JsonManagedReference diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementRepository.java b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementRepository.java index 6da326dd..3b68c3b3 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementRepository.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementRepository.java @@ -9,5 +9,5 @@ public interface EndorsementRepository extends JpaRepository { List findByEndorseeId(UUID userId); - List findBySkillId(UUID skillId); + List findBySkillId(Integer skillId); } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementServiceImpl.java b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementServiceImpl.java index fce1ed63..b248ed2d 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementServiceImpl.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementServiceImpl.java @@ -4,18 +4,20 @@ import com.RDS.skilltree.Exceptions.EntityAlreadyExistsException; import com.RDS.skilltree.Exceptions.InvalidParameterException; import com.RDS.skilltree.Exceptions.NoEntityException; -import com.RDS.skilltree.Skill.SkillModel; +import com.RDS.skilltree.Skill.Skill; import com.RDS.skilltree.Skill.SkillRepository; -import com.RDS.skilltree.User.UserModel; -import com.RDS.skilltree.User.UserRole; +import com.RDS.skilltree.User.JwtUserModel; +import com.RDS.skilltree.User.UserRoleEnum; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.persistence.EntityNotFoundException; + import java.io.IOException; import java.util.List; import java.util.Optional; import java.util.UUID; import java.util.stream.Collectors; + import lombok.RequiredArgsConstructor; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -78,7 +80,8 @@ public Page getEndorsementsFromDummyData( private List readEndorsementsFromJSON() throws IOException { ClassPathResource resource = new ClassPathResource(dummyEndorsementDataPath); return objectMapper.readValue( - resource.getInputStream(), new TypeReference>() {}); + resource.getInputStream(), new TypeReference>() { + }); } private List filterEndorsements( @@ -118,9 +121,9 @@ private Page createPagedEndorsements( @Override public EndorsementModel createEndorsement(EndorsementDRO endorsementDRO) { UUID userId = endorsementDRO.getEndorseeId(); - UUID skillId = endorsementDRO.getSkillId(); + Integer skillId = endorsementDRO.getSkillId(); - Optional skillOptional = skillRepository.findById(skillId); + Optional skillOptional = skillRepository.findById(skillId); if (skillOptional.isPresent()) { EndorsementModel endorsementModel = EndorsementModel.builder().endorseeId(userId).skill(skillOptional.get()).build(); @@ -134,9 +137,9 @@ public EndorsementModel createEndorsement(EndorsementDRO endorsementDRO) { @Override public GenericResponse updateEndorsementStatus(UUID id, String status) { - UserModel user = - (UserModel) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); - if (!user.getRole().equals(UserRole.SUPERUSER)) { + JwtUserModel user = + (JwtUserModel) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); + if (!user.getRole().equals(UserRoleEnum.SUPERUSER)) { throw new AccessDeniedException("Unauthorized, Access is only available to super users"); } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillModel.java b/skill-tree/src/main/java/com/RDS/skilltree/Skill/Skill.java similarity index 68% rename from skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillModel.java rename to skill-tree/src/main/java/com/RDS/skilltree/Skill/Skill.java index 97b633f4..79b6e049 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillModel.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Skill/Skill.java @@ -1,28 +1,26 @@ package com.RDS.skilltree.Skill; -import com.RDS.skilltree.User.UserModel; import com.RDS.skilltree.utils.TrackedProperties; -import com.fasterxml.jackson.annotation.JsonBackReference; -import com.fasterxml.jackson.annotation.JsonIgnore; import jakarta.persistence.*; -import java.util.Set; -import java.util.UUID; + import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; +import java.util.UUID; + @AllArgsConstructor @NoArgsConstructor @Entity @Builder @Getter @Table(name = "skills") -public class SkillModel extends TrackedProperties { +public class Skill extends TrackedProperties { @Id @GeneratedValue - @Column(name = "id", columnDefinition = "BINARY(16)") - private UUID id; + @Column(name = "id") + private Integer id; @Column(name = "name", unique = true, nullable = false) private String name; @@ -33,4 +31,11 @@ public class SkillModel extends TrackedProperties { @Column(name = "is_deleted", nullable = false) private boolean isDeleted; + + // TODO : Confirm the type of this column from tejas + @Column(name = "created_by", nullable = false) + private UUID createdBy; + + @Column(name = "updated_by") + private UUID updatedBy; } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDRO.java b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDRO.java index 48930a00..564901a4 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDRO.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDRO.java @@ -16,11 +16,8 @@ public class SkillDRO { @NotNull(message = "SkillType cannot be null") private SkillType type; - @NotNull(message = "Created by user Id cannot be null") - private UUID createdBy; - - public static SkillModel toModel(SkillDRO skillDRO) { - return SkillModel.builder() + public static Skill toModel(SkillDRO skillDRO) { + return Skill.builder() .name(skillDRO.getName()) .type(skillDRO.getType()) .isDeleted(false) diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDTO.java b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDTO.java index d95e5d6e..734821bf 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDTO.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDTO.java @@ -5,7 +5,7 @@ import java.util.HashSet; import java.util.Set; import java.util.UUID; -import java.util.stream.Collectors; + import lombok.Builder; import lombok.Getter; @@ -13,28 +13,28 @@ @Builder @JsonIgnoreProperties(ignoreUnknown = true) public class SkillDTO { - private UUID id; + private Integer id; private SkillType type; private String name; private Set users; - public static SkillDTO toDto(SkillModel skillModel) { + public static SkillDTO toDto(Skill skill) { return SkillDTO.builder() - .id(skillModel.getId()) - .name(skillModel.getName()) - .type(skillModel.getType()) + .id(skill.getId()) + .name(skill.getName()) + .type(skill.getType()) .build(); } - public static SkillDTO getSkillsWithUsers(SkillModel skillModel) { + public static SkillDTO getSkillsWithUsers(Skill skill) { Set users = new HashSet<>(); // if (skillModel.getUsers() != null) { // users = skillModel.getUsers().stream().map(UserDTO::toDTO).collect(Collectors.toSet()); // } return SkillDTO.builder() - .id(skillModel.getId()) - .name(skillModel.getName()) - .type(skillModel.getType()) + .id(skill.getId()) + .name(skill.getName()) + .type(skill.getType()) .users(users) .build(); } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillRepository.java b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillRepository.java index a523a3b0..9e9da24b 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillRepository.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillRepository.java @@ -2,14 +2,13 @@ import java.util.Optional; import java.util.UUID; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; + import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository -public interface SkillRepository extends JpaRepository { - Optional findByName(String name); +public interface SkillRepository extends JpaRepository { + Optional findByName(String name); - Page findAll(Pageable pageable); +// Page findAll(Pageable pageable); } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsController.java b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsController.java index 00e9f669..8ecdf1e8 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsController.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsController.java @@ -1,69 +1,137 @@ +//package com.RDS.skilltree.Skill; +// +//import com.RDS.skilltree.utils.MessageResponse; +//import jakarta.validation.Valid; +//import jakarta.validation.constraints.Min; +//import java.util.UUID; +//import lombok.extern.slf4j.Slf4j; +//import org.springframework.dao.DataIntegrityViolationException; +//import org.springframework.data.domain.Page; +//import org.springframework.data.domain.PageRequest; +//import org.springframework.data.domain.Pageable; +//import org.springframework.http.HttpStatus; +//import org.springframework.http.ResponseEntity; +//import org.springframework.util.ObjectUtils; +//import org.springframework.web.bind.annotation.*; +// +//@RestController +//@Slf4j +//@RequestMapping("/v1/skills") +//public class SkillsController { +// private final SkillsService skillsService; +// +// public SkillsController(SkillsService skillsService) { +// this.skillsService = skillsService; +// } +// +// @PostMapping("/") +// public ResponseEntity createSkill(@RequestBody(required = true) @Valid SkillDRO skillDRO) { +// try { +// return ResponseEntity.status(HttpStatus.CREATED).body(skillsService.createSkill(skillDRO)); +// } catch (DataIntegrityViolationException ex) { +// return ResponseEntity.status(HttpStatus.CONFLICT) +// .body(new MessageResponse("Cannot create entry for Skill as Skill name is duplicate")); +// } catch (Exception ex) { +// log.error( +// "There is some error in storing the skills, error message: {}", ex.getMessage(), ex); +// throw ex; +// } +// } +// +// @GetMapping("/") +// public Page getAllSkills( +// @RequestParam(value = "offset", defaultValue = "0", required = false) @Min(0) int offset, +// @RequestParam(value = "limit", defaultValue = "10", required = false) @Min(1) int limit) { +// Pageable pageable = PageRequest.of(offset, limit); +// return skillsService.getAllSkills(pageable); +// } +// +// @GetMapping("/name/{name}") +// public ResponseEntity getSkillByName( +// @PathVariable(value = "name", required = true) String name) { +// SkillDTO skillDTO = skillsService.getSkillByName(name); +// if (ObjectUtils.isEmpty(skillDTO)) { +// return ResponseEntity.status(HttpStatus.NOT_FOUND) +// .body(new MessageResponse("Skill not found with the given name")); +// } +// return ResponseEntity.ok(skillDTO); +// } +// +// @GetMapping("/{id}") +// public ResponseEntity getSkillById(@PathVariable(value = "id", required = true) UUID id) { +// SkillDTO skillDTO = skillsService.getSkillById(id); +// if (ObjectUtils.isEmpty(skillDTO)) { +// return ResponseEntity.status(HttpStatus.NOT_FOUND) +// .body(new MessageResponse("Skill not found with given Id")); +// } +// return ResponseEntity.ok(skillDTO); +// } +//} + package com.RDS.skilltree.Skill; -import com.RDS.skilltree.utils.MessageResponse; +import com.RDS.skilltree.Common.Response.GenericResponse; +import com.RDS.skilltree.User.JwtUserModel; import jakarta.validation.Valid; -import jakarta.validation.constraints.Min; -import java.util.UUID; import lombok.extern.slf4j.Slf4j; import org.springframework.dao.DataIntegrityViolationException; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.PageRequest; -import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Example; +import org.springframework.data.domain.ExampleMatcher; import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.util.ObjectUtils; +import org.springframework.security.core.Authentication; import org.springframework.web.bind.annotation.*; -@RestController +import java.time.Instant; +import java.util.Collections; +import java.util.List; +import java.util.UUID; + @Slf4j +@RestController @RequestMapping("/v1/skills") public class SkillsController { - private final SkillsService skillsService; + private final SkillRepository repository; - public SkillsController(SkillsService skillsService) { - this.skillsService = skillsService; + public SkillsController(SkillRepository repository) { + this.repository = repository; } - @PostMapping("/") - public ResponseEntity createSkill(@RequestBody(required = true) @Valid SkillDRO skillDRO) { - try { - return ResponseEntity.status(HttpStatus.CREATED).body(skillsService.createSkill(skillDRO)); - } catch (DataIntegrityViolationException ex) { - return ResponseEntity.status(HttpStatus.CONFLICT) - .body(new MessageResponse("Cannot create entry for Skill as Skill name is duplicate")); - } catch (Exception ex) { - log.error( - "There is some error in storing the skills, error message: {}", ex.getMessage(), ex); - throw ex; + @GetMapping + public GenericResponse> getAllSkills(@RequestParam(required = false) String name) { + if (name != null && !name.isEmpty()) { + List skills = repository.findBy() + .map(Collections::singletonList) + .orElseGet(Collections::emptyList); + + ExampleMatcher matcher = ExampleMatcher.matching().withIgnoreCase("name"); + Example skills1 = Example.of(skills, matcher); + + return new GenericResponse<>(skills, null); } + return new GenericResponse<>(repository.findAll(), null); } - @GetMapping("/") - public Page getAllSkills( - @RequestParam(value = "offset", defaultValue = "0", required = false) @Min(0) int offset, - @RequestParam(value = "limit", defaultValue = "10", required = false) @Min(1) int limit) { - Pageable pageable = PageRequest.of(offset, limit); - return skillsService.getAllSkills(pageable); - } + @PostMapping + @ResponseStatus(HttpStatus.CREATED) + public Skill createSkill(Authentication authentication, @RequestBody(required = true) @Valid SkillDRO skill) { + JwtUserModel userDetails = (JwtUserModel) authentication.getPrincipal(); - @GetMapping("/name/{name}") - public ResponseEntity getSkillByName( - @PathVariable(value = "name", required = true) String name) { - SkillDTO skillDTO = skillsService.getSkillByName(name); - if (ObjectUtils.isEmpty(skillDTO)) { - return ResponseEntity.status(HttpStatus.NOT_FOUND) - .body(new MessageResponse("Skill not found with the given name")); - } - return ResponseEntity.ok(skillDTO); - } + Skill newSkill = Skill.builder() + .name(skill.getName()) + .type(skill.getType()) + // TODO : use the id from userDetails once login is implemented + .createdBy(UUID.fromString("ae7a6673-c557-41e0-838f-209de4c644fc")) + .isDeleted(false) + .build(); + + newSkill.setCreatedAt(Instant.now()); + newSkill.setUpdatedAt(Instant.now()); - @GetMapping("/{id}") - public ResponseEntity getSkillById(@PathVariable(value = "id", required = true) UUID id) { - SkillDTO skillDTO = skillsService.getSkillById(id); - if (ObjectUtils.isEmpty(skillDTO)) { - return ResponseEntity.status(HttpStatus.NOT_FOUND) - .body(new MessageResponse("Skill not found with given Id")); + try { + return repository.save(newSkill); + } catch (DataIntegrityViolationException error) { + log.error("Error saving skill {}, error: {}", skill.getName(), error.getMessage()); + throw error; } - return ResponseEntity.ok(skillDTO); } } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsServiceImpl.java b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsServiceImpl.java index 61943723..4c604c8b 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsServiceImpl.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsServiceImpl.java @@ -1,9 +1,11 @@ package com.RDS.skilltree.Skill; import com.RDS.skilltree.User.UserRepository; + import java.time.Instant; import java.util.Optional; import java.util.UUID; + import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.dao.DataIntegrityViolationException; @@ -19,28 +21,28 @@ public class SkillsServiceImpl implements SkillsService { private final UserRepository userRepository; @Override - public SkillDTO getSkillById(UUID id) { - Optional skillModel = skillRepository.findById(id); + public SkillDTO getSkillById(Integer id) { + Optional skillModel = skillRepository.findById(id); return skillModel.map(SkillDTO::getSkillsWithUsers).orElse(null); } @Override public SkillDTO getSkillByName(String skillName) { - Optional skillModel = skillRepository.findByName(skillName); + Optional skillModel = skillRepository.findByName(skillName); return skillModel.map(SkillDTO::getSkillsWithUsers).orElse(null); } @Override public Page getAllSkills(Pageable pageable) { - Page skillModels = skillRepository.findAll(pageable); + Page skillModels = skillRepository.findAll(pageable); return skillModels.map(SkillDTO::getSkillsWithUsers); } @Override public SkillDTO createSkill(SkillDRO skillDRO) { - SkillModel newSkill = SkillDRO.toModel(skillDRO); - newSkill.setCreatedAt(Instant.now()); - newSkill.setUpdatedAt(Instant.now()); + Skill newSkill = SkillDRO.toModel(skillDRO); +// newSkill.setCreatedAt(Instant.now()); +// newSkill.setUpdatedAt(Instant.now()); try { skillRepository.save(newSkill); diff --git a/skill-tree/src/main/java/com/RDS/skilltree/User/JwtUserModel.java b/skill-tree/src/main/java/com/RDS/skilltree/User/JwtUserModel.java new file mode 100644 index 00000000..29b92988 --- /dev/null +++ b/skill-tree/src/main/java/com/RDS/skilltree/User/JwtUserModel.java @@ -0,0 +1,14 @@ +package com.RDS.skilltree.User; + +import lombok.Getter; + +@Getter +public class JwtUserModel { + private final String rdsUserId; + private final UserRoleEnum role; + + public JwtUserModel(String rdsUserId, UserRoleEnum role) { + this.role = role; + this.rdsUserId = rdsUserId; + } +} diff --git a/skill-tree/src/main/java/com/RDS/skilltree/User/UserDRO.java b/skill-tree/src/main/java/com/RDS/skilltree/User/UserDRO.java index b0a468af..aff3097e 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/User/UserDRO.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/User/UserDRO.java @@ -20,19 +20,19 @@ public class UserDRO { private URL imageUrl; - private UserRole role; + private UserRoleEnum role; public static UserModel toModel(UserDRO user) { return UserModel.builder() .rdsUserId(user.getRdsUserId()) - .role(user.getRole()) +// .role(user.getRole()) .build(); } public static UserDRO fromModel(UserModel user) { return UserDRO.builder() .rdsUserId(user.getRdsUserId()) - .role(user.getRole()) +// .role(user.getRole()) .build(); } @@ -40,9 +40,9 @@ public static UserModel compareAndUpdateModel(UserModel user, UserDRO userDRO) { if (userDRO.getRdsUserId() != null) { user.setRdsUserId(user.getRdsUserId()); } - if (userDRO.getRole() != null) { - user.setRole(user.getRole()); - } +// if (userDRO.getRole() != null) { +// user.setRole(user.getRole()); +// } user.setUpdatedAt(Instant.now()); return user; } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/User/UserDTO.java b/skill-tree/src/main/java/com/RDS/skilltree/User/UserDTO.java index 869cabab..e5b97a3f 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/User/UserDTO.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/User/UserDTO.java @@ -4,7 +4,7 @@ import java.net.URL; import java.util.Set; import java.util.UUID; -import java.util.stream.Collectors; + import lombok.Builder; import lombok.Getter; @@ -22,7 +22,7 @@ public class UserDTO { private URL imageUrl; - private UserRole role; + private UserRoleEnum role; private Set skills; @@ -31,7 +31,6 @@ public static UserDTO toDTO(UserModel user) { return UserDTO.builder() .id(user.getId()) .rdsUserId(user.getRdsUserId()) - .role(user.getRole()) .build(); } @@ -43,7 +42,7 @@ public static UserDTO getUsersWithSkills(UserModel user) { .id(user.getId()) .rdsUserId(user.getRdsUserId()) // .skills(skills) - .role(user.getRole()) +// .role(user.getRole()) .build(); } } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/User/UserModel.java b/skill-tree/src/main/java/com/RDS/skilltree/User/UserModel.java index e3505001..fd8eab73 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/User/UserModel.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/User/UserModel.java @@ -23,8 +23,4 @@ public class UserModel extends TrackedProperties { @Column(name = "rds_user_id", unique = true) private String rdsUserId; - - @Column(name = "user_role", nullable = false) - @Enumerated(value = EnumType.STRING) - private UserRole role = UserRole.USER; } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/User/UserRole.java b/skill-tree/src/main/java/com/RDS/skilltree/User/UserRoleEnum.java similarity index 55% rename from skill-tree/src/main/java/com/RDS/skilltree/User/UserRole.java rename to skill-tree/src/main/java/com/RDS/skilltree/User/UserRoleEnum.java index 04e1adaa..78caeac6 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/User/UserRole.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/User/UserRoleEnum.java @@ -2,7 +2,7 @@ import java.util.Arrays; -public enum UserRole { +public enum UserRoleEnum { USER("user"), MEMBER("member"), SUPERUSER("super_user"), @@ -10,20 +10,20 @@ public enum UserRole { public final String label; - UserRole(String label) { + UserRoleEnum(String label) { this.label = label; } - public static UserRole fromString(String text) { - for (UserRole role : UserRole.values()) { + public static UserRoleEnum fromString(String text) { + for (UserRoleEnum role : UserRoleEnum.values()) { if (role.label.equalsIgnoreCase(text)) { return role; } } - return UserRole.GUEST; + return UserRoleEnum.GUEST; } public static String[] getAllRoles() { - return Arrays.stream(UserRole.values()).map(UserRole::name).toArray(String[]::new); + return Arrays.stream(UserRoleEnum.values()).map(UserRoleEnum::name).toArray(String[]::new); } } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/User/UserService.java b/skill-tree/src/main/java/com/RDS/skilltree/User/UserService.java index f853bf04..4f7d2a38 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/User/UserService.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/User/UserService.java @@ -12,5 +12,5 @@ public interface UserService { List getAllUsers(); - void addSkill(UUID skill, UUID userId); + void addSkill(Integer skill, UUID userId); } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/User/UserServiceImpl.java b/skill-tree/src/main/java/com/RDS/skilltree/User/UserServiceImpl.java index b62e4739..5739eb48 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/User/UserServiceImpl.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/User/UserServiceImpl.java @@ -1,12 +1,14 @@ package com.RDS.skilltree.User; import com.RDS.skilltree.Exceptions.NoEntityException; -import com.RDS.skilltree.Skill.SkillModel; +import com.RDS.skilltree.Skill.Skill; import com.RDS.skilltree.Skill.SkillRepository; import jakarta.transaction.Transactional; + import java.util.List; import java.util.Optional; import java.util.UUID; + import org.springframework.stereotype.Service; @Service @@ -27,7 +29,8 @@ public UserDTO createUser(UserDRO user) { } @Override - public void updateUser(UUID id, UserDRO user) {} + public void updateUser(UUID id, UserDRO user) { + } @Override public UserDTO getUserById(UUID id) { @@ -42,24 +45,25 @@ public List getAllUsers() { /** * updates the user and skill both + * * @param skillId * @param userId */ @Override @Transactional - public void addSkill(UUID skillId, UUID userId) { + public void addSkill(Integer skillId, UUID userId) { Optional userOptional = userRepository.findById(userId); - Optional skillOptional = skillRepository.findById(skillId); + Optional skillOptional = skillRepository.findById(skillId); if (userOptional.isPresent() && skillOptional.isPresent()) { UserModel userModel = userOptional.get(); - SkillModel skillModel = skillOptional.get(); + Skill skill = skillOptional.get(); // userModel.getSkills().add(skillModel); // skillModel.getUsers().add(userModel); userRepository.save(userModel); - skillRepository.save(skillModel); + skillRepository.save(skill); } else { if (skillOptional.isEmpty()) { throw new NoEntityException("Skill Id is not passed in the input"); diff --git a/skill-tree/src/main/java/com/RDS/skilltree/User/UserSkillsModel.java b/skill-tree/src/main/java/com/RDS/skilltree/User/UserSkillsModel.java index 481c8172..a068d2f7 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/User/UserSkillsModel.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/User/UserSkillsModel.java @@ -1,6 +1,6 @@ package com.RDS.skilltree.User; -import com.RDS.skilltree.Skill.SkillModel; +import com.RDS.skilltree.Skill.Skill; import com.fasterxml.jackson.annotation.JsonBackReference; import jakarta.persistence.*; @@ -29,7 +29,7 @@ public class UserSkillsModel { @JsonBackReference @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "skill_id", nullable = false) - private SkillModel skill; + private Skill skill; @Enumerated(EnumType.STRING) @Column(name = "status", nullable = false) diff --git a/skill-tree/src/main/java/com/RDS/skilltree/utils/TrackedProperties.java b/skill-tree/src/main/java/com/RDS/skilltree/utils/TrackedProperties.java index 23601457..127d73a2 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/utils/TrackedProperties.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/utils/TrackedProperties.java @@ -9,16 +9,9 @@ @Data @MappedSuperclass public abstract class TrackedProperties { - - @Column(name = "created_by") - private UUID createdBy; - - @Column(name = "updated_by") - private UUID updatedBy; - - @Column(name = "created_at") + @Column(name = "created_at", nullable = false) private Instant createdAt; - @Column(name = "updated_at") + @Column(name = "updated_at", nullable = false) private Instant updatedAt; } diff --git a/skill-tree/src/test/java/com/RDS/skilltree/integration/EndorsementsIntegrationTests.java b/skill-tree/src/test/java/com/RDS/skilltree/integration/EndorsementsIntegrationTests.java index a4b5cb1b..a9279675 100644 --- a/skill-tree/src/test/java/com/RDS/skilltree/integration/EndorsementsIntegrationTests.java +++ b/skill-tree/src/test/java/com/RDS/skilltree/integration/EndorsementsIntegrationTests.java @@ -46,7 +46,7 @@ private void addData() throws MalformedURLException { user = userService.createUser( UserDRO.builder() - .role(UserRole.MEMBER) + .role(UserRoleEnum.MEMBER) .rdsUserId("p6Bo61VEClhtVdwW0ihg") .lastName("Doe") .firstName("John") diff --git a/skill-tree/src/test/java/com/RDS/skilltree/integration/SkillsIntegrationTests.java b/skill-tree/src/test/java/com/RDS/skilltree/integration/SkillsIntegrationTests.java index 9fd65268..aff148c6 100644 --- a/skill-tree/src/test/java/com/RDS/skilltree/integration/SkillsIntegrationTests.java +++ b/skill-tree/src/test/java/com/RDS/skilltree/integration/SkillsIntegrationTests.java @@ -41,7 +41,7 @@ private void addData() throws MalformedURLException { user = userService.createUser( UserDRO.builder() - .role(UserRole.MEMBER) + .role(UserRoleEnum.MEMBER) .rdsUserId("p6Bo61VEClhtVdwW0ihg") .lastName("Doe") .firstName("John") diff --git a/skill-tree/src/test/java/com/RDS/skilltree/unit/EndorsementServiceTest.java b/skill-tree/src/test/java/com/RDS/skilltree/unit/EndorsementServiceTest.java index 2ee4dcef..4f4ecb48 100644 --- a/skill-tree/src/test/java/com/RDS/skilltree/unit/EndorsementServiceTest.java +++ b/skill-tree/src/test/java/com/RDS/skilltree/unit/EndorsementServiceTest.java @@ -8,19 +8,21 @@ import com.RDS.skilltree.Exceptions.EntityAlreadyExistsException; import com.RDS.skilltree.Exceptions.InvalidParameterException; import com.RDS.skilltree.Exceptions.NoEntityException; -import com.RDS.skilltree.Skill.SkillModel; +import com.RDS.skilltree.Skill.Skill; import com.RDS.skilltree.Skill.SkillRepository; import com.RDS.skilltree.User.UserModel; import com.RDS.skilltree.User.UserRepository; -import com.RDS.skilltree.User.UserRole; +import com.RDS.skilltree.User.UserRoleEnum; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.persistence.EntityNotFoundException; + import java.io.IOException; import java.io.InputStream; import java.time.Instant; import java.time.LocalDateTime; import java.util.*; + import org.junit.jupiter.api.*; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; @@ -41,17 +43,24 @@ @ExtendWith(MockitoExtension.class) public class EndorsementServiceTest { - @Mock private EndorsementRepository endorsementRepository; + @Mock + private EndorsementRepository endorsementRepository; - @Mock private UserRepository userRepository; + @Mock + private UserRepository userRepository; - @Mock private SkillRepository skillRepository; + @Mock + private SkillRepository skillRepository; - @Mock private ObjectMapper objectMapper; + @Mock + private ObjectMapper objectMapper; - @InjectMocks @Autowired private EndorsementServiceImpl endorsementService; + @InjectMocks + @Autowired + private EndorsementServiceImpl endorsementService; - @Mock private Authentication auth; + @Mock + private Authentication auth; @BeforeEach public void setUp() { @@ -67,9 +76,9 @@ public void clearSecurityContext() { private void setupUpdateEndorsementTests(Boolean useSuperUserRole) { UserModel userModel = new UserModel(); if (useSuperUserRole) { - userModel.setRole(UserRole.SUPERUSER); + userModel.setRole(UserRoleEnum.SUPERUSER); } else { - userModel.setRole(UserRole.USER); + userModel.setRole(UserRoleEnum.USER); } when(auth.getPrincipal()).thenReturn(userModel); SecurityContextHolder.getContext().setAuthentication(auth); @@ -81,12 +90,12 @@ public void itShouldGetEndorsementsById() { UUID endorserId = UUID.randomUUID(); UUID skillId = UUID.randomUUID(); - SkillModel skillModel = SkillModel.builder().id(skillId).build(); + Skill skill = Skill.builder().id(skillId).build(); EndorsementModel endorsementModel = EndorsementModel.builder() .id(endorsementId) .endorseeId(endorserId) - .skill(skillModel) + .skill(skill) .build(); endorsementModel.setCreatedAt(Instant.now()); endorsementModel.setUpdatedAt(Instant.now()); @@ -124,8 +133,8 @@ public void itShouldReturnEndorsementsGivenSkillID() throws IOException { UUID.randomUUID())); when(objectMapper.readValue( - ArgumentMatchers.any(), - ArgumentMatchers.>>any())) + ArgumentMatchers.any(), + ArgumentMatchers.>>any())) .thenReturn(dummyEndorsements); Page result = endorsementService.getEndorsementsFromDummyData(pageRequest, skillID, null); @@ -154,8 +163,8 @@ void itShouldGetEndorsementsGivenUserID() throws IOException { UUID.randomUUID())); when(objectMapper.readValue( - ArgumentMatchers.any(), - ArgumentMatchers.>>any())) + ArgumentMatchers.any(), + ArgumentMatchers.>>any())) .thenReturn(dummyEndorsements); Page result = endorsementService.getEndorsementsFromDummyData(pageRequest, null, userID); @@ -184,8 +193,8 @@ public void itShouldThrowErrorIfInvalidUserIDIsGiven() throws IOException { UUID.randomUUID())); when(objectMapper.readValue( - ArgumentMatchers.any(), - ArgumentMatchers.>>any())) + ArgumentMatchers.any(), + ArgumentMatchers.>>any())) .thenReturn(dummyEndorsements); assertThrows( @@ -214,8 +223,8 @@ public void itShouldThrowIllegalArgumentExceptionIfInvalidSkillIDIsGiven() throw UUID.randomUUID())); when(objectMapper.readValue( - ArgumentMatchers.any(), - ArgumentMatchers.>>any())) + ArgumentMatchers.any(), + ArgumentMatchers.>>any())) .thenReturn(dummyEndorsements); assertThrows( @@ -244,8 +253,8 @@ public void itShouldThrowIllegalArgumentExceptionIfInvalidUserIDIsGiven() throws UUID.randomUUID())); when(objectMapper.readValue( - ArgumentMatchers.any(), - ArgumentMatchers.>>any())) + ArgumentMatchers.any(), + ArgumentMatchers.>>any())) .thenReturn(dummyEndorsements); assertThrows( @@ -277,8 +286,8 @@ public void itShouldReturnPaginatedResultOnSearch() throws IOException { } when(objectMapper.readValue( - ArgumentMatchers.any(), - ArgumentMatchers.>>any())) + ArgumentMatchers.any(), + ArgumentMatchers.>>any())) .thenReturn(dummyEndorsements); Page result = endorsementService.getEndorsementsFromDummyData(pageRequest, null, userID); @@ -308,8 +317,8 @@ public void itShouldReturnEmptyPaginatedResultOnSearch() throws IOException { } when(objectMapper.readValue( - ArgumentMatchers.any(), - ArgumentMatchers.>>any())) + ArgumentMatchers.any(), + ArgumentMatchers.>>any())) .thenReturn(dummyEndorsements); Page result = endorsementService.getEndorsementsFromDummyData(pageRequest, null, userID); @@ -339,8 +348,8 @@ public void itShouldReturnEmptyDataGivenUserIDAndSkillIDNotPresent() throws IOEx List endorsementsResult = new ArrayList<>(); when(objectMapper.readValue( - ArgumentMatchers.any(), - ArgumentMatchers.>>any())) + ArgumentMatchers.any(), + ArgumentMatchers.>>any())) .thenReturn(dummyEndorsements); Page result = endorsementService.getEndorsementsFromDummyData(pageRequest, skillID, userID); @@ -360,8 +369,8 @@ public void itShouldReturnEmptyDataWhenNoEndorsementsArePresent() throws IOExcep List dummyEndorsements = new ArrayList<>(); when(objectMapper.readValue( - ArgumentMatchers.any(), - ArgumentMatchers.>>any())) + ArgumentMatchers.any(), + ArgumentMatchers.>>any())) .thenReturn(dummyEndorsements); Page result = endorsementService.getEndorsementsFromDummyData(pageRequest, skillID, userID); @@ -391,8 +400,8 @@ public void itShouldReturnEmptyDataGivenUserIDNotPresent() throws IOException { List endorsementsResult = new ArrayList<>(); when(objectMapper.readValue( - ArgumentMatchers.any(), - ArgumentMatchers.>>any())) + ArgumentMatchers.any(), + ArgumentMatchers.>>any())) .thenReturn(dummyEndorsements); Page result = endorsementService.getEndorsementsFromDummyData( @@ -423,8 +432,8 @@ public void itShouldReturnEmptyDataGivenEmptyUserIDAndSkillID() throws IOExcepti UUID.randomUUID())); when(objectMapper.readValue( - ArgumentMatchers.any(), - ArgumentMatchers.>>any())) + ArgumentMatchers.any(), + ArgumentMatchers.>>any())) .thenReturn(dummyEndorsements); Page result = endorsementService.getEndorsementsFromDummyData(pageRequest, "", ""); @@ -454,8 +463,8 @@ public void itShouldReturnEmptyDataGivenSkillIDNotPresent() throws IOException { List endorsementsResult = new ArrayList<>(); when(objectMapper.readValue( - ArgumentMatchers.any(), - ArgumentMatchers.>>any())) + ArgumentMatchers.any(), + ArgumentMatchers.>>any())) .thenReturn(dummyEndorsements); Page result = endorsementService.getEndorsementsFromDummyData( @@ -487,8 +496,8 @@ public void itShouldReturnEmptyDataGivenSkillIDAndUserIDNotPresent() throws IOEx List endorsementsResult = new ArrayList<>(); when(objectMapper.readValue( - ArgumentMatchers.any(), - ArgumentMatchers.>>any())) + ArgumentMatchers.any(), + ArgumentMatchers.>>any())) .thenReturn(dummyEndorsements); Page result = endorsementService.getEndorsementsFromDummyData( @@ -506,8 +515,8 @@ void itShouldReturnIOExceptionIfErrorReadingData() throws IOException { String skillID = null; String userID = null; when(objectMapper.readValue( - ArgumentMatchers.any(), - ArgumentMatchers.>>any())) + ArgumentMatchers.any(), + ArgumentMatchers.>>any())) .thenThrow(new IOException("Error reading data")); assertThrows( @@ -541,7 +550,7 @@ void testCreateEndorsement() { endorsementDRO.setEndorseeId(endorserId); endorsementDRO.setSkillId(skillId); - SkillModel mockSkill = SkillModel.builder().id(skillId).build(); + Skill mockSkill = Skill.builder().id(skillId).build(); EndorsementModel mockEndorsement = EndorsementModel.builder() .id(endorsementId) @@ -648,7 +657,7 @@ public void itShouldThrowEntityAlreadyExistsExceptionGivenEndorsementIsUpdated() UUID skillId = UUID.randomUUID(); UUID endorsementId = UUID.randomUUID(); - SkillModel mockSkill = SkillModel.builder().id(skillId).build(); + Skill mockSkill = Skill.builder().id(skillId).build(); EndorsementModel mockEndorsement = EndorsementModel.builder() .id(endorsementId) @@ -704,7 +713,7 @@ public void itShouldUpdateEndorsementStatusGivenEndorsementIdAndStatusApprovedOr UUID endorsementId = UUID.randomUUID(); EndorsementStatus status = EndorsementStatus.APPROVED; - SkillModel mockSkill = SkillModel.builder().id(skillId).build(); + Skill mockSkill = Skill.builder().id(skillId).build(); EndorsementModel mockEndorsement = EndorsementModel.builder() .id(endorsementId) diff --git a/skill-tree/src/test/java/com/RDS/skilltree/unit/SkillsServiceTest.java b/skill-tree/src/test/java/com/RDS/skilltree/unit/SkillsServiceTest.java index bf5e0bf8..4fa0ff85 100644 --- a/skill-tree/src/test/java/com/RDS/skilltree/unit/SkillsServiceTest.java +++ b/skill-tree/src/test/java/com/RDS/skilltree/unit/SkillsServiceTest.java @@ -6,13 +6,12 @@ import static org.mockito.Mockito.when; import com.RDS.skilltree.Skill.SkillDTO; -import com.RDS.skilltree.Skill.SkillModel; +import com.RDS.skilltree.Skill.Skill; import com.RDS.skilltree.Skill.SkillRepository; import com.RDS.skilltree.Skill.SkillsServiceImpl; -import java.util.Arrays; -import java.util.List; -import java.util.Optional; -import java.util.UUID; + +import java.util.*; + import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; @@ -27,16 +26,19 @@ @ExtendWith(MockitoExtension.class) public class SkillsServiceTest { - @Mock private SkillRepository skillRepository; + @Mock + private SkillRepository skillRepository; - @InjectMocks @Autowired private SkillsServiceImpl skillService; + @InjectMocks + @Autowired + private SkillsServiceImpl skillService; @Test public void testGetSkillById() { - UUID skillId = UUID.randomUUID(); - SkillModel skillModel = SkillModel.builder().id(skillId).build(); + Integer skillId = new Random().nextInt(); + Skill skill = Skill.builder().id(skillId).build(); - when(skillRepository.findById(skillId)).thenReturn(Optional.of(skillModel)); + when(skillRepository.findById(skillId)).thenReturn(Optional.of(skill)); SkillDTO result = skillService.getSkillById(skillId); assertNotNull(result); @@ -46,9 +48,9 @@ public void testGetSkillById() { @Test public void testGetSkillsByName() { String skillName = "Java"; - SkillModel skillModel = SkillModel.builder().name("Java").build(); + Skill skill = Skill.builder().name("Java").build(); - when(skillRepository.findByName(skillName)).thenReturn(Optional.of(skillModel)); + when(skillRepository.findByName(skillName)).thenReturn(Optional.of(skill)); SkillDTO result = skillService.getSkillByName("Java"); assertNotNull(result); @@ -58,24 +60,24 @@ public void testGetSkillsByName() { @Test public void testGetAllSkills() { - SkillModel skillJava = SkillModel.builder().name("Java").build(); + Skill skillJava = Skill.builder().name("Java").build(); - SkillModel skillGo = SkillModel.builder().name("Go").build(); + Skill skillGo = Skill.builder().name("Go").build(); - List skillModelList = Arrays.asList(skillJava, skillGo); + List skillList = Arrays.asList(skillJava, skillGo); when(skillRepository.findAll((Pageable) any(Pageable.class))) - .thenReturn(new PageImpl<>(skillModelList)); + .thenReturn(new PageImpl<>(skillList)); Pageable pageable = PageRequest.of(2, 1); Page resultPage = skillService.getAllSkills(pageable); assertNotNull(resultPage); assertEquals( - skillModelList.size(), + skillList.size(), resultPage.getTotalElements(), "The number of elements returned is not equal to the expected size"); assertEquals( - skillModelList.size(), + skillList.size(), resultPage.getContent().size(), "The content returned is not equal to the expected content"); assertEquals( From 2d73260c296f46ab12dcff10ade9add5fc9f80bb Mon Sep 17 00:00:00 2001 From: yash raj Date: Mon, 24 Jun 2024 09:15:16 +0530 Subject: [PATCH 03/31] change id to integer --- .../src/main/java/com/RDS/skilltree/Skill/SkillsService.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsService.java b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsService.java index b8a4e015..a4001882 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsService.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsService.java @@ -1,11 +1,10 @@ package com.RDS.skilltree.Skill; -import java.util.UUID; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; public interface SkillsService { - SkillDTO getSkillById(UUID id); + SkillDTO getSkillById(Integer id); SkillDTO getSkillByName(String skillName); From b1b20a097253880d5eac567a1629b96d0f9b16ae Mon Sep 17 00:00:00 2001 From: yash raj Date: Mon, 24 Jun 2024 15:35:36 +0530 Subject: [PATCH 04/31] check if a skill already exists before creating one, make `updated_at` optional in TrackedProperties --- .../RDS/skilltree/Skill/SkillsController.java | 24 +++++++------------ .../skilltree/utils/TrackedProperties.java | 2 +- .../integration/SkillsIntegrationTests.java | 6 ++--- 3 files changed, 13 insertions(+), 19 deletions(-) diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsController.java b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsController.java index 8ecdf1e8..0fd5bfbb 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsController.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsController.java @@ -78,6 +78,7 @@ import org.springframework.data.domain.Example; import org.springframework.data.domain.ExampleMatcher; import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.security.core.Authentication; import org.springframework.web.bind.annotation.*; @@ -97,25 +98,19 @@ public SkillsController(SkillRepository repository) { } @GetMapping - public GenericResponse> getAllSkills(@RequestParam(required = false) String name) { - if (name != null && !name.isEmpty()) { - List skills = repository.findBy() - .map(Collections::singletonList) - .orElseGet(Collections::emptyList); - - ExampleMatcher matcher = ExampleMatcher.matching().withIgnoreCase("name"); - Example skills1 = Example.of(skills, matcher); - - return new GenericResponse<>(skills, null); - } + public GenericResponse> getAllSkills() { return new GenericResponse<>(repository.findAll(), null); } @PostMapping @ResponseStatus(HttpStatus.CREATED) - public Skill createSkill(Authentication authentication, @RequestBody(required = true) @Valid SkillDRO skill) { + public GenericResponse createSkill(Authentication authentication, @RequestBody(required = true) @Valid SkillDRO skill) { JwtUserModel userDetails = (JwtUserModel) authentication.getPrincipal(); + if (repository.findByName(skill.getName()).isPresent()){ + return new GenericResponse<>(null, String.format("Skill with name %s already exists", skill.getName())); + } + Skill newSkill = Skill.builder() .name(skill.getName()) .type(skill.getType()) @@ -125,13 +120,12 @@ public Skill createSkill(Authentication authentication, @RequestBody(required = .build(); newSkill.setCreatedAt(Instant.now()); - newSkill.setUpdatedAt(Instant.now()); try { - return repository.save(newSkill); + return new GenericResponse<>(repository.save(newSkill), "Skill created"); } catch (DataIntegrityViolationException error) { log.error("Error saving skill {}, error: {}", skill.getName(), error.getMessage()); - throw error; + return new GenericResponse<>(null, "Something went wrong please try again"); } } } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/utils/TrackedProperties.java b/skill-tree/src/main/java/com/RDS/skilltree/utils/TrackedProperties.java index 127d73a2..7cb55e6f 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/utils/TrackedProperties.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/utils/TrackedProperties.java @@ -12,6 +12,6 @@ public abstract class TrackedProperties { @Column(name = "created_at", nullable = false) private Instant createdAt; - @Column(name = "updated_at", nullable = false) + @Column(name = "updated_at") private Instant updatedAt; } diff --git a/skill-tree/src/test/java/com/RDS/skilltree/integration/SkillsIntegrationTests.java b/skill-tree/src/test/java/com/RDS/skilltree/integration/SkillsIntegrationTests.java index aff148c6..735e32dd 100644 --- a/skill-tree/src/test/java/com/RDS/skilltree/integration/SkillsIntegrationTests.java +++ b/skill-tree/src/test/java/com/RDS/skilltree/integration/SkillsIntegrationTests.java @@ -37,7 +37,7 @@ public SkillsIntegrationTests( } @BeforeEach - private void addData() throws MalformedURLException { + public void addData() throws MalformedURLException { user = userService.createUser( UserDRO.builder() @@ -56,7 +56,7 @@ private void addData() throws MalformedURLException { } @AfterEach - private void cleanUp() { + public void cleanUp() { skillRepository.deleteAll(); userRepository.deleteAll(); } @@ -109,7 +109,7 @@ public void testAPIReturns200_OnNoSkillsFound() { @Test @DisplayName("Return 200, on skill found given skillId") public void testAPIReturns200_OnSkillFoundById() { - UUID skillId = skill.getId(); + Integer skillId = skill.getId(); Response response = given() From 8063f408c85030ec98b1d3af2d314e5e2a1f5eef Mon Sep 17 00:00:00 2001 From: yash raj Date: Mon, 24 Jun 2024 20:09:02 +0530 Subject: [PATCH 05/31] remove unit and integration test and remove skill service & skill service implementation --- .../com/RDS/skilltree/Skill/SkillDRO.java | 10 +- .../com/RDS/skilltree/Skill/SkillDTO.java | 5 +- .../RDS/skilltree/Skill/SkillRepository.java | 2 - .../RDS/skilltree/Skill/SkillsController.java | 83 +- .../RDS/skilltree/Skill/SkillsService.java | 14 - .../skilltree/Skill/SkillsServiceImpl.java | 59 -- .../src/main/resources/application.properties | 2 +- .../EndorsementsIntegrationTests.java | 589 -------------- .../SecurityContextIntegrationTest.java | 60 -- .../integration/SkillsIntegrationTests.java | 268 ------- .../integration/TestContainerManager.java | 29 - .../unit/EndorsementListServiceTest.java | 95 --- .../unit/EndorsementServiceTest.java | 755 ------------------ .../RDS/skilltree/unit/HealthCheckTest.java | 38 - .../RDS/skilltree/unit/SkillsServiceTest.java | 92 --- 15 files changed, 10 insertions(+), 2091 deletions(-) delete mode 100644 skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsService.java delete mode 100644 skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsServiceImpl.java delete mode 100644 skill-tree/src/test/java/com/RDS/skilltree/integration/EndorsementsIntegrationTests.java delete mode 100644 skill-tree/src/test/java/com/RDS/skilltree/integration/SecurityContextIntegrationTest.java delete mode 100644 skill-tree/src/test/java/com/RDS/skilltree/integration/SkillsIntegrationTests.java delete mode 100644 skill-tree/src/test/java/com/RDS/skilltree/integration/TestContainerManager.java delete mode 100644 skill-tree/src/test/java/com/RDS/skilltree/unit/EndorsementListServiceTest.java delete mode 100644 skill-tree/src/test/java/com/RDS/skilltree/unit/EndorsementServiceTest.java delete mode 100644 skill-tree/src/test/java/com/RDS/skilltree/unit/HealthCheckTest.java delete mode 100644 skill-tree/src/test/java/com/RDS/skilltree/unit/SkillsServiceTest.java diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDRO.java b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDRO.java index 564901a4..3ef8848c 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDRO.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDRO.java @@ -2,7 +2,9 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import jakarta.validation.constraints.NotNull; + import java.util.UUID; + import lombok.Builder; import lombok.Getter; @@ -15,12 +17,4 @@ public class SkillDRO { @NotNull(message = "SkillType cannot be null") private SkillType type; - - public static Skill toModel(SkillDRO skillDRO) { - return Skill.builder() - .name(skillDRO.getName()) - .type(skillDRO.getType()) - .isDeleted(false) - .build(); - } } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDTO.java b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDTO.java index 734821bf..46880e1c 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDTO.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDTO.java @@ -2,6 +2,7 @@ import com.RDS.skilltree.User.UserDTO; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; + import java.util.HashSet; import java.util.Set; import java.util.UUID; @@ -28,9 +29,7 @@ public static SkillDTO toDto(Skill skill) { public static SkillDTO getSkillsWithUsers(Skill skill) { Set users = new HashSet<>(); -// if (skillModel.getUsers() != null) { -// users = skillModel.getUsers().stream().map(UserDTO::toDTO).collect(Collectors.toSet()); -// } + return SkillDTO.builder() .id(skill.getId()) .name(skill.getName()) diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillRepository.java b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillRepository.java index 9e9da24b..39f048dc 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillRepository.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillRepository.java @@ -9,6 +9,4 @@ @Repository public interface SkillRepository extends JpaRepository { Optional findByName(String name); - -// Page findAll(Pageable pageable); } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsController.java b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsController.java index 0fd5bfbb..0a0bd5f2 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsController.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsController.java @@ -1,73 +1,3 @@ -//package com.RDS.skilltree.Skill; -// -//import com.RDS.skilltree.utils.MessageResponse; -//import jakarta.validation.Valid; -//import jakarta.validation.constraints.Min; -//import java.util.UUID; -//import lombok.extern.slf4j.Slf4j; -//import org.springframework.dao.DataIntegrityViolationException; -//import org.springframework.data.domain.Page; -//import org.springframework.data.domain.PageRequest; -//import org.springframework.data.domain.Pageable; -//import org.springframework.http.HttpStatus; -//import org.springframework.http.ResponseEntity; -//import org.springframework.util.ObjectUtils; -//import org.springframework.web.bind.annotation.*; -// -//@RestController -//@Slf4j -//@RequestMapping("/v1/skills") -//public class SkillsController { -// private final SkillsService skillsService; -// -// public SkillsController(SkillsService skillsService) { -// this.skillsService = skillsService; -// } -// -// @PostMapping("/") -// public ResponseEntity createSkill(@RequestBody(required = true) @Valid SkillDRO skillDRO) { -// try { -// return ResponseEntity.status(HttpStatus.CREATED).body(skillsService.createSkill(skillDRO)); -// } catch (DataIntegrityViolationException ex) { -// return ResponseEntity.status(HttpStatus.CONFLICT) -// .body(new MessageResponse("Cannot create entry for Skill as Skill name is duplicate")); -// } catch (Exception ex) { -// log.error( -// "There is some error in storing the skills, error message: {}", ex.getMessage(), ex); -// throw ex; -// } -// } -// -// @GetMapping("/") -// public Page getAllSkills( -// @RequestParam(value = "offset", defaultValue = "0", required = false) @Min(0) int offset, -// @RequestParam(value = "limit", defaultValue = "10", required = false) @Min(1) int limit) { -// Pageable pageable = PageRequest.of(offset, limit); -// return skillsService.getAllSkills(pageable); -// } -// -// @GetMapping("/name/{name}") -// public ResponseEntity getSkillByName( -// @PathVariable(value = "name", required = true) String name) { -// SkillDTO skillDTO = skillsService.getSkillByName(name); -// if (ObjectUtils.isEmpty(skillDTO)) { -// return ResponseEntity.status(HttpStatus.NOT_FOUND) -// .body(new MessageResponse("Skill not found with the given name")); -// } -// return ResponseEntity.ok(skillDTO); -// } -// -// @GetMapping("/{id}") -// public ResponseEntity getSkillById(@PathVariable(value = "id", required = true) UUID id) { -// SkillDTO skillDTO = skillsService.getSkillById(id); -// if (ObjectUtils.isEmpty(skillDTO)) { -// return ResponseEntity.status(HttpStatus.NOT_FOUND) -// .body(new MessageResponse("Skill not found with given Id")); -// } -// return ResponseEntity.ok(skillDTO); -// } -//} - package com.RDS.skilltree.Skill; import com.RDS.skilltree.Common.Response.GenericResponse; @@ -75,15 +5,11 @@ import jakarta.validation.Valid; import lombok.extern.slf4j.Slf4j; import org.springframework.dao.DataIntegrityViolationException; -import org.springframework.data.domain.Example; -import org.springframework.data.domain.ExampleMatcher; import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; import org.springframework.security.core.Authentication; import org.springframework.web.bind.annotation.*; import java.time.Instant; -import java.util.Collections; import java.util.List; import java.util.UUID; @@ -98,7 +24,8 @@ public SkillsController(SkillRepository repository) { } @GetMapping - public GenericResponse> getAllSkills() { + public GenericResponse> getAllSkills(@RequestParam(required = false) String name) { + repository.findByName(name); return new GenericResponse<>(repository.findAll(), null); } @@ -107,9 +34,9 @@ public GenericResponse> getAllSkills() { public GenericResponse createSkill(Authentication authentication, @RequestBody(required = true) @Valid SkillDRO skill) { JwtUserModel userDetails = (JwtUserModel) authentication.getPrincipal(); - if (repository.findByName(skill.getName()).isPresent()){ - return new GenericResponse<>(null, String.format("Skill with name %s already exists", skill.getName())); - } + if (repository.findByName(skill.getName()).isPresent()) { + return new GenericResponse<>(null, String.format("Skill with name %s already exists", skill.getName())); + } Skill newSkill = Skill.builder() .name(skill.getName()) diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsService.java b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsService.java deleted file mode 100644 index a4001882..00000000 --- a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsService.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.RDS.skilltree.Skill; - -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; - -public interface SkillsService { - SkillDTO getSkillById(Integer id); - - SkillDTO getSkillByName(String skillName); - - Page getAllSkills(Pageable pageable); - - SkillDTO createSkill(SkillDRO skillDRO); -} diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsServiceImpl.java b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsServiceImpl.java deleted file mode 100644 index 4c604c8b..00000000 --- a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsServiceImpl.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.RDS.skilltree.Skill; - -import com.RDS.skilltree.User.UserRepository; - -import java.time.Instant; -import java.util.Optional; -import java.util.UUID; - -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.springframework.dao.DataIntegrityViolationException; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; -import org.springframework.stereotype.Service; - -@Service -@Slf4j -@RequiredArgsConstructor -public class SkillsServiceImpl implements SkillsService { - private final SkillRepository skillRepository; - private final UserRepository userRepository; - - @Override - public SkillDTO getSkillById(Integer id) { - Optional skillModel = skillRepository.findById(id); - return skillModel.map(SkillDTO::getSkillsWithUsers).orElse(null); - } - - @Override - public SkillDTO getSkillByName(String skillName) { - Optional skillModel = skillRepository.findByName(skillName); - return skillModel.map(SkillDTO::getSkillsWithUsers).orElse(null); - } - - @Override - public Page getAllSkills(Pageable pageable) { - Page skillModels = skillRepository.findAll(pageable); - return skillModels.map(SkillDTO::getSkillsWithUsers); - } - - @Override - public SkillDTO createSkill(SkillDRO skillDRO) { - Skill newSkill = SkillDRO.toModel(skillDRO); -// newSkill.setCreatedAt(Instant.now()); -// newSkill.setUpdatedAt(Instant.now()); - - try { - skillRepository.save(newSkill); - } catch (DataIntegrityViolationException ex) { - log.error( - "Error saving the skills object with name : {}, with exception :{}", - skillDRO.getName(), - ex.getMessage(), - ex); - throw ex; - } - return SkillDTO.toDto(newSkill); - } -} diff --git a/skill-tree/src/main/resources/application.properties b/skill-tree/src/main/resources/application.properties index 098a53b3..49a55de9 100644 --- a/skill-tree/src/main/resources/application.properties +++ b/skill-tree/src/main/resources/application.properties @@ -8,7 +8,7 @@ jwt.rds.public.key=${RDS_PUBLIC_KEY} API_V1_PREFIX=/api/v1 spring.datasource.version=8.1.0 management.endpoints.web.exposure.include=health,info,metrics -logging.level.root=ERROR +logging.level.root=DEBUG # If no value received from env default is dev spring.profiles.active=${SPRING_PROFILES_ACTIVE:dev} # TODO:Dummy JSON code, needs to be changed as part of #103 diff --git a/skill-tree/src/test/java/com/RDS/skilltree/integration/EndorsementsIntegrationTests.java b/skill-tree/src/test/java/com/RDS/skilltree/integration/EndorsementsIntegrationTests.java deleted file mode 100644 index a9279675..00000000 --- a/skill-tree/src/test/java/com/RDS/skilltree/integration/EndorsementsIntegrationTests.java +++ /dev/null @@ -1,589 +0,0 @@ -package com.RDS.skilltree.integration; - -import static io.restassured.RestAssured.given; -import static org.hamcrest.Matchers.*; - -import com.RDS.skilltree.Endorsement.*; -import com.RDS.skilltree.Skill.*; -import com.RDS.skilltree.User.*; -import io.restassured.response.Response; -import java.net.MalformedURLException; -import java.net.URL; -import java.util.UUID; -import org.junit.jupiter.api.*; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import utils.RestAPIHelper; - -@TestInstance(TestInstance.Lifecycle.PER_CLASS) -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) -public class EndorsementsIntegrationTests extends TestContainerManager { - - private UserRepository userRepository; - private SkillRepository skillRepository; - private final UserService userService; - private final SkillsService skillsService; - private UserDTO user; - private SkillDTO skill; - private EndorsementRepository endorsementRepository; - - @Autowired - public EndorsementsIntegrationTests( - UserService userService, - UserRepository userRepository, - SkillsService skillsService, - SkillRepository skillRepository, - EndorsementRepository endorsementRepository) { - this.userService = userService; - this.userRepository = userRepository; - this.skillsService = skillsService; - this.skillRepository = skillRepository; - this.endorsementRepository = endorsementRepository; - } - - @BeforeEach - private void addData() throws MalformedURLException { - user = - userService.createUser( - UserDRO.builder() - .role(UserRoleEnum.MEMBER) - .rdsUserId("p6Bo61VEClhtVdwW0ihg") - .lastName("Doe") - .firstName("John") - .imageUrl( - new URL( - "https://res.cloudinary.com/realdevsquad/image/upload/v1666193594/profile/p6Bo61VEClhtVdwW0iGH/lezguwdq5bgzawa3.jpg")) - .build()); - - skill = - skillsService.createSkill( - SkillDRO.builder().name("Java").type(SkillType.ATOMIC).createdBy(user.getId()).build()); - } - - @AfterEach - private void cleanUp() { - endorsementRepository.deleteAll(); - skillRepository.deleteAll(); - userRepository.deleteAll(); - } - - private UUID createEndorsementModel(Boolean isStatusPending) { - EndorsementStatus endorsementStatus; - if (isStatusPending) { - endorsementStatus = EndorsementStatus.PENDING; - } else { - endorsementStatus = EndorsementStatus.APPROVED; - } - EndorsementModel endorsementModel = - EndorsementModel.builder().status(endorsementStatus).build(); - return endorsementRepository.save(endorsementModel).getId(); - } - - @Test - @Disabled - @DisplayName("Fetch all the endorsements") - public void testAPIReturnsAllEndorsements() { - Response response = given().cookies(RestAPIHelper.getUserCookie()).get("/endorsements"); - - response - .then() - .statusCode(200) - .contentType("application/json") - .body("data", hasSize(1)) - .body("data[0].endorseeId", equalTo("user-1")) - .body("data[0].skillName", equalTo("Java")) - .body("data[0].status", anyOf(equalTo("APPROVED"), equalTo("REJECTED"), equalTo("PENDING"))) - .body("data[0].endorsementType", anyOf(equalTo("POSITIVE"), equalTo("NEGATIVE"))) - .body("data[0].endorsersList", hasSize(1)) - .body("data[0].endorserList[0].endorseeId", equalTo("user-2")) - .body("data[0].endorserList[0].description", isA(String.class)) - .body( - "data[0].endorserList[0].userType", - anyOf(equalTo("NORMAL_USER"), equalTo("SUPER_USER"), equalTo("MAVEN"))); - } - - @Test - @Disabled - @DisplayName("Fetch all the endorsements given endorsement status") - public void testAPIReturnsEndorsementsGivenStatus() { - Response response = - given() - .cookies(RestAPIHelper.getUserCookie()) - .queryParam("status", "PENDING") - .get("/endorsements"); - - response - .then() - .statusCode(200) - .contentType("application/json") - .body("data", hasSize(1)) - .body("data[0].endorseeId", equalTo("user-1")) - .body("data[0].skillName", equalTo("Java")) - .body("data[0].status", equalTo("PENDING")) - .body("data[0].endorsementType", anyOf(equalTo("POSITIVE"), equalTo("NEGATIVE"))) - .body("data[0].endorsersList", hasSize(1)) - .body("data[0].endorserList[0].endorseeId", equalTo("user-2")) - .body("data[0].endorserList[0].description", isA(String.class)) - .body( - "data[0].endorserList[0].userType", - anyOf(equalTo("NORMAL_USER"), equalTo("SUPER_USER"), equalTo("MAVEN"))); - } - - @Test - @Disabled - @DisplayName("Return 400 on invalid endorsement status passed") - public void testAPIReturns400_OnInvalidStatusPassed() { - Response response = - given() - .cookies(RestAPIHelper.getUserCookie()) - .queryParam("status", "APPROVAL") - .get("/endorsements"); - - response - .then() - .statusCode(400) - .body("code", equalTo(400)) - .body("message", equalTo("Invalid status passed")); - } - - @Test - @Disabled - @DisplayName("Return 400 on invalid endorsementId passed") - public void testAPIReturns400_OnInvalidParameterPassed() { - String endorsementId = "randomId"; - Response response = - given() - .cookies(RestAPIHelper.getUserCookie()) - .pathParam("endorsementId", endorsementId) - .patch("/endorsements"); - - response - .then() - .statusCode(400) - .body("code", equalTo(400)) - .body("message", equalTo("Invalid endorsementId passed")); - } - - @Test - @DisplayName("Return 201 on endorsements creation") - public void testAPIReturns201_OnEndorsementCreation() { - UUID endorseeId = user.getId(); - UUID skillId = skill.getId(); - - EndorsementDRO endorsementDRO = new EndorsementDRO(); - endorsementDRO.setEndorseeId(endorseeId); - endorsementDRO.setSkillId(skillId); - Response response = - given() - .cookies(RestAPIHelper.getUserCookie()) - .contentType("application/json") - .body(endorsementDRO) - .post("/v1/endorsements"); - - response - .then() - .statusCode(201) - .contentType("application/json") - .body("data.endorseeId", equalTo(endorseeId.toString())) - .body("data.skill.name", equalTo("Java")); - } - - @Test - @DisplayName("Return 400 on endorsements userid null") - public void testAPIReturns400_OnEndorsementCreationUserIdNull() { - - UUID skillId = skill.getId(); - - EndorsementDRO endorsementDRO = new EndorsementDRO(); - - endorsementDRO.setSkillId(skillId); - Response response = - given() - .cookies(RestAPIHelper.getUserCookie()) - .contentType("application/json") - .body(endorsementDRO) - .post("/v1/endorsements"); - - response - .then() - .statusCode(400) - .contentType("application/json") - .body("data", equalTo(null)) - .body("message", equalTo("user id cannot be null")); - } - - @Test - @DisplayName("Return 400 on endorsements skillid null") - public void testAPIReturns400_OnEndorsementCreationSkillIdNull() { - UUID endorseeId = user.getId(); - - EndorsementDRO endorsementDRO = new EndorsementDRO(); - endorsementDRO.setEndorseeId(endorseeId); - - Response response = - given() - .cookies(RestAPIHelper.getUserCookie()) - .contentType("application/json") - .body(endorsementDRO) - .post("/v1/endorsements"); - - response - .then() - .statusCode(400) - .contentType("application/json") - .body("data", equalTo(null)) - .body("message", equalTo("skill id cannot be null")); - } - - @Test - @Disabled - @DisplayName("Return 200 on endorsements updation") - public void testAPIReturns200_OnEndorsementGivenId() { - String endorsementId = "e-1"; - Response response = - given() - .cookies(RestAPIHelper.getUserCookie()) - .pathParam("endorsementId", endorsementId) - .patch("/endorsements"); - - response - .then() - .statusCode(200) - .contentType("application/json") - .body("code", equalTo(200)) - .body("data.endorseeId", equalTo("user-1")) - .body("data.skillName", equalTo("Java")) - .body("data.status", anyOf(equalTo("APPROVED"), equalTo("PENDING"), equalTo("REJECTED"))) - .body("data.endorsementType", anyOf(equalTo("POSITIVE"), equalTo("NEGATIVE"))) - .body("data.endorsersList", hasSize(1)) - .body("data.endorsersList[0].endorseeId", equalTo("user-2")) - .body("data.endorsersList[0].description", isA(String.class)) - .body( - "data.endorsersList[0].userType", - anyOf(equalTo("SUPER_USER"), equalTo("MAVEN"), equalTo("USER"))); - } - - @Test - @Disabled - @DisplayName("Return 400 on invalid endorsementId passed") - public void testAPIReturn400_OnInvalidIdPassed() { - String endorsementId = "randomId"; - Response response = - given() - .cookies(RestAPIHelper.getUserCookie()) - .pathParam("endorsementId", endorsementId) - .get("/endorsements"); - - response - .then() - .statusCode(400) - .body("code", equalTo(400)) - .body("message", equalTo("Invalid endorsementId passed")); - } - - @Test - @Disabled - @DisplayName("Return 404 when endorsement not found given endorsementId") - public void testAPIReturn404_OnEndorsementNotFound() { - String endorsementId = "randomId"; - Response response = - given() - .cookies(RestAPIHelper.getUserCookie()) - .pathParam("endorsementId", endorsementId) - .get("/endorsements"); - - response - .then() - .statusCode(404) - .body("code", equalTo(404)) - .body("message", equalTo("Endorsement not found")); - } - - @Test - @DisplayName("Return 200, with the endorsements of a particular user given userID") - public void itShouldReturn200OnEndorsementSearchByUserIDPresentInList() { - String userID = "f13ac7a0-76ab-4215-8bfc-2dd5d9f8ebeb"; - - Response response = given().get("/v1/endorsements?dummyData=true&userID=" + userID); - - response - .then() - .statusCode(200) - .contentType("application/json") - .body("content", everyItem(hasKey("user_id"))) - .body("content.user_id", everyItem(equalTo(userID))) - .body("content.size()", equalTo(7)) - .body("totalPages", equalTo(1)) - .body("pageable.pageNumber", equalTo(0)) - .body("pageable.pageSize", equalTo(10)) - .body("totalElements", equalTo(7)); - } - - @Test - @DisplayName("Return 200, with the endorsements of a particular skill given skillID") - public void itShouldReturn200OnEndorsementSearchBySkillIDPresentInList() { - String skillID = "7a6b8876-44e3-4b18-8579-79e9d4a5f0c9"; - - Response response = given().get("/v1/endorsements?dummyData=true&skillID=" + skillID); - - response - .then() - .statusCode(200) - .contentType("application/json") - .body("content", everyItem(hasKey("skill_id"))) - .body("content.skill_id", everyItem(equalTo(skillID))) - .body("content.size()", equalTo(1)) - .body("totalPages", equalTo(1)) - .body("pageable.pageNumber", equalTo(0)) - .body("pageable.pageSize", equalTo(10)) - .body("totalElements", equalTo(1)); - } - - @Test - @DisplayName("Return 200, with 1st page all the endorsements with default pagesize") - public void itShouldReturn200OnEndorsementSearchAllEndorsements() { - Response response = given().get("/v1/endorsements?dummyData=true"); - - response - .then() - .statusCode(200) - .contentType("application/json") - .body("totalPages", equalTo(2)) - .body("content.size()", equalTo(10)) - .body("pageable.pageNumber", equalTo(0)) - .body("pageable.pageSize", equalTo(10)) - .body("totalElements", equalTo(14)); - } - - @Test - @DisplayName("Return 200, with 1st page all the endorsements with custom limit value") - public void itShouldReturn200OnEndorsementSearchAllEndorsementsWithLimit() { - Response response = given().get("/v1/endorsements?dummyData=true&limit=15"); - - response - .then() - .statusCode(200) - .contentType("application/json") - .body("totalPages", equalTo(1)) - .body("content.size()", equalTo(14)) - .body("pageable.pageNumber", equalTo(0)) - .body("pageable.pageSize", equalTo(15)) - .body("totalElements", equalTo(14)); - } - - @Test - @DisplayName("Return 200, with 1st page of all endorsements result where page size is 5") - public void itShouldReturn200OnEndorsementSearchAllEndorsementsWithMultiplePages() { - Response response = given().get("/v1/endorsements?dummyData=true&limit=5"); - - response - .then() - .statusCode(200) - .contentType("application/json") - .body("totalPages", equalTo(3)) - .body("content.size()", equalTo(5)) - .body("pageable.pageNumber", equalTo(0)) - .body("pageable.pageSize", equalTo(5)) - .body("totalElements", equalTo(14)); - } - - @Test - @DisplayName("Return 200, with 2nd page of all the endorsements result") - public void itShouldReturn200With2ndPageOnEndorsementSearchAllEndorsementsWithMultiplePages() { - Response response = given().get("/v1/endorsements?dummyData=true&limit=5&offset=1"); - - response - .then() - .statusCode(200) - .contentType("application/json") - .body("totalPages", equalTo(3)) - .body("content.size()", equalTo(5)) - .body("pageable.pageNumber", equalTo(1)) - .body("pageable.pageSize", equalTo(5)) - .body("totalElements", equalTo(14)); - } - - @Test - @DisplayName("Return 200, with the endorsements matching the given userID and skillID") - public void itShouldReturn200OnEndorsementSearchGivenBothUserIDAndSkillID() { - String userID = "73e0b7c4-d128-4e53-9501-0e7f4ff5a261"; - String skillID = "7a6b8876-44e3-4b18-8579-79e9d4a5f0c9"; - - Response response = - given().get("/v1/endorsements?dummyData=true&skillID=" + skillID + "&userID=" + userID); - - response - .then() - .statusCode(200) - .contentType("application/json") - .body("content", everyItem(hasKey("skill_id"))) - .body("content.skill_id", everyItem(equalTo(skillID))) - .body("content", everyItem(hasKey("user_id"))) - .body("content.user_id", everyItem(equalTo(userID))) - .body("content.size()", equalTo(1)) - .body("totalPages", equalTo(1)) - .body("pageable.pageNumber", equalTo(0)) - .body("pageable.pageSize", equalTo(10)) - .body("totalElements", equalTo(1)); - } - - @Test - @DisplayName( - "Return 204, when there are no endorsements present for the given userID in UUID form") - public void itShouldReturn204OnEndorsementSearchWithValidUserIDButNotPresentInList() { - String userID = UUID.randomUUID().toString(); - - Response response = given().get("/v1/endorsements?dummyData=true&userID=" + userID); - - response.then().statusCode(204); - } - - @Test - @DisplayName( - "Return 204, when there are no endorsements present for the given skillID in UUID form") - public void itShouldReturn204OnEndorsementSearchWithValidSkillIDButNotPresentInList() { - String skillID = UUID.randomUUID().toString(); - - Response response = given().get("/v1/endorsements?dummyData=true&skillID=" + skillID); - - response.then().statusCode(204); - } - - @Test - @DisplayName("Return 400, given a userID which is not a UUID") - public void itShouldReturn400OnEndorsementSearchWithInvalidUserID() { - String userID = "invalid-user-id"; - - Response response = given().get("/v1/endorsements?dummyData=true&userID=" + userID); - - response.then().statusCode(400); - } - - @Test - @DisplayName("Return 400, given a skillID which is not a UUID") - public void itShouldReturn400OnEndorsementSearchWithInvalidSkillID() { - String skillID = "invalid-skill-id"; - - Response response = given().get("/v1/endorsements?dummyData=true&skillID=" + skillID); - - response.then().statusCode(400); - } - - @Test - @DisplayName("Return 204, given an offset value greater than maximum endorsements") - public void itShouldReturn204OnEndorsementSearchWithOffsetGreaterThanMaximumEndorsements() { - Response response = given().get("/v1/endorsements?dummyData=true&offset=10"); - - response.then().statusCode(204); - } - - @Test - @Disabled - @DisplayName("Return 401, when request is made without a valid cookie") - public void itShouldReturn401OnEndorsementSearchWithoutCookie() { - Response response = given().get("/v1/endorsements"); - - response - .then() - .statusCode(401) - .body( - "message", - equalTo( - "The access token provided is expired, revoked, malformed, or invalid for other reasons.")); - } - - @Test - @DisplayName( - "Return 200, when request is made using super user cookie and status is APPROVED/REJECTED") - public void - itShouldReturn200OnUpdateEndorsementStatusWithSuperUserCookieAndAcceptOrRejectEndorsementStatus() { - UUID endorsementId = createEndorsementModel(true); - Response response = - given() - .cookies(RestAPIHelper.getSuperUserCookie()) - .queryParam("status", EndorsementStatus.APPROVED.name()) - .patch("/v1/endorsements/{id}", endorsementId); - - response - .then() - .statusCode(200) - .body("data", equalTo(null)) - .body("message", equalTo("Successfully updated endorsement status")); - } - - @Test - @DisplayName( - "Return 403, when request is made without using super user cookie and status is APPROVED/REJECTED") - public void - itShouldReturn403OnUpdateEndorsementStatusWithOutSuperUserCookieAndAcceptOrRejectEndorsementStatus() { - UUID endorsementId = createEndorsementModel(true); - Response response = - given() - .cookies(RestAPIHelper.getUserCookie()) - .queryParam("status", EndorsementStatus.APPROVED.name()) - .patch("/v1/endorsements/{id}", endorsementId); - - response - .then() - .statusCode(403) - .body("data", equalTo(null)) - .body("message", equalTo("Unauthorized, Access is only available to super users")); - } - - @Test - @DisplayName( - "Return 400, when request is made with using super user cookie and status is invalid") - public void - itShouldReturn400OnUpdateEndorsementStatusWithSuperUserCookieAndEndorsementStatusIsInvalid() { - UUID endorsementId = createEndorsementModel(true); - Response response = - given() - .cookies(RestAPIHelper.getSuperUserCookie()) - .queryParam("status", "invalid-status") - .patch("/v1/endorsements/{id}", endorsementId); - - response - .then() - .statusCode(400) - .body("data", equalTo(null)) - .body("message", equalTo("Invalid parameter endorsement status: invalid-status")); - } - - @Test - @DisplayName( - "Return 400, when request is made with using super user cookie and status is PENDING") - public void - itShouldReturn400OnUpdateEndorsementStatusWithSuperUserCookieAndEndorsementStatusIsPending() { - UUID endorsementId = createEndorsementModel(true); - Response response = - given() - .cookies(RestAPIHelper.getSuperUserCookie()) - .queryParam("status", EndorsementStatus.PENDING.name()) - .patch("/v1/endorsements/{id}", endorsementId); - - response - .then() - .statusCode(400) - .body("data", equalTo(null)) - .body("message", equalTo("Invalid parameter endorsement status: PENDING")); - } - - @Test - @DisplayName( - "Return 409, when request is made with using super user cookie and endorsement is already updated") - public void - itShouldReturn409OnUpdateEndorsementStatusWithSuperUserCookieAndEndorsementAlreadyUpdated() { - UUID endorsementId = createEndorsementModel(false); - Response response = - given() - .cookies(RestAPIHelper.getSuperUserCookie()) - .queryParam("status", EndorsementStatus.APPROVED.name()) - .patch("/v1/endorsements/{id}", endorsementId); - - response - .then() - .statusCode(409) - .body("data", equalTo(null)) - .body("message", equalTo("Endorsement is already updated. Cannot modify status")); - } -} diff --git a/skill-tree/src/test/java/com/RDS/skilltree/integration/SecurityContextIntegrationTest.java b/skill-tree/src/test/java/com/RDS/skilltree/integration/SecurityContextIntegrationTest.java deleted file mode 100644 index 19754e71..00000000 --- a/skill-tree/src/test/java/com/RDS/skilltree/integration/SecurityContextIntegrationTest.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.RDS.skilltree.integration; - -import static io.restassured.RestAssured.given; -import static org.hamcrest.Matchers.equalTo; - -import io.restassured.response.Response; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInstance; -import org.springframework.boot.test.context.SpringBootTest; -import utils.RestAPIHelper; - -@TestInstance(TestInstance.Lifecycle.PER_CLASS) -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) -public class SecurityContextIntegrationTest extends TestContainerManager { - - @Test - public void testTokenIsNotPresent() { - - Response response = given().get("/v1/endorsement"); - response - .then() - .statusCode(401) - .body( - "message", - equalTo( - "The access token provided is expired, revoked, malformed, or invalid for other reasons.")); - } - - @Test - public void testInvalidToken() { - Response response = given().cookie("rds-session-v2", "invalidtoken").get("/v1/endorsement"); - response - .then() - .statusCode(401) - .body( - "message", - equalTo( - "The access token provided is expired, revoked, malformed, or invalid for other reasons.")); - } - - @Test - public void test_GetSkill_WithGuestToken() { - Response response = - given() - .cookies(RestAPIHelper.getGuestUserCookie()) - .contentType("application/json") - .get("/v1/skills/"); - response.then().statusCode(200); - } - - @Test - public void test_CreateSkill_WithGuestToken() { - Response response = - given() - .cookies(RestAPIHelper.getGuestUserCookie()) - .contentType("application/json") - .post("/v1/skills/"); - response.then().statusCode(403).body("message", equalTo("Access Denied")); - } -} diff --git a/skill-tree/src/test/java/com/RDS/skilltree/integration/SkillsIntegrationTests.java b/skill-tree/src/test/java/com/RDS/skilltree/integration/SkillsIntegrationTests.java deleted file mode 100644 index 735e32dd..00000000 --- a/skill-tree/src/test/java/com/RDS/skilltree/integration/SkillsIntegrationTests.java +++ /dev/null @@ -1,268 +0,0 @@ -package com.RDS.skilltree.integration; - -import static io.restassured.RestAssured.given; -import static org.hamcrest.Matchers.*; - -import com.RDS.skilltree.Skill.*; -import com.RDS.skilltree.User.*; -import io.restassured.response.Response; -import java.net.MalformedURLException; -import java.net.URL; -import java.util.UUID; -import org.junit.jupiter.api.*; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import utils.RestAPIHelper; - -@TestInstance(TestInstance.Lifecycle.PER_CLASS) -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) -public class SkillsIntegrationTests extends TestContainerManager { - private UserRepository userRepository; - private SkillRepository skillRepository; - private final UserService userService; - private final SkillsService skillsService; - private UserDTO user; - private SkillDTO skill; - - @Autowired - public SkillsIntegrationTests( - UserService userService, - UserRepository userRepository, - SkillsService skillsService, - SkillRepository skillRepository) { - this.userService = userService; - this.userRepository = userRepository; - this.skillsService = skillsService; - this.skillRepository = skillRepository; - } - - @BeforeEach - public void addData() throws MalformedURLException { - user = - userService.createUser( - UserDRO.builder() - .role(UserRoleEnum.MEMBER) - .rdsUserId("p6Bo61VEClhtVdwW0ihg") - .lastName("Doe") - .firstName("John") - .imageUrl( - new URL( - "https://res.cloudinary.com/realdevsquad/image/upload/v1666193594/profile/p6Bo61VEClhtVdwW0iGH/lezguwdq5bgzawa3.jpg")) - .build()); - - skill = - skillsService.createSkill( - SkillDRO.builder().name("Java").type(SkillType.ATOMIC).createdBy(user.getId()).build()); - } - - @AfterEach - public void cleanUp() { - skillRepository.deleteAll(); - userRepository.deleteAll(); - } - - @Test - @DisplayName("Return 200, on all skills") - public void testAPIReturns200_OnAllSkillsFound() { - Response response = - given() - .queryParam("offset", 0) - .queryParam("limit", 1) - .cookies(RestAPIHelper.getUserCookie()) - .get("/v1/skills/"); - - response - .then() - .statusCode(200) - .body("content", hasSize(1)) - .body("content[0].type", equalTo("ATOMIC")) - .body("content[0].name", equalTo("Java")) - .body("content[0].users", empty()) - .body("totalPages", equalTo(1)) - .body("totalElements", equalTo(1)) - .body("last", equalTo(true)) - .body("size", equalTo(1)) - .body("number", equalTo(0)) - .body("numberOfElements", equalTo(1)) - .body("empty", equalTo(false)); - } - - @Test - @DisplayName("Return 200, on no skills found") - public void testAPIReturns200_OnNoSkillsFound() { - skillRepository.deleteAll(); - Response response = given().cookies(RestAPIHelper.getUserCookie()).get("/v1/skills/"); - - response - .then() - .statusCode(200) - .body("content", hasSize(0)) - .body("totalPages", equalTo(0)) - .body("totalElements", equalTo(0)) - .body("last", equalTo(true)) - .body("size", equalTo(10)) - .body("number", equalTo(0)) - .body("numberOfElements", equalTo(0)) - .body("empty", equalTo(true)); - } - - @Test - @DisplayName("Return 200, on skill found given skillId") - public void testAPIReturns200_OnSkillFoundById() { - Integer skillId = skill.getId(); - - Response response = - given() - .cookies(RestAPIHelper.getUserCookie()) - .pathParam("id", skillId) - .get("/v1/skills/{id}"); - - response - .then() - .statusCode(200) - .contentType("application/json") - .body("id", equalTo(String.valueOf(skillId))) - .body("name", equalTo("Java")) - .body("type", equalTo("ATOMIC")); - } - - @Test - @DisplayName("Return 404, on skill not found given SkillId") - public void testAPIReturns404_OnSkillNotFound() { - UUID skillId = UUID.randomUUID(); - Response response = - given() - .cookies(RestAPIHelper.getUserCookie()) - .pathParam("id", skillId) - .get("/v1/skills/{id}"); - - response.then().statusCode(404).body("message", equalTo("Skill not found with given Id")); - } - - @Test - @DisplayName("Return 200, on skill with given name") - public void testAPIReturns200_OnSkillFoundGivenName() { - Response response = - given() - .cookies(RestAPIHelper.getUserCookie()) - .pathParam("name", "Java") - .get("/v1/skills/name/{name}"); - - response - .then() - .statusCode(200) - .contentType("application/json") - .body("name", equalTo("Java")) - .body("id", equalTo(skill.getId().toString())) - .body("type", equalTo("ATOMIC")); - } - - @Test - @DisplayName("Return 404, if skill given skill name is not found") - public void testAPIReturns404_OnSkillGivenSkillNameNotFound() { - Response response = - given() - .cookies(RestAPIHelper.getUserCookie()) - .pathParam("name", "Go") - .get("/v1/skills/name/{name}"); - - response.then().statusCode(404).body("message", equalTo("Skill not found with the given name")); - } - - @Test - @DisplayName("Return 400, if createdBy is not passed for Skill creation") - public void testAPIReturns400_OnCreatedByNotPassedForSKillCreation() { - SkillDRO skillDRO = SkillDRO.builder().name("Go").type(SkillType.ATOMIC).build(); - - Response response = - given() - .cookies(RestAPIHelper.getUserCookie()) - .contentType("application/json") - .body(skillDRO) - .post("/v1/skills/"); - - response - .then() - .statusCode(400) - .body("data", equalTo(null)) - .body("message", equalTo("Created by user Id cannot be null")); - } - - @Test - @DisplayName("Return 400, if type is not passed for Skill creation") - public void testAPIReturns400_OnTypeNotPassedForSkillCreation() { - SkillDRO skillDRO = SkillDRO.builder().name("Go").createdBy(user.getId()).build(); - - Response response = - given() - .cookies(RestAPIHelper.getUserCookie()) - .contentType("application/json") - .body(skillDRO) - .post("/v1/skills/"); - - response - .then() - .statusCode(400) - .body("data", equalTo(null)) - .body("message", equalTo("SkillType cannot be null")); - } - - @Test - @DisplayName("Return 400, if name is not passed for Skill creation") - public void testAPIReturns400_OnNameNotPassedForSkillCreation() { - SkillDRO skillDRO = SkillDRO.builder().type(SkillType.ATOMIC).createdBy(user.getId()).build(); - - Response response = - given() - .cookies(RestAPIHelper.getUserCookie()) - .contentType("application/json") - .body(skillDRO) - .post("/v1/skills/"); - - response - .then() - .statusCode(400) - .body("data", equalTo(null)) - .body("message", equalTo("Name cannot be null")); - } - - @Test - @DisplayName("Return 409, if name is already used for Skill creation") - public void testAPIReturns409_OnNameAlreadyUsedForSkillCreation() { - SkillDRO skillDRO = - SkillDRO.builder().type(SkillType.ATOMIC).name("Java").createdBy(user.getId()).build(); - - Response response = - given() - .cookies(RestAPIHelper.getUserCookie()) - .contentType("application/json") - .body(skillDRO) - .post("/v1/skills/"); - - response - .then() - .statusCode(409) - .body("message", equalTo("Cannot create entry for Skill as Skill name is duplicate")); - } - - @Test - @DisplayName("Return 201, on successful Skill creation") - public void testAPIReturns201_OnSuccessfulSkillCreation() { - SkillDRO skillDRO = - SkillDRO.builder().type(SkillType.ATOMIC).name("Go").createdBy(user.getId()).build(); - - Response response = - given() - .cookies(RestAPIHelper.getUserCookie()) - .contentType("application/json") - .body(skillDRO) - .post("/v1/skills/"); - - response - .then() - .statusCode(201) - .contentType("application/json") - .body("name", equalTo("Go")) - .body("type", equalTo("ATOMIC")); - } -} diff --git a/skill-tree/src/test/java/com/RDS/skilltree/integration/TestContainerManager.java b/skill-tree/src/test/java/com/RDS/skilltree/integration/TestContainerManager.java deleted file mode 100644 index ecd0ff75..00000000 --- a/skill-tree/src/test/java/com/RDS/skilltree/integration/TestContainerManager.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.RDS.skilltree.integration; - -import org.springframework.boot.testcontainers.service.connection.ServiceConnection; -import org.springframework.test.context.DynamicPropertyRegistry; -import org.springframework.test.context.DynamicPropertySource; -import org.testcontainers.containers.MySQLContainer; - -abstract class TestContainerManager { - @ServiceConnection static final MySQLContainer mysqlContainer; - - static { - mysqlContainer = - new MySQLContainer("mysql:8.1") - .withDatabaseName("skilltree-test") - .withUsername("root") - .withPassword("password"); - mysqlContainer.start(); - } - - @DynamicPropertySource - static void configureTestProperties(DynamicPropertyRegistry registry) { - registry.add("spring.jpa.hibernate.ddl-auto", () -> "update"); - registry.add("cookieName", () -> "rds-session-v2"); - registry.add( - "jwt.rds.public.key", - () -> - "-----BEGIN PUBLIC KEY-----MIICITANBgkqhkiG9w0BAQEFAAOCAg4AMIICCQKCAgBpAet8sOf64PtzdnwtkZB4JEJTCtQT9ZQMuuWUDXZGTG0iO7x3WZw6GanBboKGblU4VZEgd8H7bKOOIaQF4AsiXsw/vUsOV5Ue73a9Jj5d57jyon7M8fFmjna3afZfb5SBru5Iv0ECePqIKUIhSmToML+y3bFKF2cbUTEe2qPK5xzBeH4AWq4Zb2N0gHNstinwrXL9LWawQPkJr23TohZZEFSzyZbeklWWwz67A6YnE01w42R/TLE3LmU8YKkrHkgFsAHtUMQO++JsH4q3F9J0e0VkLzj5sB5RgAYscs6YFKoFD5jKgtSRPIXz7O9GsC76dHtwGXOk47/NWxu7bUQ0VcD2hJYprR28PjdNk5KiRKO5Z83JkiM6ed9UAkiD/fIRI8LITaLayHdFfQvXM+d9v4ugPHEq+aVllFMH1lUu/2B1aJpk4D4w5JcIzIZ9og4cMz00EGU/1o+BX2S55/Ok 4MaxX6Zl3QYm1K0cPLOdisYoygPtnNEav32JLgM2yOXdyuhpYzmn66yyFFck2nnCkezG5Gvlf3MejMavRO+sfIz0gDIhXEwWu0EJDrG5nmNRwejrSXx42YxmYZGkK/c8 2fiwbOVqIuFgsI6lWGdyDayFRg9bjrk6KiQZFnP4KcmUXk4PhiSItDJAUEkNz0+4StHNoqFhNH5pnEj4VbmJqwIDAQAB -----END PUBLIC KEY-----"); - } -} diff --git a/skill-tree/src/test/java/com/RDS/skilltree/unit/EndorsementListServiceTest.java b/skill-tree/src/test/java/com/RDS/skilltree/unit/EndorsementListServiceTest.java deleted file mode 100644 index 4a7e0ad4..00000000 --- a/skill-tree/src/test/java/com/RDS/skilltree/unit/EndorsementListServiceTest.java +++ /dev/null @@ -1,95 +0,0 @@ -package com.RDS.skilltree.unit; - -import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.Mockito.*; - -import com.RDS.skilltree.Endorsement.EndorsementModel; -import com.RDS.skilltree.Endorsement.EndorsementRepository; -import com.RDS.skilltree.EndorsementList.*; -import com.RDS.skilltree.Exceptions.NoEntityException; -import com.RDS.skilltree.User.UserModel; -import com.RDS.skilltree.User.UserRepository; -import java.util.Optional; -import java.util.UUID; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; - -class EndorsementListServiceTest { - - @Mock private EndorsementListRepository endorsementListRepository; - - @Mock private EndorsementRepository endorsementRepository; - - @Mock private UserRepository userRepository; - - @InjectMocks private EndorsementListService endorsementListService; - - @BeforeEach - void setUp() { - MockitoAnnotations.openMocks(this); - } - - @Test - void testCreateEndorsementListEntry() { - // Mock data - UUID endorserId = UUID.randomUUID(); - UUID endorsementId = UUID.randomUUID(); - EndorsementListDRO endorsementListDRO = new EndorsementListDRO(); - endorsementListDRO.setEndorserId(endorserId); - endorsementListDRO.setEndorsementId(endorsementId); - endorsementListDRO.setDescription("Test Description"); - endorsementListDRO.setType(EndorsementType.POSITIVE); - - UserModel mockUser = new UserModel(); - mockUser.setId(endorserId); - - EndorsementModel mockEndorsement = EndorsementModel.builder().id(endorsementId).build(); - - // Mock the repository behavior - when(userRepository.findById(endorserId)).thenReturn(Optional.of(mockUser)); - when(endorsementRepository.findById(endorsementId)).thenReturn(Optional.of(mockEndorsement)); - - // Call the service method - EndorsementListModel result = - endorsementListService.createEndorsementListEntry(endorsementListDRO); - - // Verify the interactions - verify(endorsementListRepository, times(1)).save(any(EndorsementListModel.class)); - - // Assertions - assertNotNull(result); - assertEquals(endorserId, result.getEndorserId()); - assertEquals(endorsementId, result.getEndorsement().getId()); - assertEquals("Test Description", result.getDescription()); - assertEquals(EndorsementType.POSITIVE, result.getType()); - } - - @Test - void testCreateEndorsementListEntryWithInvalidEndorsement() { - UUID endorserId = UUID.randomUUID(); - UUID endorsementId = UUID.randomUUID(); - EndorsementListDRO endorsementListDRO = new EndorsementListDRO(); - endorsementListDRO.setEndorserId(endorserId); - endorsementListDRO.setEndorsementId(endorsementId); - - UserModel mockUser = new UserModel(); - mockUser.setId(endorserId); - - // Mock the repository behavior for an invalid endorsement - when(userRepository.findById(endorserId)).thenReturn(Optional.of(mockUser)); - when(endorsementRepository.findById(endorsementId)).thenReturn(Optional.empty()); - - // Assert that a NoEntityException is thrown - NoEntityException exception = - assertThrows( - NoEntityException.class, - () -> endorsementListService.createEndorsementListEntry(endorsementListDRO)); - assertEquals("Endorsement with id:" + endorsementId + " not found", exception.getMessage()); - - // Verify that save method is not called - verify(endorsementListRepository, never()).save(any(EndorsementListModel.class)); - } -} diff --git a/skill-tree/src/test/java/com/RDS/skilltree/unit/EndorsementServiceTest.java b/skill-tree/src/test/java/com/RDS/skilltree/unit/EndorsementServiceTest.java deleted file mode 100644 index 4f4ecb48..00000000 --- a/skill-tree/src/test/java/com/RDS/skilltree/unit/EndorsementServiceTest.java +++ /dev/null @@ -1,755 +0,0 @@ -package com.RDS.skilltree.unit; - -import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.Mockito.*; - -import com.RDS.skilltree.Common.Response.GenericResponse; -import com.RDS.skilltree.Endorsement.*; -import com.RDS.skilltree.Exceptions.EntityAlreadyExistsException; -import com.RDS.skilltree.Exceptions.InvalidParameterException; -import com.RDS.skilltree.Exceptions.NoEntityException; -import com.RDS.skilltree.Skill.Skill; -import com.RDS.skilltree.Skill.SkillRepository; -import com.RDS.skilltree.User.UserModel; -import com.RDS.skilltree.User.UserRepository; -import com.RDS.skilltree.User.UserRoleEnum; -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; -import jakarta.persistence.EntityNotFoundException; - -import java.io.IOException; -import java.io.InputStream; -import java.time.Instant; -import java.time.LocalDateTime; -import java.util.*; - -import org.junit.jupiter.api.*; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.ArgumentMatchers; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.PageImpl; -import org.springframework.data.domain.PageRequest; -import org.springframework.security.access.AccessDeniedException; -import org.springframework.security.core.Authentication; -import org.springframework.security.core.context.SecurityContextHolder; -import org.springframework.test.util.ReflectionTestUtils; - -@ExtendWith(MockitoExtension.class) -public class EndorsementServiceTest { - @Mock - private EndorsementRepository endorsementRepository; - - @Mock - private UserRepository userRepository; - - @Mock - private SkillRepository skillRepository; - - @Mock - private ObjectMapper objectMapper; - - @InjectMocks - @Autowired - private EndorsementServiceImpl endorsementService; - - @Mock - private Authentication auth; - - @BeforeEach - public void setUp() { - ReflectionTestUtils.setField( - endorsementService, "dummyEndorsementDataPath", "dummy-data/endorsements.json"); - } - - @AfterEach - public void clearSecurityContext() { - SecurityContextHolder.clearContext(); - } - - private void setupUpdateEndorsementTests(Boolean useSuperUserRole) { - UserModel userModel = new UserModel(); - if (useSuperUserRole) { - userModel.setRole(UserRoleEnum.SUPERUSER); - } else { - userModel.setRole(UserRoleEnum.USER); - } - when(auth.getPrincipal()).thenReturn(userModel); - SecurityContextHolder.getContext().setAuthentication(auth); - } - - @Test - public void itShouldGetEndorsementsById() { - UUID endorsementId = UUID.randomUUID(); - UUID endorserId = UUID.randomUUID(); - UUID skillId = UUID.randomUUID(); - - Skill skill = Skill.builder().id(skillId).build(); - EndorsementModel endorsementModel = - EndorsementModel.builder() - .id(endorsementId) - .endorseeId(endorserId) - .skill(skill) - .build(); - endorsementModel.setCreatedAt(Instant.now()); - endorsementModel.setUpdatedAt(Instant.now()); - endorsementModel.setCreatedBy(endorsementId); - endorsementModel.setUpdatedBy(endorsementId); - - when(endorsementRepository.findById(endorsementId)).thenReturn(Optional.of(endorsementModel)); - - EndorsementDTO result = endorsementService.getEndorsementById(endorsementId); - - assertNotNull(result); - assertEquals( - endorsementId, - result.getId(), - "The Endorsement Id doesn't matches the expected endorsement Id"); - } - - @Test - @DisplayName("Get endorsements given a valid skillID") - public void itShouldReturnEndorsementsGivenSkillID() throws IOException { - PageRequest pageRequest = PageRequest.of(0, 10); - String skillID = UUID.randomUUID().toString(); - String userID = UUID.randomUUID().toString(); - - List dummyEndorsements = new ArrayList<>(); - dummyEndorsements.add( - new EndorsementModelFromJSON( - UUID.randomUUID(), - UUID.fromString(userID), - UUID.fromString(skillID), - "APPROVED", - LocalDateTime.now(), - UUID.randomUUID(), - LocalDateTime.now(), - UUID.randomUUID())); - - when(objectMapper.readValue( - ArgumentMatchers.any(), - ArgumentMatchers.>>any())) - .thenReturn(dummyEndorsements); - Page result = - endorsementService.getEndorsementsFromDummyData(pageRequest, skillID, null); - - assertEquals(new PageImpl<>(dummyEndorsements, pageRequest, dummyEndorsements.size()), result); - assertEquals(1, result.getTotalElements()); - } - - @Test - @DisplayName("Get endorsements given a valid userID") - void itShouldGetEndorsementsGivenUserID() throws IOException { - PageRequest pageRequest = PageRequest.of(0, 10); - String skillID = UUID.randomUUID().toString(); - String userID = UUID.randomUUID().toString(); - - List dummyEndorsements = new ArrayList<>(); - dummyEndorsements.add( - new EndorsementModelFromJSON( - UUID.randomUUID(), - UUID.fromString(userID), - UUID.fromString(skillID), - "PENDING", - LocalDateTime.now(), - UUID.randomUUID(), - LocalDateTime.now(), - UUID.randomUUID())); - - when(objectMapper.readValue( - ArgumentMatchers.any(), - ArgumentMatchers.>>any())) - .thenReturn(dummyEndorsements); - Page result = - endorsementService.getEndorsementsFromDummyData(pageRequest, null, userID); - - assertEquals(new PageImpl<>(dummyEndorsements, pageRequest, dummyEndorsements.size()), result); - assertEquals(1, result.getTotalElements()); - } - - @Test - @DisplayName("Get endorsements given an invalid userID") - public void itShouldThrowErrorIfInvalidUserIDIsGiven() throws IOException { - PageRequest pageRequest = PageRequest.of(0, 10); - String skillID = UUID.randomUUID().toString(); - String userID = UUID.randomUUID().toString(); - - List dummyEndorsements = new ArrayList<>(); - dummyEndorsements.add( - new EndorsementModelFromJSON( - UUID.randomUUID(), - UUID.fromString(userID), - UUID.fromString(skillID), - "PENDING", - LocalDateTime.now(), - UUID.randomUUID(), - LocalDateTime.now(), - UUID.randomUUID())); - - when(objectMapper.readValue( - ArgumentMatchers.any(), - ArgumentMatchers.>>any())) - .thenReturn(dummyEndorsements); - - assertThrows( - IllegalArgumentException.class, - () -> - endorsementService.getEndorsementsFromDummyData(pageRequest, null, "invalid-user-id")); - } - - @Test - @DisplayName("Get endorsements given an invalid skillID") - public void itShouldThrowIllegalArgumentExceptionIfInvalidSkillIDIsGiven() throws IOException { - PageRequest pageRequest = PageRequest.of(0, 10); - String skillID = UUID.randomUUID().toString(); - String userID = UUID.randomUUID().toString(); - - List dummyEndorsements = new ArrayList<>(); - dummyEndorsements.add( - new EndorsementModelFromJSON( - UUID.randomUUID(), - UUID.fromString(userID), - UUID.fromString(skillID), - "PENDING", - LocalDateTime.now(), - UUID.randomUUID(), - LocalDateTime.now(), - UUID.randomUUID())); - - when(objectMapper.readValue( - ArgumentMatchers.any(), - ArgumentMatchers.>>any())) - .thenReturn(dummyEndorsements); - - assertThrows( - IllegalArgumentException.class, - () -> - endorsementService.getEndorsementsFromDummyData(pageRequest, "invalid-skill-id", null)); - } - - @Test - @DisplayName("Get endorsements given a valid userID and invalid skillID") - public void itShouldThrowIllegalArgumentExceptionIfInvalidUserIDIsGiven() throws IOException { - PageRequest pageRequest = PageRequest.of(0, 10); - String skillID = UUID.randomUUID().toString(); - String userID = UUID.randomUUID().toString(); - - List dummyEndorsements = new ArrayList<>(); - dummyEndorsements.add( - new EndorsementModelFromJSON( - UUID.randomUUID(), - UUID.fromString(userID), - UUID.fromString(skillID), - "PENDING", - LocalDateTime.now(), - UUID.randomUUID(), - LocalDateTime.now(), - UUID.randomUUID())); - - when(objectMapper.readValue( - ArgumentMatchers.any(), - ArgumentMatchers.>>any())) - .thenReturn(dummyEndorsements); - - assertThrows( - IllegalArgumentException.class, - () -> - endorsementService.getEndorsementsFromDummyData( - pageRequest, "invalid-skill-id", UUID.randomUUID().toString())); - } - - @Test - @DisplayName( - "Return paginated result having 2 pages when number of endorsements with a given userID is 15") - public void itShouldReturnPaginatedResultOnSearch() throws IOException { - PageRequest pageRequest = PageRequest.of(0, 10); - String userID = UUID.randomUUID().toString(); - - List dummyEndorsements = new ArrayList<>(); - for (int i = 0; i < 15; i++) { - dummyEndorsements.add( - new EndorsementModelFromJSON( - UUID.randomUUID(), - UUID.fromString(userID), - UUID.randomUUID(), - "APPROVED", - LocalDateTime.now(), - UUID.randomUUID(), - LocalDateTime.now(), - UUID.randomUUID())); - } - - when(objectMapper.readValue( - ArgumentMatchers.any(), - ArgumentMatchers.>>any())) - .thenReturn(dummyEndorsements); - Page result = - endorsementService.getEndorsementsFromDummyData(pageRequest, null, userID); - - assertEquals(2, result.getTotalPages()); - assertEquals(15, result.getTotalElements()); - } - - @Test - @DisplayName("Return empty page when accessing out of bound page in paginated result") - public void itShouldReturnEmptyPaginatedResultOnSearch() throws IOException { - PageRequest pageRequest = PageRequest.of(10, 10); - String userID = UUID.randomUUID().toString(); - - List dummyEndorsements = new ArrayList<>(); - for (int i = 0; i < 15; i++) { - dummyEndorsements.add( - new EndorsementModelFromJSON( - UUID.randomUUID(), - UUID.fromString(userID), - UUID.randomUUID(), - "APPROVED", - LocalDateTime.now(), - UUID.randomUUID(), - LocalDateTime.now(), - UUID.randomUUID())); - } - - when(objectMapper.readValue( - ArgumentMatchers.any(), - ArgumentMatchers.>>any())) - .thenReturn(dummyEndorsements); - Page result = - endorsementService.getEndorsementsFromDummyData(pageRequest, null, userID); - - assertEquals(Page.empty(pageRequest), result); - } - - @Test - @DisplayName( - "Return empty endorsement list given a valid userID but skillID which is not present") - public void itShouldReturnEmptyDataGivenUserIDAndSkillIDNotPresent() throws IOException { - PageRequest pageRequest = PageRequest.of(0, 10); - String skillID = UUID.randomUUID().toString(); - String userID = UUID.randomUUID().toString(); - - List dummyEndorsements = new ArrayList<>(); - dummyEndorsements.add( - new EndorsementModelFromJSON( - UUID.randomUUID(), - UUID.fromString(userID), - UUID.randomUUID(), - "APPROVED", - LocalDateTime.now(), - UUID.randomUUID(), - LocalDateTime.now(), - UUID.randomUUID())); - List endorsementsResult = new ArrayList<>(); - - when(objectMapper.readValue( - ArgumentMatchers.any(), - ArgumentMatchers.>>any())) - .thenReturn(dummyEndorsements); - Page result = - endorsementService.getEndorsementsFromDummyData(pageRequest, skillID, userID); - - assertEquals( - new PageImpl<>(endorsementsResult, pageRequest, endorsementsResult.size()), result); - assertEquals(0, result.getTotalElements()); - } - - @Test - @DisplayName("Return empty page when no endorsements are present in dummy data") - public void itShouldReturnEmptyDataWhenNoEndorsementsArePresent() throws IOException { - PageRequest pageRequest = PageRequest.of(0, 10); - String skillID = UUID.randomUUID().toString(); - String userID = UUID.randomUUID().toString(); - - List dummyEndorsements = new ArrayList<>(); - - when(objectMapper.readValue( - ArgumentMatchers.any(), - ArgumentMatchers.>>any())) - .thenReturn(dummyEndorsements); - Page result = - endorsementService.getEndorsementsFromDummyData(pageRequest, skillID, userID); - - assertEquals(Page.empty(pageRequest), result); - assertEquals(0, result.getTotalElements()); - } - - @Test - @DisplayName("Return empty endorsement list given a userID which is not present") - public void itShouldReturnEmptyDataGivenUserIDNotPresent() throws IOException { - PageRequest pageRequest = PageRequest.of(0, 10); - String skillID = UUID.randomUUID().toString(); - String userID = UUID.randomUUID().toString(); - - List dummyEndorsements = new ArrayList<>(); - dummyEndorsements.add( - new EndorsementModelFromJSON( - UUID.randomUUID(), - UUID.fromString(userID), - UUID.fromString(skillID), - "APPROVED", - LocalDateTime.now(), - UUID.randomUUID(), - LocalDateTime.now(), - UUID.randomUUID())); - List endorsementsResult = new ArrayList<>(); - - when(objectMapper.readValue( - ArgumentMatchers.any(), - ArgumentMatchers.>>any())) - .thenReturn(dummyEndorsements); - Page result = - endorsementService.getEndorsementsFromDummyData( - pageRequest, null, UUID.randomUUID().toString()); - - assertEquals( - new PageImpl<>(endorsementsResult, pageRequest, endorsementsResult.size()), result); - assertEquals(0, result.getTotalElements()); - } - - @Test - @DisplayName("Return empty endorsement list given empty userID and skillID") - public void itShouldReturnEmptyDataGivenEmptyUserIDAndSkillID() throws IOException { - PageRequest pageRequest = PageRequest.of(0, 10); - String skillID = UUID.randomUUID().toString(); - String userID = UUID.randomUUID().toString(); - - List dummyEndorsements = new ArrayList<>(); - dummyEndorsements.add( - new EndorsementModelFromJSON( - UUID.randomUUID(), - UUID.fromString(userID), - UUID.fromString(skillID), - "APPROVED", - LocalDateTime.now(), - UUID.randomUUID(), - LocalDateTime.now(), - UUID.randomUUID())); - - when(objectMapper.readValue( - ArgumentMatchers.any(), - ArgumentMatchers.>>any())) - .thenReturn(dummyEndorsements); - Page result = - endorsementService.getEndorsementsFromDummyData(pageRequest, "", ""); - - assertEquals(new PageImpl<>(dummyEndorsements, pageRequest, dummyEndorsements.size()), result); - assertEquals(1, result.getTotalElements()); - } - - @Test - @DisplayName("Return empty endorsement list given a skillID which is not present") - public void itShouldReturnEmptyDataGivenSkillIDNotPresent() throws IOException { - PageRequest pageRequest = PageRequest.of(0, 10); - String skillID = UUID.randomUUID().toString(); - String userID = UUID.randomUUID().toString(); - - List dummyEndorsements = new ArrayList<>(); - dummyEndorsements.add( - new EndorsementModelFromJSON( - UUID.randomUUID(), - UUID.fromString(userID), - UUID.fromString(skillID), - "APPROVED", - LocalDateTime.now(), - UUID.randomUUID(), - LocalDateTime.now(), - UUID.randomUUID())); - List endorsementsResult = new ArrayList<>(); - - when(objectMapper.readValue( - ArgumentMatchers.any(), - ArgumentMatchers.>>any())) - .thenReturn(dummyEndorsements); - Page result = - endorsementService.getEndorsementsFromDummyData( - pageRequest, UUID.randomUUID().toString(), null); - - assertEquals( - new PageImpl<>(endorsementsResult, pageRequest, endorsementsResult.size()), result); - assertEquals(0, result.getTotalElements()); - } - - @Test - @DisplayName("Return empty endorsement list given a skillID and userID which is not present") - public void itShouldReturnEmptyDataGivenSkillIDAndUserIDNotPresent() throws IOException { - PageRequest pageRequest = PageRequest.of(0, 10); - String skillID = UUID.randomUUID().toString(); - String userID = UUID.randomUUID().toString(); - - List dummyEndorsements = new ArrayList<>(); - dummyEndorsements.add( - new EndorsementModelFromJSON( - UUID.randomUUID(), - UUID.fromString(userID), - UUID.fromString(skillID), - "APPROVED", - LocalDateTime.now(), - UUID.randomUUID(), - LocalDateTime.now(), - UUID.randomUUID())); - List endorsementsResult = new ArrayList<>(); - - when(objectMapper.readValue( - ArgumentMatchers.any(), - ArgumentMatchers.>>any())) - .thenReturn(dummyEndorsements); - Page result = - endorsementService.getEndorsementsFromDummyData( - pageRequest, UUID.randomUUID().toString(), UUID.randomUUID().toString()); - - assertEquals( - new PageImpl<>(endorsementsResult, pageRequest, endorsementsResult.size()), result); - assertEquals(0, result.getTotalElements()); - } - - @Test - @DisplayName("Return IO exception on error reading data") - void itShouldReturnIOExceptionIfErrorReadingData() throws IOException { - PageRequest pageRequest = PageRequest.of(0, 10); - String skillID = null; - String userID = null; - when(objectMapper.readValue( - ArgumentMatchers.any(), - ArgumentMatchers.>>any())) - .thenThrow(new IOException("Error reading data")); - - assertThrows( - IOException.class, - () -> endorsementService.getEndorsementsFromDummyData(pageRequest, skillID, userID)); - } - - @Test - public void itShouldHandleEndorsementNotFound() { - UUID nonExistentEndorsementId = UUID.randomUUID(); - when(endorsementRepository.findById(nonExistentEndorsementId)).thenReturn(Optional.empty()); - - EntityNotFoundException exception = - assertThrows( - EntityNotFoundException.class, - () -> endorsementService.getEndorsementById(nonExistentEndorsementId)); - - // Verify the exception message - assertEquals( - "No endorsement with the id " + nonExistentEndorsementId + " found", - exception.getMessage()); - } - - @Test - void testCreateEndorsement() { - // Mock data - UUID endorserId = UUID.randomUUID(); - UUID skillId = UUID.randomUUID(); - UUID endorsementId = UUID.randomUUID(); - EndorsementDRO endorsementDRO = new EndorsementDRO(); - endorsementDRO.setEndorseeId(endorserId); - endorsementDRO.setSkillId(skillId); - - Skill mockSkill = Skill.builder().id(skillId).build(); - EndorsementModel mockEndorsement = - EndorsementModel.builder() - .id(endorsementId) - .endorseeId(endorserId) - .skill(mockSkill) - .build(); - mockEndorsement.setCreatedAt(Instant.now()); - mockEndorsement.setUpdatedAt(Instant.now()); - mockEndorsement.setCreatedBy(endorserId); - mockEndorsement.setUpdatedBy(endorserId); - - // Mock the repository behavior - when(skillRepository.findById(skillId)).thenReturn(Optional.of(mockSkill)); - when(endorsementRepository.save(any(EndorsementModel.class))).thenReturn(mockEndorsement); - - // Call the service method - EndorsementModel result = endorsementService.createEndorsement(endorsementDRO); - - // Verify the interactions - verify(endorsementRepository, times(1)).save(any(EndorsementModel.class)); - - // Assertions - assertNotNull(result); - assertEquals(endorserId, result.getEndorseeId()); - assertEquals(skillId, result.getSkill().getId()); - } - - @Test - void testCreateEndorsementWithInvalidSkill() { - UUID endorserId = UUID.randomUUID(); - UUID skillId = UUID.randomUUID(); - EndorsementDRO endorsementDRO = new EndorsementDRO(); - endorsementDRO.setEndorseeId(endorserId); - endorsementDRO.setSkillId(skillId); - - // Mock the repository behavior for an invalid skill - when(skillRepository.findById(skillId)).thenReturn(Optional.empty()); - - // Assert that a NoEntityException is thrown - NoEntityException exception = - assertThrows( - NoEntityException.class, () -> endorsementService.createEndorsement(endorsementDRO)); - assertEquals("Skill with id:" + skillId + " not found", exception.getMessage()); - - // Verify that save method is not called - verify(endorsementRepository, never()).save(any(EndorsementModel.class)); - } - - @Test - @DisplayName( - "Return unauthorized access, given user is not a super user to update endorsement status") - public void itShouldReturnUnauthorizedGivenUserIsNotSuperUser() { - setupUpdateEndorsementTests(false); - - UUID endorsementId = UUID.randomUUID(); - String status = EndorsementStatus.APPROVED.name(); - - AccessDeniedException exception = - assertThrows( - AccessDeniedException.class, - () -> endorsementService.updateEndorsementStatus(endorsementId, status)); - assertEquals("Unauthorized, Access is only available to super users", exception.getMessage()); - verify(endorsementRepository, never()).save(any(EndorsementModel.class)); - } - - @Test - @DisplayName("Return invalid status given status is pending") - public void itShouldReturnInvalidStatusGivenEndorsementStatusIsPending() { - setupUpdateEndorsementTests(true); - - UUID endorsementId = UUID.randomUUID(); - String status = EndorsementStatus.PENDING.name(); - - InvalidParameterException exception = - assertThrows( - InvalidParameterException.class, - () -> endorsementService.updateEndorsementStatus(endorsementId, status)); - assertEquals("Invalid parameter endorsement status: " + status, exception.getMessage()); - verify(endorsementRepository, never()).save(any(EndorsementModel.class)); - } - - @Test - @DisplayName("Return invalid status given status is invalid") - public void itShouldReturnInvalidStatusGivenInvalidEndorsementStatus() { - setupUpdateEndorsementTests(true); - - UUID endorsementId = UUID.randomUUID(); - String status = "invalid-status"; - - InvalidParameterException exception = - assertThrows( - InvalidParameterException.class, - () -> endorsementService.updateEndorsementStatus(endorsementId, status)); - assertEquals("Invalid parameter endorsement status: " + status, exception.getMessage()); - verify(endorsementRepository, never()).save(any(EndorsementModel.class)); - } - - @Test - @DisplayName("Return cannot modify status given status is already updated") - public void itShouldThrowEntityAlreadyExistsExceptionGivenEndorsementIsUpdated() { - setupUpdateEndorsementTests(true); - - UUID endorseeId = UUID.randomUUID(); - UUID skillId = UUID.randomUUID(); - UUID endorsementId = UUID.randomUUID(); - - Skill mockSkill = Skill.builder().id(skillId).build(); - EndorsementModel mockEndorsement = - EndorsementModel.builder() - .id(endorsementId) - .status(EndorsementStatus.APPROVED) - .endorseeId(endorseeId) - .skill(mockSkill) - .build(); - mockEndorsement.setCreatedAt(Instant.now()); - mockEndorsement.setUpdatedAt(Instant.now()); - mockEndorsement.setCreatedBy(endorseeId); - mockEndorsement.setUpdatedBy(endorseeId); - - when(endorsementRepository.findById(endorsementId)).thenReturn(Optional.of(mockEndorsement)); - - EntityAlreadyExistsException exception = - assertThrows( - EntityAlreadyExistsException.class, - () -> - endorsementService.updateEndorsementStatus( - endorsementId, EndorsementStatus.APPROVED.name())); - assertEquals("Endorsement is already updated. Cannot modify status", exception.getMessage()); - verify(endorsementRepository, never()).save(any(EndorsementModel.class)); - } - - @Test - @DisplayName("Return endorsement not found given an unknown endorsement id") - public void itShouldReturnEndorsementNotFoundGivenUnknownEndorsementId() { - setupUpdateEndorsementTests(true); - - UUID nonExistentEndorsementId = UUID.randomUUID(); - String status = EndorsementStatus.APPROVED.name(); - - when(endorsementRepository.findById(nonExistentEndorsementId)).thenReturn(Optional.empty()); - - NoEntityException exception = - assertThrows( - NoEntityException.class, - () -> endorsementService.updateEndorsementStatus(nonExistentEndorsementId, status)); - assertEquals( - "No endorsement with id " + nonExistentEndorsementId + " was found", - exception.getMessage()); - verify(endorsementRepository, never()).save(any(EndorsementModel.class)); - } - - @Test - @DisplayName( - "Update endorsement status given a valid endorsement id and status is approved or rejected") - public void itShouldUpdateEndorsementStatusGivenEndorsementIdAndStatusApprovedOrRejected() { - setupUpdateEndorsementTests(true); - - UUID endorseeId = UUID.randomUUID(); - UUID skillId = UUID.randomUUID(); - UUID endorsementId = UUID.randomUUID(); - EndorsementStatus status = EndorsementStatus.APPROVED; - - Skill mockSkill = Skill.builder().id(skillId).build(); - EndorsementModel mockEndorsement = - EndorsementModel.builder() - .id(endorsementId) - .status(EndorsementStatus.PENDING) - .endorseeId(endorseeId) - .skill(mockSkill) - .build(); - mockEndorsement.setCreatedAt(Instant.now()); - mockEndorsement.setUpdatedAt(Instant.now()); - mockEndorsement.setCreatedBy(endorseeId); - mockEndorsement.setUpdatedBy(endorseeId); - - when(endorsementRepository.findById(endorsementId)).thenReturn(Optional.of(mockEndorsement)); - - GenericResponse result = - endorsementService.updateEndorsementStatus(endorsementId, status.name()); - assertEquals("Successfully updated endorsement status", result.getMessage()); - - verify(endorsementRepository, times(1)).save(any(EndorsementModel.class)); - - EndorsementModel updatedMockEndorsement = - EndorsementModel.builder() - .id(endorsementId) - .endorseeId(endorseeId) - .skill(mockSkill) - .status(EndorsementStatus.APPROVED) - .build(); - mockEndorsement.setCreatedAt(Instant.now()); - mockEndorsement.setUpdatedAt(Instant.now()); - mockEndorsement.setCreatedBy(endorseeId); - mockEndorsement.setUpdatedBy(endorseeId); - - when(endorsementRepository.findById(endorsementId)) - .thenReturn(Optional.of(updatedMockEndorsement)); - Optional updatedEndorsement = endorsementRepository.findById(endorsementId); - assertTrue(updatedEndorsement.isPresent()); - assertEquals(EndorsementStatus.APPROVED, updatedEndorsement.get().getStatus()); - } -} diff --git a/skill-tree/src/test/java/com/RDS/skilltree/unit/HealthCheckTest.java b/skill-tree/src/test/java/com/RDS/skilltree/unit/HealthCheckTest.java deleted file mode 100644 index a10ae257..00000000 --- a/skill-tree/src/test/java/com/RDS/skilltree/unit/HealthCheckTest.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.RDS.skilltree.unit; - -import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.Mockito.*; - -import com.RDS.skilltree.Health.HealthCheckController; -import com.RDS.skilltree.metrics.MetricService; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; - -class HealthCheckTest { - - @Mock private MetricService metricService; - - private HealthCheckController healthCheckController; - - @BeforeEach - void setUp() { - MockitoAnnotations.openMocks(this); - healthCheckController = new HealthCheckController(metricService); - } - - @Test - void checkHealth() { - // Setup - when(metricService.getUptime()).thenReturn(123.0); - - // Execute - var result = healthCheckController.checkHealth(); - - // Assert - assertNotNull(result); - assertTrue(result.containsKey("uptimeInSeconds")); - assertEquals(123.0, result.get("uptimeInSeconds")); - } -} diff --git a/skill-tree/src/test/java/com/RDS/skilltree/unit/SkillsServiceTest.java b/skill-tree/src/test/java/com/RDS/skilltree/unit/SkillsServiceTest.java deleted file mode 100644 index 4fa0ff85..00000000 --- a/skill-tree/src/test/java/com/RDS/skilltree/unit/SkillsServiceTest.java +++ /dev/null @@ -1,92 +0,0 @@ -package com.RDS.skilltree.unit; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.when; - -import com.RDS.skilltree.Skill.SkillDTO; -import com.RDS.skilltree.Skill.Skill; -import com.RDS.skilltree.Skill.SkillRepository; -import com.RDS.skilltree.Skill.SkillsServiceImpl; - -import java.util.*; - -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.PageImpl; -import org.springframework.data.domain.PageRequest; -import org.springframework.data.domain.Pageable; - -@ExtendWith(MockitoExtension.class) -public class SkillsServiceTest { - - @Mock - private SkillRepository skillRepository; - - @InjectMocks - @Autowired - private SkillsServiceImpl skillService; - - @Test - public void testGetSkillById() { - Integer skillId = new Random().nextInt(); - Skill skill = Skill.builder().id(skillId).build(); - - when(skillRepository.findById(skillId)).thenReturn(Optional.of(skill)); - - SkillDTO result = skillService.getSkillById(skillId); - assertNotNull(result); - assertEquals(skillId, result.getId(), "The skill Id doesn't matches the expected skillId"); - } - - @Test - public void testGetSkillsByName() { - String skillName = "Java"; - Skill skill = Skill.builder().name("Java").build(); - - when(skillRepository.findByName(skillName)).thenReturn(Optional.of(skill)); - - SkillDTO result = skillService.getSkillByName("Java"); - assertNotNull(result); - assertEquals( - skillName, result.getName(), "The skill name doesn't match the expected skill name"); - } - - @Test - public void testGetAllSkills() { - Skill skillJava = Skill.builder().name("Java").build(); - - Skill skillGo = Skill.builder().name("Go").build(); - - List skillList = Arrays.asList(skillJava, skillGo); - - when(skillRepository.findAll((Pageable) any(Pageable.class))) - .thenReturn(new PageImpl<>(skillList)); - - Pageable pageable = PageRequest.of(2, 1); - Page resultPage = skillService.getAllSkills(pageable); - assertNotNull(resultPage); - assertEquals( - skillList.size(), - resultPage.getTotalElements(), - "The number of elements returned is not equal to the expected size"); - assertEquals( - skillList.size(), - resultPage.getContent().size(), - "The content returned is not equal to the expected content"); - assertEquals( - "Java", - resultPage.getContent().get(0).getName(), - "The returned skill on page 0 doesn't match the actual skill"); - assertEquals( - "Go", - resultPage.getContent().get(1).getName(), - "The returned skill on page 0 doesn't match the actual skill"); - } -} From 7f09890ba146858b32231b82c8334ffaa8357f3a Mon Sep 17 00:00:00 2001 From: yash raj Date: Mon, 24 Jun 2024 20:10:07 +0530 Subject: [PATCH 06/31] add todo --- .../src/main/java/com/RDS/skilltree/Skill/SkillsController.java | 1 + 1 file changed, 1 insertion(+) diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsController.java b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsController.java index 0a0bd5f2..7177e012 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsController.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsController.java @@ -31,6 +31,7 @@ public GenericResponse> getAllSkills(@RequestParam(required = false) @PostMapping @ResponseStatus(HttpStatus.CREATED) + // TODO : Return only the valid fields by using a DTO. public GenericResponse createSkill(Authentication authentication, @RequestBody(required = true) @Valid SkillDRO skill) { JwtUserModel userDetails = (JwtUserModel) authentication.getPrincipal(); From 8f17bbcf2a884a85e2b76a9503ce12aa8fa32056 Mon Sep 17 00:00:00 2001 From: yash raj Date: Mon, 24 Jun 2024 20:22:32 +0530 Subject: [PATCH 07/31] rename SkillType to SkillTypeEnum --- skill-tree/src/main/java/com/RDS/skilltree/Skill/Skill.java | 2 +- .../src/main/java/com/RDS/skilltree/Skill/SkillDRO.java | 4 +--- .../src/main/java/com/RDS/skilltree/Skill/SkillDTO.java | 3 +-- .../skilltree/Skill/{SkillType.java => SkillTypeEnum.java} | 2 +- 4 files changed, 4 insertions(+), 7 deletions(-) rename skill-tree/src/main/java/com/RDS/skilltree/Skill/{SkillType.java => SkillTypeEnum.java} (63%) diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Skill/Skill.java b/skill-tree/src/main/java/com/RDS/skilltree/Skill/Skill.java index 79b6e049..574674d9 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Skill/Skill.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Skill/Skill.java @@ -27,7 +27,7 @@ public class Skill extends TrackedProperties { @Column(name = "skill_type", nullable = false) @Enumerated(value = EnumType.STRING) - private SkillType type = SkillType.ATOMIC; + private SkillTypeEnum type = SkillTypeEnum.ATOMIC; @Column(name = "is_deleted", nullable = false) private boolean isDeleted; diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDRO.java b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDRO.java index 3ef8848c..f541daab 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDRO.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDRO.java @@ -3,8 +3,6 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import jakarta.validation.constraints.NotNull; -import java.util.UUID; - import lombok.Builder; import lombok.Getter; @@ -16,5 +14,5 @@ public class SkillDRO { private String name; @NotNull(message = "SkillType cannot be null") - private SkillType type; + private SkillTypeEnum type; } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDTO.java b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDTO.java index 46880e1c..b3748e35 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDTO.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDTO.java @@ -5,7 +5,6 @@ import java.util.HashSet; import java.util.Set; -import java.util.UUID; import lombok.Builder; import lombok.Getter; @@ -15,7 +14,7 @@ @JsonIgnoreProperties(ignoreUnknown = true) public class SkillDTO { private Integer id; - private SkillType type; + private SkillTypeEnum type; private String name; private Set users; diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillType.java b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillTypeEnum.java similarity index 63% rename from skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillType.java rename to skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillTypeEnum.java index 86780f0a..0a5bc9b1 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillType.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillTypeEnum.java @@ -1,5 +1,5 @@ package com.RDS.skilltree.Skill; -public enum SkillType { +public enum SkillTypeEnum { ATOMIC, } From 76b71bfce71df1c34233ad13d4afd06140ea5620 Mon Sep 17 00:00:00 2001 From: yash raj Date: Mon, 24 Jun 2024 20:23:56 +0530 Subject: [PATCH 08/31] set logging level debug in application-dev instead of application --- skill-tree/src/main/resources/application-dev.properties | 3 ++- skill-tree/src/main/resources/application.properties | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/skill-tree/src/main/resources/application-dev.properties b/skill-tree/src/main/resources/application-dev.properties index 8fa24448..21199428 100644 --- a/skill-tree/src/main/resources/application-dev.properties +++ b/skill-tree/src/main/resources/application-dev.properties @@ -1 +1,2 @@ -cookieName=rds-session-v2-development \ No newline at end of file +cookieName=rds-session-v2-development +logging.level.root=DEBUG \ No newline at end of file diff --git a/skill-tree/src/main/resources/application.properties b/skill-tree/src/main/resources/application.properties index 49a55de9..098a53b3 100644 --- a/skill-tree/src/main/resources/application.properties +++ b/skill-tree/src/main/resources/application.properties @@ -8,7 +8,7 @@ jwt.rds.public.key=${RDS_PUBLIC_KEY} API_V1_PREFIX=/api/v1 spring.datasource.version=8.1.0 management.endpoints.web.exposure.include=health,info,metrics -logging.level.root=DEBUG +logging.level.root=ERROR # If no value received from env default is dev spring.profiles.active=${SPRING_PROFILES_ACTIVE:dev} # TODO:Dummy JSON code, needs to be changed as part of #103 From 711757d9157ae3a216fff56be6f909f54cab8644 Mon Sep 17 00:00:00 2001 From: yash raj Date: Mon, 24 Jun 2024 20:34:32 +0530 Subject: [PATCH 09/31] add todo --- .../src/main/java/com/RDS/skilltree/Skill/SkillsController.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsController.java b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsController.java index 7177e012..837607a0 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsController.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsController.java @@ -35,6 +35,7 @@ public GenericResponse> getAllSkills(@RequestParam(required = false) public GenericResponse createSkill(Authentication authentication, @RequestBody(required = true) @Valid SkillDRO skill) { JwtUserModel userDetails = (JwtUserModel) authentication.getPrincipal(); + // TODO : return a correct http status instead of 201 if (repository.findByName(skill.getName()).isPresent()) { return new GenericResponse<>(null, String.format("Skill with name %s already exists", skill.getName())); } @@ -53,6 +54,7 @@ public GenericResponse createSkill(Authentication authentication, @Reques return new GenericResponse<>(repository.save(newSkill), "Skill created"); } catch (DataIntegrityViolationException error) { log.error("Error saving skill {}, error: {}", skill.getName(), error.getMessage()); + // TODO : return a correct http status instead of 201 return new GenericResponse<>(null, "Something went wrong please try again"); } } From ff0f665e92cb1e616c99bf18c753c03de769d009 Mon Sep 17 00:00:00 2001 From: yash raj Date: Mon, 24 Jun 2024 22:41:31 +0530 Subject: [PATCH 10/31] remove endorsements list --- .../Endorsement/EndorsementController.java | 25 +-- .../skilltree/Endorsement/EndorsementDRO.java | 2 +- .../skilltree/Endorsement/EndorsementDTO.java | 19 +-- .../Endorsement/EndorsementModel.java | 35 +++-- .../Endorsement/EndorsementRepository.java | 8 +- .../Endorsement/EndorsementService.java | 8 +- .../Endorsement/EndorsementServiceImpl.java | 107 +------------ .../Endorsement/EndorsementStatus.java | 25 --- .../EndorsementList/EndorsementListDRO.java | 18 --- .../EndorsementList/EndorsementListDTO.java | 17 --- .../EndorsementList/EndorsementListModel.java | 49 ------ .../EndorsementListRepository.java | 8 - .../EndorsementListService.java | 43 ------ .../EndorsementList/EndorsementType.java | 6 - .../java/com/RDS/skilltree/Skill/Skill.java | 15 +- .../RDS/skilltree/Skill/SkillsController.java | 24 ++- .../java/com/RDS/skilltree/User/UserDTO.java | 3 +- .../com/RDS/skilltree/User/UserModel.java | 6 +- .../skilltree/utils/TrackedProperties.java | 15 +- .../src/main/resources/application.properties | 2 - .../resources/dummy-data/endorsements.json | 142 ------------------ 21 files changed, 83 insertions(+), 494 deletions(-) delete mode 100644 skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementStatus.java delete mode 100644 skill-tree/src/main/java/com/RDS/skilltree/EndorsementList/EndorsementListDRO.java delete mode 100644 skill-tree/src/main/java/com/RDS/skilltree/EndorsementList/EndorsementListDTO.java delete mode 100644 skill-tree/src/main/java/com/RDS/skilltree/EndorsementList/EndorsementListModel.java delete mode 100644 skill-tree/src/main/java/com/RDS/skilltree/EndorsementList/EndorsementListRepository.java delete mode 100644 skill-tree/src/main/java/com/RDS/skilltree/EndorsementList/EndorsementListService.java delete mode 100644 skill-tree/src/main/java/com/RDS/skilltree/EndorsementList/EndorsementType.java delete mode 100644 skill-tree/src/main/resources/dummy-data/endorsements.json diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementController.java b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementController.java index 7b154118..cb5321f1 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementController.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementController.java @@ -4,8 +4,9 @@ import jakarta.persistence.EntityNotFoundException; import jakarta.validation.Valid; import jakarta.validation.constraints.Min; -import java.io.IOException; + import java.util.UUID; + import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.data.domain.Page; @@ -22,28 +23,6 @@ public class EndorsementController { private final EndorsementService endorsementService; - @GetMapping(value = "") - public ResponseEntity> getAllEndorsements( - @RequestParam(name = "offset", defaultValue = "0", required = false) @Min(0) int offset, - @RequestParam(name = "limit", defaultValue = "10", required = false) @Min(1) int limit, - @RequestParam(name = "skillID", required = false) String skillID, - @RequestParam(name = "userID", required = false) String userID, - @RequestParam(name = "dummyData", required = false) boolean dummyData) - throws IOException { - PageRequest pageRequest = PageRequest.of(offset, limit); - if (dummyData) { - Page pagedEndorsements = - endorsementService.getEndorsementsFromDummyData(pageRequest, skillID, userID); - if (pagedEndorsements.isEmpty()) { - return ResponseEntity.noContent().build(); - } else { - return ResponseEntity.ok(pagedEndorsements); - } - } else { - return ResponseEntity.ok(endorsementService.getEndorsements(pageRequest)); - } - } - @GetMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity> getEndorsementById( @PathVariable(value = "id", required = true) String id) { diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementDRO.java b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementDRO.java index de152bab..ae360c1a 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementDRO.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementDRO.java @@ -15,7 +15,7 @@ @Builder public class EndorsementDRO { @NotNull(message = "user id cannot be null") - private UUID endorseeId; + private String endorseId; @NotNull(message = "skill id cannot be null") private Integer skillId; diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementDTO.java b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementDTO.java index e0698bb3..f7068a9c 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementDTO.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementDTO.java @@ -1,31 +1,26 @@ package com.RDS.skilltree.Endorsement; -import com.RDS.skilltree.EndorsementList.EndorsementListModel; -import com.RDS.skilltree.Skill.SkillDTO; import com.RDS.skilltree.utils.TrackedProperties; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import java.util.List; + import java.util.UUID; + import lombok.*; @Getter @Builder @JsonIgnoreProperties(ignoreUnknown = true) public class EndorsementDTO extends TrackedProperties { - private UUID id; - private UUID endorseeId; - private SkillDTO skill; - private EndorsementStatus status; - private List endorsersList; + private Integer id; + private String endorseId; + private Integer skillId; public static EndorsementDTO toDto(EndorsementModel endorsementModel) { EndorsementDTO endorsementDTO = EndorsementDTO.builder() .id(endorsementModel.getId()) - .endorseeId(endorsementModel.getEndorseeId()) - .skill(SkillDTO.toDto(endorsementModel.getSkill())) - .status(endorsementModel.getStatus()) - .endorsersList(endorsementModel.getEndorsersList()) + .endorseId(endorsementModel.getEndorseId()) + .skillId(endorsementModel.getSkillId()) .build(); endorsementDTO.setCreatedAt(endorsementModel.getCreatedAt()); endorsementDTO.setUpdatedAt(endorsementModel.getUpdatedAt()); diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementModel.java b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementModel.java index 19691be9..9ab3ceb9 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementModel.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementModel.java @@ -1,15 +1,14 @@ package com.RDS.skilltree.Endorsement; -import com.RDS.skilltree.EndorsementList.EndorsementListModel; import com.RDS.skilltree.Skill.Skill; +import com.RDS.skilltree.User.UserModel; import com.RDS.skilltree.utils.TrackedProperties; -import com.fasterxml.jackson.annotation.JsonManagedReference; import jakarta.persistence.*; -import java.util.ArrayList; -import java.util.List; -import java.util.UUID; + import lombok.*; +import java.util.UUID; + @AllArgsConstructor @NoArgsConstructor @Entity @@ -19,21 +18,21 @@ public class EndorsementModel extends TrackedProperties { @Id @GeneratedValue - @Column(name = "id", columnDefinition = "BINARY(16)") - private UUID id; - - @Column(name = "endorsee_id") - private UUID endorseeId; + @Column(name = "id") + private Integer id; - @ManyToOne(targetEntity = Skill.class, cascade = CascadeType.ALL) + @ManyToOne(targetEntity = Skill.class, cascade = CascadeType.ALL, optional = false) @JoinColumn(name = "skill_id", referencedColumnName = "id") - private Skill skill; + private Integer skillId; + + @ManyToOne(targetEntity = UserModel.class, cascade = CascadeType.ALL, optional = false) + @JoinColumn(name = "endorse_id", referencedColumnName = "id") + private String endorseId; - @OneToMany(mappedBy = "endorsement") - @JsonManagedReference - private List endorsersList = new ArrayList<>(); + @ManyToOne(targetEntity = UserModel.class, cascade = CascadeType.ALL, optional = false) + @JoinColumn(name = "endorser_id", referencedColumnName = "id") + private String endorserId; - @Column(name = "endorsement_status") - @Enumerated(value = EnumType.STRING) - private EndorsementStatus status = EndorsementStatus.PENDING; + @Column(name = "message", nullable = false) + private String message; } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementRepository.java b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementRepository.java index 3b68c3b3..ce4d4e00 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementRepository.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementRepository.java @@ -1,13 +1,13 @@ package com.RDS.skilltree.Endorsement; -import java.util.List; import java.util.UUID; + +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository public interface EndorsementRepository extends JpaRepository { - List findByEndorseeId(UUID userId); - - List findBySkillId(Integer skillId); + Page findBySkillId(Integer skillId, Pageable pageable); } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementService.java b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementService.java index a7254a48..00717112 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementService.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementService.java @@ -1,20 +1,16 @@ package com.RDS.skilltree.Endorsement; import com.RDS.skilltree.Common.Response.GenericResponse; + import java.io.IOException; import java.util.UUID; + import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; public interface EndorsementService { EndorsementDTO getEndorsementById(UUID id); - Page getEndorsements(PageRequest pageRequest); - - /* TODO:Dummy JSON code, needs to be changed as part of #103 */ - Page getEndorsementsFromDummyData( - PageRequest pageRequest, String skillID, String userID) throws IOException; - EndorsementModel createEndorsement(EndorsementDRO endorsementDRO); GenericResponse updateEndorsementStatus(UUID id, String status); diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementServiceImpl.java b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementServiceImpl.java index b248ed2d..5bbfbf9f 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementServiceImpl.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementServiceImpl.java @@ -1,31 +1,17 @@ package com.RDS.skilltree.Endorsement; import com.RDS.skilltree.Common.Response.GenericResponse; -import com.RDS.skilltree.Exceptions.EntityAlreadyExistsException; -import com.RDS.skilltree.Exceptions.InvalidParameterException; import com.RDS.skilltree.Exceptions.NoEntityException; import com.RDS.skilltree.Skill.Skill; import com.RDS.skilltree.Skill.SkillRepository; import com.RDS.skilltree.User.JwtUserModel; import com.RDS.skilltree.User.UserRoleEnum; -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.persistence.EntityNotFoundException; -import java.io.IOException; -import java.util.List; import java.util.Optional; import java.util.UUID; -import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.core.io.ClassPathResource; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.PageImpl; -import org.springframework.data.domain.PageRequest; import org.springframework.security.access.AccessDeniedException; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Service; @@ -36,13 +22,6 @@ public class EndorsementServiceImpl implements EndorsementService { private final EndorsementRepository endorsementRepository; private final SkillRepository skillRepository; - private final ObjectMapper objectMapper; - - @Value("${endorsements.dummmy-data.path}") - private String dummyEndorsementDataPath; - - private static final Logger logger = LoggerFactory.getLogger(EndorsementServiceImpl.class); - @Override public EndorsementDTO getEndorsementById(UUID id) throws IllegalStateException { Optional endorsementModel = endorsementRepository.findById(id); @@ -51,82 +30,15 @@ public EndorsementDTO getEndorsementById(UUID id) throws IllegalStateException { () -> new EntityNotFoundException("No endorsement with the id " + id + " found"))); } - @Override - public Page getEndorsements(PageRequest pageRequest) { - return endorsementRepository.findAll(pageRequest); - } - - /* TODO:Dummy JSON code, needs to be changed as part of #103 */ - @Override - public Page getEndorsementsFromDummyData( - PageRequest pageRequest, String skillID, String userID) throws IOException { - - try { - List endorsementModelFromJSONList = readEndorsementsFromJSON(); - - if (endorsementModelFromJSONList.isEmpty()) { - return Page.empty(pageRequest); - } - - List filteredEndorsements = - filterEndorsements(endorsementModelFromJSONList, skillID, userID); - return createPagedEndorsements(filteredEndorsements, pageRequest); - } catch (IOException e) { - logger.error("Error reading endorsements JSON data: {}", e.getMessage()); - throw new IOException("Error reading endorsements JSON data", e); - } - } - - private List readEndorsementsFromJSON() throws IOException { - ClassPathResource resource = new ClassPathResource(dummyEndorsementDataPath); - return objectMapper.readValue( - resource.getInputStream(), new TypeReference>() { - }); - } - - private List filterEndorsements( - List endorsements, String skillID, String userID) { - return endorsements.stream() - .filter(endorsement -> isMatchingSkillId(endorsement, skillID)) - .filter(endorsement -> isMatchingUserId(endorsement, userID)) - .collect(Collectors.toList()); - } - - private boolean isMatchingSkillId(EndorsementModelFromJSON endorsement, String skillID) { - return skillID == null - || skillID.isEmpty() - || endorsement.getSkillId().equals(UUID.fromString(skillID)); - } - - private boolean isMatchingUserId(EndorsementModelFromJSON endorsement, String userID) { - return userID == null - || userID.isEmpty() - || endorsement.getUserID().equals(UUID.fromString(userID)); - } - - private Page createPagedEndorsements( - List endorsements, PageRequest pageRequest) { - int startIdx = (int) pageRequest.getOffset(); - int totalEndorsements = endorsements.size(); - - if (startIdx >= totalEndorsements) { - return Page.empty(pageRequest); - } - - int endIdx = Math.min(startIdx + pageRequest.getPageSize(), endorsements.size()); - List currentPageEndorsements = endorsements.subList(startIdx, endIdx); - return new PageImpl<>(currentPageEndorsements, pageRequest, endorsements.size()); - } - @Override public EndorsementModel createEndorsement(EndorsementDRO endorsementDRO) { - UUID userId = endorsementDRO.getEndorseeId(); + String userId = endorsementDRO.getEndorseId(); Integer skillId = endorsementDRO.getSkillId(); Optional skillOptional = skillRepository.findById(skillId); if (skillOptional.isPresent()) { EndorsementModel endorsementModel = - EndorsementModel.builder().endorseeId(userId).skill(skillOptional.get()).build(); + EndorsementModel.builder().endorseId(userId).skillId(skillOptional.get().getId()).build(); return endorsementRepository.save(endorsementModel); } else { @@ -143,23 +55,14 @@ public GenericResponse updateEndorsementStatus(UUID id, String status) { throw new AccessDeniedException("Unauthorized, Access is only available to super users"); } - EndorsementStatus endorsementStatus = EndorsementStatus.fromString(status); - if (endorsementStatus.equals(EndorsementStatus.PENDING)) { - throw new InvalidParameterException("endorsement status", status); - } + Optional optionalEndorsementModel = endorsementRepository.findById(id); if (optionalEndorsementModel.isPresent()) { - if (optionalEndorsementModel.get().getStatus() != EndorsementStatus.PENDING) { - throw new EntityAlreadyExistsException( - "Endorsement is already updated. Cannot modify status"); - } EndorsementModel updatedEndorsementModel = EndorsementModel.builder() .id(optionalEndorsementModel.get().getId()) - .endorseeId(optionalEndorsementModel.get().getEndorseeId()) - .skill(optionalEndorsementModel.get().getSkill()) - .endorsersList(optionalEndorsementModel.get().getEndorsersList()) - .status(EndorsementStatus.valueOf(status)) + .endorseId(optionalEndorsementModel.get().getEndorseId()) + .skillId(optionalEndorsementModel.get().getSkillId()) .build(); endorsementRepository.save(updatedEndorsementModel); return new GenericResponse<>(null, "Successfully updated endorsement status"); diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementStatus.java b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementStatus.java deleted file mode 100644 index 9300b618..00000000 --- a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementStatus.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.RDS.skilltree.Endorsement; - -import com.RDS.skilltree.Exceptions.InvalidParameterException; -import java.util.Map; -import java.util.function.Function; -import java.util.stream.Collectors; -import java.util.stream.Stream; - -public enum EndorsementStatus { - APPROVED, - PENDING, - REJECTED; - - private static final Map endorsementStatusMap = - Stream.of(values()) - .collect(Collectors.toMap(EndorsementStatus::toString, Function.identity())); - - public static EndorsementStatus fromString(final String name) { - EndorsementStatus endorsementStatus = endorsementStatusMap.get(name); - if (endorsementStatus == null) { - throw new InvalidParameterException("endorsement status", name); - } - return endorsementStatus; - } -} diff --git a/skill-tree/src/main/java/com/RDS/skilltree/EndorsementList/EndorsementListDRO.java b/skill-tree/src/main/java/com/RDS/skilltree/EndorsementList/EndorsementListDRO.java deleted file mode 100644 index 99ad11b2..00000000 --- a/skill-tree/src/main/java/com/RDS/skilltree/EndorsementList/EndorsementListDRO.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.RDS.skilltree.EndorsementList; - -import java.util.UUID; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Data; -import lombok.NoArgsConstructor; - -@AllArgsConstructor -@NoArgsConstructor -@Data -@Builder -public class EndorsementListDRO { - private String description; - private UUID endorsementId; - private UUID endorserId; - private EndorsementType type; -} diff --git a/skill-tree/src/main/java/com/RDS/skilltree/EndorsementList/EndorsementListDTO.java b/skill-tree/src/main/java/com/RDS/skilltree/EndorsementList/EndorsementListDTO.java deleted file mode 100644 index d840d6c9..00000000 --- a/skill-tree/src/main/java/com/RDS/skilltree/EndorsementList/EndorsementListDTO.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.RDS.skilltree.EndorsementList; - -import com.RDS.skilltree.User.UserModel; -import java.util.UUID; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Data; - -@Builder -@Data -@AllArgsConstructor -public class EndorsementListDTO { - private UUID id; - private String description; - private UserModel endorser; - private EndorsementType type; -} diff --git a/skill-tree/src/main/java/com/RDS/skilltree/EndorsementList/EndorsementListModel.java b/skill-tree/src/main/java/com/RDS/skilltree/EndorsementList/EndorsementListModel.java deleted file mode 100644 index c734925d..00000000 --- a/skill-tree/src/main/java/com/RDS/skilltree/EndorsementList/EndorsementListModel.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.RDS.skilltree.EndorsementList; - -import com.RDS.skilltree.Endorsement.EndorsementModel; -import com.RDS.skilltree.utils.TrackedProperties; -import com.fasterxml.jackson.annotation.JsonBackReference; -import jakarta.persistence.*; -import java.util.UUID; -import lombok.Data; -import lombok.EqualsAndHashCode; -import lombok.NoArgsConstructor; - -@EqualsAndHashCode(callSuper = true) -@Entity -@NoArgsConstructor -@Data -@Table(name = "endorsement_list") -public class EndorsementListModel extends TrackedProperties { - @Id - @GeneratedValue - @Column(name = "id", columnDefinition = "BINARY(16)") - private UUID id; - - @JoinColumn(name = "endorsement_id", referencedColumnName = "id") - @ManyToOne(targetEntity = EndorsementModel.class, cascade = CascadeType.ALL) - @JsonBackReference - private EndorsementModel endorsement; - - @Column(name = "endorser_id") - private UUID endorserId; - - @Column(name = "description") - private String description; - - @Column(name = "is_deleted", nullable = false) - private boolean deleted; - - @Column(name = "type", nullable = false) - @Enumerated(value = EnumType.STRING) - private EndorsementType type; - - public EndorsementListModel( - EndorsementModel endorsement, UUID endorserId, String description, EndorsementType type) { - this.endorsement = endorsement; - this.endorserId = endorserId; - this.description = description; - this.type = type; - this.deleted = false; - } -} diff --git a/skill-tree/src/main/java/com/RDS/skilltree/EndorsementList/EndorsementListRepository.java b/skill-tree/src/main/java/com/RDS/skilltree/EndorsementList/EndorsementListRepository.java deleted file mode 100644 index ce372c5f..00000000 --- a/skill-tree/src/main/java/com/RDS/skilltree/EndorsementList/EndorsementListRepository.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.RDS.skilltree.EndorsementList; - -import java.util.UUID; -import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.stereotype.Repository; - -@Repository -public interface EndorsementListRepository extends JpaRepository {} diff --git a/skill-tree/src/main/java/com/RDS/skilltree/EndorsementList/EndorsementListService.java b/skill-tree/src/main/java/com/RDS/skilltree/EndorsementList/EndorsementListService.java deleted file mode 100644 index c26308b2..00000000 --- a/skill-tree/src/main/java/com/RDS/skilltree/EndorsementList/EndorsementListService.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.RDS.skilltree.EndorsementList; - -import com.RDS.skilltree.Endorsement.EndorsementModel; -import com.RDS.skilltree.Endorsement.EndorsementRepository; -import com.RDS.skilltree.Exceptions.NoEntityException; -import java.util.Optional; -import java.util.UUID; -import org.springframework.stereotype.Service; - -@Service -public class EndorsementListService { - - private final EndorsementListRepository endorsementListRepository; - private final EndorsementRepository endorsementRepository; - - public EndorsementListService( - EndorsementListRepository endorsementListRepository, - EndorsementRepository endorsementRepository) { - this.endorsementListRepository = endorsementListRepository; - this.endorsementRepository = endorsementRepository; - } - - public EndorsementListModel createEndorsementListEntry(EndorsementListDRO endorsementListDRO) { - EndorsementListModel endorsementListEntry = new EndorsementListModel(); - - UUID endorserId = endorsementListDRO.getEndorserId(); - UUID endorsementId = endorsementListDRO.getEndorsementId(); - - Optional endorsementOptional = endorsementRepository.findById(endorsementId); - if (endorsementOptional.isPresent()) { - - endorsementListEntry.setEndorserId(endorserId); - endorsementListEntry.setEndorsement(endorsementOptional.get()); - endorsementListEntry.setDescription(endorsementListDRO.getDescription()); - endorsementListEntry.setType(endorsementListDRO.getType()); - endorsementListRepository.save(endorsementListEntry); - return endorsementListEntry; - - } else { - throw new NoEntityException("Endorsement with id:" + endorsementId + " not found"); - } - } -} diff --git a/skill-tree/src/main/java/com/RDS/skilltree/EndorsementList/EndorsementType.java b/skill-tree/src/main/java/com/RDS/skilltree/EndorsementList/EndorsementType.java deleted file mode 100644 index 1d028fc8..00000000 --- a/skill-tree/src/main/java/com/RDS/skilltree/EndorsementList/EndorsementType.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.RDS.skilltree.EndorsementList; - -public enum EndorsementType { - POSITIVE, - NEGATIVE -} diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Skill/Skill.java b/skill-tree/src/main/java/com/RDS/skilltree/Skill/Skill.java index 574674d9..76a534cc 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Skill/Skill.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Skill/Skill.java @@ -1,5 +1,6 @@ package com.RDS.skilltree.Skill; +import com.RDS.skilltree.User.UserModel; import com.RDS.skilltree.utils.TrackedProperties; import jakarta.persistence.*; @@ -29,13 +30,11 @@ public class Skill extends TrackedProperties { @Enumerated(value = EnumType.STRING) private SkillTypeEnum type = SkillTypeEnum.ATOMIC; - @Column(name = "is_deleted", nullable = false) - private boolean isDeleted; + @ManyToOne(targetEntity = UserModel.class, cascade = CascadeType.ALL, optional = false) + @JoinColumn(name = "created_by", referencedColumnName = "id") + private String createdBy; - // TODO : Confirm the type of this column from tejas - @Column(name = "created_by", nullable = false) - private UUID createdBy; - - @Column(name = "updated_by") - private UUID updatedBy; + @ManyToOne(targetEntity = UserModel.class, cascade = CascadeType.ALL, optional = false) + @JoinColumn(name = "updated_by", referencedColumnName = "id") + private String updatedBy; } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsController.java b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsController.java index 837607a0..6564ddc1 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsController.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsController.java @@ -1,11 +1,16 @@ package com.RDS.skilltree.Skill; import com.RDS.skilltree.Common.Response.GenericResponse; +import com.RDS.skilltree.Endorsement.EndorsementRepository; import com.RDS.skilltree.User.JwtUserModel; import jakarta.validation.Valid; +import jakarta.validation.constraints.Min; import lombok.extern.slf4j.Slf4j; import org.springframework.dao.DataIntegrityViolationException; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.security.core.Authentication; import org.springframework.web.bind.annotation.*; @@ -18,9 +23,11 @@ @RequestMapping("/v1/skills") public class SkillsController { private final SkillRepository repository; + private final EndorsementRepository endorsementRepository; - public SkillsController(SkillRepository repository) { + public SkillsController(SkillRepository repository, EndorsementRepository endorsementRepository) { this.repository = repository; + this.endorsementRepository = endorsementRepository; } @GetMapping @@ -44,11 +51,11 @@ public GenericResponse createSkill(Authentication authentication, @Reques .name(skill.getName()) .type(skill.getType()) // TODO : use the id from userDetails once login is implemented - .createdBy(UUID.fromString("ae7a6673-c557-41e0-838f-209de4c644fc")) - .isDeleted(false) + .createdBy("ae7a6673c5574140838f209de4c644fc") +// .isDeleted(false) .build(); - newSkill.setCreatedAt(Instant.now()); +// newSkill.setCreatedAt(Instant.now()); try { return new GenericResponse<>(repository.save(newSkill), "Skill created"); @@ -58,4 +65,13 @@ public GenericResponse createSkill(Authentication authentication, @Reques return new GenericResponse<>(null, "Something went wrong please try again"); } } + + @GetMapping("/{id}/endorsements") + public ResponseEntity> getEndorsementsBySkillId( + @RequestParam(name = "offset", defaultValue = "0", required = false) @Min(0) int offset, + @RequestParam(name = "limit", defaultValue = "10", required = false) @Min(1) int limit, + @PathVariable(value = "id") Integer skillID) { + PageRequest pageRequest = PageRequest.of(offset, limit); + return ResponseEntity.ok(endorsementRepository.findBySkillId(skillID, pageRequest)); + } } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/User/UserDTO.java b/skill-tree/src/main/java/com/RDS/skilltree/User/UserDTO.java index e5b97a3f..b9502253 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/User/UserDTO.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/User/UserDTO.java @@ -1,6 +1,7 @@ package com.RDS.skilltree.User; import com.RDS.skilltree.Skill.SkillDTO; + import java.net.URL; import java.util.Set; import java.util.UUID; @@ -12,7 +13,7 @@ @Builder public class UserDTO { - private UUID id; + private String id; private String rdsUserId; diff --git a/skill-tree/src/main/java/com/RDS/skilltree/User/UserModel.java b/skill-tree/src/main/java/com/RDS/skilltree/User/UserModel.java index fd8eab73..07296ea6 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/User/UserModel.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/User/UserModel.java @@ -5,6 +5,7 @@ import jakarta.persistence.*; import java.util.UUID; + import lombok.*; @Entity @@ -17,9 +18,8 @@ @Table(name = "users") public class UserModel extends TrackedProperties { @Id - @GeneratedValue - @Column(name = "id", columnDefinition = "BINARY(16)") - private UUID id; + @Column(name = "id") + private String id; @Column(name = "rds_user_id", unique = true) private String rdsUserId; diff --git a/skill-tree/src/main/java/com/RDS/skilltree/utils/TrackedProperties.java b/skill-tree/src/main/java/com/RDS/skilltree/utils/TrackedProperties.java index 7cb55e6f..5c1685e3 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/utils/TrackedProperties.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/utils/TrackedProperties.java @@ -2,16 +2,27 @@ import jakarta.persistence.Column; import jakarta.persistence.MappedSuperclass; + import java.time.Instant; -import java.util.UUID; + import lombok.Data; +import org.springframework.data.annotation.CreatedDate; +import org.springframework.data.annotation.LastModifiedDate; @Data @MappedSuperclass public abstract class TrackedProperties { - @Column(name = "created_at", nullable = false) + @CreatedDate + @Column(name = "created_at", nullable = false, updatable = false) private Instant createdAt; + @LastModifiedDate @Column(name = "updated_at") private Instant updatedAt; + + @Column(name = "is_deleted", nullable = false, columnDefinition = "boolean default false") + private boolean isDeleted; + + @Column(name = "deleted_at") + private Instant deletedAt; } diff --git a/skill-tree/src/main/resources/application.properties b/skill-tree/src/main/resources/application.properties index 098a53b3..6929702e 100644 --- a/skill-tree/src/main/resources/application.properties +++ b/skill-tree/src/main/resources/application.properties @@ -11,5 +11,3 @@ management.endpoints.web.exposure.include=health,info,metrics logging.level.root=ERROR # If no value received from env default is dev spring.profiles.active=${SPRING_PROFILES_ACTIVE:dev} -# TODO:Dummy JSON code, needs to be changed as part of #103 -endorsements.dummmy-data.path=dummy-data/endorsements.json diff --git a/skill-tree/src/main/resources/dummy-data/endorsements.json b/skill-tree/src/main/resources/dummy-data/endorsements.json deleted file mode 100644 index cdad6516..00000000 --- a/skill-tree/src/main/resources/dummy-data/endorsements.json +++ /dev/null @@ -1,142 +0,0 @@ -[ - { - "id": "7f6cb5e8-2e22-4a33-a120-6a0f684fb1fc", - "user_id": "73e0b7c4-d128-4e53-9501-0e7f4ff5a261", - "skill_id": "7a6b8876-44e3-4b18-8579-79e9d4a5f0c9", - "status": "PENDING", - "created_at": "2024-02-18 17:34:47.504189", - "created_by": "7a6b8876-44e3-4b18-8579-79e9d4a5f0c9", - "updated_at": "2024-02-18 17:34:47.504189", - "updated_by": "7a6b8876-44e3-4b18-8579-79e9d4a5f0c9" - }, - { - "id": "c13a5db1-8df0-4b63-99fd-4e75f97d383c", - "user_id": "f13ac7a0-76ab-4215-8bfc-2dd5d9f8ebeb", - "skill_id": "4dcf2994-e5b4-45f3-8974-d2db0272a6cc", - "status": "APPROVED", - "created_at": "2024-02-18 17:34:47.504189", - "created_by": "f13ac7a0-76ab-4215-8bfc-2dd5d9f8ebeb", - "updated_at": "2024-02-18 17:34:47.504189", - "updated_by": "f13ac7a0-76ab-4215-8bfc-2dd5d9f8ebeb" - }, - { - "id": "d2a40739-53a3-4a47-8c14-5b64ee5eaa3d", - "user_id": "73e0b7c4-d128-4e53-9501-0e7f4ff5a261", - "skill_id": "1e7af93e-3de5-4a24-a4b7-9fc4ff48e37e", - "status": "PENDING", - "created_at": "2024-02-19 12:45:32.092461", - "created_by": "73e0b7c4-d128-4e53-9501-0e7f4ff5a261", - "updated_at": "2024-02-19 12:45:32.092461", - "updated_by": "73e0b7c4-d128-4e53-9501-0e7f4ff5a261" - }, - { - "id": "bd177123-0c50-458d-8d56-0d6f1a78f69c", - "user_id": "f13ac7a0-76ab-4215-8bfc-2dd5d9f8ebeb", - "skill_id": "f9d0cf6d-44b5-4c3e-867d-ee4a5ffad017", - "status": "APPROVED", - "created_at": "2024-02-20 09:21:18.735582", - "created_by": "f13ac7a0-76ab-4215-8bfc-2dd5d9f8ebeb", - "updated_at": "2024-02-20 09:21:18.735582", - "updated_by": "f13ac7a0-76ab-4215-8bfc-2dd5d9f8ebeb" - }, - { - "id": "56d27502-39c3-4744-822f-c1e0504d149d", - "user_id": "73e0b7c4-d128-4e53-9501-0e7f4ff5a261", - "skill_id": "b29d4941-ef22-4c39-86f9-b048554c1419", - "status": "PENDING", - "created_at": "2024-02-25 19:17:08.201468", - "created_by": "73e0b7c4-d128-4e53-9501-0e7f4ff5a261", - "updated_at": "2024-02-25 19:17:08.201468", - "updated_by": "73e0b7c4-d128-4e53-9501-0e7f4ff5a261" - }, - { - "id": "3a5c6a30-8c68-4d38-8b70-720a0e7a4a6b", - "user_id": "f13ac7a0-76ab-4215-8bfc-2dd5d9f8ebeb", - "skill_id": "7d6db19e-442e-4247-b5cf-87cf6db26bb2", - "status": "APPROVED", - "created_at": "2024-02-26 22:09:57.994362", - "created_by": "f13ac7a0-76ab-4215-8bfc-2dd5d9f8ebeb", - "updated_at": "2024-02-26 22:09:57.994362", - "updated_by": "f13ac7a0-76ab-4215-8bfc-2dd5d9f8ebeb" - }, - { - "id": "1a0c1baf-86c6-4f13-8fd5-c736a94c2c54", - "user_id": "73e0b7c4-d128-4e53-9501-0e7f4ff5a261", - "skill_id": "d30e4b2d-9c53-48d7-8920-8a225526d688", - "status": "PENDING", - "created_at": "2024-02-27 19:42:31.628175", - "created_by": "73e0b7c4-d128-4e53-9501-0e7f4ff5a261", - "updated_at": "2024-02-27 19:42:31.628175", - "updated_by": "73e0b7c4-d128-4e53-9501-0e7f4ff5a261" - }, - { - "id": "f4f4827d-5f22-4e01-9b97-d10889124578", - "user_id": "f13ac7a0-76ab-4215-8bfc-2dd5d9f8ebeb", - "skill_id": "0b37ef80-21d5-4960-995b-089221efc84f", - "status": "PENDING", - "created_at": "2024-02-28 14:51:22.941799", - "created_by": "f13ac7a0-76ab-4215-8bfc-2dd5d9f8ebeb", - "updated_at": "2024-02-28 14:51:22.941799", - "updated_by": "f13ac7a0-76ab-4215-8bfc-2dd5d9f8ebeb" - }, - { - "id": "b0bda1de-3cf4-4f90-961f-d928334abf5b", - "user_id": "73e0b7c4-d128-4e53-9501-0e7f4ff5a261", - "skill_id": "e11cd9d8-0649-4d33-b4e3-60f9f68b6c39", - "status": "APPROVED", - "created_at": "2024-02-29 08:11:45.202497", - "created_by": "73e0b7c4-d128-4e53-9501-0e7f4ff5a261", - "updated_at": "2024-02-29 08:11:45.202497", - "updated_by": "73e0b7c4-d128-4e53-9501-0e7f4ff5a261" - }, - { - "id": "c7e026b9-f184-4fb1-881f-625ccf937fd0", - "user_id": "f13ac7a0-76ab-4215-8bfc-2dd5d9f8ebeb", - "skill_id": "a94a7322-4f52-456d-850a-b2c03485be61", - "status": "PENDING", - "created_at": "2024-03-01 16:22:33.795328", - "created_by": "f13ac7a0-76ab-4215-8bfc-2dd5d9f8ebeb", - "updated_at": "2024-03-01 16:22:33.795328", - "updated_by": "f13ac7a0-76ab-4215-8bfc-2dd5d9f8ebeb" - }, - { - "id": "99f2035f-4a6a-4f8c-938f-5e51b04d6a8d", - "user_id": "73e0b7c4-d128-4e53-9501-0e7f4ff5a261", - "skill_id": "e3084315-0355-4644-ae89-5a84f16d91e6", - "status": "APPROVED", - "created_at": "2024-03-02 11:59:59.999999", - "created_by": "73e0b7c4-d128-4e53-9501-0e7f4ff5a261", - "updated_at": "2024-03-02 11:59:59.999999", - "updated_by": "73e0b7c4-d128-4e53-9501-0e7f4ff5a261" - }, - { - "id": "2513c590-78ff-4a56-90e4-5b3e06370b64", - "user_id": "f13ac7a0-76ab-4215-8bfc-2dd5d9f8ebeb", - "skill_id": "87490d7c-37a6-442d-8255-fd086555a35a", - "status": "PENDING", - "created_at": "2024-03-03 17:48:04.564729", - "created_by": "f13ac7a0-76ab-4215-8bfc-2dd5d9f8ebeb", - "updated_at": "2024-03-03 17:48:04.564729", - "updated_by": "f13ac7a0-76ab-4215-8bfc-2dd5d9f8ebeb" - }, - { - "id": "cf4d6fc5-79c0-49ed-8f5b-4cc9c0e0c7b0", - "user_id": "73e0b7c4-d128-4e53-9501-0e7f4ff5a261", - "skill_id": "2bb70a9b-31d2-4a9b-9751-efb3feae4cd9", - "status": "APPROVED", - "created_at": "2024-03-04 14:33:27.901382", - "created_by": "73e0b7c4-d128-4e53-9501-0e7f4ff5a261", - "updated_at": "2024-03-04 14:33:27.901382", - "updated_by": "73e0b7c4-d128-4e53-9501-0e7f4ff5a261" - }, - { - "id": "d1c947cb-ae6f-422e-ae67-bfd057f4177d", - "user_id": "f13ac7a0-76ab-4215-8bfc-2dd5d9f8ebeb", - "skill_id": "07a5b5c5-fd7f-4992-9fa7-5c4e41468b68", - "status": "PENDING", - "created_at": "2024-03-05 09:15:38.283750", - "created_by": "f13ac7a0-76ab-4215-8bfc-2dd5d9f8ebeb", - "updated_at": "2024-03-05 09:15:38.283750", - "updated_by": "f13ac7a0-76ab-4215-8bfc-2dd5d9f8ebeb" - } -] \ No newline at end of file From dc03b6dc53790294ab272a4e83e39c52de34bf0a Mon Sep 17 00:00:00 2001 From: yash raj Date: Tue, 25 Jun 2024 01:32:26 +0530 Subject: [PATCH 11/31] add reference to user table in skills modal --- .../java/com/RDS/skilltree/Skill/Skill.java | 14 ++++---- .../RDS/skilltree/Skill/SkillsController.java | 35 +++++++++++-------- .../RDS/skilltree/User/UserRepository.java | 4 +-- .../com/RDS/skilltree/User/UserService.java | 7 ++-- .../RDS/skilltree/User/UserServiceImpl.java | 7 ++-- .../skilltree/utils/TrackedProperties.java | 10 +++--- 6 files changed, 41 insertions(+), 36 deletions(-) diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Skill/Skill.java b/skill-tree/src/main/java/com/RDS/skilltree/Skill/Skill.java index 76a534cc..8c845096 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Skill/Skill.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Skill/Skill.java @@ -9,8 +9,6 @@ import lombok.Getter; import lombok.NoArgsConstructor; -import java.util.UUID; - @AllArgsConstructor @NoArgsConstructor @Entity @@ -30,11 +28,11 @@ public class Skill extends TrackedProperties { @Enumerated(value = EnumType.STRING) private SkillTypeEnum type = SkillTypeEnum.ATOMIC; - @ManyToOne(targetEntity = UserModel.class, cascade = CascadeType.ALL, optional = false) - @JoinColumn(name = "created_by", referencedColumnName = "id") - private String createdBy; + @ManyToOne + @JoinColumn(name = "created_by", nullable = false) + private UserModel createdBy; - @ManyToOne(targetEntity = UserModel.class, cascade = CascadeType.ALL, optional = false) - @JoinColumn(name = "updated_by", referencedColumnName = "id") - private String updatedBy; + @ManyToOne + @JoinColumn(name = "updated_by") + private UserModel updatedBy; } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsController.java b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsController.java index 6564ddc1..7025ec1b 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsController.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsController.java @@ -3,6 +3,8 @@ import com.RDS.skilltree.Common.Response.GenericResponse; import com.RDS.skilltree.Endorsement.EndorsementRepository; import com.RDS.skilltree.User.JwtUserModel; +import com.RDS.skilltree.User.UserModel; +import com.RDS.skilltree.User.UserRepository; import jakarta.validation.Valid; import jakarta.validation.constraints.Min; import lombok.extern.slf4j.Slf4j; @@ -14,26 +16,27 @@ import org.springframework.security.core.Authentication; import org.springframework.web.bind.annotation.*; -import java.time.Instant; import java.util.List; -import java.util.UUID; +import java.util.Optional; @Slf4j @RestController @RequestMapping("/v1/skills") public class SkillsController { - private final SkillRepository repository; + private final UserRepository userRepository; + private final SkillRepository skillRepository; private final EndorsementRepository endorsementRepository; - public SkillsController(SkillRepository repository, EndorsementRepository endorsementRepository) { - this.repository = repository; + public SkillsController(UserRepository userRepository, SkillRepository skillRepository, EndorsementRepository endorsementRepository) { + this.userRepository = userRepository; + this.skillRepository = skillRepository; this.endorsementRepository = endorsementRepository; } @GetMapping public GenericResponse> getAllSkills(@RequestParam(required = false) String name) { - repository.findByName(name); - return new GenericResponse<>(repository.findAll(), null); + skillRepository.findByName(name); + return new GenericResponse<>(skillRepository.findAll(), null); } @PostMapping @@ -42,23 +45,27 @@ public GenericResponse> getAllSkills(@RequestParam(required = false) public GenericResponse createSkill(Authentication authentication, @RequestBody(required = true) @Valid SkillDRO skill) { JwtUserModel userDetails = (JwtUserModel) authentication.getPrincipal(); + String userId = "ae7a6673c5574140838f209de4c644fc"; + Optional user = userRepository.findById(userId); + // TODO : return a correct http status instead of 201 - if (repository.findByName(skill.getName()).isPresent()) { + if (skillRepository.findByName(skill.getName()).isPresent()) { return new GenericResponse<>(null, String.format("Skill with name %s already exists", skill.getName())); } + // TODO : return a correct http status instead of 201 + if (user.isEmpty()) { + return new GenericResponse<>(null, "User not found"); + } + Skill newSkill = Skill.builder() .name(skill.getName()) .type(skill.getType()) - // TODO : use the id from userDetails once login is implemented - .createdBy("ae7a6673c5574140838f209de4c644fc") -// .isDeleted(false) + .createdBy(user.get()) .build(); -// newSkill.setCreatedAt(Instant.now()); - try { - return new GenericResponse<>(repository.save(newSkill), "Skill created"); + return new GenericResponse<>(skillRepository.save(newSkill), "Skill created"); } catch (DataIntegrityViolationException error) { log.error("Error saving skill {}, error: {}", skill.getName(), error.getMessage()); // TODO : return a correct http status instead of 201 diff --git a/skill-tree/src/main/java/com/RDS/skilltree/User/UserRepository.java b/skill-tree/src/main/java/com/RDS/skilltree/User/UserRepository.java index a557c19e..4bc8dd3b 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/User/UserRepository.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/User/UserRepository.java @@ -1,8 +1,8 @@ package com.RDS.skilltree.User; -import java.util.UUID; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository -public interface UserRepository extends JpaRepository {} +public interface UserRepository extends JpaRepository { +} diff --git a/skill-tree/src/main/java/com/RDS/skilltree/User/UserService.java b/skill-tree/src/main/java/com/RDS/skilltree/User/UserService.java index 4f7d2a38..2115b995 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/User/UserService.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/User/UserService.java @@ -1,16 +1,15 @@ package com.RDS.skilltree.User; import java.util.List; -import java.util.UUID; public interface UserService { UserDTO createUser(UserDRO user); - void updateUser(UUID id, UserDRO user); + void updateUser(String id, UserDRO user); - UserDTO getUserById(UUID id); + UserDTO getUserById(String id); List getAllUsers(); - void addSkill(Integer skill, UUID userId); + void addSkill(Integer skill, String userId); } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/User/UserServiceImpl.java b/skill-tree/src/main/java/com/RDS/skilltree/User/UserServiceImpl.java index 5739eb48..95e8969f 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/User/UserServiceImpl.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/User/UserServiceImpl.java @@ -7,7 +7,6 @@ import java.util.List; import java.util.Optional; -import java.util.UUID; import org.springframework.stereotype.Service; @@ -29,11 +28,11 @@ public UserDTO createUser(UserDRO user) { } @Override - public void updateUser(UUID id, UserDRO user) { + public void updateUser(String id, UserDRO user) { } @Override - public UserDTO getUserById(UUID id) { + public UserDTO getUserById(String id) { Optional userModel = userRepository.findById(id); return userModel.map(UserDTO::getUsersWithSkills).orElse(null); } @@ -51,7 +50,7 @@ public List getAllUsers() { */ @Override @Transactional - public void addSkill(Integer skillId, UUID userId) { + public void addSkill(Integer skillId, String userId) { Optional userOptional = userRepository.findById(userId); Optional skillOptional = skillRepository.findById(skillId); diff --git a/skill-tree/src/main/java/com/RDS/skilltree/utils/TrackedProperties.java b/skill-tree/src/main/java/com/RDS/skilltree/utils/TrackedProperties.java index 5c1685e3..57a6d566 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/utils/TrackedProperties.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/utils/TrackedProperties.java @@ -6,17 +6,19 @@ import java.time.Instant; import lombok.Data; -import org.springframework.data.annotation.CreatedDate; -import org.springframework.data.annotation.LastModifiedDate; +import org.hibernate.annotations.CreationTimestamp; +import org.hibernate.annotations.SourceType; +import org.hibernate.annotations.UpdateTimestamp; @Data @MappedSuperclass public abstract class TrackedProperties { - @CreatedDate + @CreationTimestamp(source = SourceType.DB) @Column(name = "created_at", nullable = false, updatable = false) private Instant createdAt; - @LastModifiedDate + + @UpdateTimestamp(source = SourceType.DB) @Column(name = "updated_at") private Instant updatedAt; From 4f52d11889fd27fe54864f486530f727696ed24c Mon Sep 17 00:00:00 2001 From: yash raj Date: Tue, 25 Jun 2024 02:36:30 +0530 Subject: [PATCH 12/31] Build api to create a new endorsement --- ...mentDRO.java => CreateEndorsementDro.java} | 7 +- .../Endorsement/EndorsementController.java | 125 ++++++++++++------ .../skilltree/Endorsement/EndorsementDTO.java | 22 +-- .../Endorsement/EndorsementModel.java | 12 +- .../Endorsement/EndorsementService.java | 10 +- .../Endorsement/EndorsementServiceImpl.java | 83 ++++++------ 6 files changed, 146 insertions(+), 113 deletions(-) rename skill-tree/src/main/java/com/RDS/skilltree/Endorsement/{EndorsementDRO.java => CreateEndorsementDro.java} (78%) diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementDRO.java b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/CreateEndorsementDro.java similarity index 78% rename from skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementDRO.java rename to skill-tree/src/main/java/com/RDS/skilltree/Endorsement/CreateEndorsementDro.java index ae360c1a..9bb9bc2c 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementDRO.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/CreateEndorsementDro.java @@ -2,8 +2,6 @@ import jakarta.validation.constraints.NotNull; -import java.util.UUID; - import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; @@ -13,7 +11,10 @@ @AllArgsConstructor @Data @Builder -public class EndorsementDRO { +public class CreateEndorsementDro { + @NotNull(message = "Message cannot be empty") + private String message; + @NotNull(message = "user id cannot be null") private String endorseId; diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementController.java b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementController.java index cb5321f1..613e2c7b 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementController.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementController.java @@ -1,18 +1,19 @@ package com.RDS.skilltree.Endorsement; import com.RDS.skilltree.Common.Response.GenericResponse; -import jakarta.persistence.EntityNotFoundException; +import com.RDS.skilltree.Skill.Skill; +import com.RDS.skilltree.Skill.SkillRepository; +import com.RDS.skilltree.User.UserModel; +import com.RDS.skilltree.User.UserRepository; import jakarta.validation.Valid; -import jakarta.validation.constraints.Min; -import java.util.UUID; +import java.util.Objects; +import java.util.Optional; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.PageRequest; +import org.springframework.dao.DataIntegrityViolationException; import org.springframework.http.HttpStatus; -import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @@ -21,47 +22,85 @@ @Slf4j @RequiredArgsConstructor public class EndorsementController { + private final UserRepository userRepository; + private final EndorsementRepository endorsementRepository; private final EndorsementService endorsementService; + private final SkillRepository skillRepository; - @GetMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity> getEndorsementById( - @PathVariable(value = "id", required = true) String id) { - try { - UUID uuid = UUID.fromString(id); - EndorsementDTO response = endorsementService.getEndorsementById(uuid); - return ResponseEntity.ok() - .body(new GenericResponse(response, "Data retrieved successfully")); - } catch (IllegalArgumentException e) { - String message = "Invalid UUID: " + id; - return ResponseEntity.status(HttpStatus.BAD_REQUEST) - .body(new GenericResponse(null, message)); - } catch (EntityNotFoundException e) { - return ResponseEntity.status(HttpStatus.NOT_FOUND) - .body(new GenericResponse(null, e.getMessage())); - } catch (Exception e) { - String message = "Something went wrong. Please contact admin."; - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) - .body(new GenericResponse(null, message)); +// @GetMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE) +// public ResponseEntity> getEndorsementById( +// @PathVariable(value = "id", required = true) String id) { +// try { +// UUID uuid = UUID.fromString(id); +// EndorsementDTO response = endorsementService.getEndorsementById(uuid); +// return ResponseEntity.ok() +// .body(new GenericResponse(response, "Data retrieved successfully")); +// } catch (IllegalArgumentException e) { +// String message = "Invalid UUID: " + id; +// return ResponseEntity.status(HttpStatus.BAD_REQUEST) +// .body(new GenericResponse(null, message)); +// } catch (EntityNotFoundException e) { +// return ResponseEntity.status(HttpStatus.NOT_FOUND) +// .body(new GenericResponse(null, e.getMessage())); +// } catch (Exception e) { +// String message = "Something went wrong. Please contact admin."; +// return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) +// .body(new GenericResponse(null, message)); +// } +// } + + @PostMapping + public ResponseEntity> postEndorsement( + @RequestBody @Valid CreateEndorsementDro endorsementDro) { + + String message = endorsementDro.getMessage(); + Integer skillId = endorsementDro.getSkillId(); + String endorseId = endorsementDro.getEndorseId(); + + // TODO: Get this from security context once the login api is implemented. + String endorserId = "ae7a6673c5574140838f209de4c644fc"; + + if (Objects.equals(endorseId, endorserId)) { + return new ResponseEntity<>(new GenericResponse<>(null, "Self endorsement not allowed"), HttpStatus.METHOD_NOT_ALLOWED); } - } - @PostMapping(value = "") - public ResponseEntity> postEndorsement( - @RequestBody @Valid EndorsementDRO endorsementDRO) { - - EndorsementModel endorsementModel = endorsementService.createEndorsement(endorsementDRO); - if (endorsementModel != null) - return new ResponseEntity<>( - new GenericResponse(EndorsementDTO.toDto(endorsementModel), ""), - HttpStatus.CREATED); - return new ResponseEntity<>( - new GenericResponse(null, "Failed to create endorsement"), - HttpStatus.BAD_REQUEST); - } + Optional skillDetails = skillRepository.findById(skillId); + Optional endorseDetails = userRepository.findById(endorseId); + Optional endorserDetails = userRepository.findById(endorserId); + + if (endorserDetails.isEmpty()) { + return new ResponseEntity<>(new GenericResponse<>(null, "Endorser details not found"), HttpStatus.NOT_FOUND); + } - @PatchMapping(value = "/{id}") - public ResponseEntity> updateEndorsementStatus( - @PathVariable(value = "id") UUID id, @RequestParam String status) { - return ResponseEntity.ok().body(endorsementService.updateEndorsementStatus(id, status)); + if (endorseDetails.isEmpty()) { + return new ResponseEntity<>(new GenericResponse<>(null, "Endorse not found"), HttpStatus.NOT_FOUND); + } + + if (skillDetails.isEmpty()) { + return new ResponseEntity<>(new GenericResponse<>(null, String.format("Skill id: %s not found.", skillId)), HttpStatus.NOT_FOUND); + } + + EndorsementModel newEndorsement = EndorsementModel.builder() + .endorser(endorserDetails.get()) + .endorse(endorseDetails.get()) + .skill(skillDetails.get()) + .message(message) + .build(); + + try { + return + new ResponseEntity<>( + new GenericResponse<>(endorsementRepository.save(newEndorsement), ""), + HttpStatus.CREATED); + } catch (DataIntegrityViolationException error) { + log.error("Error saving endorsement with skillId {} and endorse id {}, error: {}", skillId, endorseId, error.getMessage()); + return new ResponseEntity<>(new GenericResponse<>(null, "Something went wrong"), HttpStatus.INTERNAL_SERVER_ERROR); + } } + +// @PatchMapping(value = "/{id}") +// public ResponseEntity> updateEndorsementStatus( +// @PathVariable(value = "id") UUID id, @RequestParam String status) { +// return ResponseEntity.ok().body(endorsementService.updateEndorsementStatus(id, status)); +// } } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementDTO.java b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementDTO.java index f7068a9c..19fd16b3 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementDTO.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementDTO.java @@ -15,15 +15,15 @@ public class EndorsementDTO extends TrackedProperties { private String endorseId; private Integer skillId; - public static EndorsementDTO toDto(EndorsementModel endorsementModel) { - EndorsementDTO endorsementDTO = - EndorsementDTO.builder() - .id(endorsementModel.getId()) - .endorseId(endorsementModel.getEndorseId()) - .skillId(endorsementModel.getSkillId()) - .build(); - endorsementDTO.setCreatedAt(endorsementModel.getCreatedAt()); - endorsementDTO.setUpdatedAt(endorsementModel.getUpdatedAt()); - return endorsementDTO; - } +// public static EndorsementDTO toDto(EndorsementModel endorsementModel) { +// EndorsementDTO endorsementDTO = +// EndorsementDTO.builder() +// .id(endorsementModel.getId()) +// .endorseId(endorsementModel.getEndorseId()) +// .skillId(endorsementModel.getSkillId()) +// .build(); +// endorsementDTO.setCreatedAt(endorsementModel.getCreatedAt()); +// endorsementDTO.setUpdatedAt(endorsementModel.getUpdatedAt()); +// return endorsementDTO; +// } } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementModel.java b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementModel.java index 9ab3ceb9..698b40d1 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementModel.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementModel.java @@ -7,8 +7,6 @@ import lombok.*; -import java.util.UUID; - @AllArgsConstructor @NoArgsConstructor @Entity @@ -21,17 +19,17 @@ public class EndorsementModel extends TrackedProperties { @Column(name = "id") private Integer id; - @ManyToOne(targetEntity = Skill.class, cascade = CascadeType.ALL, optional = false) + @ManyToOne @JoinColumn(name = "skill_id", referencedColumnName = "id") - private Integer skillId; + private Skill skill; - @ManyToOne(targetEntity = UserModel.class, cascade = CascadeType.ALL, optional = false) + @ManyToOne @JoinColumn(name = "endorse_id", referencedColumnName = "id") - private String endorseId; + private UserModel endorse; @ManyToOne(targetEntity = UserModel.class, cascade = CascadeType.ALL, optional = false) @JoinColumn(name = "endorser_id", referencedColumnName = "id") - private String endorserId; + private UserModel endorser; @Column(name = "message", nullable = false) private String message; diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementService.java b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementService.java index 00717112..f46b90b5 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementService.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementService.java @@ -2,16 +2,12 @@ import com.RDS.skilltree.Common.Response.GenericResponse; -import java.io.IOException; import java.util.UUID; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.PageRequest; - public interface EndorsementService { - EndorsementDTO getEndorsementById(UUID id); + EndorsementModel getEndorsementById(UUID id); - EndorsementModel createEndorsement(EndorsementDRO endorsementDRO); +// EndorsementModel createEndorsement(CreateEndorsementDro createEndorsementDro); - GenericResponse updateEndorsementStatus(UUID id, String status); +// GenericResponse updateEndorsementStatus(UUID id, String status); } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementServiceImpl.java b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementServiceImpl.java index 5bbfbf9f..c85634bb 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementServiceImpl.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementServiceImpl.java @@ -23,50 +23,49 @@ public class EndorsementServiceImpl implements EndorsementService { private final SkillRepository skillRepository; @Override - public EndorsementDTO getEndorsementById(UUID id) throws IllegalStateException { + public EndorsementModel getEndorsementById(UUID id) throws IllegalStateException { Optional endorsementModel = endorsementRepository.findById(id); - return EndorsementDTO.toDto( - endorsementModel.orElseThrow( - () -> new EntityNotFoundException("No endorsement with the id " + id + " found"))); + return endorsementModel.orElseThrow( + () -> new EntityNotFoundException("No endorsement with the id " + id + " found")); } - @Override - public EndorsementModel createEndorsement(EndorsementDRO endorsementDRO) { - String userId = endorsementDRO.getEndorseId(); - Integer skillId = endorsementDRO.getSkillId(); - - Optional skillOptional = skillRepository.findById(skillId); - if (skillOptional.isPresent()) { - EndorsementModel endorsementModel = - EndorsementModel.builder().endorseId(userId).skillId(skillOptional.get().getId()).build(); - - return endorsementRepository.save(endorsementModel); - } else { - - throw new NoEntityException("Skill with id:" + skillId + " not found"); - } - } +// @Override +// public EndorsementModel createEndorsement(CreateEndorsementDro createEndorsementDro) { +// String userId = createEndorsementDro.getEndorseId(); +// Integer skillId = createEndorsementDro.getSkillId(); +// +// Optional skillOptional = skillRepository.findById(skillId); +// if (skillOptional.isPresent()) { +// EndorsementModel endorsementModel = +// EndorsementModel.builder().endorseId(userId).skillId(skillOptional.get().getId()).build(); +// +// return endorsementRepository.save(endorsementModel); +// } else { +// +// throw new NoEntityException("Skill with id:" + skillId + " not found"); +// } +// } - @Override - public GenericResponse updateEndorsementStatus(UUID id, String status) { - JwtUserModel user = - (JwtUserModel) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); - if (!user.getRole().equals(UserRoleEnum.SUPERUSER)) { - throw new AccessDeniedException("Unauthorized, Access is only available to super users"); - } - - - Optional optionalEndorsementModel = endorsementRepository.findById(id); - if (optionalEndorsementModel.isPresent()) { - EndorsementModel updatedEndorsementModel = - EndorsementModel.builder() - .id(optionalEndorsementModel.get().getId()) - .endorseId(optionalEndorsementModel.get().getEndorseId()) - .skillId(optionalEndorsementModel.get().getSkillId()) - .build(); - endorsementRepository.save(updatedEndorsementModel); - return new GenericResponse<>(null, "Successfully updated endorsement status"); - } - throw new NoEntityException("No endorsement with id " + id + " was found"); - } +// @Override +// public GenericResponse updateEndorsementStatus(UUID id, String status) { +// JwtUserModel user = +// (JwtUserModel) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); +// if (!user.getRole().equals(UserRoleEnum.SUPERUSER)) { +// throw new AccessDeniedException("Unauthorized, Access is only available to super users"); +// } +// +// +// Optional optionalEndorsementModel = endorsementRepository.findById(id); +// if (optionalEndorsementModel.isPresent()) { +// EndorsementModel updatedEndorsementModel = +// EndorsementModel.builder() +// .id(optionalEndorsementModel.get().getId()) +// .endorseId(optionalEndorsementModel.get().getEndorseId()) +// .skillId(optionalEndorsementModel.get().getSkillId()) +// .build(); +// endorsementRepository.save(updatedEndorsementModel); +// return new GenericResponse<>(null, "Successfully updated endorsement status"); +// } +// throw new NoEntityException("No endorsement with id " + id + " was found"); +// } } From 14424c475de4fbd0544f12270e6a08c9c713ef06 Mon Sep 17 00:00:00 2001 From: yash raj Date: Tue, 25 Jun 2024 16:52:10 +0530 Subject: [PATCH 13/31] create api to update an endorsement --- .../RDS/skilltree/Config/SecurityConfig.java | 4 +- .../Endorsement/EndorsementController.java | 124 +++++++++++------- .../skilltree/Endorsement/EndorsementDTO.java | 25 ++-- .../Endorsement/EndorsementModel.java | 2 +- .../Endorsement/EndorsementRepository.java | 4 +- .../Endorsement/EndorsementService.java | 10 +- .../Endorsement/EndorsementServiceImpl.java | 55 +------- .../Endorsement/UpdateEndorsementDro.java | 16 +++ .../java/com/RDS/skilltree/Skill/Skill.java | 1 - .../com/RDS/skilltree/Skill/SkillDRO.java | 1 - .../com/RDS/skilltree/Skill/SkillDTO.java | 8 +- .../RDS/skilltree/Skill/SkillRepository.java | 2 - .../RDS/skilltree/Skill/SkillsController.java | 23 ++-- .../java/com/RDS/skilltree/User/UserDRO.java | 10 +- .../java/com/RDS/skilltree/User/UserDTO.java | 16 +-- .../com/RDS/skilltree/User/UserModel.java | 3 - .../RDS/skilltree/User/UserRepository.java | 3 +- .../RDS/skilltree/User/UserServiceImpl.java | 9 +- .../RDS/skilltree/User/UserSkillsModel.java | 4 +- .../skilltree/utils/TrackedProperties.java | 3 - 20 files changed, 142 insertions(+), 181 deletions(-) create mode 100644 skill-tree/src/main/java/com/RDS/skilltree/Endorsement/UpdateEndorsementDro.java diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Config/SecurityConfig.java b/skill-tree/src/main/java/com/RDS/skilltree/Config/SecurityConfig.java index 24c0e28a..b54f9b4e 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Config/SecurityConfig.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Config/SecurityConfig.java @@ -53,7 +53,9 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { .hasAnyAuthority(UserRoleEnum.getAllRoles()) // give read-only access to all .requestMatchers("/v1/**") .hasAnyAuthority( - UserRoleEnum.USER.name(), UserRoleEnum.MEMBER.name(), UserRoleEnum.SUPERUSER.name()) + UserRoleEnum.USER.name(), + UserRoleEnum.MEMBER.name(), + UserRoleEnum.SUPERUSER.name()) .anyRequest() .authenticated()) .exceptionHandling( diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementController.java b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementController.java index 613e2c7b..d2838b93 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementController.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementController.java @@ -6,10 +6,6 @@ import com.RDS.skilltree.User.UserModel; import com.RDS.skilltree.User.UserRepository; import jakarta.validation.Valid; - -import java.util.Objects; -import java.util.Optional; - import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.dao.DataIntegrityViolationException; @@ -17,6 +13,9 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; +import java.util.Objects; +import java.util.Optional; + @RestController @RequestMapping("/v1/endorsements") @Slf4j @@ -27,29 +26,31 @@ public class EndorsementController { private final EndorsementService endorsementService; private final SkillRepository skillRepository; -// @GetMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE) -// public ResponseEntity> getEndorsementById( -// @PathVariable(value = "id", required = true) String id) { -// try { -// UUID uuid = UUID.fromString(id); -// EndorsementDTO response = endorsementService.getEndorsementById(uuid); -// return ResponseEntity.ok() -// .body(new GenericResponse(response, "Data retrieved successfully")); -// } catch (IllegalArgumentException e) { -// String message = "Invalid UUID: " + id; -// return ResponseEntity.status(HttpStatus.BAD_REQUEST) -// .body(new GenericResponse(null, message)); -// } catch (EntityNotFoundException e) { -// return ResponseEntity.status(HttpStatus.NOT_FOUND) -// .body(new GenericResponse(null, e.getMessage())); -// } catch (Exception e) { -// String message = "Something went wrong. Please contact admin."; -// return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) -// .body(new GenericResponse(null, message)); -// } -// } + // @GetMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE) + // public ResponseEntity> getEndorsementById( + // @PathVariable(value = "id", required = true) String id) { + // try { + // UUID uuid = UUID.fromString(id); + // EndorsementDTO response = endorsementService.getEndorsementById(uuid); + // return ResponseEntity.ok() + // .body(new GenericResponse(response, "Data retrieved + // successfully")); + // } catch (IllegalArgumentException e) { + // String message = "Invalid UUID: " + id; + // return ResponseEntity.status(HttpStatus.BAD_REQUEST) + // .body(new GenericResponse(null, message)); + // } catch (EntityNotFoundException e) { + // return ResponseEntity.status(HttpStatus.NOT_FOUND) + // .body(new GenericResponse(null, e.getMessage())); + // } catch (Exception e) { + // String message = "Something went wrong. Please contact admin."; + // return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) + // .body(new GenericResponse(null, message)); + // } + // } @PostMapping + // TODO : add a check for when a endorsement is already created by a user for a particular skill. public ResponseEntity> postEndorsement( @RequestBody @Valid CreateEndorsementDro endorsementDro) { @@ -61,7 +62,9 @@ public ResponseEntity> postEndorsement( String endorserId = "ae7a6673c5574140838f209de4c644fc"; if (Objects.equals(endorseId, endorserId)) { - return new ResponseEntity<>(new GenericResponse<>(null, "Self endorsement not allowed"), HttpStatus.METHOD_NOT_ALLOWED); + return new ResponseEntity<>( + new GenericResponse<>(null, "Self endorsement not allowed"), + HttpStatus.METHOD_NOT_ALLOWED); } Optional skillDetails = skillRepository.findById(skillId); @@ -69,38 +72,67 @@ public ResponseEntity> postEndorsement( Optional endorserDetails = userRepository.findById(endorserId); if (endorserDetails.isEmpty()) { - return new ResponseEntity<>(new GenericResponse<>(null, "Endorser details not found"), HttpStatus.NOT_FOUND); + return new ResponseEntity<>( + new GenericResponse<>(null, "Endorser details not found"), HttpStatus.NOT_FOUND); } if (endorseDetails.isEmpty()) { - return new ResponseEntity<>(new GenericResponse<>(null, "Endorse not found"), HttpStatus.NOT_FOUND); + return new ResponseEntity<>( + new GenericResponse<>(null, "Endorse not found"), HttpStatus.NOT_FOUND); } if (skillDetails.isEmpty()) { - return new ResponseEntity<>(new GenericResponse<>(null, String.format("Skill id: %s not found.", skillId)), HttpStatus.NOT_FOUND); + return new ResponseEntity<>( + new GenericResponse<>(null, String.format("Skill id: %s not found.", skillId)), + HttpStatus.NOT_FOUND); } - EndorsementModel newEndorsement = EndorsementModel.builder() - .endorser(endorserDetails.get()) - .endorse(endorseDetails.get()) - .skill(skillDetails.get()) - .message(message) - .build(); + EndorsementModel newEndorsement = + EndorsementModel.builder() + .endorser(endorserDetails.get()) + .endorse(endorseDetails.get()) + .skill(skillDetails.get()) + .message(message) + .build(); try { - return - new ResponseEntity<>( - new GenericResponse<>(endorsementRepository.save(newEndorsement), ""), - HttpStatus.CREATED); + return new ResponseEntity<>( + new GenericResponse<>(endorsementRepository.save(newEndorsement), ""), + HttpStatus.CREATED); } catch (DataIntegrityViolationException error) { - log.error("Error saving endorsement with skillId {} and endorse id {}, error: {}", skillId, endorseId, error.getMessage()); - return new ResponseEntity<>(new GenericResponse<>(null, "Something went wrong"), HttpStatus.INTERNAL_SERVER_ERROR); + log.error( + "Error saving endorsement with skillId {} and endorse id {}, error: {}", + skillId, + endorseId, + error.getMessage()); + return new ResponseEntity<>( + new GenericResponse<>(null, "Something went wrong"), HttpStatus.INTERNAL_SERVER_ERROR); } } -// @PatchMapping(value = "/{id}") -// public ResponseEntity> updateEndorsementStatus( -// @PathVariable(value = "id") UUID id, @RequestParam String status) { -// return ResponseEntity.ok().body(endorsementService.updateEndorsementStatus(id, status)); -// } + @PatchMapping("/{id}") + public ResponseEntity> updateEndorsement( + @PathVariable Integer id, @RequestBody UpdateEndorsementDro body) { + Optional endorsementDetails = endorsementRepository.findById(id); + + if (endorsementDetails.isEmpty()) { + return new ResponseEntity<>( + new GenericResponse<>(null, "Endorsement not found"), HttpStatus.NOT_FOUND); + } + + EndorsementModel endorsement = endorsementDetails.get(); + + if (body.getMessage() != null) { + endorsement.setMessage(body.getMessage()); + } + + try { + return new ResponseEntity<>(new GenericResponse<>(endorsementRepository.save(endorsement), "Endorsement updated"), HttpStatus.OK); + } catch ( + DataIntegrityViolationException error) { + log.error("Error endorsement id: {}, error: {}", endorsement.getId(), error.getMessage()); + return new ResponseEntity<>(new GenericResponse<>(null, "Something went wrong please try again"), HttpStatus.INTERNAL_SERVER_ERROR); + } + + } } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementDTO.java b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementDTO.java index 19fd16b3..6d71a26a 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementDTO.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementDTO.java @@ -2,9 +2,6 @@ import com.RDS.skilltree.utils.TrackedProperties; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; - -import java.util.UUID; - import lombok.*; @Getter @@ -15,15 +12,15 @@ public class EndorsementDTO extends TrackedProperties { private String endorseId; private Integer skillId; -// public static EndorsementDTO toDto(EndorsementModel endorsementModel) { -// EndorsementDTO endorsementDTO = -// EndorsementDTO.builder() -// .id(endorsementModel.getId()) -// .endorseId(endorsementModel.getEndorseId()) -// .skillId(endorsementModel.getSkillId()) -// .build(); -// endorsementDTO.setCreatedAt(endorsementModel.getCreatedAt()); -// endorsementDTO.setUpdatedAt(endorsementModel.getUpdatedAt()); -// return endorsementDTO; -// } + // public static EndorsementDTO toDto(EndorsementModel endorsementModel) { + // EndorsementDTO endorsementDTO = + // EndorsementDTO.builder() + // .id(endorsementModel.getId()) + // .endorseId(endorsementModel.getEndorseId()) + // .skillId(endorsementModel.getSkillId()) + // .build(); + // endorsementDTO.setCreatedAt(endorsementModel.getCreatedAt()); + // endorsementDTO.setUpdatedAt(endorsementModel.getUpdatedAt()); + // return endorsementDTO; + // } } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementModel.java b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementModel.java index 698b40d1..3fa3e9e1 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementModel.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementModel.java @@ -4,7 +4,6 @@ import com.RDS.skilltree.User.UserModel; import com.RDS.skilltree.utils.TrackedProperties; import jakarta.persistence.*; - import lombok.*; @AllArgsConstructor @@ -12,6 +11,7 @@ @Entity @Builder @Getter +@Setter @Table(name = "endorsements") public class EndorsementModel extends TrackedProperties { @Id diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementRepository.java b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementRepository.java index ce4d4e00..a02bef98 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementRepository.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementRepository.java @@ -1,13 +1,11 @@ package com.RDS.skilltree.Endorsement; -import java.util.UUID; - import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository -public interface EndorsementRepository extends JpaRepository { +public interface EndorsementRepository extends JpaRepository { Page findBySkillId(Integer skillId, Pageable pageable); } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementService.java b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementService.java index f46b90b5..2b1fee2e 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementService.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementService.java @@ -1,13 +1,5 @@ package com.RDS.skilltree.Endorsement; -import com.RDS.skilltree.Common.Response.GenericResponse; - -import java.util.UUID; - public interface EndorsementService { - EndorsementModel getEndorsementById(UUID id); - -// EndorsementModel createEndorsement(CreateEndorsementDro createEndorsementDro); - -// GenericResponse updateEndorsementStatus(UUID id, String status); + EndorsementModel getEndorsementById(Integer id); } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementServiceImpl.java b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementServiceImpl.java index c85634bb..36bf4b86 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementServiceImpl.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementServiceImpl.java @@ -1,21 +1,12 @@ package com.RDS.skilltree.Endorsement; -import com.RDS.skilltree.Common.Response.GenericResponse; -import com.RDS.skilltree.Exceptions.NoEntityException; -import com.RDS.skilltree.Skill.Skill; import com.RDS.skilltree.Skill.SkillRepository; -import com.RDS.skilltree.User.JwtUserModel; -import com.RDS.skilltree.User.UserRoleEnum; import jakarta.persistence.EntityNotFoundException; - -import java.util.Optional; -import java.util.UUID; - import lombok.RequiredArgsConstructor; -import org.springframework.security.access.AccessDeniedException; -import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Service; +import java.util.Optional; + @Service @RequiredArgsConstructor public class EndorsementServiceImpl implements EndorsementService { @@ -23,49 +14,9 @@ public class EndorsementServiceImpl implements EndorsementService { private final SkillRepository skillRepository; @Override - public EndorsementModel getEndorsementById(UUID id) throws IllegalStateException { + public EndorsementModel getEndorsementById(Integer id) throws IllegalStateException { Optional endorsementModel = endorsementRepository.findById(id); return endorsementModel.orElseThrow( () -> new EntityNotFoundException("No endorsement with the id " + id + " found")); } - -// @Override -// public EndorsementModel createEndorsement(CreateEndorsementDro createEndorsementDro) { -// String userId = createEndorsementDro.getEndorseId(); -// Integer skillId = createEndorsementDro.getSkillId(); -// -// Optional skillOptional = skillRepository.findById(skillId); -// if (skillOptional.isPresent()) { -// EndorsementModel endorsementModel = -// EndorsementModel.builder().endorseId(userId).skillId(skillOptional.get().getId()).build(); -// -// return endorsementRepository.save(endorsementModel); -// } else { -// -// throw new NoEntityException("Skill with id:" + skillId + " not found"); -// } -// } - -// @Override -// public GenericResponse updateEndorsementStatus(UUID id, String status) { -// JwtUserModel user = -// (JwtUserModel) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); -// if (!user.getRole().equals(UserRoleEnum.SUPERUSER)) { -// throw new AccessDeniedException("Unauthorized, Access is only available to super users"); -// } -// -// -// Optional optionalEndorsementModel = endorsementRepository.findById(id); -// if (optionalEndorsementModel.isPresent()) { -// EndorsementModel updatedEndorsementModel = -// EndorsementModel.builder() -// .id(optionalEndorsementModel.get().getId()) -// .endorseId(optionalEndorsementModel.get().getEndorseId()) -// .skillId(optionalEndorsementModel.get().getSkillId()) -// .build(); -// endorsementRepository.save(updatedEndorsementModel); -// return new GenericResponse<>(null, "Successfully updated endorsement status"); -// } -// throw new NoEntityException("No endorsement with id " + id + " was found"); -// } } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/UpdateEndorsementDro.java b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/UpdateEndorsementDro.java new file mode 100644 index 00000000..0263a6ff --- /dev/null +++ b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/UpdateEndorsementDro.java @@ -0,0 +1,16 @@ +package com.RDS.skilltree.Endorsement; + +import jakarta.validation.constraints.NotNull; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@NoArgsConstructor +@AllArgsConstructor +@Data +@Builder +public class UpdateEndorsementDro { + @NotNull(message = "Message cannot be empty") + private String message; +} diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Skill/Skill.java b/skill-tree/src/main/java/com/RDS/skilltree/Skill/Skill.java index 8c845096..bc4b199e 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Skill/Skill.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Skill/Skill.java @@ -3,7 +3,6 @@ import com.RDS.skilltree.User.UserModel; import com.RDS.skilltree.utils.TrackedProperties; import jakarta.persistence.*; - import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Getter; diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDRO.java b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDRO.java index f541daab..435ce8df 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDRO.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDRO.java @@ -2,7 +2,6 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import jakarta.validation.constraints.NotNull; - import lombok.Builder; import lombok.Getter; diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDTO.java b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDTO.java index b3748e35..89f3a8e9 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDTO.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDTO.java @@ -2,10 +2,8 @@ import com.RDS.skilltree.User.UserDTO; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; - import java.util.HashSet; import java.util.Set; - import lombok.Builder; import lombok.Getter; @@ -19,11 +17,7 @@ public class SkillDTO { private Set users; public static SkillDTO toDto(Skill skill) { - return SkillDTO.builder() - .id(skill.getId()) - .name(skill.getName()) - .type(skill.getType()) - .build(); + return SkillDTO.builder().id(skill.getId()).name(skill.getName()).type(skill.getType()).build(); } public static SkillDTO getSkillsWithUsers(Skill skill) { diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillRepository.java b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillRepository.java index 39f048dc..76cf4530 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillRepository.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillRepository.java @@ -1,8 +1,6 @@ package com.RDS.skilltree.Skill; import java.util.Optional; -import java.util.UUID; - import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsController.java b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsController.java index 7025ec1b..df49ac68 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsController.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsController.java @@ -7,6 +7,8 @@ import com.RDS.skilltree.User.UserRepository; import jakarta.validation.Valid; import jakarta.validation.constraints.Min; +import java.util.List; +import java.util.Optional; import lombok.extern.slf4j.Slf4j; import org.springframework.dao.DataIntegrityViolationException; import org.springframework.data.domain.Page; @@ -16,9 +18,6 @@ import org.springframework.security.core.Authentication; import org.springframework.web.bind.annotation.*; -import java.util.List; -import java.util.Optional; - @Slf4j @RestController @RequestMapping("/v1/skills") @@ -27,7 +26,10 @@ public class SkillsController { private final SkillRepository skillRepository; private final EndorsementRepository endorsementRepository; - public SkillsController(UserRepository userRepository, SkillRepository skillRepository, EndorsementRepository endorsementRepository) { + public SkillsController( + UserRepository userRepository, + SkillRepository skillRepository, + EndorsementRepository endorsementRepository) { this.userRepository = userRepository; this.skillRepository = skillRepository; this.endorsementRepository = endorsementRepository; @@ -42,7 +44,8 @@ public GenericResponse> getAllSkills(@RequestParam(required = false) @PostMapping @ResponseStatus(HttpStatus.CREATED) // TODO : Return only the valid fields by using a DTO. - public GenericResponse createSkill(Authentication authentication, @RequestBody(required = true) @Valid SkillDRO skill) { + public GenericResponse createSkill( + Authentication authentication, @RequestBody(required = true) @Valid SkillDRO skill) { JwtUserModel userDetails = (JwtUserModel) authentication.getPrincipal(); String userId = "ae7a6673c5574140838f209de4c644fc"; @@ -50,7 +53,8 @@ public GenericResponse createSkill(Authentication authentication, @Reques // TODO : return a correct http status instead of 201 if (skillRepository.findByName(skill.getName()).isPresent()) { - return new GenericResponse<>(null, String.format("Skill with name %s already exists", skill.getName())); + return new GenericResponse<>( + null, String.format("Skill with name %s already exists", skill.getName())); } // TODO : return a correct http status instead of 201 @@ -58,11 +62,8 @@ public GenericResponse createSkill(Authentication authentication, @Reques return new GenericResponse<>(null, "User not found"); } - Skill newSkill = Skill.builder() - .name(skill.getName()) - .type(skill.getType()) - .createdBy(user.get()) - .build(); + Skill newSkill = + Skill.builder().name(skill.getName()).type(skill.getType()).createdBy(user.get()).build(); try { return new GenericResponse<>(skillRepository.save(newSkill), "Skill created"); diff --git a/skill-tree/src/main/java/com/RDS/skilltree/User/UserDRO.java b/skill-tree/src/main/java/com/RDS/skilltree/User/UserDRO.java index aff3097e..79bd6b2c 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/User/UserDRO.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/User/UserDRO.java @@ -25,14 +25,14 @@ public class UserDRO { public static UserModel toModel(UserDRO user) { return UserModel.builder() .rdsUserId(user.getRdsUserId()) -// .role(user.getRole()) + // .role(user.getRole()) .build(); } public static UserDRO fromModel(UserModel user) { return UserDRO.builder() .rdsUserId(user.getRdsUserId()) -// .role(user.getRole()) + // .role(user.getRole()) .build(); } @@ -40,9 +40,9 @@ public static UserModel compareAndUpdateModel(UserModel user, UserDRO userDRO) { if (userDRO.getRdsUserId() != null) { user.setRdsUserId(user.getRdsUserId()); } -// if (userDRO.getRole() != null) { -// user.setRole(user.getRole()); -// } + // if (userDRO.getRole() != null) { + // user.setRole(user.getRole()); + // } user.setUpdatedAt(Instant.now()); return user; } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/User/UserDTO.java b/skill-tree/src/main/java/com/RDS/skilltree/User/UserDTO.java index b9502253..7bce2ed6 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/User/UserDTO.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/User/UserDTO.java @@ -1,11 +1,8 @@ package com.RDS.skilltree.User; import com.RDS.skilltree.Skill.SkillDTO; - import java.net.URL; import java.util.Set; -import java.util.UUID; - import lombok.Builder; import lombok.Getter; @@ -29,21 +26,18 @@ public class UserDTO { public static UserDTO toDTO(UserModel user) { - return UserDTO.builder() - .id(user.getId()) - .rdsUserId(user.getRdsUserId()) - .build(); + return UserDTO.builder().id(user.getId()).rdsUserId(user.getRdsUserId()).build(); } public static UserDTO getUsersWithSkills(UserModel user) { -// Set skills = [] -// user.getSkills().stream().map(SkillDTO::toDto).collect(Collectors.toSet()); + // Set skills = [] + // user.getSkills().stream().map(SkillDTO::toDto).collect(Collectors.toSet()); return UserDTO.builder() .id(user.getId()) .rdsUserId(user.getRdsUserId()) -// .skills(skills) -// .role(user.getRole()) + // .skills(skills) + // .role(user.getRole()) .build(); } } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/User/UserModel.java b/skill-tree/src/main/java/com/RDS/skilltree/User/UserModel.java index 07296ea6..65f4f8af 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/User/UserModel.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/User/UserModel.java @@ -3,9 +3,6 @@ import com.RDS.skilltree.utils.TrackedProperties; import com.fasterxml.jackson.databind.annotation.JsonSerialize; import jakarta.persistence.*; - -import java.util.UUID; - import lombok.*; @Entity diff --git a/skill-tree/src/main/java/com/RDS/skilltree/User/UserRepository.java b/skill-tree/src/main/java/com/RDS/skilltree/User/UserRepository.java index 4bc8dd3b..6700fd30 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/User/UserRepository.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/User/UserRepository.java @@ -4,5 +4,4 @@ import org.springframework.stereotype.Repository; @Repository -public interface UserRepository extends JpaRepository { -} +public interface UserRepository extends JpaRepository {} diff --git a/skill-tree/src/main/java/com/RDS/skilltree/User/UserServiceImpl.java b/skill-tree/src/main/java/com/RDS/skilltree/User/UserServiceImpl.java index 95e8969f..32dd1466 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/User/UserServiceImpl.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/User/UserServiceImpl.java @@ -4,10 +4,8 @@ import com.RDS.skilltree.Skill.Skill; import com.RDS.skilltree.Skill.SkillRepository; import jakarta.transaction.Transactional; - import java.util.List; import java.util.Optional; - import org.springframework.stereotype.Service; @Service @@ -28,8 +26,7 @@ public UserDTO createUser(UserDRO user) { } @Override - public void updateUser(String id, UserDRO user) { - } + public void updateUser(String id, UserDRO user) {} @Override public UserDTO getUserById(String id) { @@ -58,8 +55,8 @@ public void addSkill(Integer skillId, String userId) { UserModel userModel = userOptional.get(); Skill skill = skillOptional.get(); -// userModel.getSkills().add(skillModel); -// skillModel.getUsers().add(userModel); + // userModel.getSkills().add(skillModel); + // skillModel.getUsers().add(userModel); userRepository.save(userModel); skillRepository.save(skill); diff --git a/skill-tree/src/main/java/com/RDS/skilltree/User/UserSkillsModel.java b/skill-tree/src/main/java/com/RDS/skilltree/User/UserSkillsModel.java index a068d2f7..f92cb872 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/User/UserSkillsModel.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/User/UserSkillsModel.java @@ -3,10 +3,8 @@ import com.RDS.skilltree.Skill.Skill; import com.fasterxml.jackson.annotation.JsonBackReference; import jakarta.persistence.*; - -import lombok.*; - import java.util.UUID; +import lombok.*; @Entity @Getter diff --git a/skill-tree/src/main/java/com/RDS/skilltree/utils/TrackedProperties.java b/skill-tree/src/main/java/com/RDS/skilltree/utils/TrackedProperties.java index 57a6d566..d11dd1e1 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/utils/TrackedProperties.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/utils/TrackedProperties.java @@ -2,9 +2,7 @@ import jakarta.persistence.Column; import jakarta.persistence.MappedSuperclass; - import java.time.Instant; - import lombok.Data; import org.hibernate.annotations.CreationTimestamp; import org.hibernate.annotations.SourceType; @@ -17,7 +15,6 @@ public abstract class TrackedProperties { @Column(name = "created_at", nullable = false, updatable = false) private Instant createdAt; - @UpdateTimestamp(source = SourceType.DB) @Column(name = "updated_at") private Instant updatedAt; From 7a48e9c91e9846e66897fe2dfafa340a484cc293 Mon Sep 17 00:00:00 2001 From: yash raj Date: Tue, 25 Jun 2024 21:40:59 +0530 Subject: [PATCH 14/31] remove unused files --- .../Endorsement/EndorsementController.java | 1 - .../Endorsement/EndorsementModelFromJSON.java | 39 ------------------- .../Endorsement/EndorsementService.java | 5 --- .../Endorsement/EndorsementServiceImpl.java | 22 ----------- 4 files changed, 67 deletions(-) delete mode 100644 skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementModelFromJSON.java delete mode 100644 skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementService.java delete mode 100644 skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementServiceImpl.java diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementController.java b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementController.java index d2838b93..9fefd345 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementController.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementController.java @@ -23,7 +23,6 @@ public class EndorsementController { private final UserRepository userRepository; private final EndorsementRepository endorsementRepository; - private final EndorsementService endorsementService; private final SkillRepository skillRepository; // @GetMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE) diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementModelFromJSON.java b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementModelFromJSON.java deleted file mode 100644 index c399043d..00000000 --- a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementModelFromJSON.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.RDS.skilltree.Endorsement; - -import com.fasterxml.jackson.annotation.JsonFormat; -import com.fasterxml.jackson.annotation.JsonProperty; -import java.time.LocalDateTime; -import java.util.UUID; -import lombok.*; - -/* TODO:Dummy JSON code, needs to be changed as part of #103 */ -@Data -@NoArgsConstructor -@AllArgsConstructor -@Getter -@Setter -public class EndorsementModelFromJSON { - private UUID id; - - @JsonProperty("user_id") - private UUID userID; - - @JsonProperty("skill_id") - private UUID skillId; - - private String status; - - @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss.SSSSSS") - @JsonProperty("created_at") - private LocalDateTime createdAt; - - @JsonProperty("created_by") - private UUID createdBy; - - @JsonProperty("updated_at") - @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss.SSSSSS") - private LocalDateTime updatedAt; - - @JsonProperty("updated_by") - private UUID updatedBy; -} diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementService.java b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementService.java deleted file mode 100644 index 2b1fee2e..00000000 --- a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementService.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.RDS.skilltree.Endorsement; - -public interface EndorsementService { - EndorsementModel getEndorsementById(Integer id); -} diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementServiceImpl.java b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementServiceImpl.java deleted file mode 100644 index 36bf4b86..00000000 --- a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementServiceImpl.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.RDS.skilltree.Endorsement; - -import com.RDS.skilltree.Skill.SkillRepository; -import jakarta.persistence.EntityNotFoundException; -import lombok.RequiredArgsConstructor; -import org.springframework.stereotype.Service; - -import java.util.Optional; - -@Service -@RequiredArgsConstructor -public class EndorsementServiceImpl implements EndorsementService { - private final EndorsementRepository endorsementRepository; - private final SkillRepository skillRepository; - - @Override - public EndorsementModel getEndorsementById(Integer id) throws IllegalStateException { - Optional endorsementModel = endorsementRepository.findById(id); - return endorsementModel.orElseThrow( - () -> new EntityNotFoundException("No endorsement with the id " + id + " found")); - } -} From 22eece341eaf3d1ded51a7c36d96082403a8d03b Mon Sep 17 00:00:00 2001 From: yash raj Date: Tue, 25 Jun 2024 22:39:29 +0530 Subject: [PATCH 15/31] remove unused code in endorsement controller --- .../Endorsement/EndorsementController.java | 23 ------------------- .../RDS/skilltree/Skill/SkillsController.java | 15 ++++-------- 2 files changed, 5 insertions(+), 33 deletions(-) diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementController.java b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementController.java index 9fefd345..0284e763 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementController.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementController.java @@ -25,29 +25,6 @@ public class EndorsementController { private final EndorsementRepository endorsementRepository; private final SkillRepository skillRepository; - // @GetMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE) - // public ResponseEntity> getEndorsementById( - // @PathVariable(value = "id", required = true) String id) { - // try { - // UUID uuid = UUID.fromString(id); - // EndorsementDTO response = endorsementService.getEndorsementById(uuid); - // return ResponseEntity.ok() - // .body(new GenericResponse(response, "Data retrieved - // successfully")); - // } catch (IllegalArgumentException e) { - // String message = "Invalid UUID: " + id; - // return ResponseEntity.status(HttpStatus.BAD_REQUEST) - // .body(new GenericResponse(null, message)); - // } catch (EntityNotFoundException e) { - // return ResponseEntity.status(HttpStatus.NOT_FOUND) - // .body(new GenericResponse(null, e.getMessage())); - // } catch (Exception e) { - // String message = "Something went wrong. Please contact admin."; - // return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) - // .body(new GenericResponse(null, message)); - // } - // } - @PostMapping // TODO : add a check for when a endorsement is already created by a user for a particular skill. public ResponseEntity> postEndorsement( diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsController.java b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsController.java index df49ac68..cce7ba87 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsController.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsController.java @@ -7,8 +7,11 @@ import com.RDS.skilltree.User.UserRepository; import jakarta.validation.Valid; import jakarta.validation.constraints.Min; + import java.util.List; import java.util.Optional; + +import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.dao.DataIntegrityViolationException; import org.springframework.data.domain.Page; @@ -20,21 +23,13 @@ @Slf4j @RestController +@RequiredArgsConstructor @RequestMapping("/v1/skills") public class SkillsController { private final UserRepository userRepository; private final SkillRepository skillRepository; private final EndorsementRepository endorsementRepository; - - public SkillsController( - UserRepository userRepository, - SkillRepository skillRepository, - EndorsementRepository endorsementRepository) { - this.userRepository = userRepository; - this.skillRepository = skillRepository; - this.endorsementRepository = endorsementRepository; - } - + @GetMapping public GenericResponse> getAllSkills(@RequestParam(required = false) String name) { skillRepository.findByName(name); From 514ffdefe90948f455f07dc70f4ab178b74b7e50 Mon Sep 17 00:00:00 2001 From: yash raj Date: Wed, 26 Jun 2024 00:11:41 +0530 Subject: [PATCH 16/31] chagne import order --- .../com/RDS/skilltree/Endorsement/EndorsementController.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementController.java b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementController.java index 0284e763..00ab7dad 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementController.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementController.java @@ -16,10 +16,10 @@ import java.util.Objects; import java.util.Optional; -@RestController -@RequestMapping("/v1/endorsements") @Slf4j +@RestController @RequiredArgsConstructor +@RequestMapping("/v1/endorsements") public class EndorsementController { private final UserRepository userRepository; private final EndorsementRepository endorsementRepository; From 15e5740906ec1e0751b38d0059241a5c4d35b72b Mon Sep 17 00:00:00 2001 From: yash raj Date: Wed, 26 Jun 2024 00:46:43 +0530 Subject: [PATCH 17/31] change the skills project structure to match the new one --- .../java/com/RDS/skilltree/Skill/Skill.java | 2 +- .../RDS/skilltree/Skill/SkillsController.java | 4 +- .../com/RDS/skilltree/apis/SkillsApi.java | 25 +++++++++++++ .../java/com/RDS/skilltree/models/Skill.java | 37 +++++++++++++++++++ .../repositories/SkillRepository.java | 9 +++++ .../RDS/skilltree/services/SkillService.java | 9 +++++ .../services/SkillServiceImplementation.java | 30 +++++++++++++++ .../skilltree/viewmodels/SkillViewModel.java | 13 +++++++ 8 files changed, 126 insertions(+), 3 deletions(-) create mode 100644 skill-tree/src/main/java/com/RDS/skilltree/apis/SkillsApi.java create mode 100644 skill-tree/src/main/java/com/RDS/skilltree/models/Skill.java create mode 100644 skill-tree/src/main/java/com/RDS/skilltree/repositories/SkillRepository.java create mode 100644 skill-tree/src/main/java/com/RDS/skilltree/services/SkillService.java create mode 100644 skill-tree/src/main/java/com/RDS/skilltree/services/SkillServiceImplementation.java create mode 100644 skill-tree/src/main/java/com/RDS/skilltree/viewmodels/SkillViewModel.java diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Skill/Skill.java b/skill-tree/src/main/java/com/RDS/skilltree/Skill/Skill.java index bc4b199e..7e741779 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Skill/Skill.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Skill/Skill.java @@ -13,7 +13,7 @@ @Entity @Builder @Getter -@Table(name = "skills") +//@Table(name = "skills") public class Skill extends TrackedProperties { @Id @GeneratedValue diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsController.java b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsController.java index cce7ba87..1a1700be 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsController.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsController.java @@ -24,12 +24,12 @@ @Slf4j @RestController @RequiredArgsConstructor -@RequestMapping("/v1/skills") +@RequestMapping("/v2/skills") public class SkillsController { private final UserRepository userRepository; private final SkillRepository skillRepository; private final EndorsementRepository endorsementRepository; - + @GetMapping public GenericResponse> getAllSkills(@RequestParam(required = false) String name) { skillRepository.findByName(name); 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 new file mode 100644 index 00000000..0b0fd40e --- /dev/null +++ b/skill-tree/src/main/java/com/RDS/skilltree/apis/SkillsApi.java @@ -0,0 +1,25 @@ +package com.RDS.skilltree.apis; + +import com.RDS.skilltree.services.SkillService; +import com.RDS.skilltree.viewmodels.SkillViewModel; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +@Slf4j +@RestController +@RequiredArgsConstructor +@RequestMapping("v1/skills") +public class SkillsApi { + private final SkillService skillService; + + @GetMapping + public ResponseEntity> getAll() { + return ResponseEntity.ok(skillService.getAll()); + } +} diff --git a/skill-tree/src/main/java/com/RDS/skilltree/models/Skill.java b/skill-tree/src/main/java/com/RDS/skilltree/models/Skill.java new file mode 100644 index 00000000..0505ba83 --- /dev/null +++ b/skill-tree/src/main/java/com/RDS/skilltree/models/Skill.java @@ -0,0 +1,37 @@ +package com.RDS.skilltree.models; + +import com.RDS.skilltree.Skill.SkillTypeEnum; +import com.RDS.skilltree.User.UserModel; +import jakarta.persistence.*; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@AllArgsConstructor +@NoArgsConstructor +@Entity +@Builder +@Getter +@Table(name = "skills") +public class Skill { + @Id + @GeneratedValue + @Column(name = "id") + private Integer id; + + @Column(name = "name", unique = true, nullable = false) + private String name; + + @Column(name = "skill_type", nullable = false) + @Enumerated(value = EnumType.STRING) + private SkillTypeEnum type = SkillTypeEnum.ATOMIC; + + @ManyToOne + @JoinColumn(name = "created_by", nullable = false) + private UserModel createdBy; + + @ManyToOne + @JoinColumn(name = "updated_by") + private UserModel updatedBy; +} diff --git a/skill-tree/src/main/java/com/RDS/skilltree/repositories/SkillRepository.java b/skill-tree/src/main/java/com/RDS/skilltree/repositories/SkillRepository.java new file mode 100644 index 00000000..a42ca526 --- /dev/null +++ b/skill-tree/src/main/java/com/RDS/skilltree/repositories/SkillRepository.java @@ -0,0 +1,9 @@ +package com.RDS.skilltree.repositories; + +import com.RDS.skilltree.models.Skill; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface SkillRepository extends JpaRepository { +} \ No newline at end of file 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 new file mode 100644 index 00000000..85a776a8 --- /dev/null +++ b/skill-tree/src/main/java/com/RDS/skilltree/services/SkillService.java @@ -0,0 +1,9 @@ +package com.RDS.skilltree.services; + +import com.RDS.skilltree.viewmodels.SkillViewModel; + +import java.util.List; + +public interface SkillService { + List getAll(); +} 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 new file mode 100644 index 00000000..108976db --- /dev/null +++ b/skill-tree/src/main/java/com/RDS/skilltree/services/SkillServiceImplementation.java @@ -0,0 +1,30 @@ +package com.RDS.skilltree.services; + +import com.RDS.skilltree.models.Skill; +import com.RDS.skilltree.repositories.SkillRepository; +import com.RDS.skilltree.viewmodels.SkillViewModel; +import lombok.RequiredArgsConstructor; +import org.springframework.beans.BeanUtils; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.stream.Collectors; + +@Service +@RequiredArgsConstructor +public class SkillServiceImplementation implements SkillService { + private final SkillRepository skillRepository; + + @Override + public List getAll() { + return skillRepository.findAll().stream() + .map(this::toViewModel) + .collect(Collectors.toList()); + } + + public SkillViewModel toViewModel(Skill entity) { + SkillViewModel viewModel = new SkillViewModel(); + BeanUtils.copyProperties(entity, viewModel); + return viewModel; + } +} diff --git a/skill-tree/src/main/java/com/RDS/skilltree/viewmodels/SkillViewModel.java b/skill-tree/src/main/java/com/RDS/skilltree/viewmodels/SkillViewModel.java new file mode 100644 index 00000000..9128471e --- /dev/null +++ b/skill-tree/src/main/java/com/RDS/skilltree/viewmodels/SkillViewModel.java @@ -0,0 +1,13 @@ +package com.RDS.skilltree.viewmodels; + +import com.RDS.skilltree.Skill.SkillTypeEnum; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class SkillViewModel { + private Integer id; + private String name; + private SkillTypeEnum type = SkillTypeEnum.ATOMIC; +} From c77efb4e5e8f79f335e69b42071dd381a79cdd06 Mon Sep 17 00:00:00 2001 From: yash raj Date: Wed, 26 Jun 2024 02:04:28 +0530 Subject: [PATCH 18/31] rename exceptions folder to small case exceptions and create user not found and skill already exists exceptions --- .../Endorsement/EndorsementController.java | 4 +- .../Endorsement/EndorsementModel.java | 2 +- .../java/com/RDS/skilltree/Skill/Skill.java | 37 ------------------ .../com/RDS/skilltree/Skill/SkillDTO.java | 6 ++- .../RDS/skilltree/Skill/SkillRepository.java | 10 ----- .../RDS/skilltree/Skill/SkillsController.java | 18 ++++----- .../RDS/skilltree/User/UserServiceImpl.java | 12 +++--- .../RDS/skilltree/User/UserSkillsModel.java | 5 ++- .../com/RDS/skilltree/apis/SkillsApi.java | 11 ++++-- .../EntityAlreadyExistsException.java | 2 +- .../GlobalExceptionHandler.java | 23 ++++++++--- .../InvalidParameterException.java | 2 +- .../NoEntityException.java | 2 +- .../SkillAlreadyExistsException.java | 11 ++++++ .../exceptions/UserNotFoundException.java | 11 ++++++ .../java/com/RDS/skilltree/models/Skill.java | 9 ++--- .../repositories/SkillRepository.java | 5 +++ .../RDS/skilltree/services/SkillService.java | 3 ++ .../services/SkillServiceImplementation.java | 38 +++++++++++++++++++ .../utils/UUIDValidationInterceptor.java | 2 +- .../viewmodels/CreateSkillViewModel.java | 19 ++++++++++ .../utils/UUIDValidationInterceptorTest.java | 21 ++++++---- 22 files changed, 159 insertions(+), 94 deletions(-) delete mode 100644 skill-tree/src/main/java/com/RDS/skilltree/Skill/Skill.java delete mode 100644 skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillRepository.java rename skill-tree/src/main/java/com/RDS/skilltree/{Exceptions => exceptions}/EntityAlreadyExistsException.java (80%) rename skill-tree/src/main/java/com/RDS/skilltree/{utils => exceptions}/GlobalExceptionHandler.java (85%) rename skill-tree/src/main/java/com/RDS/skilltree/{Exceptions => exceptions}/InvalidParameterException.java (85%) rename skill-tree/src/main/java/com/RDS/skilltree/{Exceptions => exceptions}/NoEntityException.java (89%) create mode 100644 skill-tree/src/main/java/com/RDS/skilltree/exceptions/SkillAlreadyExistsException.java create mode 100644 skill-tree/src/main/java/com/RDS/skilltree/exceptions/UserNotFoundException.java create mode 100644 skill-tree/src/main/java/com/RDS/skilltree/viewmodels/CreateSkillViewModel.java diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementController.java b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementController.java index 00ab7dad..79fe672b 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementController.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementController.java @@ -1,10 +1,10 @@ package com.RDS.skilltree.Endorsement; import com.RDS.skilltree.Common.Response.GenericResponse; -import com.RDS.skilltree.Skill.Skill; -import com.RDS.skilltree.Skill.SkillRepository; import com.RDS.skilltree.User.UserModel; import com.RDS.skilltree.User.UserRepository; +import com.RDS.skilltree.models.Skill; +import com.RDS.skilltree.repositories.SkillRepository; import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementModel.java b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementModel.java index 3fa3e9e1..bad95a3b 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementModel.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementModel.java @@ -1,7 +1,7 @@ package com.RDS.skilltree.Endorsement; -import com.RDS.skilltree.Skill.Skill; import com.RDS.skilltree.User.UserModel; +import com.RDS.skilltree.models.Skill; import com.RDS.skilltree.utils.TrackedProperties; import jakarta.persistence.*; import lombok.*; diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Skill/Skill.java b/skill-tree/src/main/java/com/RDS/skilltree/Skill/Skill.java deleted file mode 100644 index 7e741779..00000000 --- a/skill-tree/src/main/java/com/RDS/skilltree/Skill/Skill.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.RDS.skilltree.Skill; - -import com.RDS.skilltree.User.UserModel; -import com.RDS.skilltree.utils.TrackedProperties; -import jakarta.persistence.*; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Getter; -import lombok.NoArgsConstructor; - -@AllArgsConstructor -@NoArgsConstructor -@Entity -@Builder -@Getter -//@Table(name = "skills") -public class Skill extends TrackedProperties { - @Id - @GeneratedValue - @Column(name = "id") - private Integer id; - - @Column(name = "name", unique = true, nullable = false) - private String name; - - @Column(name = "skill_type", nullable = false) - @Enumerated(value = EnumType.STRING) - private SkillTypeEnum type = SkillTypeEnum.ATOMIC; - - @ManyToOne - @JoinColumn(name = "created_by", nullable = false) - private UserModel createdBy; - - @ManyToOne - @JoinColumn(name = "updated_by") - private UserModel updatedBy; -} diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDTO.java b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDTO.java index 89f3a8e9..6abf3ff6 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDTO.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDTO.java @@ -1,12 +1,14 @@ package com.RDS.skilltree.Skill; import com.RDS.skilltree.User.UserDTO; +import com.RDS.skilltree.models.Skill; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import java.util.HashSet; -import java.util.Set; import lombok.Builder; import lombok.Getter; +import java.util.HashSet; +import java.util.Set; + @Getter @Builder @JsonIgnoreProperties(ignoreUnknown = true) diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillRepository.java b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillRepository.java deleted file mode 100644 index 76cf4530..00000000 --- a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillRepository.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.RDS.skilltree.Skill; - -import java.util.Optional; -import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.stereotype.Repository; - -@Repository -public interface SkillRepository extends JpaRepository { - Optional findByName(String name); -} diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsController.java b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsController.java index 1a1700be..ba30502e 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsController.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsController.java @@ -5,12 +5,10 @@ import com.RDS.skilltree.User.JwtUserModel; import com.RDS.skilltree.User.UserModel; import com.RDS.skilltree.User.UserRepository; +import com.RDS.skilltree.models.Skill; +import com.RDS.skilltree.repositories.SkillRepository; import jakarta.validation.Valid; import jakarta.validation.constraints.Min; - -import java.util.List; -import java.util.Optional; - import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.dao.DataIntegrityViolationException; @@ -21,6 +19,8 @@ import org.springframework.security.core.Authentication; import org.springframework.web.bind.annotation.*; +import java.util.Optional; + @Slf4j @RestController @RequiredArgsConstructor @@ -30,11 +30,11 @@ public class SkillsController { private final SkillRepository skillRepository; private final EndorsementRepository endorsementRepository; - @GetMapping - public GenericResponse> getAllSkills(@RequestParam(required = false) String name) { - skillRepository.findByName(name); - return new GenericResponse<>(skillRepository.findAll(), null); - } +// @GetMapping +// public GenericResponse> getAllSkills(@RequestParam(required = false) String name) { +// skillRepository.findByName(name); +// return new GenericResponse<>(skillRepository.findAll(), null); +// } @PostMapping @ResponseStatus(HttpStatus.CREATED) diff --git a/skill-tree/src/main/java/com/RDS/skilltree/User/UserServiceImpl.java b/skill-tree/src/main/java/com/RDS/skilltree/User/UserServiceImpl.java index 32dd1466..0008750b 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/User/UserServiceImpl.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/User/UserServiceImpl.java @@ -1,12 +1,13 @@ package com.RDS.skilltree.User; -import com.RDS.skilltree.Exceptions.NoEntityException; -import com.RDS.skilltree.Skill.Skill; -import com.RDS.skilltree.Skill.SkillRepository; +import com.RDS.skilltree.exceptions.NoEntityException; +import com.RDS.skilltree.models.Skill; +import com.RDS.skilltree.repositories.SkillRepository; import jakarta.transaction.Transactional; +import org.springframework.stereotype.Service; + import java.util.List; import java.util.Optional; -import org.springframework.stereotype.Service; @Service public class UserServiceImpl implements UserService { @@ -26,7 +27,8 @@ public UserDTO createUser(UserDRO user) { } @Override - public void updateUser(String id, UserDRO user) {} + public void updateUser(String id, UserDRO user) { + } @Override public UserDTO getUserById(String id) { diff --git a/skill-tree/src/main/java/com/RDS/skilltree/User/UserSkillsModel.java b/skill-tree/src/main/java/com/RDS/skilltree/User/UserSkillsModel.java index f92cb872..4fd9ed32 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/User/UserSkillsModel.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/User/UserSkillsModel.java @@ -1,11 +1,12 @@ package com.RDS.skilltree.User; -import com.RDS.skilltree.Skill.Skill; +import com.RDS.skilltree.models.Skill; import com.fasterxml.jackson.annotation.JsonBackReference; import jakarta.persistence.*; -import java.util.UUID; import lombok.*; +import java.util.UUID; + @Entity @Getter @Setter 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 0b0fd40e..f1c26a93 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 @@ -1,13 +1,13 @@ package com.RDS.skilltree.apis; import com.RDS.skilltree.services.SkillService; +import com.RDS.skilltree.viewmodels.CreateSkillViewModel; import com.RDS.skilltree.viewmodels.SkillViewModel; +import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import java.util.List; @@ -22,4 +22,9 @@ public class SkillsApi { public ResponseEntity> getAll() { return ResponseEntity.ok(skillService.getAll()); } + + @PostMapping + public ResponseEntity create(@Valid @RequestBody CreateSkillViewModel skill) { + return ResponseEntity.ok(skillService.create(skill)); + } } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Exceptions/EntityAlreadyExistsException.java b/skill-tree/src/main/java/com/RDS/skilltree/exceptions/EntityAlreadyExistsException.java similarity index 80% rename from skill-tree/src/main/java/com/RDS/skilltree/Exceptions/EntityAlreadyExistsException.java rename to skill-tree/src/main/java/com/RDS/skilltree/exceptions/EntityAlreadyExistsException.java index a013a043..3fc9a5d7 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Exceptions/EntityAlreadyExistsException.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/exceptions/EntityAlreadyExistsException.java @@ -1,4 +1,4 @@ -package com.RDS.skilltree.Exceptions; +package com.RDS.skilltree.exceptions; public class EntityAlreadyExistsException extends RuntimeException { public EntityAlreadyExistsException(String message) { diff --git a/skill-tree/src/main/java/com/RDS/skilltree/utils/GlobalExceptionHandler.java b/skill-tree/src/main/java/com/RDS/skilltree/exceptions/GlobalExceptionHandler.java similarity index 85% rename from skill-tree/src/main/java/com/RDS/skilltree/utils/GlobalExceptionHandler.java rename to skill-tree/src/main/java/com/RDS/skilltree/exceptions/GlobalExceptionHandler.java index 35cfff7e..e032182e 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/utils/GlobalExceptionHandler.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/exceptions/GlobalExceptionHandler.java @@ -1,11 +1,7 @@ -package com.RDS.skilltree.utils; +package com.RDS.skilltree.exceptions; import com.RDS.skilltree.Common.Response.GenericResponse; -import com.RDS.skilltree.Exceptions.EntityAlreadyExistsException; -import com.RDS.skilltree.Exceptions.InvalidParameterException; -import com.RDS.skilltree.Exceptions.NoEntityException; import jakarta.validation.ConstraintViolationException; -import java.util.List; import lombok.extern.slf4j.Slf4j; import org.apache.tomcat.websocket.AuthenticationException; import org.springframework.http.HttpStatus; @@ -16,9 +12,12 @@ import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.context.request.WebRequest; + +import java.util.List; -@ControllerAdvice @Slf4j +@ControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler({NoEntityException.class}) public ResponseEntity> handleNoEntityException(NoEntityException ex) { @@ -95,4 +94,16 @@ public ResponseEntity> handleException(ConstraintViolati return ResponseEntity.status(HttpStatus.BAD_REQUEST) .body(new GenericResponse<>(null, ex.getMessage())); } + + @ExceptionHandler(UserNotFoundException.class) + public ResponseEntity handleUserNotFoundException(UserNotFoundException ex, WebRequest request) { + log.error("Exception - Error : {}", ex.getMessage(), ex); + return new ResponseEntity<>(new GenericResponse<>(null, ex.getMessage()), HttpStatus.NOT_FOUND); + } + + @ExceptionHandler(SkillAlreadyExistsException.class) + public ResponseEntity handleSkillAlreadyExistsException(SkillAlreadyExistsException ex, WebRequest request) { + log.error("Exception - Error : {}", ex.getMessage(), ex); + return new ResponseEntity<>(new GenericResponse<>(null, ex.getMessage()), HttpStatus.NOT_FOUND); + } } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Exceptions/InvalidParameterException.java b/skill-tree/src/main/java/com/RDS/skilltree/exceptions/InvalidParameterException.java similarity index 85% rename from skill-tree/src/main/java/com/RDS/skilltree/Exceptions/InvalidParameterException.java rename to skill-tree/src/main/java/com/RDS/skilltree/exceptions/InvalidParameterException.java index fda72a6d..0e5bd832 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Exceptions/InvalidParameterException.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/exceptions/InvalidParameterException.java @@ -1,4 +1,4 @@ -package com.RDS.skilltree.Exceptions; +package com.RDS.skilltree.exceptions; public class InvalidParameterException extends IllegalArgumentException { public InvalidParameterException(String parameterName, String message) { diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Exceptions/NoEntityException.java b/skill-tree/src/main/java/com/RDS/skilltree/exceptions/NoEntityException.java similarity index 89% rename from skill-tree/src/main/java/com/RDS/skilltree/Exceptions/NoEntityException.java rename to skill-tree/src/main/java/com/RDS/skilltree/exceptions/NoEntityException.java index 0b8685bc..16143d89 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Exceptions/NoEntityException.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/exceptions/NoEntityException.java @@ -1,4 +1,4 @@ -package com.RDS.skilltree.Exceptions; +package com.RDS.skilltree.exceptions; public class NoEntityException extends RuntimeException { diff --git a/skill-tree/src/main/java/com/RDS/skilltree/exceptions/SkillAlreadyExistsException.java b/skill-tree/src/main/java/com/RDS/skilltree/exceptions/SkillAlreadyExistsException.java new file mode 100644 index 00000000..c51cdf54 --- /dev/null +++ b/skill-tree/src/main/java/com/RDS/skilltree/exceptions/SkillAlreadyExistsException.java @@ -0,0 +1,11 @@ +package com.RDS.skilltree.exceptions; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +@ResponseStatus(HttpStatus.CONFLICT) +public class SkillAlreadyExistsException extends RuntimeException { + public SkillAlreadyExistsException(String message) { + super(message); + } +} diff --git a/skill-tree/src/main/java/com/RDS/skilltree/exceptions/UserNotFoundException.java b/skill-tree/src/main/java/com/RDS/skilltree/exceptions/UserNotFoundException.java new file mode 100644 index 00000000..643e1d55 --- /dev/null +++ b/skill-tree/src/main/java/com/RDS/skilltree/exceptions/UserNotFoundException.java @@ -0,0 +1,11 @@ +package com.RDS.skilltree.exceptions; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +@ResponseStatus(HttpStatus.NOT_FOUND) +public class UserNotFoundException extends RuntimeException { + public UserNotFoundException(String message) { + super(message); + } +} diff --git a/skill-tree/src/main/java/com/RDS/skilltree/models/Skill.java b/skill-tree/src/main/java/com/RDS/skilltree/models/Skill.java index 0505ba83..23111d6d 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/models/Skill.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/models/Skill.java @@ -2,19 +2,18 @@ import com.RDS.skilltree.Skill.SkillTypeEnum; import com.RDS.skilltree.User.UserModel; +import com.RDS.skilltree.utils.TrackedProperties; import jakarta.persistence.*; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Getter; -import lombok.NoArgsConstructor; +import lombok.*; @AllArgsConstructor @NoArgsConstructor @Entity @Builder @Getter +@Setter @Table(name = "skills") -public class Skill { +public class Skill extends TrackedProperties { @Id @GeneratedValue @Column(name = "id") diff --git a/skill-tree/src/main/java/com/RDS/skilltree/repositories/SkillRepository.java b/skill-tree/src/main/java/com/RDS/skilltree/repositories/SkillRepository.java index a42ca526..ee230e3b 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/repositories/SkillRepository.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/repositories/SkillRepository.java @@ -4,6 +4,11 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; +import java.util.Optional; + @Repository public interface SkillRepository extends JpaRepository { + Optional findByName(String name); + + boolean existsByName(String name); } \ No newline at end of file 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 85a776a8..77923b6f 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,9 +1,12 @@ package com.RDS.skilltree.services; +import com.RDS.skilltree.viewmodels.CreateSkillViewModel; import com.RDS.skilltree.viewmodels.SkillViewModel; import java.util.List; public interface SkillService { List getAll(); + + SkillViewModel create(CreateSkillViewModel skill); } 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 108976db..cfa5c875 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 @@ -1,19 +1,28 @@ 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.exceptions.SkillAlreadyExistsException; +import com.RDS.skilltree.exceptions.UserNotFoundException; import com.RDS.skilltree.models.Skill; import com.RDS.skilltree.repositories.SkillRepository; +import com.RDS.skilltree.viewmodels.CreateSkillViewModel; import com.RDS.skilltree.viewmodels.SkillViewModel; import lombok.RequiredArgsConstructor; import org.springframework.beans.BeanUtils; +import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Service; import java.util.List; +import java.util.Optional; import java.util.stream.Collectors; @Service @RequiredArgsConstructor public class SkillServiceImplementation implements SkillService { private final SkillRepository skillRepository; + private final UserRepository userRepository; @Override public List getAll() { @@ -22,9 +31,38 @@ public List getAll() { .collect(Collectors.toList()); } + @Override + public SkillViewModel create(CreateSkillViewModel skill) { + if (skillRepository.existsByName(skill.getName())) { + throw new SkillAlreadyExistsException(String.format("Skill with name %s already exists", skill.getName())); + } + + JwtUserModel jwtDetails = + (JwtUserModel) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); + + // TODO : user the userId from jwtDetails after the login api is implemented + String userId = "ae7a6673c5574140838f209de4c644fc"; + Optional user = userRepository.findById(userId); + + if (user.isEmpty()) { + throw new UserNotFoundException("unable to create skill for the current user"); + } + + Skill newSkill = toEntity(skill); + newSkill.setCreatedBy(user.get()); + + return toViewModel(skillRepository.saveAndFlush(newSkill)); + } + public SkillViewModel toViewModel(Skill entity) { SkillViewModel viewModel = new SkillViewModel(); BeanUtils.copyProperties(entity, viewModel); return viewModel; } + + private Skill toEntity(CreateSkillViewModel viewModel) { + Skill entity = new Skill(); + BeanUtils.copyProperties(viewModel, entity); + return entity; + } } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/utils/UUIDValidationInterceptor.java b/skill-tree/src/main/java/com/RDS/skilltree/utils/UUIDValidationInterceptor.java index cccc1d5b..06393977 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/utils/UUIDValidationInterceptor.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/utils/UUIDValidationInterceptor.java @@ -1,6 +1,6 @@ package com.RDS.skilltree.utils; -import com.RDS.skilltree.Exceptions.InvalidParameterException; +import com.RDS.skilltree.exceptions.InvalidParameterException; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import org.slf4j.Logger; diff --git a/skill-tree/src/main/java/com/RDS/skilltree/viewmodels/CreateSkillViewModel.java b/skill-tree/src/main/java/com/RDS/skilltree/viewmodels/CreateSkillViewModel.java new file mode 100644 index 00000000..29a1dad9 --- /dev/null +++ b/skill-tree/src/main/java/com/RDS/skilltree/viewmodels/CreateSkillViewModel.java @@ -0,0 +1,19 @@ +package com.RDS.skilltree.viewmodels; + +import com.RDS.skilltree.Skill.SkillTypeEnum; +import com.RDS.skilltree.User.UserModel; +import jakarta.validation.constraints.NotNull; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class CreateSkillViewModel { + @NotNull(message = "Name cannot be empty") + private String name; + + @NotNull(message = "SkillType cannot be empty") + private SkillTypeEnum type; + + private UserModel createdBy; +} diff --git a/skill-tree/src/test/java/com/RDS/skilltree/utils/UUIDValidationInterceptorTest.java b/skill-tree/src/test/java/com/RDS/skilltree/utils/UUIDValidationInterceptorTest.java index a02c5d16..6fc9e67b 100644 --- a/skill-tree/src/test/java/com/RDS/skilltree/utils/UUIDValidationInterceptorTest.java +++ b/skill-tree/src/test/java/com/RDS/skilltree/utils/UUIDValidationInterceptorTest.java @@ -1,12 +1,8 @@ package com.RDS.skilltree.utils; -import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.Mockito.*; - -import com.RDS.skilltree.Exceptions.InvalidParameterException; +import com.RDS.skilltree.exceptions.InvalidParameterException; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; -import java.util.UUID; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -14,14 +10,23 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.when; + @ExtendWith(MockitoExtension.class) class UUIDValidationInterceptorTest { - @Mock private HttpServletRequest request; + @Mock + private HttpServletRequest request; - @Mock private HttpServletResponse response; + @Mock + private HttpServletResponse response; - @InjectMocks private UUIDValidationInterceptor interceptor; + @InjectMocks + private UUIDValidationInterceptor interceptor; @Test @Disabled From 43ae5b52d0b8b408a687305df1b0f2c2f6e62615 Mon Sep 17 00:00:00 2001 From: yash raj Date: Wed, 26 Jun 2024 02:07:29 +0530 Subject: [PATCH 19/31] create enums folder and move skill type enum to the folder --- .../com/RDS/skilltree/Skill/SkillDRO.java | 1 + .../com/RDS/skilltree/Skill/SkillDTO.java | 1 + .../RDS/skilltree/Skill/SkillsController.java | 74 ++++++++----------- .../{Skill => enums}/SkillTypeEnum.java | 2 +- .../java/com/RDS/skilltree/models/Skill.java | 2 +- .../viewmodels/CreateSkillViewModel.java | 2 +- .../skilltree/viewmodels/SkillViewModel.java | 2 +- 7 files changed, 38 insertions(+), 46 deletions(-) rename skill-tree/src/main/java/com/RDS/skilltree/{Skill => enums}/SkillTypeEnum.java (56%) diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDRO.java b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDRO.java index 435ce8df..8456d7ea 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDRO.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDRO.java @@ -1,5 +1,6 @@ package com.RDS.skilltree.Skill; +import com.RDS.skilltree.enums.SkillTypeEnum; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import jakarta.validation.constraints.NotNull; import lombok.Builder; diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDTO.java b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDTO.java index 6abf3ff6..dd506975 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDTO.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDTO.java @@ -1,6 +1,7 @@ package com.RDS.skilltree.Skill; import com.RDS.skilltree.User.UserDTO; +import com.RDS.skilltree.enums.SkillTypeEnum; import com.RDS.skilltree.models.Skill; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import lombok.Builder; diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsController.java b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsController.java index ba30502e..8d7c10bc 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsController.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsController.java @@ -1,26 +1,16 @@ package com.RDS.skilltree.Skill; -import com.RDS.skilltree.Common.Response.GenericResponse; import com.RDS.skilltree.Endorsement.EndorsementRepository; -import com.RDS.skilltree.User.JwtUserModel; -import com.RDS.skilltree.User.UserModel; import com.RDS.skilltree.User.UserRepository; -import com.RDS.skilltree.models.Skill; import com.RDS.skilltree.repositories.SkillRepository; -import jakarta.validation.Valid; import jakarta.validation.constraints.Min; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.springframework.dao.DataIntegrityViolationException; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; -import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import org.springframework.security.core.Authentication; import org.springframework.web.bind.annotation.*; -import java.util.Optional; - @Slf4j @RestController @RequiredArgsConstructor @@ -36,38 +26,38 @@ public class SkillsController { // return new GenericResponse<>(skillRepository.findAll(), null); // } - @PostMapping - @ResponseStatus(HttpStatus.CREATED) - // TODO : Return only the valid fields by using a DTO. - public GenericResponse createSkill( - Authentication authentication, @RequestBody(required = true) @Valid SkillDRO skill) { - JwtUserModel userDetails = (JwtUserModel) authentication.getPrincipal(); - - String userId = "ae7a6673c5574140838f209de4c644fc"; - Optional user = userRepository.findById(userId); - - // TODO : return a correct http status instead of 201 - if (skillRepository.findByName(skill.getName()).isPresent()) { - return new GenericResponse<>( - null, String.format("Skill with name %s already exists", skill.getName())); - } - - // TODO : return a correct http status instead of 201 - if (user.isEmpty()) { - return new GenericResponse<>(null, "User not found"); - } - - Skill newSkill = - Skill.builder().name(skill.getName()).type(skill.getType()).createdBy(user.get()).build(); - - try { - return new GenericResponse<>(skillRepository.save(newSkill), "Skill created"); - } catch (DataIntegrityViolationException error) { - log.error("Error saving skill {}, error: {}", skill.getName(), error.getMessage()); - // TODO : return a correct http status instead of 201 - return new GenericResponse<>(null, "Something went wrong please try again"); - } - } +// @PostMapping +// @ResponseStatus(HttpStatus.CREATED) +// // TODO : Return only the valid fields by using a DTO. +// public GenericResponse createSkill( +// Authentication authentication, @RequestBody(required = true) @Valid SkillDRO skill) { +// JwtUserModel userDetails = (JwtUserModel) authentication.getPrincipal(); +// +// String userId = "ae7a6673c5574140838f209de4c644fc"; +// Optional user = userRepository.findById(userId); +// +// // TODO : return a correct http status instead of 201 +// if (skillRepository.findByName(skill.getName()).isPresent()) { +// return new GenericResponse<>( +// null, String.format("Skill with name %s already exists", skill.getName())); +// } +// +// // TODO : return a correct http status instead of 201 +// if (user.isEmpty()) { +// return new GenericResponse<>(null, "User not found"); +// } +// +// Skill newSkill = +// Skill.builder().name(skill.getName()).type(skill.getType()).createdBy(user.get()).build(); +// +// try { +// return new GenericResponse<>(skillRepository.save(newSkill), "Skill created"); +// } catch (DataIntegrityViolationException error) { +// log.error("Error saving skill {}, error: {}", skill.getName(), error.getMessage()); +// // TODO : return a correct http status instead of 201 +// return new GenericResponse<>(null, "Something went wrong please try again"); +// } +// } @GetMapping("/{id}/endorsements") public ResponseEntity> getEndorsementsBySkillId( diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillTypeEnum.java b/skill-tree/src/main/java/com/RDS/skilltree/enums/SkillTypeEnum.java similarity index 56% rename from skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillTypeEnum.java rename to skill-tree/src/main/java/com/RDS/skilltree/enums/SkillTypeEnum.java index 0a5bc9b1..255de25c 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillTypeEnum.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/enums/SkillTypeEnum.java @@ -1,4 +1,4 @@ -package com.RDS.skilltree.Skill; +package com.RDS.skilltree.enums; public enum SkillTypeEnum { ATOMIC, diff --git a/skill-tree/src/main/java/com/RDS/skilltree/models/Skill.java b/skill-tree/src/main/java/com/RDS/skilltree/models/Skill.java index 23111d6d..48b93981 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/models/Skill.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/models/Skill.java @@ -1,7 +1,7 @@ package com.RDS.skilltree.models; -import com.RDS.skilltree.Skill.SkillTypeEnum; import com.RDS.skilltree.User.UserModel; +import com.RDS.skilltree.enums.SkillTypeEnum; import com.RDS.skilltree.utils.TrackedProperties; import jakarta.persistence.*; import lombok.*; diff --git a/skill-tree/src/main/java/com/RDS/skilltree/viewmodels/CreateSkillViewModel.java b/skill-tree/src/main/java/com/RDS/skilltree/viewmodels/CreateSkillViewModel.java index 29a1dad9..8cbba309 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/viewmodels/CreateSkillViewModel.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/viewmodels/CreateSkillViewModel.java @@ -1,7 +1,7 @@ package com.RDS.skilltree.viewmodels; -import com.RDS.skilltree.Skill.SkillTypeEnum; import com.RDS.skilltree.User.UserModel; +import com.RDS.skilltree.enums.SkillTypeEnum; import jakarta.validation.constraints.NotNull; import lombok.Getter; import lombok.Setter; diff --git a/skill-tree/src/main/java/com/RDS/skilltree/viewmodels/SkillViewModel.java b/skill-tree/src/main/java/com/RDS/skilltree/viewmodels/SkillViewModel.java index 9128471e..68b87b2c 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/viewmodels/SkillViewModel.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/viewmodels/SkillViewModel.java @@ -1,6 +1,6 @@ package com.RDS.skilltree.viewmodels; -import com.RDS.skilltree.Skill.SkillTypeEnum; +import com.RDS.skilltree.enums.SkillTypeEnum; import lombok.Getter; import lombok.Setter; From a2aabe78e1599d5faa84e3885c666d7b52d6467d Mon Sep 17 00:00:00 2001 From: yash raj Date: Wed, 26 Jun 2024 02:28:34 +0530 Subject: [PATCH 20/31] rename Conifg to config, move generic response and jwtAuthenticationFilter to utils --- .../RDS/skilltree/Endorsement/EndorsementController.java | 2 +- .../RDS/skilltree/{Config => config}/SecurityConfig.java | 9 +++++---- .../RDS/skilltree/{Config => config}/WebMvcConfig.java | 2 +- .../RDS/skilltree/exceptions/GlobalExceptionHandler.java | 2 +- .../{Common/Response => utils}/GenericResponse.java | 2 +- .../{Filters => utils}/JWTAuthenticationFilter.java | 9 +++++---- 6 files changed, 14 insertions(+), 12 deletions(-) rename skill-tree/src/main/java/com/RDS/skilltree/{Config => config}/SecurityConfig.java (98%) rename skill-tree/src/main/java/com/RDS/skilltree/{Config => config}/WebMvcConfig.java (96%) rename skill-tree/src/main/java/com/RDS/skilltree/{Common/Response => utils}/GenericResponse.java (89%) rename skill-tree/src/main/java/com/RDS/skilltree/{Filters => utils}/JWTAuthenticationFilter.java (95%) diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementController.java b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementController.java index 79fe672b..abf34e93 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementController.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementController.java @@ -1,10 +1,10 @@ package com.RDS.skilltree.Endorsement; -import com.RDS.skilltree.Common.Response.GenericResponse; import com.RDS.skilltree.User.UserModel; import com.RDS.skilltree.User.UserRepository; import com.RDS.skilltree.models.Skill; import com.RDS.skilltree.repositories.SkillRepository; +import com.RDS.skilltree.utils.GenericResponse; import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Config/SecurityConfig.java b/skill-tree/src/main/java/com/RDS/skilltree/config/SecurityConfig.java similarity index 98% rename from skill-tree/src/main/java/com/RDS/skilltree/Config/SecurityConfig.java rename to skill-tree/src/main/java/com/RDS/skilltree/config/SecurityConfig.java index b54f9b4e..76826060 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Config/SecurityConfig.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/config/SecurityConfig.java @@ -1,11 +1,9 @@ -package com.RDS.skilltree.Config; +package com.RDS.skilltree.config; import com.RDS.skilltree.Authentication.AuthEntryPoint; import com.RDS.skilltree.Authentication.CustomAccessDeniedHandler; -import com.RDS.skilltree.Filters.JWTAuthenticationFilter; import com.RDS.skilltree.User.UserRoleEnum; -import java.util.Arrays; -import java.util.List; +import com.RDS.skilltree.utils.JWTAuthenticationFilter; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.HttpHeaders; @@ -20,6 +18,9 @@ import org.springframework.web.cors.CorsConfigurationSource; import org.springframework.web.cors.UrlBasedCorsConfigurationSource; +import java.util.Arrays; +import java.util.List; + @EnableWebSecurity @Configuration public class SecurityConfig { diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Config/WebMvcConfig.java b/skill-tree/src/main/java/com/RDS/skilltree/config/WebMvcConfig.java similarity index 96% rename from skill-tree/src/main/java/com/RDS/skilltree/Config/WebMvcConfig.java rename to skill-tree/src/main/java/com/RDS/skilltree/config/WebMvcConfig.java index f02f79ea..6b5a96e0 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Config/WebMvcConfig.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/config/WebMvcConfig.java @@ -1,4 +1,4 @@ -package com.RDS.skilltree.Config; +package com.RDS.skilltree.config; import com.RDS.skilltree.utils.UUIDValidationInterceptor; import org.springframework.beans.factory.annotation.Autowired; diff --git a/skill-tree/src/main/java/com/RDS/skilltree/exceptions/GlobalExceptionHandler.java b/skill-tree/src/main/java/com/RDS/skilltree/exceptions/GlobalExceptionHandler.java index e032182e..1c90d9f4 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/exceptions/GlobalExceptionHandler.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/exceptions/GlobalExceptionHandler.java @@ -1,6 +1,6 @@ package com.RDS.skilltree.exceptions; -import com.RDS.skilltree.Common.Response.GenericResponse; +import com.RDS.skilltree.utils.GenericResponse; import jakarta.validation.ConstraintViolationException; import lombok.extern.slf4j.Slf4j; import org.apache.tomcat.websocket.AuthenticationException; diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Common/Response/GenericResponse.java b/skill-tree/src/main/java/com/RDS/skilltree/utils/GenericResponse.java similarity index 89% rename from skill-tree/src/main/java/com/RDS/skilltree/Common/Response/GenericResponse.java rename to skill-tree/src/main/java/com/RDS/skilltree/utils/GenericResponse.java index ee31959f..70d73df3 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Common/Response/GenericResponse.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/utils/GenericResponse.java @@ -1,4 +1,4 @@ -package com.RDS.skilltree.Common.Response; +package com.RDS.skilltree.utils; import com.fasterxml.jackson.annotation.JsonInclude; import lombok.AllArgsConstructor; diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Filters/JWTAuthenticationFilter.java b/skill-tree/src/main/java/com/RDS/skilltree/utils/JWTAuthenticationFilter.java similarity index 95% rename from skill-tree/src/main/java/com/RDS/skilltree/Filters/JWTAuthenticationFilter.java rename to skill-tree/src/main/java/com/RDS/skilltree/utils/JWTAuthenticationFilter.java index 5392978e..f40b5e30 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Filters/JWTAuthenticationFilter.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/utils/JWTAuthenticationFilter.java @@ -1,14 +1,12 @@ -package com.RDS.skilltree.Filters; +package com.RDS.skilltree.utils; import com.RDS.skilltree.Authentication.UserAuthenticationToken; -import com.RDS.skilltree.utils.JWTUtils; import io.jsonwebtoken.Claims; import jakarta.servlet.FilterChain; import jakarta.servlet.ServletException; import jakarta.servlet.http.Cookie; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; -import java.io.IOException; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; @@ -19,13 +17,16 @@ import org.springframework.web.filter.OncePerRequestFilter; import org.springframework.web.util.WebUtils; +import java.io.IOException; + @Slf4j public class JWTAuthenticationFilter extends OncePerRequestFilter { @Value("${cookieName}") private String cookieName; - @Autowired private JWTUtils jwtUtils; + @Autowired + private JWTUtils jwtUtils; private static final String BEARER_PREFIX = "Bearer "; @Override From f6d364c3596432ae019d7d65d02cd931d527b17c Mon Sep 17 00:00:00 2001 From: yash raj Date: Wed, 26 Jun 2024 16:17:40 +0530 Subject: [PATCH 21/31] add api to get all endorsements for a skill using skill id in skillsapi --- .../Endorsement/EndorsementController.java | 14 +++++----- .../RDS/skilltree/Skill/SkillsController.java | 2 +- .../com/RDS/skilltree/apis/SkillsApi.java | 16 +++++++++++ .../Endorsement.java} | 7 +++-- .../EndorsementRepository.java | 9 +++---- .../repositories/SkillRepository.java | 4 --- .../services/EndorsementService.java | 9 +++++++ .../EndorsementServiceImplementation.java | 21 +++++++++++++++ .../services/SkillServiceImplementation.java | 10 ++----- .../viewmodels/EndorsementViewModel.java | 27 +++++++++++++++++++ .../skilltree/viewmodels/SkillViewModel.java | 12 +++++++++ .../skilltree/viewmodels/UserViewModel.java | 22 +++++++++++++++ 12 files changed, 125 insertions(+), 28 deletions(-) rename skill-tree/src/main/java/com/RDS/skilltree/{Endorsement/EndorsementModel.java => models/Endorsement.java} (85%) rename skill-tree/src/main/java/com/RDS/skilltree/{Endorsement => repositories}/EndorsementRepository.java (52%) create mode 100644 skill-tree/src/main/java/com/RDS/skilltree/services/EndorsementService.java create mode 100644 skill-tree/src/main/java/com/RDS/skilltree/services/EndorsementServiceImplementation.java create mode 100644 skill-tree/src/main/java/com/RDS/skilltree/viewmodels/EndorsementViewModel.java create mode 100644 skill-tree/src/main/java/com/RDS/skilltree/viewmodels/UserViewModel.java diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementController.java b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementController.java index abf34e93..b2896236 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementController.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementController.java @@ -2,7 +2,9 @@ import com.RDS.skilltree.User.UserModel; import com.RDS.skilltree.User.UserRepository; +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.utils.GenericResponse; import jakarta.validation.Valid; @@ -27,7 +29,7 @@ public class EndorsementController { @PostMapping // TODO : add a check for when a endorsement is already created by a user for a particular skill. - public ResponseEntity> postEndorsement( + public ResponseEntity> postEndorsement( @RequestBody @Valid CreateEndorsementDro endorsementDro) { String message = endorsementDro.getMessage(); @@ -63,8 +65,8 @@ public ResponseEntity> postEndorsement( HttpStatus.NOT_FOUND); } - EndorsementModel newEndorsement = - EndorsementModel.builder() + Endorsement newEndorsement = + Endorsement.builder() .endorser(endorserDetails.get()) .endorse(endorseDetails.get()) .skill(skillDetails.get()) @@ -87,16 +89,16 @@ public ResponseEntity> postEndorsement( } @PatchMapping("/{id}") - public ResponseEntity> updateEndorsement( + public ResponseEntity> updateEndorsement( @PathVariable Integer id, @RequestBody UpdateEndorsementDro body) { - Optional endorsementDetails = endorsementRepository.findById(id); + Optional endorsementDetails = endorsementRepository.findById(id); if (endorsementDetails.isEmpty()) { return new ResponseEntity<>( new GenericResponse<>(null, "Endorsement not found"), HttpStatus.NOT_FOUND); } - EndorsementModel endorsement = endorsementDetails.get(); + Endorsement endorsement = endorsementDetails.get(); if (body.getMessage() != null) { endorsement.setMessage(body.getMessage()); diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsController.java b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsController.java index 8d7c10bc..d5a2f659 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsController.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsController.java @@ -1,7 +1,7 @@ package com.RDS.skilltree.Skill; -import com.RDS.skilltree.Endorsement.EndorsementRepository; import com.RDS.skilltree.User.UserRepository; +import com.RDS.skilltree.repositories.EndorsementRepository; import com.RDS.skilltree.repositories.SkillRepository; import jakarta.validation.constraints.Min; import lombok.RequiredArgsConstructor; 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 f1c26a93..3cec0ed2 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 @@ -1,11 +1,16 @@ package com.RDS.skilltree.apis; +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 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.*; @@ -17,6 +22,7 @@ @RequestMapping("v1/skills") public class SkillsApi { private final SkillService skillService; + private final EndorsementService endorsementService; @GetMapping public ResponseEntity> getAll() { @@ -27,4 +33,14 @@ public ResponseEntity> getAll() { public ResponseEntity create(@Valid @RequestBody CreateSkillViewModel skill) { return ResponseEntity.ok(skillService.create(skill)); } + + @GetMapping("/{id}/endorsements") + public ResponseEntity> getEndorsementsBySkillId( + @RequestParam(name = "offset", defaultValue = "0", required = false) @Min(0) int offset, + @RequestParam(name = "limit", defaultValue = "10", required = false) @Min(1) int limit, + @PathVariable(value = "id") Integer skillID) { + PageRequest pageRequest = PageRequest.of(offset, limit); + return ResponseEntity.ok(endorsementService.getAllEndorsementsBySkillId(skillID, pageRequest)); + + } } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementModel.java b/skill-tree/src/main/java/com/RDS/skilltree/models/Endorsement.java similarity index 85% rename from skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementModel.java rename to skill-tree/src/main/java/com/RDS/skilltree/models/Endorsement.java index bad95a3b..01351c13 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementModel.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/models/Endorsement.java @@ -1,7 +1,6 @@ -package com.RDS.skilltree.Endorsement; +package com.RDS.skilltree.models; import com.RDS.skilltree.User.UserModel; -import com.RDS.skilltree.models.Skill; import com.RDS.skilltree.utils.TrackedProperties; import jakarta.persistence.*; import lombok.*; @@ -13,7 +12,7 @@ @Getter @Setter @Table(name = "endorsements") -public class EndorsementModel extends TrackedProperties { +public class Endorsement extends TrackedProperties { @Id @GeneratedValue @Column(name = "id") @@ -33,4 +32,4 @@ public class EndorsementModel extends TrackedProperties { @Column(name = "message", nullable = false) private String message; -} +} \ No newline at end of file diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementRepository.java b/skill-tree/src/main/java/com/RDS/skilltree/repositories/EndorsementRepository.java similarity index 52% rename from skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementRepository.java rename to skill-tree/src/main/java/com/RDS/skilltree/repositories/EndorsementRepository.java index a02bef98..c5e8d670 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementRepository.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/repositories/EndorsementRepository.java @@ -1,11 +1,10 @@ -package com.RDS.skilltree.Endorsement; +package com.RDS.skilltree.repositories; +import com.RDS.skilltree.models.Endorsement; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.stereotype.Repository; -@Repository -public interface EndorsementRepository extends JpaRepository { - Page findBySkillId(Integer skillId, Pageable pageable); +public interface EndorsementRepository extends JpaRepository { + Page findBySkillId(Integer skillId, Pageable pageable); } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/repositories/SkillRepository.java b/skill-tree/src/main/java/com/RDS/skilltree/repositories/SkillRepository.java index ee230e3b..8a5fdfc0 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/repositories/SkillRepository.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/repositories/SkillRepository.java @@ -4,11 +4,7 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; -import java.util.Optional; - @Repository public interface SkillRepository extends JpaRepository { - Optional findByName(String name); - boolean existsByName(String name); } \ No newline at end of file diff --git a/skill-tree/src/main/java/com/RDS/skilltree/services/EndorsementService.java b/skill-tree/src/main/java/com/RDS/skilltree/services/EndorsementService.java new file mode 100644 index 00000000..7910c81c --- /dev/null +++ b/skill-tree/src/main/java/com/RDS/skilltree/services/EndorsementService.java @@ -0,0 +1,9 @@ +package com.RDS.skilltree.services; + +import com.RDS.skilltree.viewmodels.EndorsementViewModel; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; + +public interface EndorsementService { + Page getAllEndorsementsBySkillId(Integer skillId, Pageable pageable); +} diff --git a/skill-tree/src/main/java/com/RDS/skilltree/services/EndorsementServiceImplementation.java b/skill-tree/src/main/java/com/RDS/skilltree/services/EndorsementServiceImplementation.java new file mode 100644 index 00000000..ab10df4a --- /dev/null +++ b/skill-tree/src/main/java/com/RDS/skilltree/services/EndorsementServiceImplementation.java @@ -0,0 +1,21 @@ +package com.RDS.skilltree.services; + +import com.RDS.skilltree.models.Endorsement; +import com.RDS.skilltree.repositories.EndorsementRepository; +import com.RDS.skilltree.viewmodels.EndorsementViewModel; +import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.stereotype.Service; + +@Service +@RequiredArgsConstructor +public class EndorsementServiceImplementation implements EndorsementService { + private final EndorsementRepository endorsementRepository; + + @Override + public Page getAllEndorsementsBySkillId(Integer skillId, Pageable pageable) { + Page endorsementPage = endorsementRepository.findBySkillId(skillId, pageable); + return endorsementPage.map(EndorsementViewModel::toViewModel); + } +} 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 cfa5c875..29cc5936 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 @@ -27,7 +27,7 @@ public class SkillServiceImplementation implements SkillService { @Override public List getAll() { return skillRepository.findAll().stream() - .map(this::toViewModel) + .map(SkillViewModel::toViewModel) .collect(Collectors.toList()); } @@ -51,13 +51,7 @@ public SkillViewModel create(CreateSkillViewModel skill) { Skill newSkill = toEntity(skill); newSkill.setCreatedBy(user.get()); - return toViewModel(skillRepository.saveAndFlush(newSkill)); - } - - public SkillViewModel toViewModel(Skill entity) { - SkillViewModel viewModel = new SkillViewModel(); - BeanUtils.copyProperties(entity, viewModel); - return viewModel; + return SkillViewModel.toViewModel(skillRepository.saveAndFlush(newSkill)); } private Skill toEntity(CreateSkillViewModel viewModel) { diff --git a/skill-tree/src/main/java/com/RDS/skilltree/viewmodels/EndorsementViewModel.java b/skill-tree/src/main/java/com/RDS/skilltree/viewmodels/EndorsementViewModel.java new file mode 100644 index 00000000..a4faad31 --- /dev/null +++ b/skill-tree/src/main/java/com/RDS/skilltree/viewmodels/EndorsementViewModel.java @@ -0,0 +1,27 @@ +package com.RDS.skilltree.viewmodels; + +import com.RDS.skilltree.models.Endorsement; +import lombok.Getter; +import lombok.Setter; +import org.springframework.beans.BeanUtils; + +@Getter +@Setter +public class EndorsementViewModel { + private Integer id; + private SkillViewModel skill; + private UserViewModel endorse; + private UserViewModel endorser; + private String message; + + public static EndorsementViewModel toViewModel(Endorsement endorsement) { + EndorsementViewModel viewModel = new EndorsementViewModel(); + BeanUtils.copyProperties(endorsement, viewModel); + + viewModel.setSkill(SkillViewModel.toViewModel(endorsement.getSkill())); + viewModel.setEndorser(UserViewModel.toViewModel(endorsement.getEndorser())); + viewModel.setEndorse(UserViewModel.toViewModel(endorsement.getEndorse())); + + return viewModel; + } +} diff --git a/skill-tree/src/main/java/com/RDS/skilltree/viewmodels/SkillViewModel.java b/skill-tree/src/main/java/com/RDS/skilltree/viewmodels/SkillViewModel.java index 68b87b2c..6b9321e6 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/viewmodels/SkillViewModel.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/viewmodels/SkillViewModel.java @@ -1,8 +1,10 @@ package com.RDS.skilltree.viewmodels; import com.RDS.skilltree.enums.SkillTypeEnum; +import com.RDS.skilltree.models.Skill; import lombok.Getter; import lombok.Setter; +import org.springframework.beans.BeanUtils; @Getter @Setter @@ -10,4 +12,14 @@ public class SkillViewModel { private Integer id; private String name; private SkillTypeEnum type = SkillTypeEnum.ATOMIC; + + public static SkillViewModel toViewModel(Skill skill) { + if (skill == null) { + return null; + } + + SkillViewModel viewModel = new SkillViewModel(); + BeanUtils.copyProperties(skill, viewModel); + return viewModel; + } } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/viewmodels/UserViewModel.java b/skill-tree/src/main/java/com/RDS/skilltree/viewmodels/UserViewModel.java new file mode 100644 index 00000000..e9a98d3a --- /dev/null +++ b/skill-tree/src/main/java/com/RDS/skilltree/viewmodels/UserViewModel.java @@ -0,0 +1,22 @@ +package com.RDS.skilltree.viewmodels; + +import com.RDS.skilltree.User.UserModel; +import lombok.Getter; +import lombok.Setter; +import org.springframework.beans.BeanUtils; + +@Getter +@Setter +public class UserViewModel { + private String name; + + public static UserViewModel toViewModel(UserModel user) { + if (user == null) { + return null; + } + + UserViewModel viewModel = new UserViewModel(); + BeanUtils.copyProperties(user, viewModel); + return viewModel; + } +} From 9592f6fc5778814456b456a04ec75edbc622f741 Mon Sep 17 00:00:00 2001 From: yash raj Date: Wed, 26 Jun 2024 16:18:18 +0530 Subject: [PATCH 22/31] remove skills package --- .../com/RDS/skilltree/Skill/SkillDRO.java | 18 ----- .../com/RDS/skilltree/Skill/SkillDTO.java | 36 ---------- .../RDS/skilltree/Skill/SkillsController.java | 70 ------------------- 3 files changed, 124 deletions(-) delete mode 100644 skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDRO.java delete mode 100644 skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDTO.java delete mode 100644 skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsController.java diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDRO.java b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDRO.java deleted file mode 100644 index 8456d7ea..00000000 --- a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDRO.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.RDS.skilltree.Skill; - -import com.RDS.skilltree.enums.SkillTypeEnum; -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import jakarta.validation.constraints.NotNull; -import lombok.Builder; -import lombok.Getter; - -@Getter -@Builder -@JsonIgnoreProperties(ignoreUnknown = true) -public class SkillDRO { - @NotNull(message = "Name cannot be null") - private String name; - - @NotNull(message = "SkillType cannot be null") - private SkillTypeEnum type; -} diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDTO.java b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDTO.java deleted file mode 100644 index dd506975..00000000 --- a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillDTO.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.RDS.skilltree.Skill; - -import com.RDS.skilltree.User.UserDTO; -import com.RDS.skilltree.enums.SkillTypeEnum; -import com.RDS.skilltree.models.Skill; -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import lombok.Builder; -import lombok.Getter; - -import java.util.HashSet; -import java.util.Set; - -@Getter -@Builder -@JsonIgnoreProperties(ignoreUnknown = true) -public class SkillDTO { - private Integer id; - private SkillTypeEnum type; - private String name; - private Set users; - - public static SkillDTO toDto(Skill skill) { - return SkillDTO.builder().id(skill.getId()).name(skill.getName()).type(skill.getType()).build(); - } - - public static SkillDTO getSkillsWithUsers(Skill skill) { - Set users = new HashSet<>(); - - return SkillDTO.builder() - .id(skill.getId()) - .name(skill.getName()) - .type(skill.getType()) - .users(users) - .build(); - } -} diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsController.java b/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsController.java deleted file mode 100644 index d5a2f659..00000000 --- a/skill-tree/src/main/java/com/RDS/skilltree/Skill/SkillsController.java +++ /dev/null @@ -1,70 +0,0 @@ -package com.RDS.skilltree.Skill; - -import com.RDS.skilltree.User.UserRepository; -import com.RDS.skilltree.repositories.EndorsementRepository; -import com.RDS.skilltree.repositories.SkillRepository; -import jakarta.validation.constraints.Min; -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.*; - -@Slf4j -@RestController -@RequiredArgsConstructor -@RequestMapping("/v2/skills") -public class SkillsController { - private final UserRepository userRepository; - private final SkillRepository skillRepository; - private final EndorsementRepository endorsementRepository; - -// @GetMapping -// public GenericResponse> getAllSkills(@RequestParam(required = false) String name) { -// skillRepository.findByName(name); -// return new GenericResponse<>(skillRepository.findAll(), null); -// } - -// @PostMapping -// @ResponseStatus(HttpStatus.CREATED) -// // TODO : Return only the valid fields by using a DTO. -// public GenericResponse createSkill( -// Authentication authentication, @RequestBody(required = true) @Valid SkillDRO skill) { -// JwtUserModel userDetails = (JwtUserModel) authentication.getPrincipal(); -// -// String userId = "ae7a6673c5574140838f209de4c644fc"; -// Optional user = userRepository.findById(userId); -// -// // TODO : return a correct http status instead of 201 -// if (skillRepository.findByName(skill.getName()).isPresent()) { -// return new GenericResponse<>( -// null, String.format("Skill with name %s already exists", skill.getName())); -// } -// -// // TODO : return a correct http status instead of 201 -// if (user.isEmpty()) { -// return new GenericResponse<>(null, "User not found"); -// } -// -// Skill newSkill = -// Skill.builder().name(skill.getName()).type(skill.getType()).createdBy(user.get()).build(); -// -// try { -// return new GenericResponse<>(skillRepository.save(newSkill), "Skill created"); -// } catch (DataIntegrityViolationException error) { -// log.error("Error saving skill {}, error: {}", skill.getName(), error.getMessage()); -// // TODO : return a correct http status instead of 201 -// return new GenericResponse<>(null, "Something went wrong please try again"); -// } -// } - - @GetMapping("/{id}/endorsements") - public ResponseEntity> getEndorsementsBySkillId( - @RequestParam(name = "offset", defaultValue = "0", required = false) @Min(0) int offset, - @RequestParam(name = "limit", defaultValue = "10", required = false) @Min(1) int limit, - @PathVariable(value = "id") Integer skillID) { - PageRequest pageRequest = PageRequest.of(offset, limit); - return ResponseEntity.ok(endorsementRepository.findBySkillId(skillID, pageRequest)); - } -} From 169c21212e2371e65d989d46906f03c6e8e5a4cf Mon Sep 17 00:00:00 2001 From: yash raj Date: Wed, 26 Jun 2024 16:18:52 +0530 Subject: [PATCH 23/31] fix build error --- .../src/main/java/com/RDS/skilltree/User/UserDTO.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/skill-tree/src/main/java/com/RDS/skilltree/User/UserDTO.java b/skill-tree/src/main/java/com/RDS/skilltree/User/UserDTO.java index 7bce2ed6..91a11cba 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/User/UserDTO.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/User/UserDTO.java @@ -1,11 +1,12 @@ package com.RDS.skilltree.User; -import com.RDS.skilltree.Skill.SkillDTO; -import java.net.URL; -import java.util.Set; +import com.RDS.skilltree.models.Skill; import lombok.Builder; import lombok.Getter; +import java.net.URL; +import java.util.Set; + @Getter @Builder public class UserDTO { @@ -22,7 +23,7 @@ public class UserDTO { private UserRoleEnum role; - private Set skills; + private Set skills; public static UserDTO toDTO(UserModel user) { From 2cfa44431c1954b38409683a371d3a031de19989 Mon Sep 17 00:00:00 2001 From: yash raj Date: Wed, 26 Jun 2024 17:08:56 +0530 Subject: [PATCH 24/31] move api to create a endorsement to the new folder structure --- .../Endorsement/EndorsementController.java | 125 +++++++++--------- .../RDS/skilltree/apis/EndorsementsApi.java | 27 ++++ .../exceptions/GlobalExceptionHandler.java | 12 ++ .../SelfEndorsementNotAllowedException.java | 7 + .../SkillAlreadyExistsException.java | 4 - .../exceptions/SkillNotFoundException.java | 11 ++ .../exceptions/UserNotFoundException.java | 4 - .../services/EndorsementService.java | 3 + .../EndorsementServiceImplementation.java | 53 ++++++++ .../CreateEndorsementViewModel.java | 18 +++ 10 files changed, 191 insertions(+), 73 deletions(-) create mode 100644 skill-tree/src/main/java/com/RDS/skilltree/apis/EndorsementsApi.java create mode 100644 skill-tree/src/main/java/com/RDS/skilltree/exceptions/SelfEndorsementNotAllowedException.java create mode 100644 skill-tree/src/main/java/com/RDS/skilltree/exceptions/SkillNotFoundException.java create mode 100644 skill-tree/src/main/java/com/RDS/skilltree/viewmodels/CreateEndorsementViewModel.java diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementController.java b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementController.java index b2896236..fbbfb072 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementController.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementController.java @@ -1,13 +1,10 @@ package com.RDS.skilltree.Endorsement; -import com.RDS.skilltree.User.UserModel; import com.RDS.skilltree.User.UserRepository; 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.utils.GenericResponse; -import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.dao.DataIntegrityViolationException; @@ -15,78 +12,76 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; -import java.util.Objects; import java.util.Optional; @Slf4j @RestController @RequiredArgsConstructor -@RequestMapping("/v1/endorsements") +@RequestMapping("/v2/endorsements") public class EndorsementController { private final UserRepository userRepository; private final EndorsementRepository endorsementRepository; private final SkillRepository skillRepository; - @PostMapping - // TODO : add a check for when a endorsement is already created by a user for a particular skill. - public ResponseEntity> postEndorsement( - @RequestBody @Valid CreateEndorsementDro endorsementDro) { - - String message = endorsementDro.getMessage(); - Integer skillId = endorsementDro.getSkillId(); - String endorseId = endorsementDro.getEndorseId(); - - // TODO: Get this from security context once the login api is implemented. - String endorserId = "ae7a6673c5574140838f209de4c644fc"; - - if (Objects.equals(endorseId, endorserId)) { - return new ResponseEntity<>( - new GenericResponse<>(null, "Self endorsement not allowed"), - HttpStatus.METHOD_NOT_ALLOWED); - } - - Optional skillDetails = skillRepository.findById(skillId); - Optional endorseDetails = userRepository.findById(endorseId); - Optional endorserDetails = userRepository.findById(endorserId); - - if (endorserDetails.isEmpty()) { - return new ResponseEntity<>( - new GenericResponse<>(null, "Endorser details not found"), HttpStatus.NOT_FOUND); - } - - if (endorseDetails.isEmpty()) { - return new ResponseEntity<>( - new GenericResponse<>(null, "Endorse not found"), HttpStatus.NOT_FOUND); - } - - if (skillDetails.isEmpty()) { - return new ResponseEntity<>( - new GenericResponse<>(null, String.format("Skill id: %s not found.", skillId)), - HttpStatus.NOT_FOUND); - } - - Endorsement newEndorsement = - Endorsement.builder() - .endorser(endorserDetails.get()) - .endorse(endorseDetails.get()) - .skill(skillDetails.get()) - .message(message) - .build(); - - try { - return new ResponseEntity<>( - new GenericResponse<>(endorsementRepository.save(newEndorsement), ""), - HttpStatus.CREATED); - } catch (DataIntegrityViolationException error) { - log.error( - "Error saving endorsement with skillId {} and endorse id {}, error: {}", - skillId, - endorseId, - error.getMessage()); - return new ResponseEntity<>( - new GenericResponse<>(null, "Something went wrong"), HttpStatus.INTERNAL_SERVER_ERROR); - } - } +// @PostMapping +// public ResponseEntity> postEndorsement( +// @RequestBody @Valid CreateEndorsementDro endorsementDro) { +// +// String message = endorsementDro.getMessage(); +// Integer skillId = endorsementDro.getSkillId(); +// String endorseId = endorsementDro.getEndorseId(); +// +// // TODO: Get this from security context once the login api is implemented. +// String endorserId = "ae7a6673c5574140838f209de4c644fc"; +// +// if (Objects.equals(endorseId, endorserId)) { +// return new ResponseEntity<>( +// new GenericResponse<>(null, "Self endorsement not allowed"), +// HttpStatus.METHOD_NOT_ALLOWED); +// } +// +// Optional skillDetails = skillRepository.findById(skillId); +// Optional endorseDetails = userRepository.findById(endorseId); +// Optional endorserDetails = userRepository.findById(endorserId); +// +// if (endorserDetails.isEmpty()) { +// return new ResponseEntity<>( +// new GenericResponse<>(null, "Endorser details not found"), HttpStatus.NOT_FOUND); +// } +// +// if (endorseDetails.isEmpty()) { +// return new ResponseEntity<>( +// new GenericResponse<>(null, "Endorse not found"), HttpStatus.NOT_FOUND); +// } +// +// if (skillDetails.isEmpty()) { +// return new ResponseEntity<>( +// new GenericResponse<>(null, String.format("Skill id: %s not found.", skillId)), +// HttpStatus.NOT_FOUND); +// } +// +// Endorsement newEndorsement = +// Endorsement.builder() +// .endorser(endorserDetails.get()) +// .endorse(endorseDetails.get()) +// .skill(skillDetails.get()) +// .message(message) +// .build(); +// +// try { +// return new ResponseEntity<>( +// new GenericResponse<>(endorsementRepository.save(newEndorsement), ""), +// HttpStatus.CREATED); +// } catch (DataIntegrityViolationException error) { +// log.error( +// "Error saving endorsement with skillId {} and endorse id {}, error: {}", +// skillId, +// endorseId, +// error.getMessage()); +// return new ResponseEntity<>( +// new GenericResponse<>(null, "Something went wrong"), HttpStatus.INTERNAL_SERVER_ERROR); +// } +// } @PatchMapping("/{id}") public ResponseEntity> updateEndorsement( diff --git a/skill-tree/src/main/java/com/RDS/skilltree/apis/EndorsementsApi.java b/skill-tree/src/main/java/com/RDS/skilltree/apis/EndorsementsApi.java new file mode 100644 index 00000000..d81b3f47 --- /dev/null +++ b/skill-tree/src/main/java/com/RDS/skilltree/apis/EndorsementsApi.java @@ -0,0 +1,27 @@ +package com.RDS.skilltree.apis; + +import com.RDS.skilltree.services.EndorsementService; +import com.RDS.skilltree.viewmodels.CreateEndorsementViewModel; +import com.RDS.skilltree.viewmodels.EndorsementViewModel; +import jakarta.validation.Valid; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@Slf4j +@RestController +@RequiredArgsConstructor +@RequestMapping("v1/endorsements") +public class EndorsementsApi { + private final EndorsementService endorsementService; + + @PostMapping + public ResponseEntity create(@Valid @RequestBody CreateEndorsementViewModel endorsement) { + return new ResponseEntity<>(endorsementService.create(endorsement), HttpStatus.CREATED); + } +} diff --git a/skill-tree/src/main/java/com/RDS/skilltree/exceptions/GlobalExceptionHandler.java b/skill-tree/src/main/java/com/RDS/skilltree/exceptions/GlobalExceptionHandler.java index 1c90d9f4..5ca613f4 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/exceptions/GlobalExceptionHandler.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/exceptions/GlobalExceptionHandler.java @@ -103,6 +103,18 @@ public ResponseEntity handleUserNotFoundException(UserNotFoundException ex, W @ExceptionHandler(SkillAlreadyExistsException.class) public ResponseEntity handleSkillAlreadyExistsException(SkillAlreadyExistsException ex, WebRequest request) { + log.error("Exception - Error : {}", ex.getMessage(), ex); + return new ResponseEntity<>(new GenericResponse<>(null, ex.getMessage()), HttpStatus.CONFLICT); + } + + @ExceptionHandler(SelfEndorsementNotAllowedException.class) + public ResponseEntity handleSelfEndorsementNotAllowedException(SelfEndorsementNotAllowedException ex, WebRequest request) { + log.error("Exception - Error : {}", ex.getMessage(), ex); + return new ResponseEntity<>(new GenericResponse<>(null, ex.getMessage()), HttpStatus.METHOD_NOT_ALLOWED); + } + + @ExceptionHandler(SkillNotFoundException.class) + public ResponseEntity handleSkillNotFoundException(SkillNotFoundException ex, WebRequest request) { log.error("Exception - Error : {}", ex.getMessage(), ex); return new ResponseEntity<>(new GenericResponse<>(null, ex.getMessage()), HttpStatus.NOT_FOUND); } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/exceptions/SelfEndorsementNotAllowedException.java b/skill-tree/src/main/java/com/RDS/skilltree/exceptions/SelfEndorsementNotAllowedException.java new file mode 100644 index 00000000..67d81250 --- /dev/null +++ b/skill-tree/src/main/java/com/RDS/skilltree/exceptions/SelfEndorsementNotAllowedException.java @@ -0,0 +1,7 @@ +package com.RDS.skilltree.exceptions; + +public class SelfEndorsementNotAllowedException extends RuntimeException { + public SelfEndorsementNotAllowedException(String message) { + super(message); + } +} diff --git a/skill-tree/src/main/java/com/RDS/skilltree/exceptions/SkillAlreadyExistsException.java b/skill-tree/src/main/java/com/RDS/skilltree/exceptions/SkillAlreadyExistsException.java index c51cdf54..16322344 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/exceptions/SkillAlreadyExistsException.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/exceptions/SkillAlreadyExistsException.java @@ -1,9 +1,5 @@ package com.RDS.skilltree.exceptions; -import org.springframework.http.HttpStatus; -import org.springframework.web.bind.annotation.ResponseStatus; - -@ResponseStatus(HttpStatus.CONFLICT) public class SkillAlreadyExistsException extends RuntimeException { public SkillAlreadyExistsException(String message) { super(message); diff --git a/skill-tree/src/main/java/com/RDS/skilltree/exceptions/SkillNotFoundException.java b/skill-tree/src/main/java/com/RDS/skilltree/exceptions/SkillNotFoundException.java new file mode 100644 index 00000000..9e692179 --- /dev/null +++ b/skill-tree/src/main/java/com/RDS/skilltree/exceptions/SkillNotFoundException.java @@ -0,0 +1,11 @@ +package com.RDS.skilltree.exceptions; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +@ResponseStatus(HttpStatus.NOT_FOUND) +public class SkillNotFoundException extends RuntimeException { + public SkillNotFoundException(String message) { + super(message); + } +} \ No newline at end of file diff --git a/skill-tree/src/main/java/com/RDS/skilltree/exceptions/UserNotFoundException.java b/skill-tree/src/main/java/com/RDS/skilltree/exceptions/UserNotFoundException.java index 643e1d55..6c7fc7fc 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/exceptions/UserNotFoundException.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/exceptions/UserNotFoundException.java @@ -1,9 +1,5 @@ package com.RDS.skilltree.exceptions; -import org.springframework.http.HttpStatus; -import org.springframework.web.bind.annotation.ResponseStatus; - -@ResponseStatus(HttpStatus.NOT_FOUND) public class UserNotFoundException extends RuntimeException { public UserNotFoundException(String message) { super(message); diff --git a/skill-tree/src/main/java/com/RDS/skilltree/services/EndorsementService.java b/skill-tree/src/main/java/com/RDS/skilltree/services/EndorsementService.java index 7910c81c..95696c50 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/services/EndorsementService.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/services/EndorsementService.java @@ -1,9 +1,12 @@ package com.RDS.skilltree.services; +import com.RDS.skilltree.viewmodels.CreateEndorsementViewModel; import com.RDS.skilltree.viewmodels.EndorsementViewModel; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; public interface EndorsementService { Page getAllEndorsementsBySkillId(Integer skillId, Pageable pageable); + + EndorsementViewModel create(CreateEndorsementViewModel endorsement); } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/services/EndorsementServiceImplementation.java b/skill-tree/src/main/java/com/RDS/skilltree/services/EndorsementServiceImplementation.java index ab10df4a..fe3a3028 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/services/EndorsementServiceImplementation.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/services/EndorsementServiceImplementation.java @@ -1,16 +1,29 @@ package com.RDS.skilltree.services; +import com.RDS.skilltree.User.UserModel; +import com.RDS.skilltree.User.UserRepository; +import com.RDS.skilltree.exceptions.SelfEndorsementNotAllowedException; +import com.RDS.skilltree.exceptions.SkillNotFoundException; +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.viewmodels.CreateEndorsementViewModel; import com.RDS.skilltree.viewmodels.EndorsementViewModel; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; +import java.util.Objects; +import java.util.Optional; + @Service @RequiredArgsConstructor public class EndorsementServiceImplementation implements EndorsementService { + private final UserRepository userRepository; + private final SkillRepository skillRepository; private final EndorsementRepository endorsementRepository; @Override @@ -18,4 +31,44 @@ public Page getAllEndorsementsBySkillId(Integer skillId, P Page endorsementPage = endorsementRepository.findBySkillId(skillId, pageable); return endorsementPage.map(EndorsementViewModel::toViewModel); } + + @Override + // TODO : add a check for when a endorsement is already created by a user for a particular skill. + public EndorsementViewModel create(CreateEndorsementViewModel endorsementViewModel) { + String message = endorsementViewModel.getMessage(); + Integer skillId = endorsementViewModel.getSkillId(); + String endorseId = endorsementViewModel.getEndorseId(); + + // TODO: Get this from security context once the login api is implemented. + String endorserId = "ae7a6673c5574140838f209de4c644fc"; + + if (Objects.equals(endorseId, endorserId)) { + throw new SelfEndorsementNotAllowedException("Self endorsement not allowed"); + } + + Optional skillDetails = skillRepository.findById(skillId); + Optional endorseDetails = userRepository.findById(endorseId); + Optional endorserDetails = userRepository.findById(endorserId); + + if (endorserDetails.isEmpty()) { + throw new UserNotFoundException("Cannot create endorsement for the current user"); + } + + if (endorseDetails.isEmpty()) { + throw new UserNotFoundException("Endorse not found"); + } + + if (skillDetails.isEmpty()) { + throw new SkillNotFoundException(String.format("Skill id: %s not found", 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)); + } } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/viewmodels/CreateEndorsementViewModel.java b/skill-tree/src/main/java/com/RDS/skilltree/viewmodels/CreateEndorsementViewModel.java new file mode 100644 index 00000000..32b3670b --- /dev/null +++ b/skill-tree/src/main/java/com/RDS/skilltree/viewmodels/CreateEndorsementViewModel.java @@ -0,0 +1,18 @@ +package com.RDS.skilltree.viewmodels; + +import jakarta.validation.constraints.NotNull; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class CreateEndorsementViewModel { + @NotNull(message = "Message cannot be empty") + private String message; + + @NotNull(message = "user id cannot be null") + private String endorseId; + + @NotNull(message = "skill id cannot be null") + private Integer skillId; +} From 0508fc16a73f26ec0d1701f7b0fb11c2e849a783 Mon Sep 17 00:00:00 2001 From: yash raj Date: Wed, 26 Jun 2024 21:03:24 +0530 Subject: [PATCH 25/31] add api to update endorsement in apis/endorsements api and remove old Endorsement folder --- .../Endorsement/CreateEndorsementDro.java | 23 ---- .../Endorsement/EndorsementController.java | 111 ------------------ .../skilltree/Endorsement/EndorsementDTO.java | 26 ---- .../Endorsement/UpdateEndorsementDro.java | 16 --- .../RDS/skilltree/apis/EndorsementsApi.java | 11 +- .../EndorsementNotFoundException.java | 7 ++ .../exceptions/GlobalExceptionHandler.java | 6 + .../exceptions/SkillNotFoundException.java | 4 - .../services/EndorsementService.java | 3 + .../EndorsementServiceImplementation.java | 20 ++++ .../UpdateEndorsementViewModel.java | 12 ++ 11 files changed, 55 insertions(+), 184 deletions(-) delete mode 100644 skill-tree/src/main/java/com/RDS/skilltree/Endorsement/CreateEndorsementDro.java delete mode 100644 skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementController.java delete mode 100644 skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementDTO.java delete mode 100644 skill-tree/src/main/java/com/RDS/skilltree/Endorsement/UpdateEndorsementDro.java create mode 100644 skill-tree/src/main/java/com/RDS/skilltree/exceptions/EndorsementNotFoundException.java create mode 100644 skill-tree/src/main/java/com/RDS/skilltree/viewmodels/UpdateEndorsementViewModel.java diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/CreateEndorsementDro.java b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/CreateEndorsementDro.java deleted file mode 100644 index 9bb9bc2c..00000000 --- a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/CreateEndorsementDro.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.RDS.skilltree.Endorsement; - -import jakarta.validation.constraints.NotNull; - -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Data; -import lombok.NoArgsConstructor; - -@NoArgsConstructor -@AllArgsConstructor -@Data -@Builder -public class CreateEndorsementDro { - @NotNull(message = "Message cannot be empty") - private String message; - - @NotNull(message = "user id cannot be null") - private String endorseId; - - @NotNull(message = "skill id cannot be null") - private Integer skillId; -} diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementController.java b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementController.java deleted file mode 100644 index fbbfb072..00000000 --- a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementController.java +++ /dev/null @@ -1,111 +0,0 @@ -package com.RDS.skilltree.Endorsement; - -import com.RDS.skilltree.User.UserRepository; -import com.RDS.skilltree.models.Endorsement; -import com.RDS.skilltree.repositories.EndorsementRepository; -import com.RDS.skilltree.repositories.SkillRepository; -import com.RDS.skilltree.utils.GenericResponse; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.springframework.dao.DataIntegrityViolationException; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.*; - -import java.util.Optional; - -@Slf4j -@RestController -@RequiredArgsConstructor -@RequestMapping("/v2/endorsements") -public class EndorsementController { - private final UserRepository userRepository; - private final EndorsementRepository endorsementRepository; - private final SkillRepository skillRepository; - -// @PostMapping -// public ResponseEntity> postEndorsement( -// @RequestBody @Valid CreateEndorsementDro endorsementDro) { -// -// String message = endorsementDro.getMessage(); -// Integer skillId = endorsementDro.getSkillId(); -// String endorseId = endorsementDro.getEndorseId(); -// -// // TODO: Get this from security context once the login api is implemented. -// String endorserId = "ae7a6673c5574140838f209de4c644fc"; -// -// if (Objects.equals(endorseId, endorserId)) { -// return new ResponseEntity<>( -// new GenericResponse<>(null, "Self endorsement not allowed"), -// HttpStatus.METHOD_NOT_ALLOWED); -// } -// -// Optional skillDetails = skillRepository.findById(skillId); -// Optional endorseDetails = userRepository.findById(endorseId); -// Optional endorserDetails = userRepository.findById(endorserId); -// -// if (endorserDetails.isEmpty()) { -// return new ResponseEntity<>( -// new GenericResponse<>(null, "Endorser details not found"), HttpStatus.NOT_FOUND); -// } -// -// if (endorseDetails.isEmpty()) { -// return new ResponseEntity<>( -// new GenericResponse<>(null, "Endorse not found"), HttpStatus.NOT_FOUND); -// } -// -// if (skillDetails.isEmpty()) { -// return new ResponseEntity<>( -// new GenericResponse<>(null, String.format("Skill id: %s not found.", skillId)), -// HttpStatus.NOT_FOUND); -// } -// -// Endorsement newEndorsement = -// Endorsement.builder() -// .endorser(endorserDetails.get()) -// .endorse(endorseDetails.get()) -// .skill(skillDetails.get()) -// .message(message) -// .build(); -// -// try { -// return new ResponseEntity<>( -// new GenericResponse<>(endorsementRepository.save(newEndorsement), ""), -// HttpStatus.CREATED); -// } catch (DataIntegrityViolationException error) { -// log.error( -// "Error saving endorsement with skillId {} and endorse id {}, error: {}", -// skillId, -// endorseId, -// error.getMessage()); -// return new ResponseEntity<>( -// new GenericResponse<>(null, "Something went wrong"), HttpStatus.INTERNAL_SERVER_ERROR); -// } -// } - - @PatchMapping("/{id}") - public ResponseEntity> updateEndorsement( - @PathVariable Integer id, @RequestBody UpdateEndorsementDro body) { - Optional endorsementDetails = endorsementRepository.findById(id); - - if (endorsementDetails.isEmpty()) { - return new ResponseEntity<>( - new GenericResponse<>(null, "Endorsement not found"), HttpStatus.NOT_FOUND); - } - - Endorsement endorsement = endorsementDetails.get(); - - if (body.getMessage() != null) { - endorsement.setMessage(body.getMessage()); - } - - try { - return new ResponseEntity<>(new GenericResponse<>(endorsementRepository.save(endorsement), "Endorsement updated"), HttpStatus.OK); - } catch ( - DataIntegrityViolationException error) { - log.error("Error endorsement id: {}, error: {}", endorsement.getId(), error.getMessage()); - return new ResponseEntity<>(new GenericResponse<>(null, "Something went wrong please try again"), HttpStatus.INTERNAL_SERVER_ERROR); - } - - } -} diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementDTO.java b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementDTO.java deleted file mode 100644 index 6d71a26a..00000000 --- a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/EndorsementDTO.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.RDS.skilltree.Endorsement; - -import com.RDS.skilltree.utils.TrackedProperties; -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import lombok.*; - -@Getter -@Builder -@JsonIgnoreProperties(ignoreUnknown = true) -public class EndorsementDTO extends TrackedProperties { - private Integer id; - private String endorseId; - private Integer skillId; - - // public static EndorsementDTO toDto(EndorsementModel endorsementModel) { - // EndorsementDTO endorsementDTO = - // EndorsementDTO.builder() - // .id(endorsementModel.getId()) - // .endorseId(endorsementModel.getEndorseId()) - // .skillId(endorsementModel.getSkillId()) - // .build(); - // endorsementDTO.setCreatedAt(endorsementModel.getCreatedAt()); - // endorsementDTO.setUpdatedAt(endorsementModel.getUpdatedAt()); - // return endorsementDTO; - // } -} diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/UpdateEndorsementDro.java b/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/UpdateEndorsementDro.java deleted file mode 100644 index 0263a6ff..00000000 --- a/skill-tree/src/main/java/com/RDS/skilltree/Endorsement/UpdateEndorsementDro.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.RDS.skilltree.Endorsement; - -import jakarta.validation.constraints.NotNull; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Data; -import lombok.NoArgsConstructor; - -@NoArgsConstructor -@AllArgsConstructor -@Data -@Builder -public class UpdateEndorsementDro { - @NotNull(message = "Message cannot be empty") - private String message; -} diff --git a/skill-tree/src/main/java/com/RDS/skilltree/apis/EndorsementsApi.java b/skill-tree/src/main/java/com/RDS/skilltree/apis/EndorsementsApi.java index d81b3f47..01e4a6f4 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/apis/EndorsementsApi.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/apis/EndorsementsApi.java @@ -3,15 +3,13 @@ import com.RDS.skilltree.services.EndorsementService; import com.RDS.skilltree.viewmodels.CreateEndorsementViewModel; import com.RDS.skilltree.viewmodels.EndorsementViewModel; +import com.RDS.skilltree.viewmodels.UpdateEndorsementViewModel; import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; @Slf4j @RestController @@ -24,4 +22,9 @@ public class EndorsementsApi { public ResponseEntity create(@Valid @RequestBody CreateEndorsementViewModel endorsement) { return new ResponseEntity<>(endorsementService.create(endorsement), HttpStatus.CREATED); } + + @PatchMapping("/{id}") + public ResponseEntity update(@PathVariable Integer id, @Valid @RequestBody UpdateEndorsementViewModel body) { + return new ResponseEntity<>(endorsementService.update(id, body), HttpStatus.OK); + } } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/exceptions/EndorsementNotFoundException.java b/skill-tree/src/main/java/com/RDS/skilltree/exceptions/EndorsementNotFoundException.java new file mode 100644 index 00000000..15790383 --- /dev/null +++ b/skill-tree/src/main/java/com/RDS/skilltree/exceptions/EndorsementNotFoundException.java @@ -0,0 +1,7 @@ +package com.RDS.skilltree.exceptions; + +public class EndorsementNotFoundException extends RuntimeException { + public EndorsementNotFoundException(String message) { + super(message); + } +} diff --git a/skill-tree/src/main/java/com/RDS/skilltree/exceptions/GlobalExceptionHandler.java b/skill-tree/src/main/java/com/RDS/skilltree/exceptions/GlobalExceptionHandler.java index 5ca613f4..6f316414 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/exceptions/GlobalExceptionHandler.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/exceptions/GlobalExceptionHandler.java @@ -118,4 +118,10 @@ public ResponseEntity handleSkillNotFoundException(SkillNotFoundException ex, log.error("Exception - Error : {}", ex.getMessage(), ex); return new ResponseEntity<>(new GenericResponse<>(null, ex.getMessage()), HttpStatus.NOT_FOUND); } + + @ExceptionHandler(EndorsementNotFoundException.class) + public ResponseEntity handleEndorsementNotException(EndorsementNotFoundException ex, WebRequest request) { + log.error("Exception - Error : {}", ex.getMessage(), ex); + return new ResponseEntity<>(new GenericResponse<>(null, ex.getMessage()), HttpStatus.NOT_FOUND); + } } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/exceptions/SkillNotFoundException.java b/skill-tree/src/main/java/com/RDS/skilltree/exceptions/SkillNotFoundException.java index 9e692179..652700f1 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/exceptions/SkillNotFoundException.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/exceptions/SkillNotFoundException.java @@ -1,9 +1,5 @@ package com.RDS.skilltree.exceptions; -import org.springframework.http.HttpStatus; -import org.springframework.web.bind.annotation.ResponseStatus; - -@ResponseStatus(HttpStatus.NOT_FOUND) public class SkillNotFoundException extends RuntimeException { public SkillNotFoundException(String message) { super(message); diff --git a/skill-tree/src/main/java/com/RDS/skilltree/services/EndorsementService.java b/skill-tree/src/main/java/com/RDS/skilltree/services/EndorsementService.java index 95696c50..b11d73f6 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/services/EndorsementService.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/services/EndorsementService.java @@ -2,6 +2,7 @@ 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; @@ -9,4 +10,6 @@ public interface EndorsementService { Page getAllEndorsementsBySkillId(Integer skillId, Pageable pageable); EndorsementViewModel create(CreateEndorsementViewModel endorsement); + + EndorsementViewModel update(Integer endorsementId, UpdateEndorsementViewModel endorsement); } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/services/EndorsementServiceImplementation.java b/skill-tree/src/main/java/com/RDS/skilltree/services/EndorsementServiceImplementation.java index fe3a3028..fa11c7d7 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/services/EndorsementServiceImplementation.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/services/EndorsementServiceImplementation.java @@ -2,6 +2,7 @@ import com.RDS.skilltree.User.UserModel; import com.RDS.skilltree.User.UserRepository; +import com.RDS.skilltree.exceptions.EndorsementNotFoundException; import com.RDS.skilltree.exceptions.SelfEndorsementNotAllowedException; import com.RDS.skilltree.exceptions.SkillNotFoundException; import com.RDS.skilltree.exceptions.UserNotFoundException; @@ -11,6 +12,7 @@ import com.RDS.skilltree.repositories.SkillRepository; import com.RDS.skilltree.viewmodels.CreateEndorsementViewModel; import com.RDS.skilltree.viewmodels.EndorsementViewModel; +import com.RDS.skilltree.viewmodels.UpdateEndorsementViewModel; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; @@ -71,4 +73,22 @@ public EndorsementViewModel create(CreateEndorsementViewModel endorsementViewMod return EndorsementViewModel.toViewModel(endorsementRepository.save(endorsement)); } + + @Override + public EndorsementViewModel update(Integer endorsementId, UpdateEndorsementViewModel body) { + Optional exitingEndorsement = endorsementRepository.findById(endorsementId); + + if (exitingEndorsement.isEmpty()) { + throw new EndorsementNotFoundException(String.format("Endorsement with id: %s not found", endorsementId)); + } + + Endorsement endorsement = exitingEndorsement.get(); + String updatedMessage = body.getMessage(); + + if (updatedMessage != null) { + endorsement.setMessage(updatedMessage); + } + + return EndorsementViewModel.toViewModel(endorsementRepository.save(endorsement)); + } } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/viewmodels/UpdateEndorsementViewModel.java b/skill-tree/src/main/java/com/RDS/skilltree/viewmodels/UpdateEndorsementViewModel.java new file mode 100644 index 00000000..0b1685a7 --- /dev/null +++ b/skill-tree/src/main/java/com/RDS/skilltree/viewmodels/UpdateEndorsementViewModel.java @@ -0,0 +1,12 @@ +package com.RDS.skilltree.viewmodels; + +import jakarta.validation.constraints.NotNull; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class UpdateEndorsementViewModel { + @NotNull(message = "Message cannot be empty") + private String message; +} From 1418fc4e241ab22eb7faf508e4f2a332f79c0fb3 Mon Sep 17 00:00:00 2001 From: yash raj Date: Wed, 26 Jun 2024 21:16:43 +0530 Subject: [PATCH 26/31] move health check api to the api folder & metric service inside the services folder --- .../Health/HealthCheckController.java | 28 ------------------- .../com/RDS/skilltree/apis/HealthApi.java | 26 +++++++++++++++++ .../RDS/skilltree/services/MetricService.java | 5 ++++ .../MetricServiceImplementation.java} | 16 ++++------- 4 files changed, 37 insertions(+), 38 deletions(-) delete mode 100644 skill-tree/src/main/java/com/RDS/skilltree/Health/HealthCheckController.java create mode 100644 skill-tree/src/main/java/com/RDS/skilltree/apis/HealthApi.java create mode 100644 skill-tree/src/main/java/com/RDS/skilltree/services/MetricService.java rename skill-tree/src/main/java/com/RDS/skilltree/{metrics/MetricService.java => services/MetricServiceImplementation.java} (57%) diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Health/HealthCheckController.java b/skill-tree/src/main/java/com/RDS/skilltree/Health/HealthCheckController.java deleted file mode 100644 index 854ddece..00000000 --- a/skill-tree/src/main/java/com/RDS/skilltree/Health/HealthCheckController.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.RDS.skilltree.Health; - -import com.RDS.skilltree.metrics.MetricService; -import java.util.Map; -import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -@RestController -@RequestMapping("/v1/health") -@Slf4j -public class HealthCheckController { - - private final MetricService metricService; - - @Autowired - public HealthCheckController(MetricService metricService) { - this.metricService = metricService; - } - - @GetMapping("") - public Map checkHealth() { - double uptime = metricService.getUptime(); - return Map.of("uptimeInSeconds", uptime); - } -} diff --git a/skill-tree/src/main/java/com/RDS/skilltree/apis/HealthApi.java b/skill-tree/src/main/java/com/RDS/skilltree/apis/HealthApi.java new file mode 100644 index 00000000..17c80ee8 --- /dev/null +++ b/skill-tree/src/main/java/com/RDS/skilltree/apis/HealthApi.java @@ -0,0 +1,26 @@ +package com.RDS.skilltree.apis; + +import com.RDS.skilltree.services.MetricService; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.Map; + +@Slf4j +@RestController +@RequiredArgsConstructor +@RequestMapping("v1/health") +public class HealthApi { + private final MetricService metricService; + + @GetMapping + public ResponseEntity> getUptime() { + return new ResponseEntity<>(Map.of("uptime in seconds", metricService.getUptime()), HttpStatus.OK); + } + +} diff --git a/skill-tree/src/main/java/com/RDS/skilltree/services/MetricService.java b/skill-tree/src/main/java/com/RDS/skilltree/services/MetricService.java new file mode 100644 index 00000000..2670a51a --- /dev/null +++ b/skill-tree/src/main/java/com/RDS/skilltree/services/MetricService.java @@ -0,0 +1,5 @@ +package com.RDS.skilltree.services; + +public interface MetricService { + Double getUptime(); +} diff --git a/skill-tree/src/main/java/com/RDS/skilltree/metrics/MetricService.java b/skill-tree/src/main/java/com/RDS/skilltree/services/MetricServiceImplementation.java similarity index 57% rename from skill-tree/src/main/java/com/RDS/skilltree/metrics/MetricService.java rename to skill-tree/src/main/java/com/RDS/skilltree/services/MetricServiceImplementation.java index 60c86997..67df4a78 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/metrics/MetricService.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/services/MetricServiceImplementation.java @@ -1,20 +1,16 @@ -package com.RDS.skilltree.metrics; +package com.RDS.skilltree.services; -import org.springframework.beans.factory.annotation.Autowired; +import lombok.RequiredArgsConstructor; import org.springframework.boot.actuate.metrics.MetricsEndpoint; import org.springframework.stereotype.Service; @Service -public class MetricService { - +@RequiredArgsConstructor +public class MetricServiceImplementation implements MetricService { private final MetricsEndpoint metricsEndpoint; - @Autowired - public MetricService(MetricsEndpoint metricsEndpoint) { - this.metricsEndpoint = metricsEndpoint; - } - - public double getUptime() { + @Override + public Double getUptime() { return metricsEndpoint.metric("process.uptime", null).getMeasurements().stream() .findFirst() .map(MetricsEndpoint.Sample::getValue) From be2e5bb72cc2b55ec93e740e07b78a6d9dd1ae88 Mon Sep 17 00:00:00 2001 From: yash raj Date: Wed, 26 Jun 2024 21:38:15 +0530 Subject: [PATCH 27/31] fix formatting --- .../java/com/RDS/skilltree/User/UserDTO.java | 5 ++--- .../RDS/skilltree/User/UserServiceImpl.java | 6 ++---- .../RDS/skilltree/User/UserSkillsModel.java | 3 +-- .../RDS/skilltree/apis/EndorsementsApi.java | 6 ++++-- .../com/RDS/skilltree/apis/HealthApi.java | 7 +++---- .../com/RDS/skilltree/apis/SkillsApi.java | 4 +--- .../RDS/skilltree/config/SecurityConfig.java | 5 ++--- .../exceptions/GlobalExceptionHandler.java | 21 ++++++++++++------- .../exceptions/SkillNotFoundException.java | 2 +- .../com/RDS/skilltree/models/Endorsement.java | 2 +- .../repositories/SkillRepository.java | 2 +- .../EndorsementServiceImplementation.java | 11 +++++----- .../RDS/skilltree/services/SkillService.java | 1 - .../services/SkillServiceImplementation.java | 10 ++++----- .../utils/JWTAuthenticationFilter.java | 6 ++---- .../utils/UUIDValidationInterceptorTest.java | 20 +++++++----------- 16 files changed, 52 insertions(+), 59 deletions(-) diff --git a/skill-tree/src/main/java/com/RDS/skilltree/User/UserDTO.java b/skill-tree/src/main/java/com/RDS/skilltree/User/UserDTO.java index 91a11cba..87ceb395 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/User/UserDTO.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/User/UserDTO.java @@ -1,11 +1,10 @@ package com.RDS.skilltree.User; import com.RDS.skilltree.models.Skill; -import lombok.Builder; -import lombok.Getter; - import java.net.URL; import java.util.Set; +import lombok.Builder; +import lombok.Getter; @Getter @Builder diff --git a/skill-tree/src/main/java/com/RDS/skilltree/User/UserServiceImpl.java b/skill-tree/src/main/java/com/RDS/skilltree/User/UserServiceImpl.java index 0008750b..1171911a 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/User/UserServiceImpl.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/User/UserServiceImpl.java @@ -4,10 +4,9 @@ import com.RDS.skilltree.models.Skill; import com.RDS.skilltree.repositories.SkillRepository; import jakarta.transaction.Transactional; -import org.springframework.stereotype.Service; - import java.util.List; import java.util.Optional; +import org.springframework.stereotype.Service; @Service public class UserServiceImpl implements UserService { @@ -27,8 +26,7 @@ public UserDTO createUser(UserDRO user) { } @Override - public void updateUser(String id, UserDRO user) { - } + public void updateUser(String id, UserDRO user) {} @Override public UserDTO getUserById(String id) { diff --git a/skill-tree/src/main/java/com/RDS/skilltree/User/UserSkillsModel.java b/skill-tree/src/main/java/com/RDS/skilltree/User/UserSkillsModel.java index 4fd9ed32..4754cb64 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/User/UserSkillsModel.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/User/UserSkillsModel.java @@ -3,9 +3,8 @@ import com.RDS.skilltree.models.Skill; import com.fasterxml.jackson.annotation.JsonBackReference; import jakarta.persistence.*; -import lombok.*; - import java.util.UUID; +import lombok.*; @Entity @Getter diff --git a/skill-tree/src/main/java/com/RDS/skilltree/apis/EndorsementsApi.java b/skill-tree/src/main/java/com/RDS/skilltree/apis/EndorsementsApi.java index 01e4a6f4..d775d5c7 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/apis/EndorsementsApi.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/apis/EndorsementsApi.java @@ -19,12 +19,14 @@ public class EndorsementsApi { private final EndorsementService endorsementService; @PostMapping - public ResponseEntity create(@Valid @RequestBody CreateEndorsementViewModel endorsement) { + public ResponseEntity create( + @Valid @RequestBody CreateEndorsementViewModel endorsement) { return new ResponseEntity<>(endorsementService.create(endorsement), HttpStatus.CREATED); } @PatchMapping("/{id}") - public ResponseEntity update(@PathVariable Integer id, @Valid @RequestBody UpdateEndorsementViewModel body) { + public ResponseEntity update( + @PathVariable Integer id, @Valid @RequestBody UpdateEndorsementViewModel body) { return new ResponseEntity<>(endorsementService.update(id, body), HttpStatus.OK); } } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/apis/HealthApi.java b/skill-tree/src/main/java/com/RDS/skilltree/apis/HealthApi.java index 17c80ee8..88dd1d20 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/apis/HealthApi.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/apis/HealthApi.java @@ -1,6 +1,7 @@ package com.RDS.skilltree.apis; import com.RDS.skilltree.services.MetricService; +import java.util.Map; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpStatus; @@ -9,8 +10,6 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; -import java.util.Map; - @Slf4j @RestController @RequiredArgsConstructor @@ -20,7 +19,7 @@ public class HealthApi { @GetMapping public ResponseEntity> getUptime() { - return new ResponseEntity<>(Map.of("uptime in seconds", metricService.getUptime()), HttpStatus.OK); + return new ResponseEntity<>( + Map.of("uptime in seconds", metricService.getUptime()), HttpStatus.OK); } - } 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 3cec0ed2..ebf07ed6 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 @@ -7,6 +7,7 @@ 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; @@ -14,8 +15,6 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; -import java.util.List; - @Slf4j @RestController @RequiredArgsConstructor @@ -41,6 +40,5 @@ public ResponseEntity> getEndorsementsBySkillId( @PathVariable(value = "id") Integer skillID) { PageRequest pageRequest = PageRequest.of(offset, limit); return ResponseEntity.ok(endorsementService.getAllEndorsementsBySkillId(skillID, pageRequest)); - } } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/config/SecurityConfig.java b/skill-tree/src/main/java/com/RDS/skilltree/config/SecurityConfig.java index 76826060..b01c3b49 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/config/SecurityConfig.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/config/SecurityConfig.java @@ -4,6 +4,8 @@ import com.RDS.skilltree.Authentication.CustomAccessDeniedHandler; import com.RDS.skilltree.User.UserRoleEnum; import com.RDS.skilltree.utils.JWTAuthenticationFilter; +import java.util.Arrays; +import java.util.List; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.HttpHeaders; @@ -18,9 +20,6 @@ import org.springframework.web.cors.CorsConfigurationSource; import org.springframework.web.cors.UrlBasedCorsConfigurationSource; -import java.util.Arrays; -import java.util.List; - @EnableWebSecurity @Configuration public class SecurityConfig { diff --git a/skill-tree/src/main/java/com/RDS/skilltree/exceptions/GlobalExceptionHandler.java b/skill-tree/src/main/java/com/RDS/skilltree/exceptions/GlobalExceptionHandler.java index 6f316414..50df4784 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/exceptions/GlobalExceptionHandler.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/exceptions/GlobalExceptionHandler.java @@ -2,6 +2,7 @@ import com.RDS.skilltree.utils.GenericResponse; import jakarta.validation.ConstraintViolationException; +import java.util.List; import lombok.extern.slf4j.Slf4j; import org.apache.tomcat.websocket.AuthenticationException; import org.springframework.http.HttpStatus; @@ -14,8 +15,6 @@ import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.context.request.WebRequest; -import java.util.List; - @Slf4j @ControllerAdvice public class GlobalExceptionHandler { @@ -96,31 +95,37 @@ public ResponseEntity> handleException(ConstraintViolati } @ExceptionHandler(UserNotFoundException.class) - public ResponseEntity handleUserNotFoundException(UserNotFoundException ex, WebRequest request) { + public ResponseEntity handleUserNotFoundException( + UserNotFoundException ex, WebRequest request) { log.error("Exception - Error : {}", ex.getMessage(), ex); return new ResponseEntity<>(new GenericResponse<>(null, ex.getMessage()), HttpStatus.NOT_FOUND); } @ExceptionHandler(SkillAlreadyExistsException.class) - public ResponseEntity handleSkillAlreadyExistsException(SkillAlreadyExistsException ex, WebRequest request) { + public ResponseEntity handleSkillAlreadyExistsException( + SkillAlreadyExistsException ex, WebRequest request) { log.error("Exception - Error : {}", ex.getMessage(), ex); return new ResponseEntity<>(new GenericResponse<>(null, ex.getMessage()), HttpStatus.CONFLICT); } @ExceptionHandler(SelfEndorsementNotAllowedException.class) - public ResponseEntity handleSelfEndorsementNotAllowedException(SelfEndorsementNotAllowedException ex, WebRequest request) { + public ResponseEntity handleSelfEndorsementNotAllowedException( + SelfEndorsementNotAllowedException ex, WebRequest request) { log.error("Exception - Error : {}", ex.getMessage(), ex); - return new ResponseEntity<>(new GenericResponse<>(null, ex.getMessage()), HttpStatus.METHOD_NOT_ALLOWED); + return new ResponseEntity<>( + new GenericResponse<>(null, ex.getMessage()), HttpStatus.METHOD_NOT_ALLOWED); } @ExceptionHandler(SkillNotFoundException.class) - public ResponseEntity handleSkillNotFoundException(SkillNotFoundException ex, WebRequest request) { + public ResponseEntity handleSkillNotFoundException( + SkillNotFoundException ex, WebRequest request) { log.error("Exception - Error : {}", ex.getMessage(), ex); return new ResponseEntity<>(new GenericResponse<>(null, ex.getMessage()), HttpStatus.NOT_FOUND); } @ExceptionHandler(EndorsementNotFoundException.class) - public ResponseEntity handleEndorsementNotException(EndorsementNotFoundException ex, WebRequest request) { + public ResponseEntity handleEndorsementNotException( + EndorsementNotFoundException ex, WebRequest request) { log.error("Exception - Error : {}", ex.getMessage(), ex); return new ResponseEntity<>(new GenericResponse<>(null, ex.getMessage()), HttpStatus.NOT_FOUND); } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/exceptions/SkillNotFoundException.java b/skill-tree/src/main/java/com/RDS/skilltree/exceptions/SkillNotFoundException.java index 652700f1..0a124fd0 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/exceptions/SkillNotFoundException.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/exceptions/SkillNotFoundException.java @@ -4,4 +4,4 @@ public class SkillNotFoundException extends RuntimeException { public SkillNotFoundException(String message) { super(message); } -} \ No newline at end of file +} 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 01351c13..9c619b36 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 @@ -32,4 +32,4 @@ public class Endorsement extends TrackedProperties { @Column(name = "message", nullable = false) private String message; -} \ No newline at end of file +} diff --git a/skill-tree/src/main/java/com/RDS/skilltree/repositories/SkillRepository.java b/skill-tree/src/main/java/com/RDS/skilltree/repositories/SkillRepository.java index 8a5fdfc0..8f005d92 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/repositories/SkillRepository.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/repositories/SkillRepository.java @@ -7,4 +7,4 @@ @Repository public interface SkillRepository extends JpaRepository { boolean existsByName(String name); -} \ No newline at end of file +} diff --git a/skill-tree/src/main/java/com/RDS/skilltree/services/EndorsementServiceImplementation.java b/skill-tree/src/main/java/com/RDS/skilltree/services/EndorsementServiceImplementation.java index fa11c7d7..e16fe967 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/services/EndorsementServiceImplementation.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/services/EndorsementServiceImplementation.java @@ -13,14 +13,13 @@ import com.RDS.skilltree.viewmodels.CreateEndorsementViewModel; import com.RDS.skilltree.viewmodels.EndorsementViewModel; import com.RDS.skilltree.viewmodels.UpdateEndorsementViewModel; +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; -import java.util.Objects; -import java.util.Optional; - @Service @RequiredArgsConstructor public class EndorsementServiceImplementation implements EndorsementService { @@ -29,7 +28,8 @@ public class EndorsementServiceImplementation implements EndorsementService { private final EndorsementRepository endorsementRepository; @Override - public Page getAllEndorsementsBySkillId(Integer skillId, Pageable pageable) { + public Page getAllEndorsementsBySkillId( + Integer skillId, Pageable pageable) { Page endorsementPage = endorsementRepository.findBySkillId(skillId, pageable); return endorsementPage.map(EndorsementViewModel::toViewModel); } @@ -79,7 +79,8 @@ public EndorsementViewModel update(Integer endorsementId, UpdateEndorsementViewM Optional exitingEndorsement = endorsementRepository.findById(endorsementId); if (exitingEndorsement.isEmpty()) { - throw new EndorsementNotFoundException(String.format("Endorsement with id: %s not found", endorsementId)); + throw new EndorsementNotFoundException( + String.format("Endorsement with id: %s not found", endorsementId)); } Endorsement endorsement = exitingEndorsement.get(); 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 77923b6f..10368a0a 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 @@ -2,7 +2,6 @@ import com.RDS.skilltree.viewmodels.CreateSkillViewModel; import com.RDS.skilltree.viewmodels.SkillViewModel; - import java.util.List; public interface SkillService { 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 29cc5936..8797205c 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 @@ -9,15 +9,14 @@ import com.RDS.skilltree.repositories.SkillRepository; import com.RDS.skilltree.viewmodels.CreateSkillViewModel; import com.RDS.skilltree.viewmodels.SkillViewModel; +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; import org.springframework.beans.BeanUtils; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Service; -import java.util.List; -import java.util.Optional; -import java.util.stream.Collectors; - @Service @RequiredArgsConstructor public class SkillServiceImplementation implements SkillService { @@ -34,7 +33,8 @@ public List getAll() { @Override public SkillViewModel create(CreateSkillViewModel skill) { if (skillRepository.existsByName(skill.getName())) { - throw new SkillAlreadyExistsException(String.format("Skill with name %s already exists", skill.getName())); + throw new SkillAlreadyExistsException( + String.format("Skill with name %s already exists", skill.getName())); } JwtUserModel jwtDetails = diff --git a/skill-tree/src/main/java/com/RDS/skilltree/utils/JWTAuthenticationFilter.java b/skill-tree/src/main/java/com/RDS/skilltree/utils/JWTAuthenticationFilter.java index f40b5e30..4d40a8a9 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/utils/JWTAuthenticationFilter.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/utils/JWTAuthenticationFilter.java @@ -7,6 +7,7 @@ import jakarta.servlet.http.Cookie; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; +import java.io.IOException; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; @@ -17,16 +18,13 @@ import org.springframework.web.filter.OncePerRequestFilter; import org.springframework.web.util.WebUtils; -import java.io.IOException; - @Slf4j public class JWTAuthenticationFilter extends OncePerRequestFilter { @Value("${cookieName}") private String cookieName; - @Autowired - private JWTUtils jwtUtils; + @Autowired private JWTUtils jwtUtils; private static final String BEARER_PREFIX = "Bearer "; @Override diff --git a/skill-tree/src/test/java/com/RDS/skilltree/utils/UUIDValidationInterceptorTest.java b/skill-tree/src/test/java/com/RDS/skilltree/utils/UUIDValidationInterceptorTest.java index 6fc9e67b..f17e6cc1 100644 --- a/skill-tree/src/test/java/com/RDS/skilltree/utils/UUIDValidationInterceptorTest.java +++ b/skill-tree/src/test/java/com/RDS/skilltree/utils/UUIDValidationInterceptorTest.java @@ -1,8 +1,13 @@ package com.RDS.skilltree.utils; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.when; + import com.RDS.skilltree.exceptions.InvalidParameterException; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; +import java.util.UUID; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -10,23 +15,14 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; -import java.util.UUID; - -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.Mockito.when; - @ExtendWith(MockitoExtension.class) class UUIDValidationInterceptorTest { - @Mock - private HttpServletRequest request; + @Mock private HttpServletRequest request; - @Mock - private HttpServletResponse response; + @Mock private HttpServletResponse response; - @InjectMocks - private UUIDValidationInterceptor interceptor; + @InjectMocks private UUIDValidationInterceptor interceptor; @Test @Disabled From 5c3b98335ba371538ad48e037f2b18690c6c44e9 Mon Sep 17 00:00:00 2001 From: yash raj Date: Sun, 7 Jul 2024 20:49:43 +0530 Subject: [PATCH 28/31] create annotation and aspect to handle authorized roles to an api --- .../annotations/AuthorizedRoles.java | 14 +++++++ .../com/RDS/skilltree/apis/SkillsApi.java | 6 ++- .../aspects/AuthorizedRolesAspect.java | 40 +++++++++++++++++++ .../exceptions/ForbiddenException.java | 7 ++++ .../exceptions/GlobalExceptionHandler.java | 9 ++++- 5 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 skill-tree/src/main/java/com/RDS/skilltree/annotations/AuthorizedRoles.java create mode 100644 skill-tree/src/main/java/com/RDS/skilltree/aspects/AuthorizedRolesAspect.java create mode 100644 skill-tree/src/main/java/com/RDS/skilltree/exceptions/ForbiddenException.java diff --git a/skill-tree/src/main/java/com/RDS/skilltree/annotations/AuthorizedRoles.java b/skill-tree/src/main/java/com/RDS/skilltree/annotations/AuthorizedRoles.java new file mode 100644 index 00000000..2a0d4dc9 --- /dev/null +++ b/skill-tree/src/main/java/com/RDS/skilltree/annotations/AuthorizedRoles.java @@ -0,0 +1,14 @@ +package com.RDS.skilltree.annotations; + +import com.RDS.skilltree.User.UserRoleEnum; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target(ElementType.METHOD) +@Retention(RetentionPolicy.RUNTIME) +public @interface AuthorizedRoles { + UserRoleEnum[] value() default {}; +} 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 ebf07ed6..23e763bb 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 @@ -1,5 +1,7 @@ package com.RDS.skilltree.apis; +import com.RDS.skilltree.User.UserRoleEnum; +import com.RDS.skilltree.annotations.AuthorizedRoles; import com.RDS.skilltree.services.EndorsementService; import com.RDS.skilltree.services.SkillService; import com.RDS.skilltree.viewmodels.CreateSkillViewModel; @@ -7,7 +9,6 @@ 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; @@ -15,6 +16,8 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; +import java.util.List; + @Slf4j @RestController @RequiredArgsConstructor @@ -24,6 +27,7 @@ public class SkillsApi { private final EndorsementService endorsementService; @GetMapping + @AuthorizedRoles({UserRoleEnum.USER, UserRoleEnum.SUPERUSER}) public ResponseEntity> getAll() { return ResponseEntity.ok(skillService.getAll()); } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/aspects/AuthorizedRolesAspect.java b/skill-tree/src/main/java/com/RDS/skilltree/aspects/AuthorizedRolesAspect.java new file mode 100644 index 00000000..223b6f04 --- /dev/null +++ b/skill-tree/src/main/java/com/RDS/skilltree/aspects/AuthorizedRolesAspect.java @@ -0,0 +1,40 @@ +package com.RDS.skilltree.aspects; + +import com.RDS.skilltree.User.JwtUserModel; +import com.RDS.skilltree.User.UserRoleEnum; +import com.RDS.skilltree.annotations.AuthorizedRoles; +import com.RDS.skilltree.exceptions.ForbiddenException; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.stereotype.Component; + +@Aspect +@Component +public class AuthorizedRolesAspect { + + @Around("@annotation(authorizedRoles)") + public Object authorize(ProceedingJoinPoint jointPoint, AuthorizedRoles authorizedRoles) throws Throwable { + JwtUserModel jwtDetails = + (JwtUserModel) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); + + UserRoleEnum role = jwtDetails.getRole(); + + if (!isAuthorized(role, authorizedRoles.value())) { + throw new ForbiddenException("You're not authorized to make this request"); + } + + return jointPoint.proceed(); + } + + private boolean isAuthorized(UserRoleEnum userRole, UserRoleEnum[] allowedRoles) { + for (UserRoleEnum role : allowedRoles) { + if (role.equals(userRole)) { + return true; + } + } + + return false; + } +} diff --git a/skill-tree/src/main/java/com/RDS/skilltree/exceptions/ForbiddenException.java b/skill-tree/src/main/java/com/RDS/skilltree/exceptions/ForbiddenException.java new file mode 100644 index 00000000..47b3c385 --- /dev/null +++ b/skill-tree/src/main/java/com/RDS/skilltree/exceptions/ForbiddenException.java @@ -0,0 +1,7 @@ +package com.RDS.skilltree.exceptions; + +public class ForbiddenException extends RuntimeException { + public ForbiddenException(String message) { + super(message); + } +} diff --git a/skill-tree/src/main/java/com/RDS/skilltree/exceptions/GlobalExceptionHandler.java b/skill-tree/src/main/java/com/RDS/skilltree/exceptions/GlobalExceptionHandler.java index 50df4784..73b0e4c7 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/exceptions/GlobalExceptionHandler.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/exceptions/GlobalExceptionHandler.java @@ -2,7 +2,6 @@ import com.RDS.skilltree.utils.GenericResponse; import jakarta.validation.ConstraintViolationException; -import java.util.List; import lombok.extern.slf4j.Slf4j; import org.apache.tomcat.websocket.AuthenticationException; import org.springframework.http.HttpStatus; @@ -15,6 +14,8 @@ import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.context.request.WebRequest; +import java.util.List; + @Slf4j @ControllerAdvice public class GlobalExceptionHandler { @@ -129,4 +130,10 @@ public ResponseEntity handleEndorsementNotException( log.error("Exception - Error : {}", ex.getMessage(), ex); return new ResponseEntity<>(new GenericResponse<>(null, ex.getMessage()), HttpStatus.NOT_FOUND); } + + @ExceptionHandler(ForbiddenException.class) + public ResponseEntity handleForbiddenException(ForbiddenException ex, WebRequest request) { + log.error("Exception - Error : {}", ex.getMessage(), ex); + return new ResponseEntity<>(new GenericResponse<>(null, ex.getMessage()), HttpStatus.FORBIDDEN); + } } From abd38a4fb8d19b8aa32e929f27caebcede1698a7 Mon Sep 17 00:00:00 2001 From: yash raj Date: Sun, 7 Jul 2024 21:48:10 +0530 Subject: [PATCH 29/31] add authorizred roles annotation to skillsapi & endorsementsapi class --- .../annotations/AuthorizedRoles.java | 2 +- .../RDS/skilltree/apis/EndorsementsApi.java | 3 ++ .../com/RDS/skilltree/apis/SkillsApi.java | 2 +- .../aspects/AuthorizedRolesAspect.java | 31 ++++++++++++++++--- 4 files changed, 31 insertions(+), 7 deletions(-) diff --git a/skill-tree/src/main/java/com/RDS/skilltree/annotations/AuthorizedRoles.java b/skill-tree/src/main/java/com/RDS/skilltree/annotations/AuthorizedRoles.java index 2a0d4dc9..93a03c1e 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/annotations/AuthorizedRoles.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/annotations/AuthorizedRoles.java @@ -7,7 +7,7 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; -@Target(ElementType.METHOD) +@Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) public @interface AuthorizedRoles { UserRoleEnum[] value() default {}; diff --git a/skill-tree/src/main/java/com/RDS/skilltree/apis/EndorsementsApi.java b/skill-tree/src/main/java/com/RDS/skilltree/apis/EndorsementsApi.java index d775d5c7..c418a5dd 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/apis/EndorsementsApi.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/apis/EndorsementsApi.java @@ -1,5 +1,7 @@ package com.RDS.skilltree.apis; +import com.RDS.skilltree.User.UserRoleEnum; +import com.RDS.skilltree.annotations.AuthorizedRoles; import com.RDS.skilltree.services.EndorsementService; import com.RDS.skilltree.viewmodels.CreateEndorsementViewModel; import com.RDS.skilltree.viewmodels.EndorsementViewModel; @@ -15,6 +17,7 @@ @RestController @RequiredArgsConstructor @RequestMapping("v1/endorsements") +@AuthorizedRoles({UserRoleEnum.USER, UserRoleEnum.SUPERUSER}) public class EndorsementsApi { private final EndorsementService endorsementService; 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 23e763bb..9811cc3f 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 @@ -22,12 +22,12 @@ @RestController @RequiredArgsConstructor @RequestMapping("v1/skills") +@AuthorizedRoles({UserRoleEnum.USER, UserRoleEnum.SUPERUSER}) public class SkillsApi { private final SkillService skillService; private final EndorsementService endorsementService; @GetMapping - @AuthorizedRoles({UserRoleEnum.USER, UserRoleEnum.SUPERUSER}) public ResponseEntity> getAll() { return ResponseEntity.ok(skillService.getAll()); } diff --git a/skill-tree/src/main/java/com/RDS/skilltree/aspects/AuthorizedRolesAspect.java b/skill-tree/src/main/java/com/RDS/skilltree/aspects/AuthorizedRolesAspect.java index 223b6f04..34e0e92d 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/aspects/AuthorizedRolesAspect.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/aspects/AuthorizedRolesAspect.java @@ -7,25 +7,46 @@ import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.reflect.MethodSignature; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Component; +import java.lang.reflect.Method; + @Aspect @Component public class AuthorizedRolesAspect { - @Around("@annotation(authorizedRoles)") - public Object authorize(ProceedingJoinPoint jointPoint, AuthorizedRoles authorizedRoles) throws Throwable { + @Around("@within(authorizedRoles) || @annotation(authorizedRoles)") + public Object authorize(ProceedingJoinPoint joinPoint, AuthorizedRoles authorizedRoles) throws Throwable { JwtUserModel jwtDetails = (JwtUserModel) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); - UserRoleEnum role = jwtDetails.getRole(); - if (!isAuthorized(role, authorizedRoles.value())) { + + MethodSignature signature = (MethodSignature) joinPoint.getSignature(); + Method method = signature.getMethod(); + Class targetClass = method.getDeclaringClass(); + + AuthorizedRoles methodAuthorized = method.getAnnotation(AuthorizedRoles.class); + AuthorizedRoles classAuthorized = targetClass.getAnnotation(AuthorizedRoles.class); + + UserRoleEnum[] allowedRoles = {}; + + if (methodAuthorized != null) { + allowedRoles = methodAuthorized.value(); + } else if (classAuthorized != null) { + allowedRoles = classAuthorized.value(); + } else { + // If no roles are specified, proceed with the method execution + joinPoint.proceed(); + } + + if (!isAuthorized(role, allowedRoles)) { throw new ForbiddenException("You're not authorized to make this request"); } - return jointPoint.proceed(); + return joinPoint.proceed(); } private boolean isAuthorized(UserRoleEnum userRole, UserRoleEnum[] allowedRoles) { From 884e8ccf7176d7191fdead1e797456a702c78f15 Mon Sep 17 00:00:00 2001 From: yash raj Date: Sun, 7 Jul 2024 21:54:34 +0530 Subject: [PATCH 30/31] set authorized role for creating a skill to only superuser --- skill-tree/src/main/java/com/RDS/skilltree/apis/SkillsApi.java | 1 + 1 file changed, 1 insertion(+) 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 9811cc3f..e7052432 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 @@ -33,6 +33,7 @@ public ResponseEntity> getAll() { } @PostMapping + @AuthorizedRoles({UserRoleEnum.SUPERUSER}) public ResponseEntity create(@Valid @RequestBody CreateSkillViewModel skill) { return ResponseEntity.ok(skillService.create(skill)); } From 8bc7c9fb286920e6e0c7c925deb9f23567ce7173 Mon Sep 17 00:00:00 2001 From: yash raj Date: Sun, 7 Jul 2024 22:01:10 +0530 Subject: [PATCH 31/31] fix formatting --- .../com/RDS/skilltree/annotations/AuthorizedRoles.java | 1 - .../src/main/java/com/RDS/skilltree/apis/SkillsApi.java | 3 +-- .../com/RDS/skilltree/aspects/AuthorizedRolesAspect.java | 7 +++---- .../RDS/skilltree/exceptions/GlobalExceptionHandler.java | 3 +-- 4 files changed, 5 insertions(+), 9 deletions(-) diff --git a/skill-tree/src/main/java/com/RDS/skilltree/annotations/AuthorizedRoles.java b/skill-tree/src/main/java/com/RDS/skilltree/annotations/AuthorizedRoles.java index 93a03c1e..32e798b8 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/annotations/AuthorizedRoles.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/annotations/AuthorizedRoles.java @@ -1,7 +1,6 @@ package com.RDS.skilltree.annotations; import com.RDS.skilltree.User.UserRoleEnum; - import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; 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 e7052432..b5e76f43 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 @@ -9,6 +9,7 @@ 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; @@ -16,8 +17,6 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; -import java.util.List; - @Slf4j @RestController @RequiredArgsConstructor diff --git a/skill-tree/src/main/java/com/RDS/skilltree/aspects/AuthorizedRolesAspect.java b/skill-tree/src/main/java/com/RDS/skilltree/aspects/AuthorizedRolesAspect.java index 34e0e92d..65ff6c2f 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/aspects/AuthorizedRolesAspect.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/aspects/AuthorizedRolesAspect.java @@ -4,6 +4,7 @@ import com.RDS.skilltree.User.UserRoleEnum; import com.RDS.skilltree.annotations.AuthorizedRoles; import com.RDS.skilltree.exceptions.ForbiddenException; +import java.lang.reflect.Method; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; @@ -11,19 +12,17 @@ import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Component; -import java.lang.reflect.Method; - @Aspect @Component public class AuthorizedRolesAspect { @Around("@within(authorizedRoles) || @annotation(authorizedRoles)") - public Object authorize(ProceedingJoinPoint joinPoint, AuthorizedRoles authorizedRoles) throws Throwable { + public Object authorize(ProceedingJoinPoint joinPoint, AuthorizedRoles authorizedRoles) + throws Throwable { JwtUserModel jwtDetails = (JwtUserModel) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); UserRoleEnum role = jwtDetails.getRole(); - MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); Class targetClass = method.getDeclaringClass(); diff --git a/skill-tree/src/main/java/com/RDS/skilltree/exceptions/GlobalExceptionHandler.java b/skill-tree/src/main/java/com/RDS/skilltree/exceptions/GlobalExceptionHandler.java index 73b0e4c7..c21e447f 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/exceptions/GlobalExceptionHandler.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/exceptions/GlobalExceptionHandler.java @@ -2,6 +2,7 @@ import com.RDS.skilltree.utils.GenericResponse; import jakarta.validation.ConstraintViolationException; +import java.util.List; import lombok.extern.slf4j.Slf4j; import org.apache.tomcat.websocket.AuthenticationException; import org.springframework.http.HttpStatus; @@ -14,8 +15,6 @@ import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.context.request.WebRequest; -import java.util.List; - @Slf4j @ControllerAdvice public class GlobalExceptionHandler {