Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding the APIs for /skills route (Skills Entity) #34

Merged
merged 34 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
ca5dd65
APIs for '/skills' endpoint
vikhyat187 Nov 15, 2023
43f2119
Remove logs added for debugging purpose
vikhyat187 Nov 15, 2023
48bae00
Updated the code for GET /skills in paginated form
vikhyat187 Nov 19, 2023
cf548bb
Updated the test for the get paginated skills
vikhyat187 Nov 20, 2023
2ea1f05
Removing commented code and removing @builder annotation
vikhyat187 Nov 20, 2023
d64e892
Removing the constructor using the builder instead
vikhyat187 Nov 20, 2023
6cfd9be
Merge branch 'develop' into skills-api
vikhyat187 Nov 22, 2023
acd229a
Merge branch 'develop' into skills-api
vikhyat187 Nov 23, 2023
7a56cc8
Adding error message incase of response not matching expected one
vikhyat187 Nov 23, 2023
6e482a0
Merge remote-tracking branch 'upstream/skills-api' into skills-api
vikhyat187 Nov 23, 2023
80e1d71
Merge branch 'develop' into skills-api
vikhyat187 Nov 27, 2023
b1cafd0
Removing AllArgs and NoArgs constructor and checking the paginated re…
vikhyat187 Nov 27, 2023
641d96f
Merge the changes from develop
vikhyat187 Nov 27, 2023
581a942
Remove the @Data from Skill Model
vikhyat187 Nov 27, 2023
18bc102
Throwing the custom exception incase of skill not found
vikhyat187 Nov 27, 2023
f4cb172
Increasing the default page size of the request
vikhyat187 Nov 27, 2023
1fa18ee
adding unique key constraint to skill name
vikhyat187 Nov 28, 2023
123c2fc
Returning an error value with custom message in Http status 404
vikhyat187 Nov 29, 2023
f5bd998
Added exception handling and logging in case save method fails
vikhyat187 Nov 29, 2023
c7cd657
Changing the http status code for created / conflicts
vikhyat187 Nov 29, 2023
fa6ca4e
Returning Skills object to the user
vikhyat187 Nov 29, 2023
17059f7
Storing UserDTO in SkillDRO instead of UserModel
vikhyat187 Dec 5, 2023
63cb4af
Merge branch 'develop' into skills-api
vikhyat187 Dec 5, 2023
34761d7
Wrapping the Response message into a class and returning
vikhyat187 Dec 7, 2023
fcadc24
Merge branch 'skills-api' of https://github.com/Real-Dev-Squad/skill-…
vikhyat187 Dec 7, 2023
8c8d9be
Merge branch 'develop' into skills-api
vikhyat187 Dec 10, 2023
bb4f2ba
Added the validations on request object using @Valid
vikhyat187 Dec 10, 2023
9074902
Merge branch 'skills-api' of https://github.com/Real-Dev-Squad/skill-…
vikhyat187 Dec 10, 2023
2a4d04b
Using the @AllArgs to reduce boilerplatecode
vikhyat187 Dec 12, 2023
8b759f0
Revert "Merge branch 'skills-api' of https://github.com/Real-Dev-Squa…
vikhyat187 Dec 12, 2023
e8c0b22
Removing the constructor and using the AllArgs annotation
vikhyat187 Dec 12, 2023
5f857f0
Update with Required Args constructor
vikhyat187 Dec 12, 2023
997050e
Update with Required Args constructor
vikhyat187 Dec 12, 2023
bcc0093
Resolving conflicts
vikhyat187 Dec 12, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
vikhyat187 marked this conversation as resolved.
Show resolved Hide resolved
@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) {
vikhyat187 marked this conversation as resolved.
Show resolved Hide resolved
return SkillDTO.builder()
.id(skillModel.getId())
.name(skillModel.getName())
.type(skillModel.getType())
.build();
}
vikhyat187 marked this conversation as resolved.
Show resolved Hide resolved

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();
}
vikhyat187 marked this conversation as resolved.
Show resolved Hide resolved
}
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("/")
vikhyat187 marked this conversation as resolved.
Show resolved Hide resolved
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);
vikhyat187 marked this conversation as resolved.
Show resolved Hide resolved
}

@GetMapping("/name/{name}")
vikhyat187 marked this conversation as resolved.
Show resolved Hide resolved
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
vikhyat187 marked this conversation as resolved.
Show resolved Hide resolved
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();
vikhyat187 marked this conversation as resolved.
Show resolved Hide resolved
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