-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from Real-Dev-Squad/skills-api
Adding the APIs for /skills route (Skills Entity)
- Loading branch information
Showing
19 changed files
with
567 additions
and
80 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
skill-tree /src/main/java/com/RDS/skilltree/Exceptions/NoEntityException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
skill-tree /src/main/java/com/RDS/skilltree/Skill/SkillDRO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.RDS.skilltree.Skill; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import jakarta.validation.constraints.NotNull; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
import java.util.UUID; | ||
|
||
@Getter | ||
@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; | ||
|
||
|
||
public static SkillModel toModel(SkillDRO skillDRO) { | ||
return SkillModel.builder() | ||
.name(skillDRO.getName()) | ||
.type(skillDRO.getType()) | ||
.deleted(false) | ||
.build(); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
skill-tree /src/main/java/com/RDS/skilltree/Skill/SkillDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.RDS.skilltree.Skill; | ||
|
||
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 | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public class SkillDTO { | ||
private UUID id; | ||
private SkillType type; | ||
private String name; | ||
private Set<UserDTO> users; | ||
|
||
public static SkillDTO toDto(SkillModel skillModel) { | ||
return SkillDTO.builder() | ||
.id(skillModel.getId()) | ||
.name(skillModel.getName()) | ||
.type(skillModel.getType()) | ||
.build(); | ||
} | ||
|
||
public static SkillDTO getSkillsWithUsers(SkillModel skillModel) { | ||
Set<UserDTO> 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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
skill-tree /src/main/java/com/RDS/skilltree/Skill/SkillRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +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.Optional; | ||
import java.util.UUID; | ||
|
||
@Repository | ||
public interface SkillRepository extends JpaRepository<SkillModel, UUID> { | ||
Optional<SkillModel> findByName(String name); | ||
Page<SkillModel> findAll(Pageable pageable); | ||
} |
67 changes: 67 additions & 0 deletions
67
skill-tree /src/main/java/com/RDS/skilltree/Skill/SkillsController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
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; | ||
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.UUID; | ||
|
||
@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<SkillDTO> getAllSkills( | ||
@RequestParam(value = "page", defaultValue = "0") int page, | ||
@RequestParam(value = "size", defaultValue = "100") int size) { | ||
Pageable pageable = PageRequest.of(page, size); | ||
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); | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
skill-tree /src/main/java/com/RDS/skilltree/Skill/SkillsService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.RDS.skilltree.Skill; | ||
|
||
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); | ||
Page<SkillDTO> getAllSkills(Pageable pageable); | ||
SkillDTO createSkill(SkillDRO skillDRO); | ||
} |
57 changes: 57 additions & 0 deletions
57
skill-tree /src/main/java/com/RDS/skilltree/Skill/SkillsServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.RDS.skilltree.Skill; | ||
|
||
import com.RDS.skilltree.User.UserModel; | ||
import com.RDS.skilltree.User.UserRepository; | ||
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; | ||
|
||
import java.time.Instant; | ||
import java.util.Optional; | ||
import java.util.UUID; | ||
|
||
@Service | ||
@Slf4j | ||
@RequiredArgsConstructor | ||
public class SkillsServiceImpl implements SkillsService{ | ||
private final SkillRepository skillRepository; | ||
private final UserRepository userRepository; | ||
|
||
@Override | ||
public SkillDTO getSkillById(UUID id){ | ||
Optional<SkillModel> skillModel = skillRepository.findById(id); | ||
return skillModel.map(SkillDTO::getSkillsWithUsers).orElse(null); | ||
} | ||
|
||
@Override | ||
public SkillDTO getSkillByName(String skillName){ | ||
Optional<SkillModel> skillModel = skillRepository.findByName(skillName); | ||
return skillModel.map(SkillDTO::getSkillsWithUsers).orElse(null); | ||
} | ||
|
||
@Override | ||
public Page<SkillDTO> getAllSkills(Pageable pageable){ | ||
Page<SkillModel> 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()); | ||
UserModel user = userRepository.findById(skillDRO.getCreatedBy()).get(); | ||
newSkill.setUpdatedBy(user); | ||
newSkill.setCreatedBy(user); | ||
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); | ||
} | ||
} |
Oops, something went wrong.