Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…king-system-server into PBS-36-Crud-RoomType
  • Loading branch information
Hoang-Nguyen-Huy committed Oct 28, 2024
2 parents 9397565 + 3b8eb20 commit 7c88b4a
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand All @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -68,14 +69,14 @@ public ApiResponse<PaginationResponse<List<OrderManagementResponse>>> searchOrde
.build();
}

@GetMapping("/{accountId}")
public ApiResponse<List<OrderResponse>> getOrdersByAccountId(@PathVariable String accountId) {
List<OrderResponse> orders = orderService.getOrdersByAccountId(accountId);
logOrders(orders);
return ApiResponse.<List<OrderResponse>>builder()
.data(orders)
.build();
}
// @GetMapping("/{accountId}")
// public ApiResponse<List<OrderResponse>> getOrdersByAccountId(@PathVariable String accountId) {
// List<OrderResponse> orders = orderService.getOrdersByAccountId(accountId);
// logOrders(orders);
// return ApiResponse.<List<OrderResponse>>builder()
// .data(orders)
// .build();
// }

//Check room available -> yes: create order Status: Successfully
// -> no: create order Status: Pending
Expand Down Expand Up @@ -108,7 +109,7 @@ public ApiResponse<String> createOrderByRequest(
}

@PutMapping
ApiResponse<OrderResponse> updateOrder(@RequestBody OrderUpdateRequest request){
ApiResponse<OrderResponse> updateOrder(@RequestBody OrderUpdateRequest request) {
orderService.updateOrderUpdateAt(request.getId());
return ApiResponse.<OrderResponse>builder()
.data(orderService.updateOrder(request))
Expand All @@ -117,9 +118,9 @@ ApiResponse<OrderResponse> updateOrder(@RequestBody OrderUpdateRequest request){
}

@PutMapping("/staff")
ApiResponse<OrderResponse> updateStaffWithOrder(@RequestBody OrderUpdateStaffRequest request){
return ApiResponse.<OrderResponse> builder()
.data(orderService.updateOrderHandlerWithOrder(request.getId(),request))
ApiResponse<OrderResponse> updateStaffWithOrder(@RequestBody OrderUpdateStaffRequest request) {
return ApiResponse.<OrderResponse>builder()
.data(orderService.updateOrderHandlerWithOrder(request.getId(), request))
.message("Update order successfully")
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/swp/PodBookingSystem/entity/Account.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class Account {
String password;
String avatar;
int point;
String phoneNumber;

@Enumerated(EnumType.STRING)
AccountRole role;
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/com/swp/PodBookingSystem/entity/Ranking.java
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public interface OrderDetailRepository extends JpaRepository<OrderDetail, String
@Query("SELECT od FROM OrderDetail od " +
"WHERE (od.customer.id = :customerId)" +
"AND (od.status = :status)" +
"ORDER BY od.createdAt DESC")
"ORDER BY od.startTime DESC")
Page<OrderDetail> findByCustomer_Id(@Param("customerId") String customerId, @Param("status") OrderStatus status, Pageable pageable);

List<OrderDetail> findByOrderId(String orderId);
Expand Down Expand Up @@ -81,7 +81,7 @@ Page<OrderDetail> 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'), " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 7c88b4a

Please sign in to comment.