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

Adds Get Endorsement End Point #40

Merged
merged 18 commits into from
Dec 16, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
0d9e2d8
Adds Get Endorsement End Point Controller Model changes
heyrandhir Nov 23, 2023
969c496
Merge branch 'develop' into feat/get-endorsement-endpoint
heyrandhir Nov 27, 2023
232d062
Use JSON API Specs format to convert data
heyrandhir Dec 1, 2023
5680e23
remove JsonInclude annotation that wasn't being used
heyrandhir Dec 2, 2023
11717e2
Refactor: Update map structure in convertToHashMap method
heyrandhir Dec 2, 2023
4e0a258
Refactor: Resolve comments by Ajey Krishna
heyrandhir Dec 2, 2023
8341960
Merge branch 'develop' into feat/get-endorsement-endpoint
heyrandhir Dec 5, 2023
c4d1242
Refactor: Resolve comments by AjeyaK Bhavika and Debanjan
heyrandhir Dec 5, 2023
e08f967
Merge branch 'develop' into feat/get-endorsement-endpoint
heyrandhir Dec 5, 2023
512d7da
Merge branch 'feat/get-endorsement-endpoint' of https://github.com/Re…
heyrandhir Dec 5, 2023
73716ab
Refactor: Remove empty file
heyrandhir Dec 5, 2023
c577994
Added required args constructor and removed redundant lines from tests
heyrandhir Dec 14, 2023
21424bf
Merge branch 'develop' into feat/get-endorsement-endpoint
heyrandhir Dec 14, 2023
aea1097
Merge branch 'develop' into feat/get-endorsement-endpoint
heyrandhir Dec 14, 2023
8291aae
convert the models to respective DTO's
heyrandhir Dec 14, 2023
7c1a2f6
Defined types to maintain a consistent format for the Api response
heyrandhir Dec 14, 2023
80e33aa
Resolve comments by vikhyat
heyrandhir Dec 14, 2023
0c295cd
Adds @JsonInclude(JsonInclude.Include.NON_NULL) to improve response c…
heyrandhir Dec 15, 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
heyrandhir marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.RDS.skilltree.Endorsement;

import java.util.Map;

public record ApiResponse<T>(T data, int statusCode, String status, String message) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.RDS.skilltree.Endorsement;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.UUID;

import static com.RDS.skilltree.Endorsement.JsonApiResponseConverter.convertToHashMap;

@RestController
@RequestMapping("/v1/endorsements")
@Slf4j
heyrandhir marked this conversation as resolved.
Show resolved Hide resolved
public class EndorsementController {
private final EndorsementService endorsementService;

public EndorsementController(EndorsementService endorsementService) {
this.endorsementService = endorsementService;
}
heyrandhir marked this conversation as resolved.
Show resolved Hide resolved

@GetMapping("/{id}")
public ResponseEntity<?> getEndorsementById(@PathVariable(value = "id", required = true) String id){
heyrandhir marked this conversation as resolved.
Show resolved Hide resolved
try {
UUID uuid = UUID.fromString(id);
heyrandhir marked this conversation as resolved.
Show resolved Hide resolved
EndorsementDTO response = endorsementService.getEndorsementById(uuid);
Map<String, Object> responseData = convertToHashMap(response);
return ResponseEntity.ok(responseData);

} catch (IllegalArgumentException e) {
heyrandhir marked this conversation as resolved.
Show resolved Hide resolved
String message = "Invalid UUID: " + id;
ApiResponse<String> response = new ApiResponse<>(null, HttpStatus.BAD_REQUEST.value(),HttpStatus.BAD_REQUEST.toString(),message);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response);
} catch (IllegalStateException e) {
String message = e.getMessage();
ApiResponse<String> response = new ApiResponse<>(null, HttpStatus.NOT_FOUND.value(),HttpStatus.NOT_FOUND.toString(),message);
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
} catch (Exception e) {
String message = e.getMessage();
ApiResponse<String> response = new ApiResponse<>(null, HttpStatus.BAD_REQUEST.value(),HttpStatus.BAD_REQUEST.toString(),message);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.RDS.skilltree.Endorsement;

import com.RDS.skilltree.EndorsementList.EndorsementListModel;
import com.RDS.skilltree.Skill.SkillModel;
import com.RDS.skilltree.User.UserModel;
import com.RDS.skilltree.utils.TrackedProperties;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.*;

import java.time.Instant;
import java.util.List;
import java.util.UUID;

@Getter
@Builder
@JsonIgnoreProperties(ignoreUnknown = true)
heyrandhir marked this conversation as resolved.
Show resolved Hide resolved
public class EndorsementDTO extends TrackedProperties {
private UUID id;
private UserModel user;
private SkillModel skill;
heyrandhir marked this conversation as resolved.
Show resolved Hide resolved
private EndorsementStatus status;
private List<EndorsementListModel> endorsersList;
heyrandhir marked this conversation as resolved.
Show resolved Hide resolved

public static EndorsementDTO toDto(EndorsementModel endorsementModel) {
EndorsementDTO endorsementDTO = EndorsementDTO.builder()
.id(endorsementModel.getId())
.user(endorsementModel.getUser())
.skill(endorsementModel.getSkill())
.status(endorsementModel.getStatus())
.endorsersList(endorsementModel.getEndorsersList())
heyrandhir marked this conversation as resolved.
Show resolved Hide resolved
.build();
endorsementDTO.setCreatedAt(endorsementModel.getCreatedAt());
endorsementDTO.setUpdatedAt(endorsementModel.getUpdatedAt());
endorsementDTO.setCreatedBy(endorsementModel.getCreatedBy());
endorsementDTO.setUpdatedBy(endorsementModel.getUpdatedBy());
return endorsementDTO;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@
import com.RDS.skilltree.Skill.SkillModel;
import com.RDS.skilltree.User.UserModel;
import com.RDS.skilltree.utils.TrackedProperties;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.*;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@EqualsAndHashCode(callSuper = true)
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Data
@Entity
@Builder
@Getter
@Table(name = "endorsements")
public class EndorsementModel extends TrackedProperties {
@Id
Expand All @@ -33,15 +33,10 @@ public class EndorsementModel extends TrackedProperties {
private SkillModel skill;

@OneToMany(mappedBy = "endorsement")
@JsonManagedReference
Ajeyakrishna-k marked this conversation as resolved.
Show resolved Hide resolved
private List<EndorsementListModel> endorsersList = new ArrayList<>();

@Column(name = "endorsement_status")
@Enumerated(value = EnumType.STRING)
private EndorsementStatus status = EndorsementStatus.PENDING;

public EndorsementModel(UserModel user, SkillModel skill) {
this.status = EndorsementStatus.PENDING;
this.user = user;
this.skill = skill;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.RDS.skilltree.Endorsement;
import java.util.UUID;

public interface EndorsementService {
EndorsementDTO getEndorsementById(UUID id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.RDS.skilltree.Endorsement;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class EndorsementServiceImpl implements EndorsementService {
private final EndorsementRepository endorsementRepository;

public EndorsementServiceImpl(EndorsementRepository endorsementRepository) {
this.endorsementRepository = endorsementRepository;
}
Ajeyakrishna-k marked this conversation as resolved.
Show resolved Hide resolved

@Override
public EndorsementDTO getEndorsementById(UUID id) throws IllegalStateException {
Optional<EndorsementModel> endorsementModel = endorsementRepository.findById(id);
if (endorsementModel.isPresent()) {
return EndorsementDTO.toDto(endorsementModel.get());
} else {
String errorMessage = "No endorsement with the id " + id + " found";
throw new IllegalStateException(errorMessage);
heyrandhir marked this conversation as resolved.
Show resolved Hide resolved
}
heyrandhir marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package com.RDS.skilltree.Endorsement;
heyrandhir marked this conversation as resolved.
Show resolved Hide resolved

import com.RDS.skilltree.EndorsementList.EndorsementListModel;
import com.RDS.skilltree.Skill.SkillModel;
import com.RDS.skilltree.User.UserModel;

import java.lang.reflect.Field;
import java.util.*;

public class JsonApiResponseConverter {
heyrandhir marked this conversation as resolved.
Show resolved Hide resolved

public static Map<String, Object> convertToHashMap(EndorsementDTO endorsementDTO) {
Map<String, Object> result = new HashMap<>();
Map<String, Object> dataMap = new HashMap<>();

dataMap.put("id", endorsementDTO.getId());
dataMap.put("type", "endorsement");
dataMap.put("links", createLinksMap(endorsementDTO.getId().toString()));
dataMap.put("attributes", createAttributesHashMap(endorsementDTO));
dataMap.put("relationships",createRelationshipsHashMap(endorsementDTO));
dataMap.put("included",createIncludedList(endorsementDTO));
result.put("data", dataMap);
return result;
}

private static Map<String, Object> createLinksMap(String id) {
Map<String, Object> links = new HashMap<>();
links.put("self", "/endorsements/" + id);
return links;
}

private static Map<String, Object> createAttributesHashMap(EndorsementDTO endorsementDTO) {
Map<String, Object> attributes = new HashMap<>();
attributes.put("status", endorsementDTO.getStatus());
attributes.put("createdAt", endorsementDTO.getCreatedAt());
attributes.put("updatedAt", endorsementDTO.getUpdatedAt());
return attributes;
}

private static Map<String, Object> createRelationshipsHashMap(EndorsementDTO endorsementDTO) {
Map<String, Object> relationships = new HashMap<>();

Map<String, Object> userRelationship = getIdHashmap(endorsementDTO.getUser(),"user");
relationships.put("user", userRelationship);


Map<String, Object> skillRelationship = getIdHashmap(endorsementDTO.getSkill(),"skill");
relationships.put("skill", skillRelationship);

Map<String, Object> createdBy = getIdHashmap(endorsementDTO.getCreatedBy(),"user");
relationships.put("createdBy", createdBy);

Map<String, Object> updatedBy = getIdHashmap(endorsementDTO.getUpdatedBy(),"user");
relationships.put("updatedBy", updatedBy);

List<Map<String, Object>> endorsersList = new ArrayList<>();
for (EndorsementListModel endorserListModel : endorsementDTO.getEndorsersList()) {
Map<String, Object> data = new HashMap<>();
data.put("id",endorserListModel.getId());
data.put("type","endorser");
endorsersList.add(data);
}

Map<String, Object> endorseRelationships = new HashMap<>();
endorseRelationships.put("data",endorsersList);
relationships.put("endorsersList",endorseRelationships);

return relationships;
}

private static List<Map<String, Object>> createIncludedList(EndorsementDTO endorsementDTO) {
List<Map<String, Object>> includedList = new ArrayList<>();
includedList.add(extractSkillsAttributes(endorsementDTO.getSkill()));
includedList.add(extractUsersAttributes(endorsementDTO.getUser()));
for (EndorsementListModel endorserListModel : endorsementDTO.getEndorsersList()) {
includedList.add(extractEndorsementListModelAttributes(endorserListModel));
}
return includedList;
}
private static Map<String,Object> extractEndorsementListModelAttributes(EndorsementListModel endorsementListModel) {
Map<String, Object> result = new HashMap<>();

Map<String, Object> attributes = new HashMap<>();
attributes.put("type",endorsementListModel.getType());
attributes.put("deleted",endorsementListModel.isDeleted());
attributes.put("description",endorsementListModel.getDescription());
attributes.put("createdAt",endorsementListModel.getCreatedAt());
attributes.put("updatedAt",endorsementListModel.getUpdatedAt());

Map<String, Object> relationships = new HashMap<>();
relationships.put("endorsersList",getIdHashmap(endorsementListModel.getEndorser(),"user"));
relationships.put("createdBy",getIdHashmap(endorsementListModel.getCreatedBy(),"user"));
relationships.put("updatedBy",getIdHashmap(endorsementListModel.getUpdatedBy(),"user"));

result.put("id",endorsementListModel.getId());
result.put("type","EndorsementList");
result.put("attributes",attributes);
result.put("relationships",relationships);
return result;
}
private static Map<String,Object> extractSkillsAttributes(SkillModel skillModel) {
Map<String, Object> result = new HashMap<>();

Map<String, Object> attributes = new HashMap<>();
attributes.put("name",skillModel.getName());
attributes.put("type",skillModel.getType());
attributes.put("createdAt",skillModel.getCreatedAt());
attributes.put("updatedAt",skillModel.getUpdatedAt());
attributes.put("isDeleted",skillModel.isDeleted());

Map<String, Object> relationships = new HashMap<>();
relationships.put("createdBy",getIdHashmap(skillModel.getCreatedBy(),"user"));
relationships.put("updatedBy",getIdHashmap(skillModel.getUpdatedBy(),"user"));

result.put("id",skillModel.getId());
result.put("type","skill");
result.put("attributes",attributes);
result.put("relationships",relationships);

return result;
}
private static Map<String,Object> extractUsersAttributes(UserModel userModel) {
Map<String, Object> result = new HashMap<>();

Map<String, Object> attributes = new HashMap<>();
attributes.put("firstName",userModel.getFirstName());
attributes.put("lastName",userModel.getLastName());
attributes.put("rdsUserId",userModel.getRdsUserId());
attributes.put("role",userModel.getRole());
attributes.put("imageUrl",userModel.getImageUrl());
attributes.put("createdAt",userModel.getCreatedAt());
attributes.put("updatedAt",userModel.getUpdatedAt());

Map<String, Object> relationships = new HashMap<>();
relationships.put("createdBy",getIdHashmap(userModel.getCreatedBy(),"user"));
relationships.put("updatedBy",getIdHashmap(userModel.getUpdatedBy(),"user"));
result.put("id",userModel.getId());
result.put("type","user");
result.put("attributes",attributes);
result.put("relationships",relationships);
return result;
}
private static Map<String, Object> getIdHashmap(Object dto, String type) {
Map<String, Object> relationship = new HashMap<>();
Map<String, Object> data = new HashMap<>();
try {
if (dto == null) {
data.put("type", type);
data.put("id", null);
} else {
Field idField = dto.getClass().getDeclaredField("id");
idField.setAccessible(true);
Object id = idField.get(dto);

data.put("type", type);
data.put("id", id);

}
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
relationship.put("data", data);
return relationship;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.RDS.skilltree.Endorsement.EndorsementModel;
import com.RDS.skilltree.User.UserModel;
import com.RDS.skilltree.utils.TrackedProperties;
import com.fasterxml.jackson.annotation.JsonBackReference;
import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
Expand All @@ -23,6 +24,7 @@ public class EndorsementListModel extends TrackedProperties {

@JoinColumn(name = "endorsement_id", referencedColumnName = "id")
@ManyToOne(targetEntity = EndorsementModel.class, cascade = CascadeType.ALL)
@JsonBackReference
Ajeyakrishna-k marked this conversation as resolved.
Show resolved Hide resolved
private EndorsementModel endorsement;

@JoinColumn(name = "user_id", referencedColumnName = "id")
Expand All @@ -40,11 +42,11 @@ public class EndorsementListModel extends TrackedProperties {
private EndorsementType type;

public EndorsementListModel(EndorsementModel endorsement, UserModel endorser, String description,
EndorsementType type) {
EndorsementType type) {
this.endorsement = endorsement;
this.endorser = endorser;
this.description = description;
this.type = type;
this.deleted = false;
}
}
}
Loading