Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ repositories {
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.data:spring-data-jpa:2.7.2'
implementation 'mysql:mysql-connector-java:8.0.30'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa:2.7.3'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ public class CategoryController {
@PostMapping("/category")
@ResponseBody
public CategoryResponse create(@RequestBody CategoryRequest request) {
Long id = service.create(request);
Long id = service.create(request);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

잘했는데 전반적으로 네이밍을 조금만 더 신경쓰면 좋을거 같아
우선 그냥 service라 던지 혹인 categoryRepository를 cateRepository라 쓰는 것과 같이 줄여쓰는게, 혼자 개발을 할때는 헷갈리지 않을 수 있어도 협업을 하거나 다른 누군가가 보기에는 가독성이 좋지 않다고 나는 생각해
그래서 웬만하면 네이밍에 있어서 줄임말은 쓰지 않는게좋다고 생각합니당


if(id == -1L) {
return new CategoryResponse(-1, null);
}
if(id == -1L) {
return new CategoryResponse(-1, null);
}

return new CategoryResponse(0, request);
return new CategoryResponse(0, request);
}

@GetMapping("/categories")
@ResponseBody
public CategoriesResponse findAll() {
List<Category> categoryList = service.findAll();
List<Category> categoryList = service.getCategoryList();
return new CategoriesResponse(categoryList.size(), categoryList);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
public class ProductController {

private final ProductService service;
private final CategoryService categoryService;

@PostMapping("/product")
@ResponseBody
public ProductResponse create(@RequestBody ProductRequest request) {
Long id = service.create(request);
Long id = service.postProduct(request);

if(id == -1) {
return new ProductResponse(-1, null);
}
Expand All @@ -32,7 +32,7 @@ public ProductResponse create(@RequestBody ProductRequest request) {
@GetMapping("/products")
@ResponseBody
public ProductsResponse findAll() {
List<Product> productsList = service.findAll();
List<Product> productsList = service.getProductList();
return new ProductsResponse(productsList.size(), productsList);
}

Expand All @@ -41,7 +41,17 @@ public ProductsResponse findAll() {
public ProductDetailResponse findDetails(@PathVariable ("productId") Long productId) {

Product product = service.findById(productId);
Category category = categoryService.findById(product.getCategoryId());
return new ProductDetailResponse(product.getId(), product.getCategoryId(), category.getName(), product.getName(),product.getDescription(), product.getCreatedAt());
Category category = product.getCategory();

return ProductDetailResponse.builder()
.id(product.getId())
.categoryId(category.getId())
.categoryName(category.getName())
.name(product.getName())
.description(product.getDescription())
.createdAt(product.getCreatedAt())
.build();

}
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package com.landvive.summer.mvc.dto.response;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.*;

import java.time.LocalDateTime;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class ProductDetailResponse {

private Long id;
Expand Down
22 changes: 16 additions & 6 deletions src/main/java/com/landvive/summer/mvc/entity/Category.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
package com.landvive.summer.mvc.entity;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.*;

import javax.persistence.*;
import java.util.List;

@Entity
@Getter
@Setter
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

엔티티를 설정함에 있어서 setter의 사용은 지양하는 편이 좋습니다
setter를 설정하게 되면 public으로 설정되기 때문에 어디서든 수정이 일어날 수있고 일관성을 유지하기 어려울 수 있고 어떤 기능인지 정확히 인지하기 어려울 수 있습니당
그래서 setter 보다는 직접 알아보기 좋은 네이밍을 통해 함수를 만들어 수정 기능을 넣어주는 것이 좋습니다~!

@Builder
@AllArgsConstructor
@NoArgsConstructor
@Table(name= "category")
public class Category {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY) // AI 형식으로 만들어짐!
private Long id;

private String name;
private Long count; //해당 카테고리의 Product 개수
}

@JsonIgnore
@OneToMany(mappedBy = "category")
private List<Product> productList;
}
19 changes: 13 additions & 6 deletions src/main/java/com/landvive/summer/mvc/entity/Product.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
package com.landvive.summer.mvc.entity;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.*;

import javax.persistence.*;
import java.time.LocalDateTime;

@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name= "product")
public class Product {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY) // AI 형식으로 만들어짐!
private Long id;
private Long categoryId;

private String name;
private String description;
private LocalDateTime createdAt;

}
@ManyToOne
@JoinColumn(name="categoryId")
private Category category;
}
16 changes: 0 additions & 16 deletions src/main/java/com/landvive/summer/mvc/entity/User.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.landvive.summer.mvc.repository;

import com.landvive.summer.mvc.entity.Category;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.Optional;

public interface CateRepository extends JpaRepository<Category, Long> {

@Query(value = "SELECT c FROM Category c WHERE c.name = :name")
Optional<Category> findByName2(String name);

@Modifying
@Query("UPDATE Category c SET c.count = c.count+1 WHERE c.id = :categoryId")
void plusProductCount(Long categoryId);

}

This file was deleted.

This file was deleted.

This file was deleted.

Loading