Skip to content

Commit

Permalink
Merge pull request #39 from Kakaotech-18-Ecommerce/SCRUM-41-seller-se…
Browse files Browse the repository at this point in the history
…rvice-develop

[Feat] Seller API 개발
  • Loading branch information
yeopyeop-82 authored Aug 12, 2024
2 parents 8fd015c + a6ea66c commit e18bfe8
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 12 deletions.
16 changes: 4 additions & 12 deletions src/main/java/com/kakaoteck/golagola/config/ApplicationConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,31 +36,23 @@ public UserDetailsService userDetailsService() {

if (buyer != null) {
System.out.println("Buyer found: " + username);
return new org.springframework.security.core.userdetails.User(
buyer.getEmail(),
buyer.getPassword(),
Collections.singleton(new SimpleGrantedAuthority("BUYER"))
);
return buyer; // Buyer 객체를 반환
}

// Try to find the user as a Seller
Seller seller = sellerRepository.findByEmail(username).orElse(null);

if (seller != null) {
System.out.println("Seller found: " + username);
System.out.println("Encoded password: " + seller.getPassword());
return new org.springframework.security.core.userdetails.User(
seller.getEmail(),
seller.getPassword(),
Collections.singleton(new SimpleGrantedAuthority("SELLER"))
);
System.out.println("Seller loaded: " + seller.getEmail());
return seller; // Seller 객체를 반환
}

throw new UsernameNotFoundException("User not found with username: " + username);
};
}



@Bean
public AuthenticationProvider authenticationProvider() {
DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.kakaoteck.golagola.domain.seller.controller;

import com.kakaoteck.golagola.domain.seller.dto.SellerRequest;
import com.kakaoteck.golagola.domain.seller.dto.SellerResponse;
import com.kakaoteck.golagola.domain.seller.entity.Seller;
import com.kakaoteck.golagola.domain.seller.repository.SellerRepository;
import com.kakaoteck.golagola.domain.seller.service.SellerService;
import com.kakaoteck.golagola.global.common.ApiResponse;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/seller")
@CrossOrigin("*")
public class SellerController {

private final SellerService sellerService;
private final SellerRepository sellerRepository;

@Operation(summary = "판매자 마이페이지 조회", description = "판매자의 정보를 조회합니다.")
@GetMapping("/mypage")
public ApiResponse<SellerResponse> getMyPage(@AuthenticationPrincipal Seller seller) {
return ApiResponse.onSuccess(sellerService.getMyPage(seller));
}

@Operation(summary = "판매자 마이페이지 수정", description = "판매자의 정보를 수정합니다.")
@PutMapping("/mypage")
public ApiResponse<SellerResponse> updateProfile(
@AuthenticationPrincipal Seller seller,
@RequestBody SellerRequest.MyPagePutDto request
) {
return ApiResponse.onSuccess(sellerService.updateMyPage(seller, request));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.kakaoteck.golagola.domain.seller.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 SellerRequest(
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.seller.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 SellerResponse(
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,8 +1,10 @@
package com.kakaoteck.golagola.domain.seller.entity;

import com.fasterxml.jackson.databind.ser.Serializers;
import com.kakaoteck.golagola.domain.buyer.dto.BuyerRequest;
import com.kakaoteck.golagola.domain.order.entity.Order;
import com.kakaoteck.golagola.domain.product.entity.Product;
import com.kakaoteck.golagola.domain.seller.dto.SellerRequest;
import com.kakaoteck.golagola.global.common.BaseEntity;
import com.kakaoteck.golagola.global.common.enums.Gender;
import com.kakaoteck.golagola.global.common.enums.Role;
Expand Down Expand Up @@ -103,6 +105,12 @@ public boolean isEnabled() {
return true;
}

public void updateProfile(SellerRequest.MyPagePutDto request) {
this.nickname = request.nickname();
this.address = request.address();
this.phoneNum = request.phoneNum();
}

public static Seller from(Long sellerId, String nickname, Gender gender, String email, String password,
String address, String phoneNum, Role role, String realName,
LocalDate registerDate, List<Product> productList, List<Order> orderList) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.kakaoteck.golagola.domain.seller.service;

import com.kakaoteck.golagola.domain.seller.dto.SellerRequest;
import com.kakaoteck.golagola.domain.seller.dto.SellerResponse;
import com.kakaoteck.golagola.domain.seller.entity.Seller;
import com.kakaoteck.golagola.domain.seller.repository.SellerRepository;
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 SellerService {

private final SellerRepository sellerRepository;

public SellerResponse getMyPage(Seller seller) {
return SellerResponse.builder()
.email(seller.getEmail())
.role(seller.getRole())
.address(seller.getAddress())
.registerDate(seller.getRegisterDate())
.realName(seller.getRealName())
.gender(seller.getGender())
.phoneNum(seller.getPhoneNum())
.nickname(seller.getNickname())
.build();
}

public SellerResponse updateMyPage(Seller seller, SellerRequest.MyPagePutDto request) {
seller.updateProfile(request);
Seller savedSeller = sellerRepository.save(seller);
return SellerResponse.builder()
.email(savedSeller.getEmail())
.role(savedSeller.getRole())
.address(savedSeller.getAddress())
.registerDate(savedSeller.getRegisterDate())
.realName(savedSeller.getRealName())
.gender(savedSeller.getGender())
.phoneNum(savedSeller.getPhoneNum())
.nickname(savedSeller.getNickname())
.build();
}
}

0 comments on commit e18bfe8

Please sign in to comment.