Skip to content

Commit

Permalink
Merge pull request #165 from Hoang-Nguyen-Huy/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
nguyenhcp2004 authored Nov 12, 2024
2 parents 4850b11 + 6b51800 commit c8d700a
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@ public class AccountController {

@PostMapping
ApiResponse<AccountResponse> createAccount(@RequestBody @Valid AccountCreationRequest request) {
AccountResponse response = accountService.createAccount(request);
return ApiResponse.<AccountResponse>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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,6 +24,7 @@
public class AssignmentController {

private final AssignmentService assignmentService;
private final AccountService accountService;

@PostMapping
ApiResponse<AssignmentResponse> createAssignment(@RequestBody @Valid AssignmentCreationRequest request){
Expand All @@ -32,8 +35,9 @@ ApiResponse<AssignmentResponse> createAssignment(@RequestBody @Valid AssignmentC
}

@GetMapping("/all")
public ApiResponse<List<AssignmentResponse>> getAllAssignment(){
List<AssignmentResponse> assignments = assignmentService.getAllAssignments();
public ApiResponse<List<AssignmentResponse>> getAllAssignment(@RequestHeader("Authorization") String token){
Account account = accountService.getAccountById(accountService.extractAccountIdFromToken(token));
List<AssignmentResponse> assignments = assignmentService.getAllAssignments(account);
return ApiResponse.<List<AssignmentResponse>>builder()
.data(assignments)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,15 @@ String findStaffForMatchingOrder(@Param("slot") String slot,
@Param("buildingId") Integer buildingId);


@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<Assignment> findAllByRoleAndBuildingAndStaff(
@Param("role") String role,
@Param("buildingId") Integer buildingId,
@Param("staffId") String staffId
);

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -53,7 +52,6 @@ public AssignmentResponse createAssignment(AssignmentCreationRequest request) {
LocalTime[] slotTimes = getSlotTimes(slot);
String slotStartTime = slotTimes[0].toString();
String slotEndTime = slotTimes[1].toString();

orderDetailRepository.assignOrdersToStaff(newAssignment.getStaffId(), weekDay, slotStartTime, slotEndTime, staff.getBuildingNumber());

return assignmentMapper.toAssignmentResponse(assignmentRepository.save(newAssignment));
Expand Down Expand Up @@ -83,19 +81,27 @@ public LocalTime[] getSlotTimes(String slot) {



public List<AssignmentResponse> getAllAssignments(){
List<Assignment> 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<AssignmentResponse> getAllAssignments(Account user){
List<Assignment> 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)
Expand All @@ -119,7 +125,7 @@ public List<AssignmentResponse> getAssignmentsByStaffId(String staffId) {
String nameStaff = accountRepository.findById(assignment.getStaffId())
.map(Account::getName)
.orElse("Unknown");
response.setNameStaff(nameStaff); // Set the staff name
response.setNameStaff(nameStaff);
return response;
})
.collect(Collectors.toList());
Expand Down

0 comments on commit c8d700a

Please sign in to comment.