From 6e42801132ab164b5aa5e9c2772e8d70218f3e21 Mon Sep 17 00:00:00 2001 From: doubletake30 <86301855+doubletake30@users.noreply.github.com> Date: Tue, 5 Nov 2024 04:58:06 +0700 Subject: [PATCH] [TriNT][PBS-106] feat: get assignment by StaffId --- .../controller/AssignmentController.java | 17 +++++++++++++++++ .../swp/PodBookingSystem/entity/Assignment.java | 2 +- .../mapper/AssignmentMapper.java | 3 +++ .../repository/AssignmentRepository.java | 4 ++++ .../service/AssignmentService.java | 13 +++++++++++++ 5 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/swp/PodBookingSystem/controller/AssignmentController.java b/src/main/java/com/swp/PodBookingSystem/controller/AssignmentController.java index 9773a38c..200498d9 100644 --- a/src/main/java/com/swp/PodBookingSystem/controller/AssignmentController.java +++ b/src/main/java/com/swp/PodBookingSystem/controller/AssignmentController.java @@ -48,5 +48,22 @@ ApiResponse updateAssignment(@PathVariable("assignmentId") S .message("Update Assignment successfully") .build(); } + + @DeleteMapping("/{assignmentId}") + ApiResponse deleteAssignment(@PathVariable("assignmentId") String assignmentId){ + return ApiResponse.builder() + .message(assignmentService.deleteAssignment(assignmentId)) + .build(); } + @GetMapping("/{staffId}") + public ApiResponse> getAssignmentByStaffId(@PathVariable("staffId") String staffId) { + List assignments = assignmentService.getAssignmentsByStaffId(staffId); + return ApiResponse.>builder() + .data(assignments) + .message("Assignments retrieved successfully") + .build(); + } + +} + diff --git a/src/main/java/com/swp/PodBookingSystem/entity/Assignment.java b/src/main/java/com/swp/PodBookingSystem/entity/Assignment.java index 859903c7..e83836d3 100644 --- a/src/main/java/com/swp/PodBookingSystem/entity/Assignment.java +++ b/src/main/java/com/swp/PodBookingSystem/entity/Assignment.java @@ -17,7 +17,7 @@ @Table(name = "assignment") public class Assignment { @Id - String Id; + String id; String staffId; diff --git a/src/main/java/com/swp/PodBookingSystem/mapper/AssignmentMapper.java b/src/main/java/com/swp/PodBookingSystem/mapper/AssignmentMapper.java index 83ecf5ec..ddd8f80f 100644 --- a/src/main/java/com/swp/PodBookingSystem/mapper/AssignmentMapper.java +++ b/src/main/java/com/swp/PodBookingSystem/mapper/AssignmentMapper.java @@ -10,6 +10,8 @@ import org.mapstruct.Mapping; import org.mapstruct.MappingTarget; +import java.util.List; + @Mapper(componentModel = "spring") public interface AssignmentMapper { @@ -19,4 +21,5 @@ public interface AssignmentMapper { @Mapping(target = "id", ignore = true) Assignment toUpdateAssignment(AssignmentRequest request, @MappingTarget Assignment assignment); + } diff --git a/src/main/java/com/swp/PodBookingSystem/repository/AssignmentRepository.java b/src/main/java/com/swp/PodBookingSystem/repository/AssignmentRepository.java index b2f96ed0..86a3ae19 100644 --- a/src/main/java/com/swp/PodBookingSystem/repository/AssignmentRepository.java +++ b/src/main/java/com/swp/PodBookingSystem/repository/AssignmentRepository.java @@ -4,6 +4,10 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; +import java.util.List; + @Repository public interface AssignmentRepository extends JpaRepository { + List findByStaffId(String staffId); + } diff --git a/src/main/java/com/swp/PodBookingSystem/service/AssignmentService.java b/src/main/java/com/swp/PodBookingSystem/service/AssignmentService.java index a54d589e..52d78aed 100644 --- a/src/main/java/com/swp/PodBookingSystem/service/AssignmentService.java +++ b/src/main/java/com/swp/PodBookingSystem/service/AssignmentService.java @@ -44,7 +44,20 @@ public AssignmentResponse updateAssignment(String id, AssignmentRequest request) .orElseThrow(() -> new EntityNotFoundException("Assignment not found")); Assignment updateAssignment = assignmentMapper.toUpdateAssignment(request, existingAssignment); return assignmentMapper.toAssignmentResponse(assignmentRepository.save(updateAssignment)); + } + public String deleteAssignment(String id){ + Assignment existingAssignment = assignmentRepository.findById(id) + .orElseThrow(() -> new EntityNotFoundException("Assignment not found")); + assignmentRepository.deleteById(id); + return "Assignment deleted successfully"; + } + public List getAssignmentsByStaffId(String staffId) { + List assignments = assignmentRepository.findByStaffId(staffId); + return assignments.stream() + .map(assignmentMapper::toAssignmentResponse) + .collect(Collectors.toList()); } + }