Skip to content

Commit

Permalink
Feat: 상품에 지역 정보 추가 (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
GGHDMS authored Feb 22, 2024
1 parent 8477a6e commit 4b75698
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 10 deletions.
12 changes: 9 additions & 3 deletions src/main/java/potato/server/product/domain/Product.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import lombok.NoArgsConstructor;
import potato.server.common.BaseTimeEntity;
import potato.server.product.dto.request.ProductUpdateRequest;
import potato.server.town.domain.Town;

import java.math.BigDecimal;

/**
Expand Down Expand Up @@ -37,22 +39,26 @@ public class Product extends BaseTimeEntity {
@Column(length = 1000, nullable = false)
private String description;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "town_id")
private Town town;

private Integer hit;
private Integer stock;

@Version
private Integer version;

@Builder
public Product(Category category, String title, BigDecimal price, String description, Integer hit, Integer stock, Integer version) {
public Product(Category category, String title, BigDecimal price, String description, Town town, Integer stock, Integer version) {
this.category = category;
this.title = title;
this.price = price;
this.description = description;
this.town = town;
this.hit = 0;
this.stock = stock;
this.version = 0;

}

public void addHit(){
Expand All @@ -69,4 +75,4 @@ public void updateProduct(ProductUpdateRequest productUpdateRequest) {
if (productUpdateRequest.getStock() != null)
this.stock = productUpdateRequest.getStock();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,9 @@ public class ProductCreateRequest {

@NotNull
private Integer stock;

@NotNull
private String townName;

private List<Long> productImageIds = new ArrayList<>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,17 @@ public class ProductResponse {
private String description;
private Integer hit;
private Integer stock;
private String townName;
private Integer version;

@QueryProjection
public ProductResponse(String title, BigDecimal price, String description, Integer stock, Integer version, Integer hit) {
public ProductResponse(String title, BigDecimal price, String description, Integer stock, Integer version, String townName, Integer hit) {
this.title = title;
this.price = price;
this.description = description;
this.stock = stock;
this.version = version;
this.townName = townName;
this.hit = hit;
}

Expand All @@ -51,6 +53,7 @@ public static ProductResponse of(Product product) {
product.getDescription(),
product.getStock(),
product.getVersion(),
product.getTown().getName(),
product.getHit());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
import lombok.RequiredArgsConstructor;
import potato.server.product.dto.response.ProductResponse;
import potato.server.product.dto.response.QProductResponse;

import java.util.List;

import static potato.server.product.domain.QProduct.product;
import static potato.server.town.domain.QTown.town;

/**
* @author: 박건휘
Expand All @@ -28,10 +31,12 @@ public List<ProductResponse> findAllProducts() {
product.description,
product.hit,
product.stock,
product.town.name,
product.version
))
.from(product)
.join(product.town, town)
.orderBy(product.createdDate.asc())
.fetch();
}
}
}
10 changes: 9 additions & 1 deletion src/main/java/potato/server/product/service/ProductService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package potato.server.product.service;

import jakarta.persistence.EntityNotFoundException;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.http.HttpStatus;
Expand All @@ -15,6 +16,8 @@
import potato.server.product.dto.response.ProductResponse;
import potato.server.product.repository.ProductImageRepository;
import potato.server.product.repository.ProductRepository;
import potato.server.town.domain.Town;
import potato.server.town.repository.TownRepository;

import java.util.List;

Expand All @@ -28,14 +31,19 @@
public class ProductService {
private final ProductRepository productRepository;
private final ProductImageRepository productImageRepository;
private final TownRepository townRepository;

@Transactional
public void createProduct(ProductCreateRequest request) {
Town town = townRepository.findByName(request.getTownName())
.orElseThrow(() -> new EntityNotFoundException("마을 정보가 없습니다"));

Product product = Product.builder()
.price(request.getPrice())
.title(request.getTitle())
.description(request.getDescription())
.stock(request.getStock())
.town(town)
.build();
productRepository.save(product);

Expand Down Expand Up @@ -89,4 +97,4 @@ public ProductResponse updateProduct(ProductUpdateRequest productUpdateRequest)
public List<ProductResponse> getAllProducts() {
return productRepository.findAllProducts();
}
}
}
22 changes: 22 additions & 0 deletions src/main/java/potato/server/town/domain/Town.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package potato.server.town.domain;

import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;

/**
* @author 허석문
* @since 2024-02-22
*/
@Entity
@Getter
@NoArgsConstructor
public class Town {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "town_id")
private Long id;

private String name;
}
10 changes: 10 additions & 0 deletions src/main/java/potato/server/town/repository/TownRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package potato.server.town.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import potato.server.town.domain.Town;

import java.util.Optional;

public interface TownRepository extends JpaRepository<Town, Long> {
Optional<Town> findByName(String name);
}
4 changes: 2 additions & 2 deletions src/main/resources/logback-spring.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
<springProperty name="SLACK_WEBHOOK_URI" source="logging.slack.webhook-uri"/>
<springProperty name="SLACK_WEBHOOK_URI" source="logging.slack.webhook-url"/>
<appender name="SLACK" class="com.github.maricn.logback.SlackAppender">
<webhookUri>${SLACK_WEBHOOK_URI}</webhookUri>
<layout class="ch.qos.logback.classic.PatternLayout">
Expand Down Expand Up @@ -28,4 +28,4 @@
<appender-ref ref="Console" />
<appender-ref ref="ASYNC_SLACK"/>
</root>
</configuration>
</configuration>

0 comments on commit 4b75698

Please sign in to comment.