From ca5dd657b21248ffd153a680dce50649421f81a5 Mon Sep 17 00:00:00 2001 From: vikhyat187 Date: Wed, 15 Nov 2023 20:55:28 +0530 Subject: [PATCH 01/22] APIs for '/skills' endpoint --- .../com/RDS/skilltree/Skill/SkillDRO.java | 29 +++++++++++ .../com/RDS/skilltree/Skill/SkillDTO.java | 32 ++++++++++++ .../com/RDS/skilltree/Skill/SkillModel.java | 6 +-- .../RDS/skilltree/Skill/SkillsController.java | 39 +++++++++++++++ .../RDS/skilltree/Skill/SkillsService.java | 11 +++++ .../skilltree/Skill/SkillsServiceImpl.java | 49 +++++++++++++++++++ 6 files changed, 163 insertions(+), 3 deletions(-) create mode 100644 skill-tree /src/main/java/com/RDS/skilltree/Skill/SkillDRO.java create mode 100644 skill-tree /src/main/java/com/RDS/skilltree/Skill/SkillDTO.java create mode 100644 skill-tree /src/main/java/com/RDS/skilltree/Skill/SkillsController.java create mode 100644 skill-tree /src/main/java/com/RDS/skilltree/Skill/SkillsService.java create mode 100644 skill-tree /src/main/java/com/RDS/skilltree/Skill/SkillsServiceImpl.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 new file mode 100644 index 00000000..55cc2543 --- /dev/null +++ b/skill-tree /src/main/java/com/RDS/skilltree/Skill/SkillDRO.java @@ -0,0 +1,29 @@ +package com.RDS.skilltree.Skill; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.UUID; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@Builder +@JsonIgnoreProperties(ignoreUnknown = true) +public class SkillDRO { + private String name; + private SkillType type; + private UUID createdBy; + + + static SkillModel toModel(SkillDRO skillDRO) { + return SkillModel.builder() + .name(skillDRO.getName()) + .type(skillDRO.getType()) + .deleted(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 new file mode 100644 index 00000000..5bb319b1 --- /dev/null +++ b/skill-tree /src/main/java/com/RDS/skilltree/Skill/SkillDTO.java @@ -0,0 +1,32 @@ +package com.RDS.skilltree.Skill; + +import com.RDS.skilltree.User.UserModel; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.Set; +import java.util.UUID; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@Builder +@JsonIgnoreProperties(ignoreUnknown = true) +public class SkillDTO { + private UUID id; + private SkillType type; + private String name; + private Set users; + + public static SkillDTO toDto(SkillModel skillModel) { + return SkillDTO.builder() + .id(skillModel.getId()) + .name(skillModel.getName()) + .type(skillModel.getType()) + .users(skillModel.getUsers()) + .build(); + } +} 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 c4bd967c..32cd7ef5 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 @@ -5,9 +5,7 @@ import com.fasterxml.jackson.annotation.JsonBackReference; import com.fasterxml.jackson.annotation.JsonIgnore; import jakarta.persistence.*; -import lombok.Data; -import lombok.EqualsAndHashCode; -import lombok.NoArgsConstructor; +import lombok.*; import java.time.Instant; import java.util.Set; @@ -16,6 +14,8 @@ @EqualsAndHashCode(callSuper = true) @Entity @NoArgsConstructor +@AllArgsConstructor +@Builder @Data @Table(name = "Skill") public class SkillModel extends TrackedProperties { 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 new file mode 100644 index 00000000..315cfb43 --- /dev/null +++ b/skill-tree /src/main/java/com/RDS/skilltree/Skill/SkillsController.java @@ -0,0 +1,39 @@ +package com.RDS.skilltree.Skill; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.*; + +import java.util.List; +import java.util.UUID; + +@RestController +@Slf4j +@RequestMapping("/v1/skills") +public class SkillsController { + private final SkillsService skillsService; + public SkillsController(SkillsService skillsService){ + this.skillsService = skillsService; + } + + @PostMapping("") + public String createSkill(@RequestBody(required = true) SkillDRO skillDRO){ + log.info("Im called"); + return skillsService.createSkill(skillDRO); + } + + @GetMapping("") + public List getAllSkills(){ + return skillsService.getAllSkills(); + } + + @GetMapping("/name/{name}") + public SkillDTO getSkillByName(@PathVariable(value = "name", required = true) String name){ + return skillsService.getSkillByName(name); + } + @GetMapping("/{id}") + public SkillDTO getSkillById(@PathVariable(value = "id", required = true) UUID id){ + log.info("The id is {}", id); + return skillsService.getSkillById(id); + } + +} 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 new file mode 100644 index 00000000..ddcd7eb9 --- /dev/null +++ b/skill-tree /src/main/java/com/RDS/skilltree/Skill/SkillsService.java @@ -0,0 +1,11 @@ +package com.RDS.skilltree.Skill; + +import java.util.List; +import java.util.UUID; + +public interface SkillsService { + SkillDTO getSkillById(UUID id); + SkillDTO getSkillByName(String skillName); + List getAllSkills(); + String 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 new file mode 100644 index 00000000..fdbd1222 --- /dev/null +++ b/skill-tree /src/main/java/com/RDS/skilltree/Skill/SkillsServiceImpl.java @@ -0,0 +1,49 @@ +package com.RDS.skilltree.Skill; + +import com.RDS.skilltree.User.UserRepository; +import org.springframework.stereotype.Service; + +import java.time.Instant; +import java.util.List; +import java.util.Optional; +import java.util.UUID; +import java.util.stream.Collectors; + +@Service +public class SkillsServiceImpl implements SkillsService{ + private final SkillRepository skillRepository; + private final UserRepository userRepository; + public SkillsServiceImpl(SkillRepository skillRepository, + UserRepository userRepository){ + this.userRepository = userRepository; + this.skillRepository = skillRepository; + } + + @Override + public SkillDTO getSkillById(UUID id){ + Optional skillModel = skillRepository.findById(id); + return skillModel.map(SkillDTO::toDto).orElse(null); + } + + @Override + public SkillDTO getSkillByName(String skillName){ + Optional skillModel = skillRepository.findByName(skillName); + return skillModel.map(SkillDTO::toDto).orElse(null); + } + + @Override + public List getAllSkills(){ + List skillModels = skillRepository.findAll(); + return skillModels.stream().map(SkillDTO::toDto).collect(Collectors.toList()); + } + + @Override + public String createSkill(SkillDRO skillDRO){ + SkillModel newSkill = SkillDRO.toModel(skillDRO); + newSkill.setCreatedAt(Instant.now()); + newSkill.setCreatedBy(userRepository.findById(skillDRO.getCreatedBy()).get()); + skillRepository.save(newSkill); + System.out.println("Saved the skill in the db"); + return "Success"; + } +} From 43f2119fd67b8a7e2b74bd47f1236bbf0bb493bf Mon Sep 17 00:00:00 2001 From: vikhyat187 Date: Wed, 15 Nov 2023 21:00:47 +0530 Subject: [PATCH 02/22] Remove logs added for debugging purpose --- .../main/java/com/RDS/skilltree/Skill/SkillRepository.java | 4 ++++ .../java/com/RDS/skilltree/Skill/SkillsController.java | 6 ++---- .../java/com/RDS/skilltree/Skill/SkillsServiceImpl.java | 7 +++++-- skill-tree /src/main/resources/application.properties | 5 +++-- 4 files changed, 14 insertions(+), 8 deletions(-) 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 e50466ee..fb34dd4a 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 @@ -3,8 +3,12 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; +import java.util.List; +import java.util.Optional; import java.util.UUID; @Repository public interface SkillRepository extends JpaRepository { + Optional findByName(String name); + List findAll(); } 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 315cfb43..51b3b16d 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 @@ -15,13 +15,12 @@ public SkillsController(SkillsService skillsService){ this.skillsService = skillsService; } - @PostMapping("") + @PostMapping("/") public String createSkill(@RequestBody(required = true) SkillDRO skillDRO){ - log.info("Im called"); return skillsService.createSkill(skillDRO); } - @GetMapping("") + @GetMapping("/") public List getAllSkills(){ return skillsService.getAllSkills(); } @@ -32,7 +31,6 @@ public SkillDTO getSkillByName(@PathVariable(value = "name", required = true) St } @GetMapping("/{id}") public SkillDTO getSkillById(@PathVariable(value = "id", required = true) UUID id){ - log.info("The id is {}", id); return skillsService.getSkillById(id); } 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 fdbd1222..cc2e6421 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,5 +1,6 @@ package com.RDS.skilltree.Skill; +import com.RDS.skilltree.User.UserModel; import com.RDS.skilltree.User.UserRepository; import org.springframework.stereotype.Service; @@ -41,9 +42,11 @@ public List getAllSkills(){ public String createSkill(SkillDRO skillDRO){ SkillModel newSkill = SkillDRO.toModel(skillDRO); newSkill.setCreatedAt(Instant.now()); - newSkill.setCreatedBy(userRepository.findById(skillDRO.getCreatedBy()).get()); + newSkill.setUpdatedAt(Instant.now()); + UserModel user = userRepository.findById(skillDRO.getCreatedBy()).get(); + newSkill.setUpdatedBy(user); + newSkill.setCreatedBy(user); skillRepository.save(newSkill); - System.out.println("Saved the skill in the db"); return "Success"; } } diff --git a/skill-tree /src/main/resources/application.properties b/skill-tree /src/main/resources/application.properties index defe3403..15eb874a 100644 --- a/skill-tree /src/main/resources/application.properties +++ b/skill-tree /src/main/resources/application.properties @@ -1,7 +1,8 @@ spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver -spring.datasource.url=jdbc:mysql://localhost:3306/skilltree +spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/${DB_NAME} spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect spring.datasource.username=${MYSQL_DB_USERNAME} spring.datasource.password=${MYSQL_DB_PASSWORD} -spring.jpa.hibernate.ddl-auto=update +spring.jpa.hibernate.ddl-auto=${DB_DDL_POLICY} +jwt.rds.public.key = ${RDS_PUBLIC_KEY} From 48bae00fc27a7360f5404a8f0ed1f7f466b121fd Mon Sep 17 00:00:00 2001 From: vikhyat187 Date: Sun, 19 Nov 2023 15:04:03 +0530 Subject: [PATCH 03/22] Updated the code for GET /skills in paginated form --- .../java/com/RDS/skilltree/Skill/SkillRepository.java | 5 +++-- .../com/RDS/skilltree/Skill/SkillsController.java | 11 ++++++++--- .../java/com/RDS/skilltree/Skill/SkillsService.java | 6 ++++-- .../com/RDS/skilltree/Skill/SkillsServiceImpl.java | 9 ++++++--- 4 files changed, 21 insertions(+), 10 deletions(-) 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 fb34dd4a..69bfe86a 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,14 +1,15 @@ package com.RDS.skilltree.Skill; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; -import java.util.List; import java.util.Optional; import java.util.UUID; @Repository public interface SkillRepository extends JpaRepository { Optional findByName(String name); - List findAll(); + 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 51b3b16d..4b466515 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,9 +1,11 @@ package com.RDS.skilltree.Skill; import lombok.extern.slf4j.Slf4j; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; import org.springframework.web.bind.annotation.*; -import java.util.List; import java.util.UUID; @RestController @@ -21,8 +23,11 @@ public String createSkill(@RequestBody(required = true) SkillDRO skillDRO){ } @GetMapping("/") - public List getAllSkills(){ - return skillsService.getAllSkills(); + public Page getAllSkills( + @RequestParam(value = "page", defaultValue = "0") int page, + @RequestParam(value = "size", defaultValue = "10") int size) { + Pageable pageable = PageRequest.of(page, size); + return skillsService.getAllSkills(pageable); } @GetMapping("/name/{name}") 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 ddcd7eb9..09a581f4 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,13 @@ package com.RDS.skilltree.Skill; -import java.util.List; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; + import java.util.UUID; public interface SkillsService { SkillDTO getSkillById(UUID id); SkillDTO getSkillByName(String skillName); - List getAllSkills(); + Page getAllSkills(Pageable pageable); String 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 index cc2e6421..f72c1af3 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 @@ -2,6 +2,9 @@ import com.RDS.skilltree.User.UserModel; import com.RDS.skilltree.User.UserRepository; +import lombok.NoArgsConstructor; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import java.time.Instant; @@ -33,9 +36,9 @@ public SkillDTO getSkillByName(String skillName){ } @Override - public List getAllSkills(){ - List skillModels = skillRepository.findAll(); - return skillModels.stream().map(SkillDTO::toDto).collect(Collectors.toList()); + public Page getAllSkills(Pageable pageable){ + Page skillModels = skillRepository.findAll(pageable); + return skillModels.map(SkillDTO::toDto); } @Override From cf548bb18b46130f4bc1882d0bb0b9b917a097ca Mon Sep 17 00:00:00 2001 From: vikhyat187 Date: Mon, 20 Nov 2023 06:17:55 +0530 Subject: [PATCH 04/22] Updated the test for the get paginated skills --- .../RDS/skilltree/unit/SkillsServiceTest.java | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 skill-tree /src/test/java/com/RDS/skilltree/unit/SkillsServiceTest.java 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 new file mode 100644 index 00000000..bd50bdbf --- /dev/null +++ b/skill-tree /src/test/java/com/RDS/skilltree/unit/SkillsServiceTest.java @@ -0,0 +1,87 @@ +package com.RDS.skilltree.unit; + +import com.RDS.skilltree.Skill.*; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; +import org.springframework.data.domain.Pageable; + +import java.util.Arrays; +import java.util.List; +import java.util.Optional; +import java.util.UUID; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +@RunWith(MockitoJUnitRunner.class) +public class SkillsServiceTest { + + @Mock + private SkillRepository skillRepository; + + @InjectMocks + @Autowired + private SkillsServiceImpl skillService; + + + @Test + public void testGetSkillById() { + UUID skillId = UUID.randomUUID(); + SkillModel skillModel = SkillModel.builder() + .id(skillId) + .build(); + + + when(skillRepository.findById(skillId)).thenReturn(Optional.of(skillModel)); + + SkillDTO result = skillService.getSkillById(skillId); + assertNotNull(result); + assertEquals("The skill Id doesn't matches the expected skillId", skillId, result.getId()); + } + + @Test + public void testGetSkillsByName() { + String skillName = "Java"; + SkillModel skillModel = SkillModel.builder() + .name(skillName) + .build(); + + when(skillRepository.findByName(skillName)).thenReturn(Optional.of(skillModel)); + + SkillDTO result = skillService.getSkillByName("Java"); + assertEquals(result.getName(), skillName); + } + +// @Test +// public void testCreateSkill(){ +// +// } + + @Test + public void testGetAllSkills(){ + List skillModelList = Arrays.asList( + SkillModel.builder().name("Java") + .type(SkillType.ATOMIC) + .build(), + SkillModel.builder() + .name("Go") + .type(SkillType.ATOMIC) + .build() + ); + + when(skillRepository.findAll((Pageable) any(Pageable.class))) + .thenReturn(new PageImpl<>(skillModelList)); + + Page resultPage = skillService.getAllSkills(Pageable.unpaged()); + assertEquals(skillModelList.size(), resultPage.getTotalElements()); + assertEquals(skillModelList.size(), resultPage.getContent().size()); + } +} From 2ea1f05a563c67b2ea947f962da0ca1fc83a60ba Mon Sep 17 00:00:00 2001 From: vikhyat187 Date: Mon, 20 Nov 2023 22:17:30 +0530 Subject: [PATCH 05/22] Removing commented code and removing @builder annotation --- .../com/RDS/skilltree/Skill/SkillDRO.java | 11 +++---- .../com/RDS/skilltree/Skill/SkillModel.java | 2 -- .../RDS/skilltree/Skill/SkillsController.java | 2 +- .../EndorsementsIntegrationTests.java | 31 ------------------- .../RDS/skilltree/unit/SkillsServiceTest.java | 29 +++++++---------- 5 files changed, 17 insertions(+), 58 deletions(-) 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 55cc2543..90a9c11b 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 @@ -19,11 +19,10 @@ public class SkillDRO { private UUID createdBy; - static SkillModel toModel(SkillDRO skillDRO) { - return SkillModel.builder() - .name(skillDRO.getName()) - .type(skillDRO.getType()) - .deleted(false) - .build(); + public static SkillModel toModel(SkillDRO skillDRO) { + SkillModel skillModel = new SkillModel(); + skillModel.setName(skillDRO.getName()); + skillModel.setType(skillDRO.getType()); + return skillModel; } } 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 32cd7ef5..79ccff4c 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 @@ -14,8 +14,6 @@ @EqualsAndHashCode(callSuper = true) @Entity @NoArgsConstructor -@AllArgsConstructor -@Builder @Data @Table(name = "Skill") public class SkillModel extends TrackedProperties { 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 4b466515..ea062f6f 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 @@ -18,7 +18,7 @@ public SkillsController(SkillsService skillsService){ } @PostMapping("/") - public String createSkill(@RequestBody(required = true) SkillDRO skillDRO){ + public String createSkill(@RequestBody(required = true) SkillDRO skillDRO){ return skillsService.createSkill(skillDRO); } diff --git a/skill-tree /src/test/java/com/RDS/skilltree/EndorsementsIntegrationTests.java b/skill-tree /src/test/java/com/RDS/skilltree/EndorsementsIntegrationTests.java index 3c00bdcf..f16acf1c 100644 --- a/skill-tree /src/test/java/com/RDS/skilltree/EndorsementsIntegrationTests.java +++ b/skill-tree /src/test/java/com/RDS/skilltree/EndorsementsIntegrationTests.java @@ -101,37 +101,6 @@ public void testAPIReturns400_OnInvalidStatusPassed() { .body("message", equalTo("Invalid status passed")); } - @Test - @Disabled - @DisplayName("Return 200 on endorsement creation") - public void testAPIReturns200_OnEndorsementCreation() throws JsonProcessingException { - -// String requestBody = objectMapper.writeValueAsString(EndorsementCreationRequest.builder() -// .endorserUserId("u-1") -// .endorseeUserId("u-2") -// .skillName("Java") -// .description("This is endorsed for the skill showcased in Skill tree") -// .endorsementType(Type.POSITIVE) -// .build()); - -// Response response = given() -// .contentType("application/json") -// .body(requestBody) -// .post("/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].endorserId", 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") 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 bd50bdbf..f5bfe8ff 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 @@ -35,9 +35,8 @@ public class SkillsServiceTest { @Test public void testGetSkillById() { UUID skillId = UUID.randomUUID(); - SkillModel skillModel = SkillModel.builder() - .id(skillId) - .build(); + SkillModel skillModel = new SkillModel(); + skillModel.setId(skillId); when(skillRepository.findById(skillId)).thenReturn(Optional.of(skillModel)); @@ -50,9 +49,8 @@ public void testGetSkillById() { @Test public void testGetSkillsByName() { String skillName = "Java"; - SkillModel skillModel = SkillModel.builder() - .name(skillName) - .build(); + SkillModel skillModel = new SkillModel(); + skillModel.setName(skillName); when(skillRepository.findByName(skillName)).thenReturn(Optional.of(skillModel)); @@ -60,21 +58,16 @@ public void testGetSkillsByName() { assertEquals(result.getName(), skillName); } -// @Test -// public void testCreateSkill(){ -// -// } - @Test public void testGetAllSkills(){ + SkillModel skillJava = new SkillModel(); + skillJava.setName("Java"); + + SkillModel skillGo = new SkillModel(); + skillGo.setName("Go"); + List skillModelList = Arrays.asList( - SkillModel.builder().name("Java") - .type(SkillType.ATOMIC) - .build(), - SkillModel.builder() - .name("Go") - .type(SkillType.ATOMIC) - .build() + skillJava, skillGo ); when(skillRepository.findAll((Pageable) any(Pageable.class))) From d64e892f1c3f2c12d61a83d057487abc032e1334 Mon Sep 17 00:00:00 2001 From: vikhyat187 Date: Mon, 20 Nov 2023 22:30:38 +0530 Subject: [PATCH 06/22] Removing the constructor using the builder instead --- .../java/com/RDS/skilltree/Skill/SkillDRO.java | 9 +++++---- .../java/com/RDS/skilltree/Skill/SkillModel.java | 7 ++----- .../RDS/skilltree/unit/SkillsServiceTest.java | 16 +++++++--------- 3 files changed, 14 insertions(+), 18 deletions(-) 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 90a9c11b..4b589c77 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 @@ -20,9 +20,10 @@ public class SkillDRO { public static SkillModel toModel(SkillDRO skillDRO) { - SkillModel skillModel = new SkillModel(); - skillModel.setName(skillDRO.getName()); - skillModel.setType(skillDRO.getType()); - return skillModel; + return SkillModel.builder() + .name(skillDRO.getName()) + .type(skillDRO.getType()) + .deleted(false) + .build(); } } 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 79ccff4c..f582485f 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 @@ -13,6 +13,8 @@ @EqualsAndHashCode(callSuper = true) @Entity +@Builder +@AllArgsConstructor @NoArgsConstructor @Data @Table(name = "Skill") @@ -36,9 +38,4 @@ public class SkillModel extends TrackedProperties { @ManyToMany(mappedBy = "skills", cascade = CascadeType.ALL, fetch = FetchType.LAZY) private Set users; - public SkillModel(String name, SkillType type) { - this.name = name; - this.type = type; - this.deleted = false; - } } 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 f5bfe8ff..919dc20b 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 @@ -35,9 +35,7 @@ public class SkillsServiceTest { @Test public void testGetSkillById() { UUID skillId = UUID.randomUUID(); - SkillModel skillModel = new SkillModel(); - skillModel.setId(skillId); - + SkillModel skillModel = SkillModel.builder().id(skillId).build(); when(skillRepository.findById(skillId)).thenReturn(Optional.of(skillModel)); @@ -49,8 +47,8 @@ public void testGetSkillById() { @Test public void testGetSkillsByName() { String skillName = "Java"; - SkillModel skillModel = new SkillModel(); - skillModel.setName(skillName); + SkillModel skillModel = SkillModel.builder() + .name("Java").build(); when(skillRepository.findByName(skillName)).thenReturn(Optional.of(skillModel)); @@ -60,11 +58,11 @@ public void testGetSkillsByName() { @Test public void testGetAllSkills(){ - SkillModel skillJava = new SkillModel(); - skillJava.setName("Java"); + SkillModel skillJava = SkillModel.builder() + .name("Java").build(); - SkillModel skillGo = new SkillModel(); - skillGo.setName("Go"); + SkillModel skillGo = SkillModel.builder() + .name("Go").build(); List skillModelList = Arrays.asList( skillJava, skillGo From 7a56cc8f0b8bb212a96d340901e39d565bf6f91a Mon Sep 17 00:00:00 2001 From: vikhyat187 Date: Thu, 23 Nov 2023 06:10:36 +0530 Subject: [PATCH 07/22] Adding error message incase of response not matching expected one --- .../src/test/java/com/RDS/skilltree/unit/SkillsServiceTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 919dc20b..4a11e6aa 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 @@ -53,7 +53,7 @@ public void testGetSkillsByName() { when(skillRepository.findByName(skillName)).thenReturn(Optional.of(skillModel)); SkillDTO result = skillService.getSkillByName("Java"); - assertEquals(result.getName(), skillName); + assertEquals("The skill name doesn't match the expected skill name", result.getName(), skillName); } @Test From b1cafd00d62bc2585d257bbb9d5f1c247e4405c1 Mon Sep 17 00:00:00 2001 From: vikhyat187 Date: Mon, 27 Nov 2023 13:50:44 +0530 Subject: [PATCH 08/22] Removing AllArgs and NoArgs constructor and checking the paginated results in the tests --- .../main/java/com/RDS/skilltree/Skill/SkillDRO.java | 8 ++------ .../main/java/com/RDS/skilltree/Skill/SkillDTO.java | 8 ++------ .../com/RDS/skilltree/unit/SkillsServiceTest.java | 11 +++++++++-- 3 files changed, 13 insertions(+), 14 deletions(-) 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 4b589c77..e2141304 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,16 +1,12 @@ package com.RDS.skilltree.Skill; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import lombok.AllArgsConstructor; import lombok.Builder; -import lombok.Data; -import lombok.NoArgsConstructor; +import lombok.Getter; import java.util.UUID; -@Data -@AllArgsConstructor -@NoArgsConstructor +@Getter @Builder @JsonIgnoreProperties(ignoreUnknown = true) public class SkillDRO { 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 5bb319b1..2ed29829 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,17 +2,13 @@ import com.RDS.skilltree.User.UserModel; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import lombok.AllArgsConstructor; import lombok.Builder; -import lombok.Data; -import lombok.NoArgsConstructor; +import lombok.Getter; import java.util.Set; import java.util.UUID; -@Data -@AllArgsConstructor -@NoArgsConstructor +@Getter @Builder @JsonIgnoreProperties(ignoreUnknown = true) public class SkillDTO { 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 4a11e6aa..176feda3 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 @@ -1,6 +1,9 @@ package com.RDS.skilltree.unit; -import com.RDS.skilltree.Skill.*; +import com.RDS.skilltree.Skill.SkillDTO; +import com.RDS.skilltree.Skill.SkillModel; +import com.RDS.skilltree.Skill.SkillRepository; +import com.RDS.skilltree.Skill.SkillsServiceImpl; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InjectMocks; @@ -9,6 +12,7 @@ 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; import java.util.Arrays; @@ -71,8 +75,11 @@ public void testGetAllSkills(){ when(skillRepository.findAll((Pageable) any(Pageable.class))) .thenReturn(new PageImpl<>(skillModelList)); - Page resultPage = skillService.getAllSkills(Pageable.unpaged()); + Pageable pageable = PageRequest.of(2, 1); + Page resultPage = skillService.getAllSkills(pageable); assertEquals(skillModelList.size(), resultPage.getTotalElements()); assertEquals(skillModelList.size(), resultPage.getContent().size()); + assertEquals("The returned skill on page 0, doesn't match the actual skill", resultPage.getContent().get(0).getName(), "Java"); + assertEquals("The returned skill on page 0, doesn't match the actual skill", resultPage.getContent().get(1).getName(), "Go"); } } From 581a9421b1666264b7373357ea72c2281b580851 Mon Sep 17 00:00:00 2001 From: vikhyat187 Date: Mon, 27 Nov 2023 14:19:47 +0530 Subject: [PATCH 09/22] Remove the @Data from Skill Model --- .../src/main/java/com/RDS/skilltree/Skill/SkillModel.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) 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 89971c70..586f72a6 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 @@ -13,9 +13,7 @@ @EqualsAndHashCode(callSuper = true) @Entity @Builder -@AllArgsConstructor -@NoArgsConstructor -@Data +@Getter @Table(name = "Skill") public class SkillModel extends TrackedProperties { @Id From 18bc102284fb189714056c6f09dc674db76201cb Mon Sep 17 00:00:00 2001 From: vikhyat187 Date: Mon, 27 Nov 2023 19:02:08 +0530 Subject: [PATCH 10/22] Throwing the custom exception incase of skill not found --- .../Exceptions/NoEntityException.java | 16 ++++++++++++++ .../RDS/skilltree/Skill/SkillsController.java | 21 +++++++++++++++---- 2 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 skill-tree /src/main/java/com/RDS/skilltree/Exceptions/NoEntityException.java 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 new file mode 100644 index 00000000..2ac6689f --- /dev/null +++ b/skill-tree /src/main/java/com/RDS/skilltree/Exceptions/NoEntityException.java @@ -0,0 +1,16 @@ +package com.RDS.skilltree.Exceptions; + +public class NoEntityException extends RuntimeException{ + + public NoEntityException(String message) { + super(message); + } + + public NoEntityException(String message, Throwable cause) { + super(message, cause); + } + + public NoEntityException(Throwable cause) { + super(cause); + } +} \ No newline at end of file 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 ea062f6f..cbfca1df 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.Exceptions.NoEntityException; import lombok.extern.slf4j.Slf4j; 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.*; +import java.util.NoSuchElementException; import java.util.UUID; @RestController @@ -31,12 +36,20 @@ public Page getAllSkills( } @GetMapping("/name/{name}") - public SkillDTO getSkillByName(@PathVariable(value = "name", required = true) String name){ - return skillsService.getSkillByName(name); + public ResponseEntity getSkillByName(@PathVariable(value = "name", required = true) String name){ + SkillDTO skillDTO = skillsService.getSkillByName(name); + if (ObjectUtils.isEmpty(skillDTO)){ + throw new NoEntityException("No skill found for given name: "+ name); + } + return ResponseEntity.ok(skillDTO); } @GetMapping("/{id}") - public SkillDTO getSkillById(@PathVariable(value = "id", required = true) UUID id){ - return skillsService.getSkillById(id); + public ResponseEntity getSkillById(@PathVariable(value = "id", required = true) UUID id){ + SkillDTO skillDTO = skillsService.getSkillById(id); + if (ObjectUtils.isEmpty(skillDTO)){ + throw new NoEntityException("No skill found for the given Id"+ id); + } + return ResponseEntity.ok(skillDTO); } } From f4cb172726fddc2412d3a8fd56c534ac4bfda199 Mon Sep 17 00:00:00 2001 From: vikhyat187 Date: Mon, 27 Nov 2023 22:49:27 +0530 Subject: [PATCH 11/22] Increasing the default page size of the request --- .../main/java/com/RDS/skilltree/Skill/SkillsController.java | 2 +- .../test/java/com/RDS/skilltree/unit/SkillsServiceTest.java | 4 ++-- 2 files changed, 3 insertions(+), 3 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 cbfca1df..306605a2 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 @@ -30,7 +30,7 @@ public String createSkill(@RequestBody(required = true) SkillDRO skillDRO){ @GetMapping("/") public Page getAllSkills( @RequestParam(value = "page", defaultValue = "0") int page, - @RequestParam(value = "size", defaultValue = "10") int size) { + @RequestParam(value = "size", defaultValue = "100") int size) { Pageable pageable = PageRequest.of(page, size); return skillsService.getAllSkills(pageable); } 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 176feda3..85474d5c 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 @@ -77,8 +77,8 @@ public void testGetAllSkills(){ Pageable pageable = PageRequest.of(2, 1); Page resultPage = skillService.getAllSkills(pageable); - assertEquals(skillModelList.size(), resultPage.getTotalElements()); - assertEquals(skillModelList.size(), resultPage.getContent().size()); + assertEquals("The number of elements returned is not equal to the expected size",skillModelList.size(), resultPage.getTotalElements()); + assertEquals("The content returned is not equal to the expected content", skillModelList.size(), resultPage.getContent().size()); assertEquals("The returned skill on page 0, doesn't match the actual skill", resultPage.getContent().get(0).getName(), "Java"); assertEquals("The returned skill on page 0, doesn't match the actual skill", resultPage.getContent().get(1).getName(), "Go"); } From 1fa18ee255ec584fe585236709e43c7f92d280c9 Mon Sep 17 00:00:00 2001 From: vikhyat187 Date: Tue, 28 Nov 2023 07:20:26 +0530 Subject: [PATCH 12/22] adding unique key constraint to skill name --- .../src/main/java/com/RDS/skilltree/Skill/SkillModel.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 586f72a6..c93d50fb 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 @@ -21,7 +21,7 @@ public class SkillModel extends TrackedProperties { @Column(name = "id", columnDefinition = "BINARY(16)") private UUID id; - @Column(name = "name", nullable = false) + @Column(name = "name", unique = true, nullable = false) private String name; @Column(name = "skill_type", nullable = false) From 123c2fcc0493310e86decec0463d1c5b57573fae Mon Sep 17 00:00:00 2001 From: vikhyat187 Date: Wed, 29 Nov 2023 06:05:02 +0530 Subject: [PATCH 13/22] Returning an error value with custom message in Http status 404 --- .../main/java/com/RDS/skilltree/Skill/SkillModel.java | 8 ++++++-- .../java/com/RDS/skilltree/Skill/SkillsController.java | 10 ++++++---- 2 files changed, 12 insertions(+), 6 deletions(-) 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 c93d50fb..5c228ea9 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 @@ -5,12 +5,16 @@ import com.fasterxml.jackson.annotation.JsonBackReference; import com.fasterxml.jackson.annotation.JsonIgnore; import jakarta.persistence.*; -import lombok.*; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; import java.util.Set; import java.util.UUID; -@EqualsAndHashCode(callSuper = true) +@AllArgsConstructor +@NoArgsConstructor @Entity @Builder @Getter 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 306605a2..03975074 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 @@ -36,18 +36,20 @@ public Page getAllSkills( } @GetMapping("/name/{name}") - public ResponseEntity getSkillByName(@PathVariable(value = "name", required = true) String name){ + public ResponseEntity getSkillByName(@PathVariable(value = "name", required = true) String name){ SkillDTO skillDTO = skillsService.getSkillByName(name); if (ObjectUtils.isEmpty(skillDTO)){ - throw new NoEntityException("No skill found for given name: "+ name); + return ResponseEntity.status(HttpStatus.NOT_FOUND) + .body("Skill not found with the given name"); } return ResponseEntity.ok(skillDTO); } @GetMapping("/{id}") - public ResponseEntity getSkillById(@PathVariable(value = "id", required = true) UUID id){ + public ResponseEntity getSkillById(@PathVariable(value = "id", required = true) UUID id){ SkillDTO skillDTO = skillsService.getSkillById(id); if (ObjectUtils.isEmpty(skillDTO)){ - throw new NoEntityException("No skill found for the given Id"+ id); + return ResponseEntity.status(HttpStatus.NOT_FOUND) + .body("Skill not found with given Id"); } return ResponseEntity.ok(skillDTO); } From f5bd99821ea51450861cc0e9cbf61d0f030be765 Mon Sep 17 00:00:00 2001 From: vikhyat187 Date: Wed, 29 Nov 2023 06:50:54 +0530 Subject: [PATCH 14/22] Added exception handling and logging in case save method fails --- .../RDS/skilltree/Skill/SkillsController.java | 20 +++++++++++++++---- .../skilltree/Skill/SkillsServiceImpl.java | 11 +++++++++- 2 files changed, 26 insertions(+), 5 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 03975074..e491d4dd 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 @@ -2,6 +2,8 @@ import com.RDS.skilltree.Exceptions.NoEntityException; import lombok.extern.slf4j.Slf4j; +import org.apache.coyote.Response; +import org.springframework.dao.DataIntegrityViolationException; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; @@ -10,6 +12,7 @@ import org.springframework.util.ObjectUtils; import org.springframework.web.bind.annotation.*; +import java.sql.SQLIntegrityConstraintViolationException; import java.util.NoSuchElementException; import java.util.UUID; @@ -23,8 +26,17 @@ public SkillsController(SkillsService skillsService){ } @PostMapping("/") - public String createSkill(@RequestBody(required = true) SkillDRO skillDRO){ - return skillsService.createSkill(skillDRO); + public ResponseEntity createSkill(@RequestBody(required = true) SkillDRO skillDRO){ + if (skillDRO.getCreatedBy() == null || skillDRO.getType() == null || skillDRO.getName() == null){ + return ResponseEntity.status(HttpStatus.BAD_REQUEST) + .body("CreatedBy, Type and Name are mandatory values, anyone cannot be null"); + } + try { + return ResponseEntity.status(HttpStatus.OK).body(skillsService.createSkill(skillDRO)); + } catch(DataIntegrityViolationException ex){ + return ResponseEntity.status(HttpStatus.BAD_REQUEST) + .body("Cannot create entry for Skill as Skill name is duplicate"); + } } @GetMapping("/") @@ -36,7 +48,7 @@ public Page getAllSkills( } @GetMapping("/name/{name}") - public ResponseEntity getSkillByName(@PathVariable(value = "name", required = true) String 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) @@ -45,7 +57,7 @@ public ResponseEntity getSkillByName(@PathVariable(value = "name", required = tr return ResponseEntity.ok(skillDTO); } @GetMapping("/{id}") - public ResponseEntity getSkillById(@PathVariable(value = "id", required = true) UUID 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) 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 f72c1af3..8e391ff5 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 @@ -3,10 +3,13 @@ import com.RDS.skilltree.User.UserModel; import com.RDS.skilltree.User.UserRepository; import lombok.NoArgsConstructor; +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; +import java.sql.SQLIntegrityConstraintViolationException; import java.time.Instant; import java.util.List; import java.util.Optional; @@ -14,6 +17,7 @@ import java.util.stream.Collectors; @Service +@Slf4j public class SkillsServiceImpl implements SkillsService{ private final SkillRepository skillRepository; private final UserRepository userRepository; @@ -49,7 +53,12 @@ public String createSkill(SkillDRO skillDRO){ UserModel user = userRepository.findById(skillDRO.getCreatedBy()).get(); newSkill.setUpdatedBy(user); newSkill.setCreatedBy(user); - skillRepository.save(newSkill); + 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 "Success"; } } From c7cd65753932a97b3fbb9a4296e582b42c3221ab Mon Sep 17 00:00:00 2001 From: vikhyat187 Date: Wed, 29 Nov 2023 06:55:48 +0530 Subject: [PATCH 15/22] Changing the http status code for created / conflicts --- .../main/java/com/RDS/skilltree/Skill/SkillsController.java | 4 ++-- 1 file changed, 2 insertions(+), 2 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 e491d4dd..74af6b7d 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 @@ -32,9 +32,9 @@ public ResponseEntity createSkill(@RequestBody(required = true) SkillDRO skil .body("CreatedBy, Type and Name are mandatory values, anyone cannot be null"); } try { - return ResponseEntity.status(HttpStatus.OK).body(skillsService.createSkill(skillDRO)); + return ResponseEntity.status(HttpStatus.CREATED).body(skillsService.createSkill(skillDRO)); } catch(DataIntegrityViolationException ex){ - return ResponseEntity.status(HttpStatus.BAD_REQUEST) + return ResponseEntity.status(HttpStatus.CONFLICT) .body("Cannot create entry for Skill as Skill name is duplicate"); } } From fa6ca4e4903df8ccf35e2cf2a711bef57749b8ab Mon Sep 17 00:00:00 2001 From: vikhyat187 Date: Wed, 29 Nov 2023 07:00:50 +0530 Subject: [PATCH 16/22] Returning Skills object to the user --- .../src/main/java/com/RDS/skilltree/Skill/SkillsService.java | 2 +- .../main/java/com/RDS/skilltree/Skill/SkillsServiceImpl.java | 4 ++-- 2 files changed, 3 insertions(+), 3 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 09a581f4..81aea4f4 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 @@ -9,5 +9,5 @@ public interface SkillsService { SkillDTO getSkillById(UUID id); SkillDTO getSkillByName(String skillName); Page getAllSkills(Pageable pageable); - String createSkill(SkillDRO skillDRO); + 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 index 8e391ff5..ab3671c3 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 @@ -46,7 +46,7 @@ public Page getAllSkills(Pageable pageable){ } @Override - public String createSkill(SkillDRO skillDRO){ + public SkillDTO createSkill(SkillDRO skillDRO){ SkillModel newSkill = SkillDRO.toModel(skillDRO); newSkill.setCreatedAt(Instant.now()); newSkill.setUpdatedAt(Instant.now()); @@ -59,6 +59,6 @@ public String createSkill(SkillDRO skillDRO){ log.error("Error saving the skills object with name : {}, with exception :{}", skillDRO.getName(), ex.getMessage(), ex); throw ex; } - return "Success"; + return SkillDTO.toDto(newSkill); } } From 17059f7ff2862f575d84972577efa331a7e519a4 Mon Sep 17 00:00:00 2001 From: vikhyat187 Date: Tue, 5 Dec 2023 22:23:09 +0530 Subject: [PATCH 17/22] Storing UserDTO in SkillDRO instead of UserModel --- .../com/RDS/skilltree/Skill/SkillDTO.java | 20 +++++- .../RDS/skilltree/Skill/SkillsController.java | 4 -- .../skilltree/Skill/SkillsServiceImpl.java | 10 +-- .../java/com/RDS/skilltree/User/UserDRO.java | 69 +++++++++++++++++++ .../java/com/RDS/skilltree/User/UserDTO.java | 58 ++++++++++++++++ .../com/RDS/skilltree/User/UserModel.java | 19 ++--- 6 files changed, 154 insertions(+), 26 deletions(-) create mode 100644 skill-tree /src/main/java/com/RDS/skilltree/User/UserDRO.java create mode 100644 skill-tree /src/main/java/com/RDS/skilltree/User/UserDTO.java 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 2ed29829..0a18875f 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,13 @@ package com.RDS.skilltree.Skill; -import com.RDS.skilltree.User.UserModel; +import com.RDS.skilltree.User.UserDTO; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import lombok.Builder; import lombok.Getter; import java.util.Set; import java.util.UUID; +import java.util.stream.Collectors; @Getter @Builder @@ -15,14 +16,27 @@ public class SkillDTO { private UUID id; private SkillType type; private String name; - private Set users; + private Set users; public static SkillDTO toDto(SkillModel skillModel) { return SkillDTO.builder() .id(skillModel.getId()) .name(skillModel.getName()) .type(skillModel.getType()) - .users(skillModel.getUsers()) + .build(); + } + + public static SkillDTO getSkillsWithUsers(SkillModel skillModel) { + Set users = skillModel.getUsers() + .stream() + .map(UserDTO::toDTO) + .collect(Collectors.toSet()); + + return SkillDTO.builder() + .id(skillModel.getId()) + .name(skillModel.getName()) + .type(skillModel.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 index 74af6b7d..a7885480 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,8 +1,6 @@ package com.RDS.skilltree.Skill; -import com.RDS.skilltree.Exceptions.NoEntityException; import lombok.extern.slf4j.Slf4j; -import org.apache.coyote.Response; import org.springframework.dao.DataIntegrityViolationException; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; @@ -12,8 +10,6 @@ import org.springframework.util.ObjectUtils; import org.springframework.web.bind.annotation.*; -import java.sql.SQLIntegrityConstraintViolationException; -import java.util.NoSuchElementException; import java.util.UUID; @RestController 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 ab3671c3..2e51656a 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 @@ -2,19 +2,15 @@ import com.RDS.skilltree.User.UserModel; import com.RDS.skilltree.User.UserRepository; -import lombok.NoArgsConstructor; 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; -import java.sql.SQLIntegrityConstraintViolationException; import java.time.Instant; -import java.util.List; import java.util.Optional; import java.util.UUID; -import java.util.stream.Collectors; @Service @Slf4j @@ -30,19 +26,19 @@ public SkillsServiceImpl(SkillRepository skillRepository, @Override public SkillDTO getSkillById(UUID id){ Optional skillModel = skillRepository.findById(id); - return skillModel.map(SkillDTO::toDto).orElse(null); + return skillModel.map(SkillDTO::getSkillsWithUsers).orElse(null); } @Override public SkillDTO getSkillByName(String skillName){ Optional skillModel = skillRepository.findByName(skillName); - return skillModel.map(SkillDTO::toDto).orElse(null); + return skillModel.map(SkillDTO::getSkillsWithUsers).orElse(null); } @Override public Page getAllSkills(Pageable pageable){ Page skillModels = skillRepository.findAll(pageable); - return skillModels.map(SkillDTO::toDto); + return skillModels.map(SkillDTO::getSkillsWithUsers); } @Override 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 new file mode 100644 index 00000000..26e747de --- /dev/null +++ b/skill-tree /src/main/java/com/RDS/skilltree/User/UserDRO.java @@ -0,0 +1,69 @@ +package com.RDS.skilltree.User; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; +import org.hibernate.usertype.UserType; + +import java.net.URL; +import java.time.Instant; + + +@AllArgsConstructor +@NoArgsConstructor +@Data +@Builder +public class UserDRO { + private String rdsUserId; + + private String firstName; + + private String lastName; + + private URL imageUrl; + + private UserRole role; + + 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(); + } + + 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(); + } + + 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()); + } + user.setUpdatedAt(Instant.now()); + user.setUpdatedBy(user); + 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 new file mode 100644 index 00000000..2f82aba1 --- /dev/null +++ b/skill-tree /src/main/java/com/RDS/skilltree/User/UserDTO.java @@ -0,0 +1,58 @@ +package com.RDS.skilltree.User; + +import com.RDS.skilltree.Skill.SkillDTO; +import lombok.Builder; +import lombok.Getter; + +import java.net.URL; +import java.util.Set; +import java.util.UUID; +import java.util.stream.Collectors; + +@Getter +@Builder +public class UserDTO { + + private UUID id; + + private String rdsUserId; + + private String firstName; + + private String lastName; + + private URL imageUrl; + + private UserRole role; + + private Set skills; + + 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()); + + return UserDTO.builder() + .id(user.getId()) + .rdsUserId(user.getRdsUserId()) + .firstName(user.getFirstName()) + .lastName(user.getLastName()) + .imageUrl(user.getImageUrl()) + .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 93e96c0a..7db15008 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,17 +5,19 @@ import com.fasterxml.jackson.annotation.JsonManagedReference; import com.fasterxml.jackson.databind.annotation.JsonSerialize; import jakarta.persistence.*; -import lombok.Data; -import lombok.NoArgsConstructor; +import lombok.*; import java.net.URL; import java.util.Set; import java.util.UUID; @Entity -@NoArgsConstructor -@Data +@Getter +@Setter +@Builder @JsonSerialize +@NoArgsConstructor +@AllArgsConstructor @Table(name = "Users") public class UserModel extends TrackedProperties { @Id @@ -23,7 +25,7 @@ public class UserModel extends TrackedProperties { @Column(name = "id", columnDefinition = "BINARY(16)") private UUID id; - @Column(name = "rds_user_id") + @Column(name = "rds_user_id", unique = true) private String rdsUserId; @Column(name = "first_name", nullable = false) @@ -44,11 +46,4 @@ public class UserModel extends TrackedProperties { @JoinTable(name = "user_skill", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "skill_id")) private Set skills; - public UserModel(String rdsUserId, String firstName, String lastName, URL imageUrl, UserRole role) { - this.firstName = firstName; - this.lastName = lastName; - this.imageUrl = imageUrl; - this.role = role; - this.rdsUserId = rdsUserId; - } } From 34761d74b0b23e5d996e567b7b9a196b46319d25 Mon Sep 17 00:00:00 2001 From: vikhyat187 Date: Thu, 7 Dec 2023 06:53:08 +0530 Subject: [PATCH 18/22] Wrapping the Response message into a class and returning --- .../java/com/RDS/skilltree/Skill/SkillsController.java | 9 +++++---- .../java/com/RDS/skilltree/utils/MessageResponse.java | 10 ++++++++++ 2 files changed, 15 insertions(+), 4 deletions(-) create mode 100644 skill-tree /src/main/java/com/RDS/skilltree/utils/MessageResponse.java 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 a7885480..a96ab247 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,5 +1,6 @@ package com.RDS.skilltree.Skill; +import com.RDS.skilltree.utils.MessageResponse; import lombok.extern.slf4j.Slf4j; import org.springframework.dao.DataIntegrityViolationException; import org.springframework.data.domain.Page; @@ -25,13 +26,13 @@ public SkillsController(SkillsService skillsService){ public ResponseEntity createSkill(@RequestBody(required = true) SkillDRO skillDRO){ if (skillDRO.getCreatedBy() == null || skillDRO.getType() == null || skillDRO.getName() == null){ return ResponseEntity.status(HttpStatus.BAD_REQUEST) - .body("CreatedBy, Type and Name are mandatory values, anyone cannot be null"); + .body(new MessageResponse("CreatedBy, Type and Name are mandatory values, anyone cannot be null")); } try { return ResponseEntity.status(HttpStatus.CREATED).body(skillsService.createSkill(skillDRO)); } catch(DataIntegrityViolationException ex){ return ResponseEntity.status(HttpStatus.CONFLICT) - .body("Cannot create entry for Skill as Skill name is duplicate"); + .body(new MessageResponse("Cannot create entry for Skill as Skill name is duplicate")); } } @@ -48,7 +49,7 @@ public ResponseEntity getSkillByName(@PathVariable(value = "name", required = SkillDTO skillDTO = skillsService.getSkillByName(name); if (ObjectUtils.isEmpty(skillDTO)){ return ResponseEntity.status(HttpStatus.NOT_FOUND) - .body("Skill not found with the given name"); + .body(new MessageResponse("Skill not found with the given name")); } return ResponseEntity.ok(skillDTO); } @@ -57,7 +58,7 @@ public ResponseEntity getSkillById(@PathVariable(value = "id", required = tru SkillDTO skillDTO = skillsService.getSkillById(id); if (ObjectUtils.isEmpty(skillDTO)){ return ResponseEntity.status(HttpStatus.NOT_FOUND) - .body("Skill not found with given Id"); + .body(new MessageResponse("Skill not found with given Id")); } return ResponseEntity.ok(skillDTO); } diff --git a/skill-tree /src/main/java/com/RDS/skilltree/utils/MessageResponse.java b/skill-tree /src/main/java/com/RDS/skilltree/utils/MessageResponse.java new file mode 100644 index 00000000..979b262d --- /dev/null +++ b/skill-tree /src/main/java/com/RDS/skilltree/utils/MessageResponse.java @@ -0,0 +1,10 @@ +package com.RDS.skilltree.utils; + +import lombok.AllArgsConstructor; +import lombok.Data; + +@AllArgsConstructor +@Data +public class MessageResponse { + private String message; +} \ No newline at end of file From bb4f2bad79da7b3748674950e9931a0ef73e4d89 Mon Sep 17 00:00:00 2001 From: vikhyat187 Date: Sun, 10 Dec 2023 13:49:23 +0530 Subject: [PATCH 19/22] Added the validations on request object using @Valid --- skill-tree /pom.xml | 4 ++++ .../main/java/com/RDS/skilltree/Skill/SkillDRO.java | 6 ++++++ .../com/RDS/skilltree/Skill/SkillsController.java | 11 ++++++----- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/skill-tree /pom.xml b/skill-tree /pom.xml index dd5a1079..bbab105b 100644 --- a/skill-tree /pom.xml +++ b/skill-tree /pom.xml @@ -40,6 +40,10 @@ spring-boot-starter-test test + + org.springframework.boot + spring-boot-starter-validation + io.rest-assured rest-assured 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 e2141304..3e1c8b1e 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,6 +1,7 @@ package com.RDS.skilltree.Skill; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import jakarta.validation.constraints.NotNull; import lombok.Builder; import lombok.Getter; @@ -10,8 +11,13 @@ @Builder @JsonIgnoreProperties(ignoreUnknown = true) public class SkillDRO { + @NotNull(message = "Name cannot be null") private String name; + + @NotNull(message = "SkillType cannot be null") private SkillType type; + + @NotNull(message = "Created by user Id cannot be null") private UUID createdBy; 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 a96ab247..5199b1ba 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,6 +1,7 @@ package com.RDS.skilltree.Skill; import com.RDS.skilltree.utils.MessageResponse; +import jakarta.validation.Valid; import lombok.extern.slf4j.Slf4j; import org.springframework.dao.DataIntegrityViolationException; import org.springframework.data.domain.Page; @@ -18,21 +19,21 @@ @RequestMapping("/v1/skills") public class SkillsController { private final SkillsService skillsService; + public SkillsController(SkillsService skillsService){ this.skillsService = skillsService; } @PostMapping("/") - public ResponseEntity createSkill(@RequestBody(required = true) SkillDRO skillDRO){ - if (skillDRO.getCreatedBy() == null || skillDRO.getType() == null || skillDRO.getName() == null){ - return ResponseEntity.status(HttpStatus.BAD_REQUEST) - .body(new MessageResponse("CreatedBy, Type and Name are mandatory values, anyone cannot be null")); - } + 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; } } From 2a4d04bae803e4ccb450c4d8c8f0cac456b1e7a4 Mon Sep 17 00:00:00 2001 From: vikhyat187 Date: Tue, 12 Dec 2023 08:35:05 +0530 Subject: [PATCH 20/22] Using the @AllArgs to reduce boilerplatecode --- .../skilltree/Skill/SkillsServiceImpl.java | 19 ++--- .../com/RDS/skilltree/User/UserService.java | 19 +++++ .../RDS/skilltree/User/UserServiceImpl.java | 73 +++++++++++++++++++ 3 files changed, 100 insertions(+), 11 deletions(-) create mode 100644 skill-tree /src/main/java/com/RDS/skilltree/User/UserService.java create mode 100644 skill-tree /src/main/java/com/RDS/skilltree/User/UserServiceImpl.java 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 2e51656a..928452f5 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 @@ -2,6 +2,7 @@ import com.RDS.skilltree.User.UserModel; import com.RDS.skilltree.User.UserRepository; +import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.dao.DataIntegrityViolationException; import org.springframework.data.domain.Page; @@ -14,35 +15,31 @@ @Service @Slf4j -public class SkillsServiceImpl implements SkillsService{ +@AllArgsConstructor +public class SkillsServiceImpl implements SkillsService { private final SkillRepository skillRepository; private final UserRepository userRepository; - public SkillsServiceImpl(SkillRepository skillRepository, - UserRepository userRepository){ - this.userRepository = userRepository; - this.skillRepository = skillRepository; - } @Override - public SkillDTO getSkillById(UUID id){ + public SkillDTO getSkillById(UUID id) { Optional skillModel = skillRepository.findById(id); return skillModel.map(SkillDTO::getSkillsWithUsers).orElse(null); } @Override - public SkillDTO getSkillByName(String skillName){ + public SkillDTO getSkillByName(String skillName) { Optional skillModel = skillRepository.findByName(skillName); return skillModel.map(SkillDTO::getSkillsWithUsers).orElse(null); } @Override - public Page getAllSkills(Pageable pageable){ + public Page getAllSkills(Pageable pageable) { Page skillModels = skillRepository.findAll(pageable); return skillModels.map(SkillDTO::getSkillsWithUsers); } @Override - public SkillDTO createSkill(SkillDRO skillDRO){ + public SkillDTO createSkill(SkillDRO skillDRO) { SkillModel newSkill = SkillDRO.toModel(skillDRO); newSkill.setCreatedAt(Instant.now()); newSkill.setUpdatedAt(Instant.now()); @@ -51,7 +48,7 @@ public SkillDTO createSkill(SkillDRO skillDRO){ newSkill.setCreatedBy(user); try { skillRepository.save(newSkill); - } catch(DataIntegrityViolationException ex){ + } catch (DataIntegrityViolationException ex) { log.error("Error saving the skills object with name : {}, with exception :{}", skillDRO.getName(), ex.getMessage(), ex); throw ex; } 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 new file mode 100644 index 00000000..082c240c --- /dev/null +++ b/skill-tree /src/main/java/com/RDS/skilltree/User/UserService.java @@ -0,0 +1,19 @@ +//package com.RDS.skilltree.User; +// +// +//import org.springframework.stereotype.Service; +// +//import java.util.List; +//import java.util.UUID; +// +//public interface UserService { +// UserDTO createUser(UserDRO user); +// +// void updateUser(UUID id,UserDRO user); +// +// UserDTO getUserById(UUID id); +// +// List getAllUsers(); +// +// void addSkill(UUID skill, UUID userId); +//} \ No newline at end of file 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 new file mode 100644 index 00000000..b61aa9a0 --- /dev/null +++ b/skill-tree /src/main/java/com/RDS/skilltree/User/UserServiceImpl.java @@ -0,0 +1,73 @@ +//package com.RDS.skilltree.User; +// +//import com.RDS.skilltree.Exceptions.NoEntityException; +//import com.RDS.skilltree.Skill.SkillModel; +//import com.RDS.skilltree.Skill.SkillRepository; +//import jakarta.transaction.Transactional; +//import org.springframework.stereotype.Service; +// +//import java.util.Collections; +//import java.util.List; +//import java.util.Optional; +//import java.util.UUID; +// +//@Service +//public class UserServiceImpl implements UserService{ +// private final UserRepository userRepository; +// private final SkillRepository skillRepository; +// public UserServiceImpl(UserRepository userRepository, SkillRepository skillRepository){ +// this.userRepository = userRepository; +// this.skillRepository = skillRepository; +// } +// +// @Override +// public UserDTO createUser(UserDRO user) { +// UserModel userModel = UserDRO.toModel(user); +// userRepository.save(userModel); +// return UserDTO.toDTO(userModel); +// } +// +// @Override +// public void updateUser(UUID id, UserDRO user) { +// +// } +// +// @Override +// public UserDTO getUserById(UUID id) { +// Optional userModel = userRepository.findById(id); +// return userModel.map(UserDTO::getUsersWithSkills).orElse(null); +// } +// +// @Override +// public List getAllUsers() { +// return null; +// } +// +// /** +// * updates the user and skill both +// * @param skillId +// * @param userId +// */ +// @Override +// @Transactional +// public void addSkill(UUID skillId, UUID userId) { +// Optional userOptional = userRepository.findById(userId); +// Optional skillOptional = skillRepository.findById(skillId); +// +// if(userOptional.isPresent() && skillOptional.isPresent()){ +// UserModel userModel = userOptional.get(); +// SkillModel skillModel = skillOptional.get(); +// +// userModel.getSkills().add(skillModel); +// skillModel.getUsers().add(userModel); +// +// userRepository.save(userModel); +// skillRepository.save(skillModel); +// } else { +// if(skillOptional.isEmpty()){ +// throw new NoEntityException("Skill Id is not passed in the input"); +// } +// throw new NoEntityException("User with Id doesn't exists"); +// } +// } +//} From 8b759f057dc9e4197a82ea5abadb1db5c92ffa55 Mon Sep 17 00:00:00 2001 From: vikhyat187 Date: Tue, 12 Dec 2023 08:36:11 +0530 Subject: [PATCH 21/22] Revert "Merge branch 'skills-api' of https://github.com/Real-Dev-Squad/skill-tree-backend into skills-api" This reverts commit 9074902dd15927f85eb063a42aa83449fc53cbdc, reversing changes made to bb4f2bad79da7b3748674950e9931a0ef73e4d89. --- .dockerignore | 34 ---------------------------------- compose.yaml | 21 --------------------- 2 files changed, 55 deletions(-) delete mode 100644 .dockerignore delete mode 100644 compose.yaml diff --git a/.dockerignore b/.dockerignore deleted file mode 100644 index 3edb0b5e..00000000 --- a/.dockerignore +++ /dev/null @@ -1,34 +0,0 @@ -# Include any files or directories that you don't want to be copied to your -# container here (e.g., local build artifacts, temporary files, etc.). -# -# For more help, visit the .dockerignore file reference guide at -# https://docs.docker.com/engine/reference/builder/#dockerignore-file - -**/.DS_Store -**/__pycache__ -**/.venv -**/.classpath -**/.dockerignore -**/.env -**/.git -**/.gitignore -**/.project -**/.settings -**/.toolstarget -**/.vs -**/.vscode -**/*.*proj.user -**/*.dbmdl -**/*.jfm -**/bin -**/charts -**/docker-compose* -**/compose* -**/Dockerfile* -**/node_modules -**/npm-debug.log -**/obj -**/secrets.dev.yaml -**/values.dev.yaml -LICENSE -README.md diff --git a/compose.yaml b/compose.yaml deleted file mode 100644 index 115ac1a7..00000000 --- a/compose.yaml +++ /dev/null @@ -1,21 +0,0 @@ -version: "3.7" - -services: - - db: - image: mysql - # NOTE: use of "mysql_native_password" is not recommended: https://dev.mysql.com/doc/refman/8.0/en/upgrading-from-previous-series.html#upgrade-caching-sha2-password - # (this is just an example, not intended to be a production configuration) - command: --default-authentication-plugin=mysql_native_password - ports: - - 3306:3306 - environment: - MYSQL_DATABASE: skilltree - MYSQL_USER: testuser - MYSQL_PASSWORD: testpassword - MYSQL_ROOT_PASSWORD: password - - adminer: - image: adminer - ports: - - 9000:8080 \ No newline at end of file From e8c0b2284243b9c4f08c19d353be7e288b714372 Mon Sep 17 00:00:00 2001 From: Vikhyat Bhatnagar <52795644+vikhyat187@users.noreply.github.com> Date: Tue, 12 Dec 2023 08:41:56 +0530 Subject: [PATCH 22/22] Removing the constructor and using the AllArgs annotation --- .../java/com/RDS/skilltree/Skill/SkillsServiceImpl.java | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) 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 2e51656a..b9f07426 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 @@ -14,14 +14,10 @@ @Service @Slf4j +@AllArgsConstructor public class SkillsServiceImpl implements SkillsService{ private final SkillRepository skillRepository; private final UserRepository userRepository; - public SkillsServiceImpl(SkillRepository skillRepository, - UserRepository userRepository){ - this.userRepository = userRepository; - this.skillRepository = skillRepository; - } @Override public SkillDTO getSkillById(UUID id){