From 21be2b7d3036d5fb6a71545763cbeb5277b3e436 Mon Sep 17 00:00:00 2001 From: bong6981 Date: Tue, 20 Apr 2021 17:37:32 +0900 Subject: [PATCH] feat: Add Constructor and useful methods MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Spring Data JDBC의 추천에 따라 생성자와 팩토리 메서드 와 get메서드 추가 issue: #4 --- .../com/team10/banchan/model/Category.java | 29 +++- .../team10/banchan/model/DetailSection.java | 14 +- .../java/com/team10/banchan/model/Item.java | 151 +++++++++++++++--- .../com/team10/banchan/model/Section.java | 29 +++- .../com/team10/banchan/model/ThumbImage.java | 14 +- .../repository/SectionRepositoryTest.java | 22 +++ 6 files changed, 232 insertions(+), 27 deletions(-) create mode 100644 BE/src/test/java/com/team10/banchan/repository/SectionRepositoryTest.java diff --git a/BE/src/main/java/com/team10/banchan/model/Category.java b/BE/src/main/java/com/team10/banchan/model/Category.java index 8c9890dbb..223072af8 100644 --- a/BE/src/main/java/com/team10/banchan/model/Category.java +++ b/BE/src/main/java/com/team10/banchan/model/Category.java @@ -2,11 +2,34 @@ import org.springframework.data.annotation.Id; +import java.util.HashSet; import java.util.Set; public class Category { @Id - private Long id; - private Set items; - private String name; + private final Long id; + private final String name; + private final Set items; + + Category(Long id, String name, Set items) { + this.id = id; + this.name = name; + this.items = items; + } + + public Long getId() { + return id; + } + + public Set getItems() { + return items; + } + + public String getName() { + return name; + } + + public static Category newCategory(String name) { + return new Category(null, name, new HashSet<>()); + } } diff --git a/BE/src/main/java/com/team10/banchan/model/DetailSection.java b/BE/src/main/java/com/team10/banchan/model/DetailSection.java index b2b97b4d4..001a60a53 100644 --- a/BE/src/main/java/com/team10/banchan/model/DetailSection.java +++ b/BE/src/main/java/com/team10/banchan/model/DetailSection.java @@ -1,5 +1,17 @@ package com.team10.banchan.model; public class DetailSection { - private String url; + private final String url; + + DetailSection(String url) { + this.url = url; + } + + public String getUrl() { + return url; + } + + public static DetailSection of(String url) { + return new DetailSection(url); + } } diff --git a/BE/src/main/java/com/team10/banchan/model/Item.java b/BE/src/main/java/com/team10/banchan/model/Item.java index e42a363d6..c00615943 100644 --- a/BE/src/main/java/com/team10/banchan/model/Item.java +++ b/BE/src/main/java/com/team10/banchan/model/Item.java @@ -1,30 +1,143 @@ package com.team10.banchan.model; - import org.springframework.data.annotation.Id; import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.HashSet; import java.util.List; import java.util.Set; public class Item { @Id - private Long id; - private Long section; - private Long category; - - private String alt; - private String topImage; - private String title; - private String description; - private BigDecimal nPrice; - private BigDecimal sPrice; - private BigDecimal deliveryFee; - private Integer stock; - - private List detailSections; - private List thumbImages; - private Set badges; - private Set deliveryTypes; - private Set deliveryDays; + private final Long id; + private final Long section; + private final Long category; + + private final String alt; + private final String topImage; + private final String title; + private final String description; + private final BigDecimal nPrice; + private final BigDecimal sPrice; + private final BigDecimal deliveryFee; + private final Integer stock; + + private final List detailSections; + private final List thumbImages; + private final Set badges; + private final Set deliveryTypes; + private final Set deliveryDays; + + Item(Long id, Long section, Long category, String alt, String topImage, String title, String description, BigDecimal nPrice, BigDecimal sPrice, BigDecimal deliveryFee, Integer stock, List detailSections, List thumbImages, Set badges, Set deliveryTypes, Set deliveryDays) { + this.id = id; + this.section = section; + this.category = category; + this.alt = alt; + this.topImage = topImage; + this.title = title; + this.description = description; + this.nPrice = nPrice; + this.sPrice = sPrice; + this.deliveryFee = deliveryFee; + this.stock = stock; + this.detailSections = detailSections; + this.thumbImages = thumbImages; + this.badges = badges; + this.deliveryTypes = deliveryTypes; + this.deliveryDays = deliveryDays; + } + + public Long getId() { + return id; + } + + public Long getSection() { + return section; + } + + public Long getCategory() { + return category; + } + + public String getAlt() { + return alt; + } + + public String getTopImage() { + return topImage; + } + + public String getTitle() { + return title; + } + + public String getDescription() { + return description; + } + + public BigDecimal getNPrice() { + return nPrice; + } + + public BigDecimal getSPrice() { + return sPrice; + } + + public BigDecimal getDeliveryFee() { + return deliveryFee; + } + + public Integer getStock() { + return stock; + } + + public List getDetailSections() { + return detailSections; + } + + public List getThumbImages() { + return thumbImages; + } + + public Set getBadges() { + return badges; + } + + public Set getDeliveryTypes() { + return deliveryTypes; + } + + public Set getDeliveryDays() { + return deliveryDays; + } + + public void addDetailSection (DetailSection detailSection) { + this.detailSections.add(detailSection); + } + + public void addThumbImage (ThumbImage thumbImage) { + this.thumbImages.add(thumbImage); + } + + public void addBadge(Badge badge) { + this.badges.add(badge); + } + + public void addDeliveryType(DeliveryType deliveryType) { + this.deliveryTypes.add(deliveryType); + } + + public void addDeliveryDay(DeliveryDay deliveryDay) { + this.deliveryDays.add(deliveryDay); + } + + public static Item newItem(Long section, Long category, + String alt, String topImage, String title, String description, + BigDecimal nPrice, BigDecimal sPrice, BigDecimal deliveryFee, Integer stock) { + return new Item(null, section, category, + alt, topImage, title, description, + nPrice, sPrice, deliveryFee, stock, + new ArrayList<>(), new ArrayList<>(), new HashSet<>(), new HashSet<>(), new HashSet<>()); + } } diff --git a/BE/src/main/java/com/team10/banchan/model/Section.java b/BE/src/main/java/com/team10/banchan/model/Section.java index 1ef27b7a4..ac8c51d02 100644 --- a/BE/src/main/java/com/team10/banchan/model/Section.java +++ b/BE/src/main/java/com/team10/banchan/model/Section.java @@ -2,11 +2,34 @@ import org.springframework.data.annotation.Id; +import java.util.HashSet; import java.util.Set; public class Section { @Id - private Long id; - private Set items; - private String name; + private final Long id; + private final String name; + private final Set items; + + Section(Long id, String name, Set items) { + this.id = id; + this.name = name; + this.items = items; + } + + public Long getId() { + return id; + } + + public Set getItems() { + return items; + } + + public String getName() { + return name; + } + + public static Section newSection(String name) { + return new Section(null, name, new HashSet<>()); + } } diff --git a/BE/src/main/java/com/team10/banchan/model/ThumbImage.java b/BE/src/main/java/com/team10/banchan/model/ThumbImage.java index d6238738f..ebe57da84 100644 --- a/BE/src/main/java/com/team10/banchan/model/ThumbImage.java +++ b/BE/src/main/java/com/team10/banchan/model/ThumbImage.java @@ -1,5 +1,17 @@ package com.team10.banchan.model; public class ThumbImage { - private String url; + private final String url; + + ThumbImage(String url) { + this.url = url; + } + + public String getUrl() { + return url; + } + + public static ThumbImage of(String url) { + return new ThumbImage(url); + } } diff --git a/BE/src/test/java/com/team10/banchan/repository/SectionRepositoryTest.java b/BE/src/test/java/com/team10/banchan/repository/SectionRepositoryTest.java new file mode 100644 index 000000000..b3cf70801 --- /dev/null +++ b/BE/src/test/java/com/team10/banchan/repository/SectionRepositoryTest.java @@ -0,0 +1,22 @@ +package com.team10.banchan.repository; + +import com.team10.banchan.model.Section; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.transaction.annotation.Transactional; + +import static org.assertj.core.api.Assertions.*; + +@SpringBootTest +@Transactional +public class SectionRepositoryTest { + @Autowired + private SectionRepository sectionRepository; + + @Test + void getSection() { + Section section = sectionRepository.findById(1L).orElseThrow(RuntimeException::new); + assertThat(section.getItems()).hasSize(1); + } +}