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 1 commit
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(Map<String, Object> data, int statusCode, String status, String message) {
heyrandhir marked this conversation as resolved.
Show resolved Hide resolved

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

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 org.springframework.web.server.ResponseStatusException;

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

@RestController
@RequestMapping("/v1/endorsements")

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
String message = "Data found successfully";
Map<String, Object> responseData = endorsementService.getEndorsementAsMap(uuid);
ApiResponse response = new ApiResponse(responseData, HttpStatus.OK.value(),HttpStatus.OK.toString(),message);
return ResponseEntity.ok(response);
} catch (IllegalArgumentException e) {
heyrandhir marked this conversation as resolved.
Show resolved Hide resolved
String message = "Invalid UUID: " + id;
ApiResponse 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 response = new ApiResponse(null, HttpStatus.NOT_FOUND.value(),HttpStatus.NOT_FOUND.toString(),message);
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
}
heyrandhir marked this conversation as resolved.
Show resolved Hide resolved
}
}

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

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

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

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@JsonIgnoreProperties(ignoreUnknown = true)
heyrandhir marked this conversation as resolved.
Show resolved Hide resolved
public class EndorsementDTO {
private UUID id;
private UUID user;
private UUID skill;
private EndorsementStatus status;
private Instant createdAt;
private Instant updatedAt;
heyrandhir marked this conversation as resolved.
Show resolved Hide resolved
private UUID createdBy;
private UUID updatedBy;

public static EndorsementDTO toDto(EndorsementModel endorsementModel) {
return EndorsementDTO.builder()
.id(endorsementModel.getId())
.user(endorsementModel.getUser().getId())
.skill(endorsementModel.getSkill().getId())
.status(endorsementModel.getStatus())
.createdAt(endorsementModel.getCreatedAt())
.createdBy(endorsementModel.getCreatedBy().getId())
.updatedAt(endorsementModel.getUpdatedAt())
.updatedBy(endorsementModel.getUpdatedBy().getId())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
import com.RDS.skilltree.User.UserModel;
import com.RDS.skilltree.utils.TrackedProperties;
import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.*;

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

@EqualsAndHashCode(callSuper = true)
@Entity
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Data
@Table(name = "endorsements")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.RDS.skilltree.Endorsement;

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

public interface EndorsementService {
EndorsementDTO getEndorsementById(UUID id);
Map<String, Object> getEndorsementAsMap(UUID uuid);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.RDS.skilltree.Endorsement;

import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
@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
}

public Map<String, Object> getEndorsementAsMap(UUID uuid) {
EndorsementDTO endorsementDTO = getEndorsementById(uuid);

Map<String, Object> endorsementMap = new HashMap<>();
endorsementMap.put("id", endorsementDTO.getId());
heyrandhir marked this conversation as resolved.
Show resolved Hide resolved
endorsementMap.put("user", endorsementDTO.getUser());
endorsementMap.put("skill", endorsementDTO.getSkill());
endorsementMap.put("status", endorsementDTO.getStatus());
endorsementMap.put("createdAt", endorsementDTO.getCreatedAt());
endorsementMap.put("updatedAt", endorsementDTO.getUpdatedAt());
endorsementMap.put("createdBy", endorsementDTO.getCreatedBy());
endorsementMap.put("updatedBy", endorsementDTO.getUpdatedBy());

return endorsementMap;
}
}