Skip to content

Commit

Permalink
@where, @SQLSelect 어노테이션을 JPA 방식으로 변환 (#38)
Browse files Browse the repository at this point in the history
* refactor: category 위치 이동
#36

* refactor: RequestMapping으로 묶음
#36

* refactor: Member 도메인 일부 수정
- SQLSelect 삭제
- isDeleted -> deletedAt
#36

* feature: 상품 등록 시 validation
- 농부인가?
- 존재하는 카테고리인가?
#36

* feature: 상품 단건 조회 & 다건 조회
#36

* feature: 상품 단건 조회 & 다건 조회
#36

* refactor: 요청 방식 수정
- 리소스 기반으로 표현
#36

* refactor: 요청 방식 수정
- 리소스 기반으로 표현
#36

* refactor: 파일 위치 수정
#36

* refactor: 파일 위치 수정
#36

* refactor: 중복코드 삭제
#36

* fix: 수량 업데이트 동작 안하는 부분 수정
#36

* feature: 상품 단건 조회
- 나중에 DTO는 바껴야할듯
#36

* feature: 회원 조회 조건 수정
- DeletedAtIsNull
#36
  • Loading branch information
stopmin authored Oct 2, 2024
1 parent a526089 commit 60b20f4
Show file tree
Hide file tree
Showing 22 changed files with 221 additions and 90 deletions.
22 changes: 0 additions & 22 deletions src/main/java/poomasi/domain/category/service/CategoryService.java

This file was deleted.

15 changes: 10 additions & 5 deletions src/main/java/poomasi/domain/member/entity/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.SQLSelect;

import java.time.LocalDateTime;

import static poomasi.domain.member.entity.LoginType.LOCAL;

@Getter
@Entity
@Table(name="member")
@Table(name = "member")
@NoArgsConstructor
@SQLDelete(sql = "UPDATE member SET deleted = true WHERE id = ?")
@SQLSelect(sql = "SELECT * FROM member WHERE deleted = false")
@SQLDelete(sql = "UPDATE member SET deleted_at = current_timestamp WHERE id = ?")
public class Member {

@Id
Expand Down Expand Up @@ -43,6 +43,8 @@ public class Member {
@OneToOne(mappedBy = "member", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private MemberProfile profile;

private LocalDateTime deletedAt;

public Member(String email, String password, LoginType loginType, Role role) {
this.email = email;
this.password = password;
Expand All @@ -57,9 +59,12 @@ public void setProfile(MemberProfile profile) {
}
}

public void kakaoToLocal(String password){
public void kakaoToLocal(String password) {
this.password = password;
this.loginType = LOCAL;
}

public boolean isFarmer() {
return role == Role.ROLE_FARMER;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ public interface MemberRepository extends JpaRepository<Member, Long> {
Optional<Member> findByEmailAndLoginType(String email, LoginType loginType);

Optional<Member> findByEmail(String email);
}

Optional<Member> findByIdAndDeletedAtIsNull(Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,13 @@ public void upgradeToFarmer(Long memberId, Boolean hasFarmerQualification) {
memberRepository.save(member);
}

public boolean isFarmer(Long memberId) {
Member member = findMemberById(memberId);
return member.isFarmer();
}

public Member findMemberById(Long memberId) {
return memberRepository.findById(memberId)
return memberRepository.findByIdAndDeletedAtIsNull(memberId)
.orElseThrow(() -> new BusinessException(MEMBER_NOT_FOUND));
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package poomasi.domain.category.controller;
package poomasi.domain.product._category.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import poomasi.domain.category.dto.CategoryRequest;
import poomasi.domain.category.service.CategoryAdminService;
import poomasi.domain.product._category.dto.CategoryRequest;
import poomasi.domain.product._category.service.CategoryAdminService;

@RestController
@RequiredArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package poomasi.domain.category.controller;
package poomasi.domain.product._category.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import poomasi.domain.category.dto.CategoryResponse;
import poomasi.domain.category.service.CategoryService;
import poomasi.domain.product._category.dto.CategoryResponse;
import poomasi.domain.product._category.service.CategoryService;

import java.util.List;
@RestController
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package poomasi.domain.category.dto;
package poomasi.domain.product._category.dto;

import poomasi.domain.category.entity.Category;
import poomasi.domain.product._category.entity.Category;

public record CategoryRequest(String name) {
public Category toEntity() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package poomasi.domain.category.dto;
package poomasi.domain.product._category.dto;

import poomasi.domain.category.entity.Category;
import poomasi.domain.product._category.entity.Category;

public record CategoryResponse(long id, String name) {
public static CategoryResponse fromEntity(Category category) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package poomasi.domain.category.entity;
package poomasi.domain.product._category.entity;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.Getter;
import lombok.NoArgsConstructor;
import poomasi.domain.category.dto.CategoryRequest;
import poomasi.domain.product._category.dto.CategoryRequest;

@Entity
@Getter
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package poomasi.domain.category.repository;
package poomasi.domain.product._category.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import poomasi.domain.category.entity.Category;
import poomasi.domain.product._category.entity.Category;

public interface CategoryRepository extends JpaRepository<Category, Long> {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package poomasi.domain.category.service;
package poomasi.domain.product._category.service;

import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import poomasi.domain.category.dto.CategoryRequest;
import poomasi.domain.category.entity.Category;
import poomasi.domain.category.repository.CategoryRepository;
import poomasi.domain.product._category.dto.CategoryRequest;
import poomasi.domain.product._category.entity.Category;
import poomasi.domain.product._category.repository.CategoryRepository;
import poomasi.global.error.BusinessError;
import poomasi.global.error.BusinessException;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package poomasi.domain.product._category.service;

import java.util.List;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import poomasi.domain.product._category.dto.CategoryResponse;
import poomasi.domain.product._category.entity.Category;
import poomasi.domain.product._category.repository.CategoryRepository;
import poomasi.global.error.BusinessError;
import poomasi.global.error.BusinessException;

@Service
@RequiredArgsConstructor
public class CategoryService {

private final CategoryRepository categoryRepository;

public List<CategoryResponse> getAllCategories() {
List<Category> categories = categoryRepository.findAll();
return categories.stream()
.map(CategoryResponse::fromEntity)
.toList();
}

public Category getCategory(Long categoryId) {
return categoryRepository.findById(categoryId)
.orElseThrow(() -> new BusinessException(BusinessError.CATEGORY_NOT_FOUND));

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import poomasi.domain.product.service.ProductAdminService;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/product")
public class ProductAdminController {

private final ProductAdminService productAdminService;

@PutMapping("/api/products/{productId}/open")
@PutMapping("/{productId}/open")
ResponseEntity<?> openProduct(@PathVariable Long productId) {
productAdminService.openProduct(productId);
return new ResponseEntity<>(productId, HttpStatus.OK);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package poomasi.domain.product.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import poomasi.domain.product.service.ProductService;
import poomasi.domain.product.dto.ProductResponse;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/product")
public class ProductController {
private final ProductService productService;

@GetMapping("")
public ResponseEntity<?> getAllProducts() {
List<ProductResponse> products = productService.getAllProducts();
return new ResponseEntity<>(products, HttpStatus.OK);
}

@GetMapping("/{productId}")
public ResponseEntity<ProductResponse> getProduct(@PathVariable Long productId) {
ProductResponse product = productService.getProductByProductId(productId);
return ResponseEntity.ok(product);
}
}
Original file line number Diff line number Diff line change
@@ -1,48 +1,52 @@
package poomasi.domain.product.controller;

import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
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.RestController;
import org.springframework.web.bind.annotation.*;
import poomasi.domain.product.dto.ProductRegisterRequest;
import poomasi.domain.product.dto.UpdateProductQuantityRequest;
import poomasi.domain.product.service.ProductFarmerService;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/product")
@Slf4j
public class ProductFarmerController {

private final ProductFarmerService productFarmerService;

@PostMapping("/api/products")
@PostMapping("")
public ResponseEntity<?> registerProduct(@RequestBody ProductRegisterRequest product) {
Long productId = productFarmerService.registerProduct(product);
return new ResponseEntity<>(productId, HttpStatus.CREATED);
}

@PutMapping("/api/products/{productId}")
@PutMapping("/{productId}")
public ResponseEntity<?> modifyProduct(@RequestBody ProductRegisterRequest product,
@PathVariable Long productId) {
productFarmerService.modifyProduct(product, productId);
return new ResponseEntity<>(productId, HttpStatus.OK);
}

@DeleteMapping("/api/products/{productId}")
@DeleteMapping("/{productId}")
public ResponseEntity<?> deleteProduct(@PathVariable Long productId) {
// TODO: farmerId를 SecurityContextHolder에서 가져와서 비교해야함.

productFarmerService.deleteProduct(productId);
return new ResponseEntity<>(HttpStatus.OK);
}

@PatchMapping("/api/products/{productId}/count/{quantity}")
public ResponseEntity<?> addQuantity(@PathVariable Long productId,
@PathVariable Integer quantity) {
productFarmerService.addQuantity(productId, quantity);

@PatchMapping("/{productId}")
public ResponseEntity<?> updateProductQuantity(@PathVariable Long productId,
@RequestBody UpdateProductQuantityRequest request) {
log.debug("Product ID: {}", productId);
log.debug("Update Request: {}", request);
productFarmerService.addQuantity(productId, request);
return new ResponseEntity<>(HttpStatus.OK);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ public record ProductRegisterRequest(
String name,
String description,
String imageUrl,
int quantity,
String price
int stock,
Long price
) {

public Product toEntity() {
Expand All @@ -19,7 +19,7 @@ public Product toEntity() {
.name(name)
.description(description)
.imageUrl(imageUrl)
.quantity(quantity)
.stock(stock)
.price(price)
.build();
}
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/poomasi/domain/product/dto/ProductResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package poomasi.domain.product.dto;

import lombok.Builder;
import poomasi.domain.product.entity.Product;

@Builder
public record ProductResponse(
long id,
String name,
Long price,
int stock,
String description,
String imageUrl,
long categoryId
) {
public static ProductResponse fromEntity(Product product) {
return ProductResponse.builder()
.id(product.getId())
.name(product.getName())
.price(product.getPrice())
.stock(product.getStock())
.description(product.getDescription())
.imageUrl(product.getImageUrl())
.categoryId(product.getCategoryId())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package poomasi.domain.product.dto;

public record UpdateProductQuantityRequest(Integer quantity) {
}

Loading

0 comments on commit 60b20f4

Please sign in to comment.