Skip to content

Commit

Permalink
[TriNT][PBS-106] feat: get assignment by StaffId
Browse files Browse the repository at this point in the history
  • Loading branch information
ThanhTriIsCoding committed Nov 4, 2024
1 parent 428c435 commit 6e42801
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,22 @@ ApiResponse<AssignmentResponse> updateAssignment(@PathVariable("assignmentId") S
.message("Update Assignment successfully")
.build();
}

@DeleteMapping("/{assignmentId}")
ApiResponse<AssignmentResponse> deleteAssignment(@PathVariable("assignmentId") String assignmentId){
return ApiResponse.<AssignmentResponse>builder()
.message(assignmentService.deleteAssignment(assignmentId))
.build();
}

@GetMapping("/{staffId}")
public ApiResponse<List<AssignmentResponse>> getAssignmentByStaffId(@PathVariable("staffId") String staffId) {
List<AssignmentResponse> assignments = assignmentService.getAssignmentsByStaffId(staffId);
return ApiResponse.<List<AssignmentResponse>>builder()
.data(assignments)
.message("Assignments retrieved successfully")
.build();
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
@Table(name = "assignment")
public class Assignment {
@Id
String Id;
String id;

String staffId;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;

import java.util.List;

@Mapper(componentModel = "spring")

public interface AssignmentMapper {
Expand All @@ -19,4 +21,5 @@ public interface AssignmentMapper {

@Mapping(target = "id", ignore = true)
Assignment toUpdateAssignment(AssignmentRequest request, @MappingTarget Assignment assignment);

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Assignment, String> {
List<Assignment> findByStaffId(String staffId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<AssignmentResponse> getAssignmentsByStaffId(String staffId) {
List<Assignment> assignments = assignmentRepository.findByStaffId(staffId);
return assignments.stream()
.map(assignmentMapper::toAssignmentResponse)
.collect(Collectors.toList());
}

}

0 comments on commit 6e42801

Please sign in to comment.