From 18cd809a148133a13935ab251a5199959f9917e7 Mon Sep 17 00:00:00 2001 From: Kim Seung-yeop Date: Mon, 12 Aug 2024 18:18:51 +0900 Subject: [PATCH] =?UTF-8?q?SCRUM-40=20feat:=20=EB=A7=88=EC=9D=B4=ED=8E=98?= =?UTF-8?q?=EC=9D=B4=EC=A7=80=20=EC=88=98=EC=A0=95/=EC=A1=B0=ED=9A=8C=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EA=B0=9C=EB=B0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../buyer/controller/BuyerController.java | 37 ++++++++++++++ .../domain/buyer/dto/BuyerRequest.java | 25 ++++++++++ .../domain/buyer/dto/BuyerResponse.java | 20 ++++++++ .../golagola/domain/buyer/entity/Buyer.java | 7 +++ .../domain/buyer/service/BuyerService.java | 48 +++++++++++++++++++ 5 files changed, 137 insertions(+) create mode 100644 src/main/java/com/kakaoteck/golagola/domain/buyer/controller/BuyerController.java create mode 100644 src/main/java/com/kakaoteck/golagola/domain/buyer/dto/BuyerRequest.java create mode 100644 src/main/java/com/kakaoteck/golagola/domain/buyer/dto/BuyerResponse.java create mode 100644 src/main/java/com/kakaoteck/golagola/domain/buyer/service/BuyerService.java diff --git a/src/main/java/com/kakaoteck/golagola/domain/buyer/controller/BuyerController.java b/src/main/java/com/kakaoteck/golagola/domain/buyer/controller/BuyerController.java new file mode 100644 index 0000000..5531ec0 --- /dev/null +++ b/src/main/java/com/kakaoteck/golagola/domain/buyer/controller/BuyerController.java @@ -0,0 +1,37 @@ +package com.kakaoteck.golagola.domain.buyer.controller; + +import com.kakaoteck.golagola.domain.buyer.dto.BuyerRequest; +import com.kakaoteck.golagola.domain.buyer.dto.BuyerResponse; +import com.kakaoteck.golagola.domain.buyer.entity.Buyer; +import com.kakaoteck.golagola.domain.buyer.service.BuyerService; +import com.kakaoteck.golagola.global.common.ApiResponse; +import io.swagger.v3.oas.annotations.Operation; +import lombok.RequiredArgsConstructor; +import org.springframework.security.core.annotation.AuthenticationPrincipal; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequiredArgsConstructor +@RequestMapping("/api/v1/buyer") +public class BuyerController { + + private final BuyerService buyerService; + + @Operation(summary = "구매자 마이페이지 조회", description = "구매자의 정보를 조회합니다.") + @GetMapping("/mypage") + public ApiResponse getMyPage( + @AuthenticationPrincipal Buyer buyer + ) { + return ApiResponse.onSuccess(BuyerService.getMyPage(buyer)); + } + + @Operation(summary = "구매자 마이페이지 수정", description = "구매자의 정보를 수정합니다.") + @PutMapping("/mypage") + public ApiResponse updateProfile( + @AuthenticationPrincipal Buyer buyer, + @RequestBody BuyerRequest.MyPagePutDto request + ) { + return ApiResponse.onSuccess(buyerService.updateMyPage(buyer, request)); + } + +} diff --git a/src/main/java/com/kakaoteck/golagola/domain/buyer/dto/BuyerRequest.java b/src/main/java/com/kakaoteck/golagola/domain/buyer/dto/BuyerRequest.java new file mode 100644 index 0000000..21c20f3 --- /dev/null +++ b/src/main/java/com/kakaoteck/golagola/domain/buyer/dto/BuyerRequest.java @@ -0,0 +1,25 @@ +package com.kakaoteck.golagola.domain.buyer.dto; + +import com.kakaoteck.golagola.global.common.enums.Gender; +import com.kakaoteck.golagola.global.common.enums.Role; +import lombok.Builder; + +import java.time.LocalDate; + +public record BuyerRequest( + String nickname, + String realName, + Gender gender, + String email, + String address, + String phoneNum, + Role role, + LocalDate registerDate) { + + @Builder + public record MyPagePutDto( + String nickname, + String address, + String phoneNum + ) {} +} diff --git a/src/main/java/com/kakaoteck/golagola/domain/buyer/dto/BuyerResponse.java b/src/main/java/com/kakaoteck/golagola/domain/buyer/dto/BuyerResponse.java new file mode 100644 index 0000000..44a9505 --- /dev/null +++ b/src/main/java/com/kakaoteck/golagola/domain/buyer/dto/BuyerResponse.java @@ -0,0 +1,20 @@ +package com.kakaoteck.golagola.domain.buyer.dto; + +import com.kakaoteck.golagola.global.common.enums.Gender; +import com.kakaoteck.golagola.global.common.enums.Role; +import lombok.Builder; + +import java.time.LocalDate; + +@Builder +public record BuyerResponse( + String nickname, + String realName, + Gender gender, + String email, + String address, + String phoneNum, + Role role, + LocalDate registerDate +) { +} diff --git a/src/main/java/com/kakaoteck/golagola/domain/buyer/entity/Buyer.java b/src/main/java/com/kakaoteck/golagola/domain/buyer/entity/Buyer.java index 064acc2..ba8a7c4 100644 --- a/src/main/java/com/kakaoteck/golagola/domain/buyer/entity/Buyer.java +++ b/src/main/java/com/kakaoteck/golagola/domain/buyer/entity/Buyer.java @@ -1,5 +1,6 @@ package com.kakaoteck.golagola.domain.buyer.entity; +import com.kakaoteck.golagola.domain.buyer.dto.BuyerRequest; import com.kakaoteck.golagola.domain.cart.entity.Cart; import com.kakaoteck.golagola.domain.order.entity.Order; import com.kakaoteck.golagola.domain.review.entity.Review; @@ -100,6 +101,12 @@ public boolean isEnabled() { return UserDetails.super.isEnabled(); } + public void updateProfile(BuyerRequest.MyPagePutDto request) { + this.nickname = request.nickname(); + this.address = request.address(); + this.phoneNum = request.phoneNum(); + } + public static Buyer from(Long buyerId, String nickname, String realName, Gender gender, String email, String address, String phoneNum, Role role, LocalDate registerDate) { return Buyer.builder() diff --git a/src/main/java/com/kakaoteck/golagola/domain/buyer/service/BuyerService.java b/src/main/java/com/kakaoteck/golagola/domain/buyer/service/BuyerService.java new file mode 100644 index 0000000..50407b2 --- /dev/null +++ b/src/main/java/com/kakaoteck/golagola/domain/buyer/service/BuyerService.java @@ -0,0 +1,48 @@ +package com.kakaoteck.golagola.domain.buyer.service; + +import com.kakaoteck.golagola.domain.buyer.dto.BuyerRequest; +import com.kakaoteck.golagola.domain.buyer.dto.BuyerResponse; +import com.kakaoteck.golagola.domain.buyer.entity.Buyer; +import com.kakaoteck.golagola.domain.buyer.repository.BuyerRepository; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@RequiredArgsConstructor +@Transactional +@Slf4j +public class BuyerService { + + private final BuyerRepository buyerRepository; + + public static BuyerResponse getMyPage(Buyer buyer) { + return BuyerResponse.builder() + .email(buyer.getEmail()) + .role(buyer.getRole()) + .address(buyer.getAddress()) + .registerDate(buyer.getRegisterDate()) + .realName(buyer.getRealName()) + .gender(buyer.getGender()) + .phoneNum(buyer.getPhoneNum()) + .nickname(buyer.getNickname()) + .build(); + } + + public BuyerResponse updateMyPage(Buyer buyer, BuyerRequest.MyPagePutDto request) { + buyer.updateProfile(request); + Buyer savedBuyer = buyerRepository.save(buyer); + return BuyerResponse.builder() + .email(savedBuyer.getEmail()) + .role(savedBuyer.getRole()) + .address(savedBuyer.getAddress()) + .registerDate(savedBuyer.getRegisterDate()) + .realName(savedBuyer.getRealName()) + .gender(savedBuyer.getGender()) + .phoneNum(savedBuyer.getPhoneNum()) + .nickname(savedBuyer.getNickname()) + .build(); + + } +}