From 4aa5fed09518a4d074c4403867060a73b9683127 Mon Sep 17 00:00:00 2001 From: bong6981 Date: Tue, 20 Apr 2021 11:43:58 +0900 Subject: [PATCH 1/4] feat: Define Models MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 설계한 스키마에 따라 모델을 정의한다 issue: #4 --- .../java/com/team10/banchan/model/Badge.java | 16 ++++++++++ .../com/team10/banchan/model/Category.java | 12 ++++++++ .../com/team10/banchan/model/DeliveryDay.java | 17 +++++++++++ .../team10/banchan/model/DeliveryType.java | 22 ++++++++++++++ .../team10/banchan/model/DetailSection.java | 5 ++++ .../java/com/team10/banchan/model/Item.java | 30 +++++++++++++++++++ .../com/team10/banchan/model/Section.java | 12 ++++++++ .../com/team10/banchan/model/ThumbImage.java | 5 ++++ 8 files changed, 119 insertions(+) create mode 100644 BE/src/main/java/com/team10/banchan/model/Badge.java create mode 100644 BE/src/main/java/com/team10/banchan/model/Category.java create mode 100644 BE/src/main/java/com/team10/banchan/model/DeliveryDay.java create mode 100644 BE/src/main/java/com/team10/banchan/model/DeliveryType.java create mode 100644 BE/src/main/java/com/team10/banchan/model/DetailSection.java create mode 100644 BE/src/main/java/com/team10/banchan/model/Item.java create mode 100644 BE/src/main/java/com/team10/banchan/model/Section.java create mode 100644 BE/src/main/java/com/team10/banchan/model/ThumbImage.java diff --git a/BE/src/main/java/com/team10/banchan/model/Badge.java b/BE/src/main/java/com/team10/banchan/model/Badge.java new file mode 100644 index 000000000..16435e300 --- /dev/null +++ b/BE/src/main/java/com/team10/banchan/model/Badge.java @@ -0,0 +1,16 @@ +package com.team10.banchan.model; + +enum Badge { + EVENT("이벤트특가"), LAUNCHIING("런칭특가"); + + private final String name; + + Badge(String name) { + this.name = name; + } + + public String getName() { + return name; + } +} + diff --git a/BE/src/main/java/com/team10/banchan/model/Category.java b/BE/src/main/java/com/team10/banchan/model/Category.java new file mode 100644 index 000000000..8c9890dbb --- /dev/null +++ b/BE/src/main/java/com/team10/banchan/model/Category.java @@ -0,0 +1,12 @@ +package com.team10.banchan.model; + +import org.springframework.data.annotation.Id; + +import java.util.Set; + +public class Category { + @Id + private Long id; + private Set items; + private String name; +} diff --git a/BE/src/main/java/com/team10/banchan/model/DeliveryDay.java b/BE/src/main/java/com/team10/banchan/model/DeliveryDay.java new file mode 100644 index 000000000..c2ae5262b --- /dev/null +++ b/BE/src/main/java/com/team10/banchan/model/DeliveryDay.java @@ -0,0 +1,17 @@ +package com.team10.banchan.model; + +enum DeliveryDay { + MON("월"), + TUE("화"), + WED("수"), + THU("목"), + FRI("금"), + SAT("토"), + SUN("일"); + + private final String korean; + + DeliveryDay(String korean) { + this.korean = korean; + } +} diff --git a/BE/src/main/java/com/team10/banchan/model/DeliveryType.java b/BE/src/main/java/com/team10/banchan/model/DeliveryType.java new file mode 100644 index 000000000..c5a305de6 --- /dev/null +++ b/BE/src/main/java/com/team10/banchan/model/DeliveryType.java @@ -0,0 +1,22 @@ +package com.team10.banchan.model; + +enum DeliveryType { + NATIONWIDE("전국택배", "전국택배 (제주 및 도서산간 불가)"), + DAWN("새벽배송", "서울 경기 새벽배송"); + + private final String name; + private final String detail; + + DeliveryType(String name, String detail) { + this.name = name; + this.detail = detail; + } + + public String getName() { + return name; + } + + public String getDetail() { + return detail; + } +} diff --git a/BE/src/main/java/com/team10/banchan/model/DetailSection.java b/BE/src/main/java/com/team10/banchan/model/DetailSection.java new file mode 100644 index 000000000..b2b97b4d4 --- /dev/null +++ b/BE/src/main/java/com/team10/banchan/model/DetailSection.java @@ -0,0 +1,5 @@ +package com.team10.banchan.model; + +public class DetailSection { + private String 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 new file mode 100644 index 000000000..e42a363d6 --- /dev/null +++ b/BE/src/main/java/com/team10/banchan/model/Item.java @@ -0,0 +1,30 @@ +package com.team10.banchan.model; + + +import org.springframework.data.annotation.Id; + +import java.math.BigDecimal; +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; +} diff --git a/BE/src/main/java/com/team10/banchan/model/Section.java b/BE/src/main/java/com/team10/banchan/model/Section.java new file mode 100644 index 000000000..1ef27b7a4 --- /dev/null +++ b/BE/src/main/java/com/team10/banchan/model/Section.java @@ -0,0 +1,12 @@ +package com.team10.banchan.model; + +import org.springframework.data.annotation.Id; + +import java.util.Set; + +public class Section { + @Id + private Long id; + private Set items; + private String name; +} diff --git a/BE/src/main/java/com/team10/banchan/model/ThumbImage.java b/BE/src/main/java/com/team10/banchan/model/ThumbImage.java new file mode 100644 index 000000000..d6238738f --- /dev/null +++ b/BE/src/main/java/com/team10/banchan/model/ThumbImage.java @@ -0,0 +1,5 @@ +package com.team10.banchan.model; + +public class ThumbImage { + private String url; +} From eac9609e7627da7c456e0e2bb3a7a3c5dc45f6cd Mon Sep 17 00:00:00 2001 From: bong6981 Date: Tue, 20 Apr 2021 13:04:12 +0900 Subject: [PATCH 2/4] test: Mapping model to schema MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 모델을 Spring Data JDBC를 이용해서 연결 - 스키마 정의 - enum이 다른 테이블로 매핑이 안되는 문제 issue: #4 --- .../java/com/team10/banchan/model/Badge.java | 14 +--- .../com/team10/banchan/model/DeliveryDay.java | 16 +---- .../team10/banchan/model/DeliveryType.java | 21 +----- .../repository/CategoryRepository.java | 11 +++ .../banchan/repository/ItemRepository.java | 11 +++ .../banchan/repository/SectionRepository.java | 11 +++ BE/src/main/resources/application.properties | 2 + BE/src/main/resources/schema.sql | 68 +++++++++++++++++++ .../repository/ItemRepositoryTest.java | 22 ++++++ BE/src/test/resources/application.properties | 3 + BE/src/test/resources/data.sql | 18 +++++ 11 files changed, 152 insertions(+), 45 deletions(-) create mode 100644 BE/src/main/java/com/team10/banchan/repository/CategoryRepository.java create mode 100644 BE/src/main/java/com/team10/banchan/repository/ItemRepository.java create mode 100644 BE/src/main/java/com/team10/banchan/repository/SectionRepository.java create mode 100644 BE/src/main/resources/schema.sql create mode 100644 BE/src/test/java/com/team10/banchan/repository/ItemRepositoryTest.java create mode 100644 BE/src/test/resources/application.properties create mode 100644 BE/src/test/resources/data.sql diff --git a/BE/src/main/java/com/team10/banchan/model/Badge.java b/BE/src/main/java/com/team10/banchan/model/Badge.java index 16435e300..73fc150eb 100644 --- a/BE/src/main/java/com/team10/banchan/model/Badge.java +++ b/BE/src/main/java/com/team10/banchan/model/Badge.java @@ -1,16 +1,6 @@ package com.team10.banchan.model; -enum Badge { - EVENT("이벤트특가"), LAUNCHIING("런칭특가"); - - private final String name; - - Badge(String name) { - this.name = name; - } - - public String getName() { - return name; - } +public class Badge { + private String name; } diff --git a/BE/src/main/java/com/team10/banchan/model/DeliveryDay.java b/BE/src/main/java/com/team10/banchan/model/DeliveryDay.java index c2ae5262b..17fbb43ba 100644 --- a/BE/src/main/java/com/team10/banchan/model/DeliveryDay.java +++ b/BE/src/main/java/com/team10/banchan/model/DeliveryDay.java @@ -1,17 +1,5 @@ package com.team10.banchan.model; -enum DeliveryDay { - MON("월"), - TUE("화"), - WED("수"), - THU("목"), - FRI("금"), - SAT("토"), - SUN("일"); - - private final String korean; - - DeliveryDay(String korean) { - this.korean = korean; - } +public class DeliveryDay { + private String name; } diff --git a/BE/src/main/java/com/team10/banchan/model/DeliveryType.java b/BE/src/main/java/com/team10/banchan/model/DeliveryType.java index c5a305de6..475f3038d 100644 --- a/BE/src/main/java/com/team10/banchan/model/DeliveryType.java +++ b/BE/src/main/java/com/team10/banchan/model/DeliveryType.java @@ -1,22 +1,5 @@ package com.team10.banchan.model; -enum DeliveryType { - NATIONWIDE("전국택배", "전국택배 (제주 및 도서산간 불가)"), - DAWN("새벽배송", "서울 경기 새벽배송"); - - private final String name; - private final String detail; - - DeliveryType(String name, String detail) { - this.name = name; - this.detail = detail; - } - - public String getName() { - return name; - } - - public String getDetail() { - return detail; - } +public class DeliveryType { + private String name; } diff --git a/BE/src/main/java/com/team10/banchan/repository/CategoryRepository.java b/BE/src/main/java/com/team10/banchan/repository/CategoryRepository.java new file mode 100644 index 000000000..57fcee477 --- /dev/null +++ b/BE/src/main/java/com/team10/banchan/repository/CategoryRepository.java @@ -0,0 +1,11 @@ +package com.team10.banchan.repository; + +import com.team10.banchan.model.Category; +import org.springframework.data.repository.CrudRepository; + +import java.util.List; + +public interface CategoryRepository extends CrudRepository { + @Override + List findAll(); +} diff --git a/BE/src/main/java/com/team10/banchan/repository/ItemRepository.java b/BE/src/main/java/com/team10/banchan/repository/ItemRepository.java new file mode 100644 index 000000000..a13b27507 --- /dev/null +++ b/BE/src/main/java/com/team10/banchan/repository/ItemRepository.java @@ -0,0 +1,11 @@ +package com.team10.banchan.repository; + +import com.team10.banchan.model.Item; +import org.springframework.data.repository.CrudRepository; + +import java.util.List; + +public interface ItemRepository extends CrudRepository { + @Override + List findAll(); +} diff --git a/BE/src/main/java/com/team10/banchan/repository/SectionRepository.java b/BE/src/main/java/com/team10/banchan/repository/SectionRepository.java new file mode 100644 index 000000000..25cf6338b --- /dev/null +++ b/BE/src/main/java/com/team10/banchan/repository/SectionRepository.java @@ -0,0 +1,11 @@ +package com.team10.banchan.repository; + +import com.team10.banchan.model.Section; +import org.springframework.data.repository.CrudRepository; + +import java.util.List; + +public interface SectionRepository extends CrudRepository { + @Override + List
findAll(); +} diff --git a/BE/src/main/resources/application.properties b/BE/src/main/resources/application.properties index 3f94977c9..08652325b 100644 --- a/BE/src/main/resources/application.properties +++ b/BE/src/main/resources/application.properties @@ -1,3 +1,5 @@ spring.datasource.url=jdbc:mysql://localhost:3306/tigerdb +spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.username=scott spring.datasource.password=tiger +logging.level.sql=debug diff --git a/BE/src/main/resources/schema.sql b/BE/src/main/resources/schema.sql new file mode 100644 index 000000000..74bd2d968 --- /dev/null +++ b/BE/src/main/resources/schema.sql @@ -0,0 +1,68 @@ +CREATE TABLE IF NOT EXISTS `section` +( + id BIGINT AUTO_INCREMENT PRIMARY KEY, + `name` VARCHAR(50) +); + +CREATE TABLE IF NOT EXISTS category +( + id BIGINT AUTO_INCREMENT PRIMARY KEY, + `name` VARCHAR(50) + ); + +CREATE TABLE IF NOT EXISTS item +( + id BIGINT AUTO_INCREMENT PRIMARY KEY, + section BIGINT, + category BIGINT, + alt VARCHAR(50), + top_image VARCHAR(65536), + title VARCHAR(50), + description VARCHAR(500), + n_price DECIMAL, + s_price DECIMAL, + delivery_fee DECIMAL, + stock int +); + +CREATE TABLE IF NOT EXISTS detail_section +( + item BIGINT, + item_key INT, + url VARCHAR(65536) +); + +CREATE TABLE IF NOT EXISTS thumb_image +( + item BIGINT, + item_key INT, + url VARCHAR(65536) +); + +CREATE TABLE IF NOT EXISTS badge ( + item BIGINT, + `name` CHAR(10) +); + + +CREATE TABLE IF NOT EXISTS delivery_type +( + item BIGINT, + `name` CHAR(10) +); + + +CREATE TABLE IF NOT EXISTS delivery_day +( + item BIGINT, + `name` CHAR(3) +); + + + + + + + + + diff --git a/BE/src/test/java/com/team10/banchan/repository/ItemRepositoryTest.java b/BE/src/test/java/com/team10/banchan/repository/ItemRepositoryTest.java new file mode 100644 index 000000000..1b1f9f8ff --- /dev/null +++ b/BE/src/test/java/com/team10/banchan/repository/ItemRepositoryTest.java @@ -0,0 +1,22 @@ +package com.team10.banchan.repository; + +import com.team10.banchan.model.Item; +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 +class ItemRepositoryTest { + @Autowired + private ItemRepository itemRepository; + + @Test + void getItem() { + Item item = itemRepository.findById(1L).orElseThrow(RuntimeException::new); + assertThat(item).hasFieldOrPropertyWithValue("alt", "alt"); + } +} diff --git a/BE/src/test/resources/application.properties b/BE/src/test/resources/application.properties new file mode 100644 index 000000000..ada63edcb --- /dev/null +++ b/BE/src/test/resources/application.properties @@ -0,0 +1,3 @@ +spring.datasource.url=jdbc:h2:mem:test;CASE_INSENSITIVE_IDENTIFIERS=TRUE;MODE=MySQL;DATABASE_TO_LOWER=TRUE +spring.datasource.driver-class-name=org.h2.Driver +logging.level.sql=debug diff --git a/BE/src/test/resources/data.sql b/BE/src/test/resources/data.sql new file mode 100644 index 000000000..c1391753b --- /dev/null +++ b/BE/src/test/resources/data.sql @@ -0,0 +1,18 @@ +INSERT INTO `section`(id, `name`) VALUES (1, 'main'); +INSERT INTO `category`(id, `name`) VALUES (1, 'best'); +INSERT INTO `item`(id, section, category, alt, top_image, title, description, n_price, s_price, delivery_fee, stock) VALUES (1, 1, 1, 'alt', 'url', 'title', 'description', 10000, 10000, 2500, 3); +INSERT INTO `detail_section`(item, item_key, url) VALUES (1, 0, 'url1'); +INSERT INTO `detail_section`(item, item_key, url) VALUES (1, 1, 'url2'); +INSERT INTO `detail_section`(item, item_key, url) VALUES (1, 2, 'url3'); +INSERT INTO `thumb_image`(item, item_key, url) VALUES (1, 0, 'thumb_url1'); +INSERT INTO `thumb_image`(item, item_key, url) VALUES (1, 1, 'thumb_url2'); +INSERT INTO `thumb_image`(item, item_key, url) VALUES (1, 2, 'thumb_url3'); +INSERT INTO `badge`(item, `name`) VALUES (1, 'EVENT'); +INSERT INTO `badge`(item, `name`) VALUES (1, 'LAUNCHING'); +INSERT INTO `delivery_type`(item, `name`) VALUES (1, 'NATIONWIDE'); +INSERT INTO `delivery_type`(item, `name`) VALUES (1, 'DAWN'); +INSERT INTO `delivery_day`(item, `name`) VALUES (1, 'MON'); +INSERT INTO `delivery_day`(item, `name`) VALUES (1, 'TUE'); +INSERT INTO `delivery_day`(item, `name`) VALUES (1, 'WED'); +INSERT INTO `delivery_day`(item, `name`) VALUES (1, 'THU'); +INSERT INTO `delivery_day`(item, `name`) VALUES (1, 'FRI'); From 1ac9d019bbdcd280c13484956a64a3e7f3c007bb Mon Sep 17 00:00:00 2001 From: bong6981 Date: Tue, 20 Apr 2021 16:48:34 +0900 Subject: [PATCH 3/4] feat: Use Enum MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Bage, DeliveryDay, DeliveryType에 Enum을 사용 issue: #4 --- .../java/com/team10/banchan/model/Badge.java | 29 +++++++++- .../com/team10/banchan/model/DeliveryDay.java | 55 ++++++++++++++++++- .../team10/banchan/model/DeliveryType.java | 36 +++++++++++- BE/src/main/resources/schema.sql | 6 +- BE/src/test/resources/data.sql | 18 +++--- 5 files changed, 129 insertions(+), 15 deletions(-) diff --git a/BE/src/main/java/com/team10/banchan/model/Badge.java b/BE/src/main/java/com/team10/banchan/model/Badge.java index 73fc150eb..1f887f2a8 100644 --- a/BE/src/main/java/com/team10/banchan/model/Badge.java +++ b/BE/src/main/java/com/team10/banchan/model/Badge.java @@ -1,6 +1,33 @@ package com.team10.banchan.model; public class Badge { - private String name; + private final BadgeType badgeType; + + Badge(BadgeType badgeType) { + this.badgeType = badgeType; + } + + public String name() { + return this.badgeType.name; + } + + public static Badge event() { + return new Badge(BadgeType.EVENT); + } + + public static Badge launching() { + return new Badge(BadgeType.LAUNCHING); + } + + enum BadgeType { + EVENT("이벤트특가"), + LAUNCHING("런칭특가"); + + private final String name; + + BadgeType(String name) { + this.name = name; + } + } } diff --git a/BE/src/main/java/com/team10/banchan/model/DeliveryDay.java b/BE/src/main/java/com/team10/banchan/model/DeliveryDay.java index 17fbb43ba..c8b31d05d 100644 --- a/BE/src/main/java/com/team10/banchan/model/DeliveryDay.java +++ b/BE/src/main/java/com/team10/banchan/model/DeliveryDay.java @@ -1,5 +1,58 @@ package com.team10.banchan.model; public class DeliveryDay { - private String name; + private final TheDayOfWeek theDayOfWeek; + + DeliveryDay(TheDayOfWeek theDayOfWeek) { + this.theDayOfWeek = theDayOfWeek; + } + + public String korean() { + return theDayOfWeek.korean; + } + + public static DeliveryDay monday() { + return new DeliveryDay(TheDayOfWeek.MON); + } + + public static DeliveryDay tuesday() { + return new DeliveryDay(TheDayOfWeek.TUE); + } + + public static DeliveryDay wednesday() { + return new DeliveryDay(TheDayOfWeek.WED); + } + + public static DeliveryDay thursday() { + return new DeliveryDay(TheDayOfWeek.THU); + } + + public static DeliveryDay friday() { + return new DeliveryDay(TheDayOfWeek.FRI); + } + + public static DeliveryDay saturday() { + return new DeliveryDay(TheDayOfWeek.SAT); + } + + public static DeliveryDay sunday() { + return new DeliveryDay(TheDayOfWeek.SUN); + } + + enum TheDayOfWeek { + MON("월"), + TUE("화"), + WED("수"), + THU("목"), + FRI("금"), + SAT("토"), + SUN("일"); + + private final String korean; + + TheDayOfWeek(String korean) { + this.korean = korean; + } + } } + diff --git a/BE/src/main/java/com/team10/banchan/model/DeliveryType.java b/BE/src/main/java/com/team10/banchan/model/DeliveryType.java index 475f3038d..120238b2f 100644 --- a/BE/src/main/java/com/team10/banchan/model/DeliveryType.java +++ b/BE/src/main/java/com/team10/banchan/model/DeliveryType.java @@ -1,5 +1,39 @@ package com.team10.banchan.model; public class DeliveryType { - private String name; + private final DeliveryTypeEnum deliveryTypeName; + + DeliveryType(DeliveryTypeEnum deliveryTypeName) { + this.deliveryTypeName = deliveryTypeName; + } + + public String getName() { + return deliveryTypeName.name; + } + + public String getDetail() { + return deliveryTypeName.detail; + } + + public static DeliveryType nationwide() { + return new DeliveryType(DeliveryTypeEnum.NATIONWIDE); + } + + public static DeliveryType dawn() { + return new DeliveryType(DeliveryTypeEnum.DAWN); + } + + enum DeliveryTypeEnum { + NATIONWIDE("전국택배", "전국택배 (제주 및 도서산간 불가)"), + DAWN("새벽배송", "서울 경기 새벽배송"); + + private final String name; + private final String detail; + + DeliveryTypeEnum(String name, String detail) { + this.name = name; + this.detail = detail; + } + } + } diff --git a/BE/src/main/resources/schema.sql b/BE/src/main/resources/schema.sql index 74bd2d968..d9962b08b 100644 --- a/BE/src/main/resources/schema.sql +++ b/BE/src/main/resources/schema.sql @@ -41,21 +41,21 @@ CREATE TABLE IF NOT EXISTS thumb_image CREATE TABLE IF NOT EXISTS badge ( item BIGINT, - `name` CHAR(10) + badge_type CHAR(10) ); CREATE TABLE IF NOT EXISTS delivery_type ( item BIGINT, - `name` CHAR(10) + delivery_type_name CHAR(10) ); CREATE TABLE IF NOT EXISTS delivery_day ( item BIGINT, - `name` CHAR(3) + `the_day_of_week` CHAR(3) ); diff --git a/BE/src/test/resources/data.sql b/BE/src/test/resources/data.sql index c1391753b..c1c4acd95 100644 --- a/BE/src/test/resources/data.sql +++ b/BE/src/test/resources/data.sql @@ -7,12 +7,12 @@ INSERT INTO `detail_section`(item, item_key, url) VALUES (1, 2, 'url3'); INSERT INTO `thumb_image`(item, item_key, url) VALUES (1, 0, 'thumb_url1'); INSERT INTO `thumb_image`(item, item_key, url) VALUES (1, 1, 'thumb_url2'); INSERT INTO `thumb_image`(item, item_key, url) VALUES (1, 2, 'thumb_url3'); -INSERT INTO `badge`(item, `name`) VALUES (1, 'EVENT'); -INSERT INTO `badge`(item, `name`) VALUES (1, 'LAUNCHING'); -INSERT INTO `delivery_type`(item, `name`) VALUES (1, 'NATIONWIDE'); -INSERT INTO `delivery_type`(item, `name`) VALUES (1, 'DAWN'); -INSERT INTO `delivery_day`(item, `name`) VALUES (1, 'MON'); -INSERT INTO `delivery_day`(item, `name`) VALUES (1, 'TUE'); -INSERT INTO `delivery_day`(item, `name`) VALUES (1, 'WED'); -INSERT INTO `delivery_day`(item, `name`) VALUES (1, 'THU'); -INSERT INTO `delivery_day`(item, `name`) VALUES (1, 'FRI'); +INSERT INTO `badge`(item, badge_type) VALUES (1, 'EVENT'); +INSERT INTO `badge`(item, badge_type) VALUES (1, 'LAUNCHING'); +INSERT INTO `delivery_type`(item, delivery_type_name) VALUES (1, 'NATIONWIDE'); +INSERT INTO `delivery_type`(item, delivery_type_name) VALUES (1, 'DAWN'); +INSERT INTO `delivery_day`(item, `the_day_of_week`) VALUES (1, 'MON'); +INSERT INTO `delivery_day`(item, `the_day_of_week`) VALUES (1, 'TUE'); +INSERT INTO `delivery_day`(item, `the_day_of_week`) VALUES (1, 'WED'); +INSERT INTO `delivery_day`(item, `the_day_of_week`) VALUES (1, 'THU'); +INSERT INTO `delivery_day`(item, `the_day_of_week`) VALUES (1, 'FRI'); From 21be2b7d3036d5fb6a71545763cbeb5277b3e436 Mon Sep 17 00:00:00 2001 From: bong6981 Date: Tue, 20 Apr 2021 17:37:32 +0900 Subject: [PATCH 4/4] 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); + } +}