From 0884c14b8eebff694e3fcf52a83acc261611347b Mon Sep 17 00:00:00 2001 From: Huynh Nguyen Date: Mon, 28 Oct 2024 09:14:25 +0700 Subject: [PATCH 1/2] [NguyenHCP] refactor: api getMe response --- .../controller/OrderController.java | 25 +++++++++-------- .../Account/AccountResponseClient.java | 4 +++ .../swp/PodBookingSystem/entity/Account.java | 1 + .../swp/PodBookingSystem/entity/Ranking.java | 28 +++++++++++++++++++ .../mapper/AccountMapper.java | 2 ++ .../repository/OrderDetailRepository.java | 4 +-- 6 files changed, 50 insertions(+), 14 deletions(-) create mode 100644 src/main/java/com/swp/PodBookingSystem/entity/Ranking.java diff --git a/src/main/java/com/swp/PodBookingSystem/controller/OrderController.java b/src/main/java/com/swp/PodBookingSystem/controller/OrderController.java index fb6fb33c..4a7c6375 100644 --- a/src/main/java/com/swp/PodBookingSystem/controller/OrderController.java +++ b/src/main/java/com/swp/PodBookingSystem/controller/OrderController.java @@ -18,6 +18,7 @@ import org.springframework.http.ResponseEntity; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.web.bind.annotation.*; + import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.List; @@ -68,14 +69,14 @@ public ApiResponse>> searchOrde .build(); } - @GetMapping("/{accountId}") - public ApiResponse> getOrdersByAccountId(@PathVariable String accountId) { - List orders = orderService.getOrdersByAccountId(accountId); - logOrders(orders); - return ApiResponse.>builder() - .data(orders) - .build(); - } +// @GetMapping("/{accountId}") +// public ApiResponse> getOrdersByAccountId(@PathVariable String accountId) { +// List orders = orderService.getOrdersByAccountId(accountId); +// logOrders(orders); +// return ApiResponse.>builder() +// .data(orders) +// .build(); +// } //Check room available -> yes: create order Status: Successfully // -> no: create order Status: Pending @@ -108,7 +109,7 @@ public ApiResponse createOrderByRequest( } @PutMapping - ApiResponse updateOrder(@RequestBody OrderUpdateRequest request){ + ApiResponse updateOrder(@RequestBody OrderUpdateRequest request) { orderService.updateOrderUpdateAt(request.getId()); return ApiResponse.builder() .data(orderService.updateOrder(request)) @@ -117,9 +118,9 @@ ApiResponse updateOrder(@RequestBody OrderUpdateRequest request){ } @PutMapping("/staff") - ApiResponse updateStaffWithOrder(@RequestBody OrderUpdateStaffRequest request){ - return ApiResponse. builder() - .data(orderService.updateOrderHandlerWithOrder(request.getId(),request)) + ApiResponse updateStaffWithOrder(@RequestBody OrderUpdateStaffRequest request) { + return ApiResponse.builder() + .data(orderService.updateOrderHandlerWithOrder(request.getId(), request)) .message("Update order successfully") .build(); } diff --git a/src/main/java/com/swp/PodBookingSystem/dto/request/Account/AccountResponseClient.java b/src/main/java/com/swp/PodBookingSystem/dto/request/Account/AccountResponseClient.java index 1999724f..1528f9b9 100644 --- a/src/main/java/com/swp/PodBookingSystem/dto/request/Account/AccountResponseClient.java +++ b/src/main/java/com/swp/PodBookingSystem/dto/request/Account/AccountResponseClient.java @@ -16,6 +16,10 @@ public class AccountResponseClient { String name; String email; String avatar; + String phoneNumber; + double balance; + String rankingName; + int point; @Enumerated(EnumType.STRING) AccountRole role; int buildingNumber; diff --git a/src/main/java/com/swp/PodBookingSystem/entity/Account.java b/src/main/java/com/swp/PodBookingSystem/entity/Account.java index fe7f215f..79a29a01 100644 --- a/src/main/java/com/swp/PodBookingSystem/entity/Account.java +++ b/src/main/java/com/swp/PodBookingSystem/entity/Account.java @@ -30,6 +30,7 @@ public class Account { String password; String avatar; int point; + String phoneNumber; @Enumerated(EnumType.STRING) AccountRole role; diff --git a/src/main/java/com/swp/PodBookingSystem/entity/Ranking.java b/src/main/java/com/swp/PodBookingSystem/entity/Ranking.java new file mode 100644 index 00000000..c2089741 --- /dev/null +++ b/src/main/java/com/swp/PodBookingSystem/entity/Ranking.java @@ -0,0 +1,28 @@ +package com.swp.PodBookingSystem.entity; + +import jakarta.persistence.*; +import lombok.*; +import lombok.experimental.FieldDefaults; + +@Getter +@Setter +@Builder +@NoArgsConstructor +@AllArgsConstructor +@FieldDefaults(level = AccessLevel.PRIVATE) +@Entity +@Table(name = "ranking") +public class Ranking { + @Id + @Column(nullable = false, unique = true) + String rankingName; + + @Column(name = "maxPoint") + int maxPoint; + + @Column(name = "minPoint") + int minPoint; + + @Column(name = "discountPercentage") + int discountPercentage; +} diff --git a/src/main/java/com/swp/PodBookingSystem/mapper/AccountMapper.java b/src/main/java/com/swp/PodBookingSystem/mapper/AccountMapper.java index dabb16bc..ae1684a3 100644 --- a/src/main/java/com/swp/PodBookingSystem/mapper/AccountMapper.java +++ b/src/main/java/com/swp/PodBookingSystem/mapper/AccountMapper.java @@ -30,6 +30,8 @@ default AccountRole stringToAccountRole(String role) { } @Mapping(source = "role", target = "role", qualifiedByName = "stringToAccountRole") + @Mapping(source = "phoneNumber", target = "phoneNumber") + @Mapping(source = "rankingName", target = "rankingName") AccountResponseClient toAccountResponseClient(Account account); @Mapping(target = "id", ignore = true) diff --git a/src/main/java/com/swp/PodBookingSystem/repository/OrderDetailRepository.java b/src/main/java/com/swp/PodBookingSystem/repository/OrderDetailRepository.java index 7106bec7..54950050 100644 --- a/src/main/java/com/swp/PodBookingSystem/repository/OrderDetailRepository.java +++ b/src/main/java/com/swp/PodBookingSystem/repository/OrderDetailRepository.java @@ -21,7 +21,7 @@ public interface OrderDetailRepository extends JpaRepository findByCustomer_Id(@Param("customerId") String customerId, @Param("status") OrderStatus status, Pageable pageable); List findByOrderId(String orderId); @@ -81,7 +81,7 @@ Page findOrdersByBuildingNumberAndTimeRange( "WHERE od.startTime >= :startTime AND od.endTime <= :endTime " + "AND od.status = com.swp.PodBookingSystem.enums.OrderStatus.Successfully") Double calculateRevenueBetweenDateTime(@Param("startTime") LocalDateTime startTime, - @Param("endTime") LocalDateTime endTime); + @Param("endTime") LocalDateTime endTime); @Query("SELECT NEW com.swp.PodBookingSystem.dto.respone.OrderDetail.RevenueByMonthDto(" + "CONCAT(YEAR(MIN(od.startTime)), '-', LPAD(CAST(MONTH(MIN(od.startTime)) AS string), 2, '0'), '-01'), " + From 89323578be0f1870897d607230e3f3fdcfb17026 Mon Sep 17 00:00:00 2001 From: Huynh Nguyen Date: Mon, 28 Oct 2024 10:25:34 +0700 Subject: [PATCH 2/2] [NguyenHCP] feat: handle login google when user is banned --- .../controller/AuthenticationController.java | 19 +++++++++++++------ .../PodBookingSystem/exception/ErrorCode.java | 1 + .../service/AuthenticationService.java | 2 ++ 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/swp/PodBookingSystem/controller/AuthenticationController.java b/src/main/java/com/swp/PodBookingSystem/controller/AuthenticationController.java index e83e78c8..12a3c397 100644 --- a/src/main/java/com/swp/PodBookingSystem/controller/AuthenticationController.java +++ b/src/main/java/com/swp/PodBookingSystem/controller/AuthenticationController.java @@ -22,6 +22,8 @@ import org.springframework.web.bind.annotation.*; import org.springframework.web.servlet.view.RedirectView; +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; import java.text.ParseException; @@ -35,12 +37,17 @@ public class AuthenticationController { SendEmailService sendEmailService; @GetMapping("/login/google") - public RedirectView loginGoogle(OAuth2AuthenticationToken token) throws ParseException { - var result = authenticationService.loginGoogle(token.getPrincipal().getAttribute("email"), - token.getPrincipal().getAttribute("name"), - token.getPrincipal().getAttribute("picture")); - return new RedirectView("http://localhost:3000/login/oauth?accessToken=" + result.getAccessToken() - + "&refreshToken=" + result.getRefreshToken() + "&status=" + 200); + public RedirectView loginGoogle(OAuth2AuthenticationToken token) throws ParseException, UnsupportedEncodingException { + try { + var result = authenticationService.loginGoogle(token.getPrincipal().getAttribute("email"), + token.getPrincipal().getAttribute("name"), + token.getPrincipal().getAttribute("picture")); + return new RedirectView("http://localhost:3000/login/oauth?accessToken=" + result.getAccessToken() + + "&refreshToken=" + result.getRefreshToken() + "&status=" + 200); + } catch (Exception e) { + String message = URLEncoder.encode("Tài khoản đã bị cấm", "UTF-8"); + return new RedirectView("http://localhost:3000/login/oauth?message=" + message + "&status=" + 500); + } } @PostMapping("/login") diff --git a/src/main/java/com/swp/PodBookingSystem/exception/ErrorCode.java b/src/main/java/com/swp/PodBookingSystem/exception/ErrorCode.java index 29fbcd0d..9126d648 100644 --- a/src/main/java/com/swp/PodBookingSystem/exception/ErrorCode.java +++ b/src/main/java/com/swp/PodBookingSystem/exception/ErrorCode.java @@ -22,6 +22,7 @@ public enum ErrorCode { REFRESH_TOKEN_NOT_EXIST(404, "Refresh token không tồn tại", HttpStatus.NOT_FOUND, "refreshToken"), INVALID_TOKEN(401, "Token không đúng", HttpStatus.UNAUTHORIZED, "accessToken"), ORDER_DETAIL_NOT_EXIST(404, "Order detail không tồn tại", HttpStatus.NOT_FOUND, "orderDetailId"), + ACCOUNT_NOT_ACTIVE(403, "Tài khoản đã bị cấm", HttpStatus.FORBIDDEN, "system"), ; ErrorCode(int code, String message, HttpStatusCode statusCode, String field) { diff --git a/src/main/java/com/swp/PodBookingSystem/service/AuthenticationService.java b/src/main/java/com/swp/PodBookingSystem/service/AuthenticationService.java index 4ef9aabc..b1dd92f6 100644 --- a/src/main/java/com/swp/PodBookingSystem/service/AuthenticationService.java +++ b/src/main/java/com/swp/PodBookingSystem/service/AuthenticationService.java @@ -83,6 +83,7 @@ public AuthenticationResponse login(AuthenticationRequest request) throws ParseE account.getPassword()); if (!authenticated) throw new AppException(ErrorCode.INCORRECT_PASSWORD); + if (account.getStatus() == 0) throw new AppException(ErrorCode.ACCOUNT_NOT_ACTIVE); var accessToken = generateAccessToken(account); var refreshToken = generateRefreshToken(account); @@ -181,6 +182,7 @@ public AuthenticationResponse loginGoogle(String email, String name, String avat accountRepository.save(account); } else { account = accountOptional.get(); // Lấy tài khoản từ Optional + if (account.getStatus() == 0) throw new AppException(ErrorCode.ACCOUNT_NOT_ACTIVE); } // Tạo access token và refresh token