Skip to content

Commit

Permalink
Merge pull request #34 from Real-Dev-Squad/skills-api
Browse files Browse the repository at this point in the history
Adding the APIs for /skills route (Skills Entity)
  • Loading branch information
vikhyat187 authored Dec 12, 2023
2 parents 37b559d + bcc0093 commit 7dd6c33
Show file tree
Hide file tree
Showing 19 changed files with 567 additions and 80 deletions.
34 changes: 0 additions & 34 deletions .dockerignore

This file was deleted.

21 changes: 0 additions & 21 deletions compose.yaml

This file was deleted.

4 changes: 4 additions & 0 deletions skill-tree /pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
Expand Down
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 skill-tree /src/main/java/com/RDS/skilltree/Skill/SkillDRO.java
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 skill-tree /src/main/java/com/RDS/skilltree/Skill/SkillDTO.java
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();
}
}
19 changes: 8 additions & 11 deletions skill-tree /src/main/java/com/RDS/skilltree/Skill/SkillModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,27 @@
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.Set;
import java.util.UUID;

@EqualsAndHashCode(callSuper = true)
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Data
@Entity
@Builder
@Getter
@Table(name = "Skill")
public class SkillModel extends TrackedProperties {
@Id
@GeneratedValue
@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)
Expand All @@ -38,9 +40,4 @@ public class SkillModel extends TrackedProperties {
@ManyToMany(mappedBy = "skills", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Set<UserModel> users;

public SkillModel(String name, SkillType type) {
this.name = name;
this.type = type;
this.deleted = false;
}
}
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);
}
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);
}

}
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);
}
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);
}
}
Loading

0 comments on commit 7dd6c33

Please sign in to comment.