Skip to content

Commit

Permalink
Merge pull request #115 from Hoang-Nguyen-Huy/PBS-64-Manage-User-Api
Browse files Browse the repository at this point in the history
Pbs 64 manage user api
  • Loading branch information
nguyenhcp2004 authored Oct 28, 2024
2 parents 24ac6fe + 9a48064 commit 812fa33
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.swp.PodBookingSystem.dto.request.Account.*;
import com.swp.PodBookingSystem.dto.request.CalendarRequest;
import com.swp.PodBookingSystem.dto.respone.Account.AccountManagementResponse;
import com.swp.PodBookingSystem.dto.respone.Account.AccountOrderResponse;
import com.swp.PodBookingSystem.dto.respone.ApiResponse;
import com.swp.PodBookingSystem.dto.respone.AccountResponse;
Expand Down Expand Up @@ -49,19 +50,20 @@ public class AccountController {
ApiResponse<AccountResponse> createAccount(@RequestBody @Valid AccountCreationRequest request) {
return ApiResponse.<AccountResponse>builder()
.data(accountService.createAccount(request))
.message("Thêm tài khoản mới thành công")
.build();
}

@GetMapping
PaginationResponse<List<Account>> getAccounts(@RequestParam(defaultValue = "1", name = "page") int page,
@RequestParam(defaultValue = "10", name = "take") int take) {
PaginationResponse<List<AccountManagementResponse>> getAccounts(@RequestParam(defaultValue = "1", name = "page") int page,
@RequestParam(defaultValue = "10", name = "take") int take) {
var authentication = SecurityContextHolder.getContext().getAuthentication();
authentication.getAuthorities().forEach(grantedAuthority -> log.info(grantedAuthority.getAuthority()));

AccountPaginationDTO dto = new AccountPaginationDTO(page, take);
Page<Account> accountPage = accountService.getAccounts(dto.page, dto.take);
Page<AccountManagementResponse> accountPage = accountService.getAccounts(dto.page, dto.take);

return PaginationResponse.<List<Account>>builder()
return PaginationResponse.<List<AccountManagementResponse>>builder()
.data(accountPage.getContent())
.currentPage(accountPage.getNumber() + 1)
.totalPage(accountPage.getTotalPages())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class AccountCreationRequest {
String name;
String email;
String password;
int buildingNumber;
String role;
int status;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.swp.PodBookingSystem.dto.respone.Account;

import com.swp.PodBookingSystem.dto.respone.Building.BuildingResponse;
import com.swp.PodBookingSystem.enums.AccountRole;
import lombok.*;
import lombok.experimental.FieldDefaults;

import java.time.LocalDate;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@FieldDefaults(level = AccessLevel.PRIVATE)
public class AccountManagementResponse {
String id;
String name;
String email;
String password;
String avatar;
int point;
AccountRole role;
double balance;
BuildingResponse building;
String rankingName;
LocalDate createdAt;
int status;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
public class PaginationResponse<T> {
@Builder.Default
private int code = 200;
private String message;
private T data;
private int currentPage;
private int totalPage;
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/swp/PodBookingSystem/entity/Account.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ public class Account {
String id;

String name;

@Email
@Column(unique = true)
String email;

String password;
String avatar;
int point;
Expand Down
42 changes: 39 additions & 3 deletions src/main/java/com/swp/PodBookingSystem/service/AccountService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,27 @@

import com.swp.PodBookingSystem.dto.request.Account.AccountCreationRequest;
import com.swp.PodBookingSystem.dto.request.Account.AccountUpdateAdminRequest;
import com.swp.PodBookingSystem.dto.respone.Account.AccountManagementResponse;
import com.swp.PodBookingSystem.dto.respone.Account.AccountOrderResponse;
import com.swp.PodBookingSystem.dto.respone.AccountResponse;
import com.swp.PodBookingSystem.dto.respone.Building.BuildingResponse;
import com.swp.PodBookingSystem.entity.Account;
import com.swp.PodBookingSystem.entity.Building;
import com.swp.PodBookingSystem.enums.AccountRole;
import com.swp.PodBookingSystem.exception.AppException;
import com.swp.PodBookingSystem.exception.ErrorCode;
import com.swp.PodBookingSystem.mapper.AccountMapper;
import com.swp.PodBookingSystem.mapper.BuildingMapper;
import com.swp.PodBookingSystem.repository.AccountRepository;
import com.swp.PodBookingSystem.repository.BuildingRepository;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import lombok.experimental.FieldDefaults;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
Expand All @@ -36,9 +42,14 @@
public class AccountService {
AccountRepository accountRepository;
AccountMapper accountMapper;
BuildingMapper buildingMapper;
JwtDecoder jwtDecoder;
BuildingRepository buildingRepository;

public AccountResponse createAccount(AccountCreationRequest request) {
if (accountRepository.existsByEmail(request.getEmail())) {
throw new AppException(ErrorCode.EMAIL_EXISTED);
}
Account account = accountMapper.toAccount(request);
PasswordEncoder passwordEncoder = new BCryptPasswordEncoder(10);
account.setPassword(passwordEncoder.encode(account.getPassword()));
Expand All @@ -49,9 +60,10 @@ public AccountResponse createAccount(AccountCreationRequest request) {
[GET]: /accounts/page&take
*/
@PreAuthorize("hasRole('Admin')")
public Page<Account> getAccounts(int page, int take) {
Pageable pageable = PageRequest.of(page - 1, take);
return accountRepository.findAll(pageable);
public Page<AccountManagementResponse> getAccounts(int page, int take) {
Pageable pageable = PageRequest.of(page - 1, take, Sort.by(Sort.Direction.DESC, "createdAt"));
Page<Account> accountPage = accountRepository.findAll(pageable);
return accountPage.map(this::convertToAccountManagementResponse);
}

public Account getAccountById(String id) {
Expand Down Expand Up @@ -98,6 +110,30 @@ public AccountOrderResponse toAccountResponse(Account account) {
.build();
}

private AccountManagementResponse convertToAccountManagementResponse(Account account) {
BuildingResponse buildingResponse = null;
if (account.getBuildingNumber() != 0) {
Building building = buildingRepository.findById(account.getBuildingNumber())
.orElseThrow(() -> null);
buildingResponse = buildingMapper.toBuildingResponse(building);
}

return AccountManagementResponse.builder()
.id(account.getId())
.name(account.getName())
.email(account.getEmail())
.password(account.getPassword())
.avatar(account.getAvatar())
.point(account.getPoint())
.role(account.getRole())
.balance(account.getBalance())
.building(buildingResponse)
.rankingName(account.getRankingName())
.createdAt(account.getCreatedAt())
.status(account.getStatus())
.build();
}

public String extractAccountIdFromToken(String token) {
if (token == null || !token.startsWith("Bearer ")) {
throw new AppException(ErrorCode.INVALID_TOKEN);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public AuthenticationResponse loginGoogle(String email, String name, String avat
// Nếu khách chưa có tài khoản trong db thì mình sẽ tạo tạm thời cho khách
if (accountOptional.isEmpty()) {
PasswordEncoder passwordEncoder = new BCryptPasswordEncoder(10);
AccountCreationRequest request = new AccountCreationRequest(name, email, passwordEncoder.encode("123123"), "Customer", 1);
AccountCreationRequest request = new AccountCreationRequest(name, email, passwordEncoder.encode("123123"), 0, "Customer", 1);
account = accountMapper.toAccount(request);
account.setAvatar(avatar);
accountRepository.save(account);
Expand Down

0 comments on commit 812fa33

Please sign in to comment.