Skip to content

Commit

Permalink
store생성하기 (#104)
Browse files Browse the repository at this point in the history
* feat: store 생성

* refactor: store를 product에서 빼기

* chore: confilct 나면서 이상한 잡버그 수정
  • Loading branch information
canyos authored Nov 9, 2024
1 parent d12704e commit f92ef30
Show file tree
Hide file tree
Showing 20 changed files with 486 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package poomasi.domain.auth.signup.service;

import jdk.jfr.Description;
import lombok.RequiredArgsConstructor;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import poomasi.domain.member.dto.request.SignupRequest;
import poomasi.domain.member.dto.response.SignUpResponse;
import poomasi.domain.member.entity.LoginType;
import poomasi.domain.member.repository.MemberRepository;
import poomasi.domain.member.entity.Member;
import poomasi.global.error.BusinessException;

import static poomasi.domain.member.entity.Role.ROLE_CUSTOMER;
import static poomasi.domain.member.entity.Role.ROLE_FARMER;
import static poomasi.global.error.BusinessError.*;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class SignupService {

private final MemberRepository memberRepository;
private final PasswordEncoder passwordEncoder;

@Description("카카오톡으로 먼저 회원가입이 되어 있는 경우, 계정 연동을 진행합니다. ")
@Transactional
public SignUpResponse signUp(SignupRequest signupRequest) {
String email = signupRequest.email();
String password = signupRequest.password();

memberRepository.findByEmail(email)
.ifPresent(member -> { throw new BusinessException(DUPLICATE_MEMBER_EMAIL); });

Member newMember = Member.builder()
.email(email)
.password(passwordEncoder.encode(password))
.loginType(LoginType.LOCAL)
.role(ROLE_FARMER)
.build();
memberRepository.save(newMember);
return new SignUpResponse(email, "회원 가입 성공");
}
}

24 changes: 19 additions & 5 deletions src/main/java/poomasi/domain/member/entity/Member.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package poomasi.domain.member.entity;

import jakarta.persistence.*;
import java.time.LocalDateTime;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.annotations.SQLDelete;
import poomasi.domain.order.entity.Order;
import poomasi.domain.store.entity.Store;
import poomasi.domain.member._profile.entity.MemberProfile;
import poomasi.domain.wishlist.entity.WishList;

import java.time.LocalDateTime;
import java.util.List;
import poomasi.global.error.BusinessError;
import poomasi.global.error.BusinessException;
import java.util.*;

@Getter
@Entity
Expand Down Expand Up @@ -49,7 +51,7 @@ public class Member {
@OneToMany(mappedBy = "member", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<WishList> wishLists;

@Column(name="deleted_at")
@Column(name = "deleted_at")
private LocalDateTime deletedAt;

@OneToMany(mappedBy = "member", cascade = CascadeType.ALL, orphanRemoval = true)
Expand All @@ -59,6 +61,10 @@ public class Member {
@Column(nullable = true)
private String farmerTierCode;

@Setter
@OneToOne(mappedBy="owner", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
Store store;

public Member(String email, String password, LoginType loginType, Role role) {
this.email = email;
this.password = password;
Expand All @@ -79,8 +85,9 @@ public void setMemberProfile(MemberProfile memberProfile) {
}

@Builder
public Member(Long id, String email, Role role, LoginType loginType, String provideId, MemberProfile memberProfile) {
public Member(Long id, String email, String password, Role role, LoginType loginType, String provideId, MemberProfile memberProfile) {
this.id = id;
this.password = password;
this.email = email;
this.role = role;
this.loginType = loginType;
Expand All @@ -99,4 +106,11 @@ public boolean isFarmer() {
public boolean isAdmin() {
return role == Role.ROLE_ADMIN;
}

public Store getStore() {
if(store == null)
throw new BusinessException(BusinessError.STORE_NOT_FOUND);
return store;
}

}
3 changes: 2 additions & 1 deletion src/main/java/poomasi/domain/product/_cart/entity/Cart.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public class Cart {
private Integer count;

@Builder
public Cart(Long memberId, Long productId, Boolean selected, Integer count) {
public Cart(Long id, Long memberId, Long productId, Boolean selected, Integer count) {
this.id = id;
this.memberId = memberId;
this.productId = productId;
this.selected = selected;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import jakarta.persistence.OneToMany;
import java.util.ArrayList;
import java.util.List;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import poomasi.domain.product._category.dto.CategoryRequest;
Expand All @@ -28,6 +29,13 @@ public class Category {
@JoinColumn(name = "categoryId")
List<Product> products = new ArrayList<>();

@Builder
public Category(Long id, String name) {
this.id = id;
this.name = name;
this.products = new ArrayList<>();
}

public Category(String name) {
this.name = name;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package poomasi.domain.product.dto;

import poomasi.domain.member.entity.Member;
import poomasi.domain.store.entity.Store;
import poomasi.domain.product.entity.Product;

public record ProductRegisterRequest(
Expand All @@ -12,7 +13,7 @@ public record ProductRegisterRequest(
Long price
) {

public Product toEntity(Member member) {
public Product toEntity(Member member, Store store) {
return Product.builder()
.categoryId(categoryId)
.farmerId(member.getId())
Expand All @@ -22,6 +23,7 @@ public Product toEntity(Member member) {
.imageUrl(imageUrl)
.stock(stock)
.price(price)
.store(store)
.build();
}
}
2 changes: 2 additions & 0 deletions src/main/java/poomasi/domain/product/dto/ProductResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public record ProductResponse(
String description,
String imageUrl,
Long categoryId,
String storeName,
List<String> tags
) {

Expand All @@ -27,6 +28,7 @@ public static ProductResponse fromEntity(Product product) {
.stock(product.getStock())
.description(product.getDescription())
.imageUrl(product.getImageUrl())
.storeName(product.getStore().getName())
.categoryId(product.getCategoryId())
.tags(tags)
.build();
Expand Down
22 changes: 10 additions & 12 deletions src/main/java/poomasi/domain/product/entity/Product.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import java.time.LocalDateTime;
import java.util.ArrayList;
Expand All @@ -21,9 +22,9 @@
import lombok.NoArgsConstructor;
import org.hibernate.annotations.Comment;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.UpdateTimestamp;
import poomasi.domain.order.entity.OrderProductDetails;
import poomasi.domain.store.entity.Store;
import poomasi.domain.product.dto.ProductRegisterRequest;
import poomasi.domain.review.entity.Review;

Expand Down Expand Up @@ -71,6 +72,10 @@ public class Product {
@JoinColumn(name = "entityId")
List<Review> reviewList = new ArrayList<>();

@ManyToOne
@JoinColumn(name = "store_id") // 외래 키 컬럼 지정
private Store store;

@ElementCollection(fetch = FetchType.LAZY)
@CollectionTable(name = "product_tag", joinColumns = @JoinColumn(name = "product_id"))
@Column(name = "enum_value")
Expand All @@ -93,14 +98,17 @@ public Product(Long productId,
String description,
String imageUrl,
Integer stock,
Long price) {
Long price,
Store store) {
this.id = productId;
this.categoryId = categoryId;
this.farmerId = farmerId;
this.name = name;
this.description = description;
this.imageUrl = imageUrl;
this.stock = stock;
this.price = price;
this.store = store;
}

public Product modify(ProductRegisterRequest productRegisterRequest) {
Expand All @@ -117,14 +125,4 @@ public void addStock(Integer stock) {
this.stock += stock;
}

public void addReview(Review pReview) {
this.reviewList.add(pReview);
this.averageRating = reviewList.stream()
.mapToDouble(Review::getRating) // 각 리뷰의 평점을 double로 변환
.average() // 평균 계산
.orElse(0.0);
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import poomasi.domain.member.entity.Member;
import poomasi.domain.product._category.entity.Category;
import poomasi.domain.product._category.repository.CategoryRepository;
import poomasi.domain.store.entity.Store;
import poomasi.domain.store.repository.StoreRepository;
import poomasi.domain.product.dto.ProductRegisterRequest;
import poomasi.domain.product.dto.UpdateProductQuantityRequest;
import poomasi.domain.product.entity.Product;
Expand All @@ -19,12 +21,16 @@ public class ProductFarmerService {

private final ProductRepository productRepository;
private final CategoryRepository categoryRepository;
private final StoreRepository storeRepository;

@Transactional
public Long registerProduct(Member member, ProductRegisterRequest request) {
Category category = getCategory(request.categoryId());
Product saveProduct = productRepository.save(request.toEntity(member));
Store store = member.getStore();
Product saveProduct = productRepository.save(request.toEntity(member,store));

category.addProduct(saveProduct);
store.addProduct(saveProduct);
return saveProduct.getId();
}

Expand All @@ -47,7 +53,6 @@ public void modifyProduct(Member member, ProductRegisterRequest productRequest,

@Transactional
public void deleteProduct(Member member, Long productId) {
//TODO: 주인인지 알아보기
Product product = getProductByProductId(productId);
checkAuth(member, product);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static ReviewResponse fromEntity(Review review) {
return new ReviewResponse(
review.getId(),
review.getEntityId(),
review.getReviewer().getMemberProfile().getName(),
review.getReviewer().getMemberProfile()==null?"":review.getReviewer().getMemberProfile().getName(),
review.getRating(),
review.getContent()
);
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/poomasi/domain/review/entity/Review.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ public class Review {
private Member reviewer;

@Builder
public Review(Float rating, String content, Long entityId, EntityType entityType,
public Review(Long id, Float rating, String content, Long entityId, EntityType entityType,
Member reviewer) {
this.id = id;
this.rating = rating;
this.content = content;
this.entityId = entityId;
Expand Down
41 changes: 41 additions & 0 deletions src/main/java/poomasi/domain/store/controller/StoreController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package poomasi.domain.store.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import poomasi.domain.store.dto.StoreRegisterRequest;
import poomasi.domain.store.service.StoreService;

@Controller
@RequiredArgsConstructor
@RequestMapping("/api/store")
public class StoreController {

private final StoreService storeService;

@Secured("ROLE_FARMER")
@PostMapping("")
public ResponseEntity<?> addStore(@RequestBody StoreRegisterRequest storeRegisterRequest) {
storeService.addStore(storeRegisterRequest);
return ResponseEntity.ok().build();
}

@Secured("ROLE_FARMER")
@GetMapping("")
public ResponseEntity<?> getStore() {
return ResponseEntity.ok(storeService.getStore());
}

@Secured("ROLE_FARMER")
@PutMapping("")
public ResponseEntity<?> updateStore(@RequestBody StoreRegisterRequest storeRegisterRequest) {
storeService.updateStore(storeRegisterRequest);
return ResponseEntity.ok().build();
}
}
7 changes: 7 additions & 0 deletions src/main/java/poomasi/domain/store/dto/StoreFeeRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package poomasi.domain.store.dto;

public record StoreFeeRequest(
Integer fee
) {

}
29 changes: 29 additions & 0 deletions src/main/java/poomasi/domain/store/dto/StoreRegisterRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package poomasi.domain.store.dto;

import org.hibernate.annotations.Comment;
import poomasi.domain.member.entity.Member;
import poomasi.domain.store.entity.Store;

public record StoreRegisterRequest(
String name,
String address,
String phone,
String ownerPhone,
@Comment("사업자 번호")
String businessNumber,
@Comment("배송비")
Integer shipingFee
) {

public Store toEntity(Member member) {
return Store.builder()
.name(name)
.address(address)
.phone(phone)
.ownerPhone(ownerPhone)
.businessNumber(businessNumber)
.shipingFee(shipingFee)
.owner(member)
.build();
}
}
Loading

0 comments on commit f92ef30

Please sign in to comment.