Skip to content

Commit

Permalink
Merge pull request #27 from Kakaotech-18-Ecommerce/SCRUM-40-buyer-ser…
Browse files Browse the repository at this point in the history
…vice-develop

[Feat] Buyer 서비스 개발
  • Loading branch information
yeopyeop-82 authored Aug 12, 2024
2 parents 1aae670 + 18cd809 commit faf5efc
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<BuyerResponse> getMyPage(
@AuthenticationPrincipal Buyer buyer
) {
return ApiResponse.onSuccess(BuyerService.getMyPage(buyer));
}

@Operation(summary = "구매자 마이페이지 수정", description = "구매자의 정보를 수정합니다.")
@PutMapping("/mypage")
public ApiResponse<BuyerResponse> updateProfile(
@AuthenticationPrincipal Buyer buyer,
@RequestBody BuyerRequest.MyPagePutDto request
) {
return ApiResponse.onSuccess(buyerService.updateMyPage(buyer, request));
}

}
Original file line number Diff line number Diff line change
@@ -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
) {}
}
Original file line number Diff line number Diff line change
@@ -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
) {
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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();

}
}

0 comments on commit faf5efc

Please sign in to comment.