From 86aae1d8453d0b4ad790c0233506498512b291f8 Mon Sep 17 00:00:00 2001 From: doubletake30 <86301855+doubletake30@users.noreply.github.com> Date: Sat, 9 Nov 2024 23:12:18 +0700 Subject: [PATCH 1/2] ref: ref function get all Assignment by role --- .../controller/AssignmentController.java | 8 +++- .../OrderDetailAmenityController.java | 1 - .../repository/AssignmentRepository.java | 10 +++++ .../service/AssignmentService.java | 41 ++++++++++--------- 4 files changed, 38 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/swp/PodBookingSystem/controller/AssignmentController.java b/src/main/java/com/swp/PodBookingSystem/controller/AssignmentController.java index 2c5f5f94..6317ca1c 100644 --- a/src/main/java/com/swp/PodBookingSystem/controller/AssignmentController.java +++ b/src/main/java/com/swp/PodBookingSystem/controller/AssignmentController.java @@ -4,6 +4,8 @@ import com.swp.PodBookingSystem.dto.request.Assignment.AssignmentRequest; import com.swp.PodBookingSystem.dto.respone.ApiResponse; import com.swp.PodBookingSystem.dto.respone.Assignment.AssignmentResponse; +import com.swp.PodBookingSystem.entity.Account; +import com.swp.PodBookingSystem.service.AccountService; import com.swp.PodBookingSystem.service.AssignmentService; import jakarta.validation.Valid; import lombok.AccessLevel; @@ -22,6 +24,7 @@ public class AssignmentController { private final AssignmentService assignmentService; + private final AccountService accountService; @PostMapping ApiResponse createAssignment(@RequestBody @Valid AssignmentCreationRequest request){ @@ -32,8 +35,9 @@ ApiResponse createAssignment(@RequestBody @Valid AssignmentC } @GetMapping("/all") - public ApiResponse> getAllAssignment(){ - List assignments = assignmentService.getAllAssignments(); + public ApiResponse> getAllAssignment(@RequestHeader("Authorization") String token){ + Account account = accountService.getAccountById(accountService.extractAccountIdFromToken(token)); + List assignments = assignmentService.getAllAssignments(account); return ApiResponse.>builder() .data(assignments) .build(); diff --git a/src/main/java/com/swp/PodBookingSystem/controller/OrderDetailAmenityController.java b/src/main/java/com/swp/PodBookingSystem/controller/OrderDetailAmenityController.java index 255fbc66..1530f9aa 100644 --- a/src/main/java/com/swp/PodBookingSystem/controller/OrderDetailAmenityController.java +++ b/src/main/java/com/swp/PodBookingSystem/controller/OrderDetailAmenityController.java @@ -18,7 +18,6 @@ import org.springframework.web.bind.annotation.*; import java.time.LocalDateTime; -import java.time.format.DateTimeFormatter; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/src/main/java/com/swp/PodBookingSystem/repository/AssignmentRepository.java b/src/main/java/com/swp/PodBookingSystem/repository/AssignmentRepository.java index e2d32e90..ff859132 100644 --- a/src/main/java/com/swp/PodBookingSystem/repository/AssignmentRepository.java +++ b/src/main/java/com/swp/PodBookingSystem/repository/AssignmentRepository.java @@ -17,5 +17,15 @@ public interface AssignmentRepository extends JpaRepository List findStaffIdsByWeekDateAndSlot(@Param("weekDate") String weekDate, @Param("slot") String slot); + @Query("SELECT a FROM Assignment a " + + "JOIN Account acc ON a.staffId = acc.id " + + "WHERE (:role = 'Admin') OR " + + "(:role = 'Manager' AND acc.buildingNumber = :buildingId) OR " + + "(:role = 'Staff' AND acc.buildingNumber = :buildingId AND a.staffId = :staffId)") + List findAllByRoleAndBuildingAndStaff( + @Param("role") String role, + @Param("buildingId") Integer buildingId, + @Param("staffId") 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 5084889d..fa854988 100644 --- a/src/main/java/com/swp/PodBookingSystem/service/AssignmentService.java +++ b/src/main/java/com/swp/PodBookingSystem/service/AssignmentService.java @@ -4,6 +4,7 @@ import com.swp.PodBookingSystem.dto.request.Assignment.AssignmentRequest; import com.swp.PodBookingSystem.dto.respone.Assignment.AssignmentResponse; import com.swp.PodBookingSystem.entity.Assignment; +import com.swp.PodBookingSystem.enums.AccountRole; import com.swp.PodBookingSystem.mapper.AccountMapper; import com.swp.PodBookingSystem.mapper.AssignmentMapper; import com.swp.PodBookingSystem.repository.AccountRepository; @@ -34,9 +35,7 @@ public class AssignmentService { private final AssignmentMapper assignmentMapper; private final AssignmentRepository assignmentRepository; private final AccountRepository accountRepository; - private final AccountMapper accountMapper; private final OrderDetailRepository orderDetailRepository; - private static final Logger logger = LoggerFactory.getLogger(AssignmentService.class); public AssignmentResponse createAssignment(AssignmentCreationRequest request) { @@ -53,10 +52,6 @@ public AssignmentResponse createAssignment(AssignmentCreationRequest request) { LocalTime[] slotTimes = getSlotTimes(slot); String slotStartTime = slotTimes[0].toString(); String slotEndTime = slotTimes[1].toString(); - - logger.info("Creating assignment for staffId: {} (Staff building: {}), weekDate: {}, dayOfWeek: {}, weekDay: {}, " + - "slot: {}, slotStartTime: {}, slotEndTime: {}, assigning orders where startTime > CURRENT_TIMESTAMP", - newAssignment.getStaffId(), staff.getBuildingNumber(), weekDate, dayOfWeek, weekDay, slot, slotStartTime, slotEndTime); orderDetailRepository.assignOrdersToStaff(newAssignment.getStaffId(), weekDay, slotStartTime, slotEndTime, staff.getBuildingNumber()); return assignmentMapper.toAssignmentResponse(assignmentRepository.save(newAssignment)); @@ -86,19 +81,27 @@ public LocalTime[] getSlotTimes(String slot) { - public List getAllAssignments(){ - List assignments = assignmentRepository.findAll(); - return assignments.stream() - .map(assignment -> { - AssignmentResponse response = assignmentMapper.toAssignmentResponse(assignment); - String nameStaff = accountRepository.findById(assignment.getStaffId()) - .map(Account::getName) - .orElse("Unknown"); - response.setNameStaff(nameStaff); - return response; - }) - .collect(Collectors.toList()); - } + public List getAllAssignments(Account user){ + List assignments = null; + Integer buildingId = user.getBuildingNumber(); + if (user.getRole() == AccountRole.Admin) { + assignments = assignmentRepository.findAllByRoleAndBuildingAndStaff("Admin", null, null); + } else if(user.getRole() == AccountRole.Manager){ + assignments = assignmentRepository.findAllByRoleAndBuildingAndStaff("Manager", buildingId, null); + } else if (user.getRole() == AccountRole.Staff){ + assignments = assignmentRepository.findAllByRoleAndBuildingAndStaff("Staff", buildingId, user.getId()); + } + return assignments.stream() + .map(assignment -> { + AssignmentResponse response = assignmentMapper.toAssignmentResponse(assignment); + String nameStaff = accountRepository.findById(assignment.getStaffId()) + .map(Account::getName) + .orElse("Unknown"); + response.setNameStaff(nameStaff); + return response; + }) + .collect(Collectors.toList()); + } public AssignmentResponse updateAssignment(String id, AssignmentRequest request){ Assignment existingAssignment = assignmentRepository.findById(id) From 51cf1368e94ab6c0f695b711be6d5721bed1d2c8 Mon Sep 17 00:00:00 2001 From: Hoang-Nguyen-Huy Date: Tue, 12 Nov 2024 16:14:56 +0700 Subject: [PATCH 2/2] [PBS-108][HoangHN] refactor: message error when existed email --- .../swp/PodBookingSystem/controller/AccountController.java | 5 +++-- .../com/swp/PodBookingSystem/service/AccountService.java | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/swp/PodBookingSystem/controller/AccountController.java b/src/main/java/com/swp/PodBookingSystem/controller/AccountController.java index df6ef268..37e8137b 100644 --- a/src/main/java/com/swp/PodBookingSystem/controller/AccountController.java +++ b/src/main/java/com/swp/PodBookingSystem/controller/AccountController.java @@ -52,9 +52,10 @@ public class AccountController { @PostMapping ApiResponse createAccount(@RequestBody @Valid AccountCreationRequest request) { + AccountResponse response = accountService.createAccount(request); return ApiResponse.builder() - .data(accountService.createAccount(request)) - .message("Thêm tài khoản mới thành công") + .data(response) + .message(response != null ? "Thêm tài khoản mới thành công" : "Email đã tồn tại") .build(); } diff --git a/src/main/java/com/swp/PodBookingSystem/service/AccountService.java b/src/main/java/com/swp/PodBookingSystem/service/AccountService.java index 2ced266b..6f860b71 100644 --- a/src/main/java/com/swp/PodBookingSystem/service/AccountService.java +++ b/src/main/java/com/swp/PodBookingSystem/service/AccountService.java @@ -50,7 +50,7 @@ public class AccountService { public AccountResponse createAccount(AccountCreationRequest request) { if (accountRepository.existsByEmail(request.getEmail())) { - throw new AppException(ErrorCode.EMAIL_EXISTED); + return null; } Account account = accountMapper.toAccount(request); PasswordEncoder passwordEncoder = new BCryptPasswordEncoder(10);