Skip to content

Commit

Permalink
Merge pull request #19 from ghis22130/service
Browse files Browse the repository at this point in the history
[BE] Service 구현 

issue: closes #5
  • Loading branch information
kihyuk-sung committed Apr 21, 2021
2 parents a66c321 + 96a8e9a commit b1df945
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 4 deletions.
14 changes: 11 additions & 3 deletions BE/src/main/java/com/team10/banchan/dto/ItemDetail.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ public class ItemDetail {
private final List<String> detailSection;
private final List<String> badges;

private ItemDetail(String topImage, List<String> thumbImages, String title, String productDescription, String point, String deliveryInfo, String deliveryFee, String nPrices, String sPrices, List<String> detailSection, List<String> badges) {
private final Boolean inStock;

private ItemDetail(String topImage, List<String> thumbImages, String title, String productDescription, String point, String deliveryInfo, String deliveryFee, String nPrices, String sPrices, List<String> detailSection, List<String> badges, Boolean inStock) {
this.topImage = topImage;
this.thumbImages = thumbImages;
this.title = title;
Expand All @@ -34,14 +36,15 @@ private ItemDetail(String topImage, List<String> thumbImages, String title, Stri
this.sPrices = sPrices;
this.detailSection = detailSection;
this.badges = badges;
this.inStock = inStock;
}

public static ItemDetail of(String topImage, List<String> thumbImages,
String title, String productDescription,
String point, String deliveryInfo,
String deliveryFee, String nPrices, String sPrices,
List<String> detailSection, List<String> badges) {
return new ItemDetail(topImage, thumbImages, title, productDescription, point, deliveryInfo, deliveryFee, nPrices, sPrices, detailSection, badges);
List<String> detailSection, List<String> badges, Boolean inStock) {
return new ItemDetail(topImage, thumbImages, title, productDescription, point, deliveryInfo, deliveryFee, nPrices, sPrices, detailSection, badges, inStock);
}

@JsonProperty("top_image")
Expand Down Expand Up @@ -99,6 +102,11 @@ public List<String> getDetailSection() {
public List<String> getBadges() {
return badges;
}

@JsonProperty("in_stock")
public Boolean getInStock() {
return inStock;
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.team10.banchan.exception;

public class NotFoundException extends RuntimeException {

public NotFoundException(String message) {
super(message);
}
}
7 changes: 6 additions & 1 deletion BE/src/main/java/com/team10/banchan/model/Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ public ItemDetail itemDetail() {
prices.getnPrice(),
prices.getsPrice(),
detailSection(),
badge()
badge(),
inStock()
);
}

Expand Down Expand Up @@ -156,4 +157,8 @@ private List<String> badge() {
.map(Badge::getName)
.collect(Collectors.toList());
}

private Boolean inStock() {
return stock > 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@
import org.springframework.data.repository.CrudRepository;

import java.util.List;
import java.util.Optional;

public interface ItemRepository extends CrudRepository<Item, Long> {

@Override
List<Item> findAll();

List<Item> findAllBySection(Long section);

List<Item> findAllByCategory(Long category);

Optional<Item> findByIdAndSection(Long id, Long section);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
import org.springframework.data.repository.CrudRepository;

import java.util.List;
import java.util.Optional;

public interface SectionRepository extends CrudRepository<Section, Long> {
@Override
List<Section> findAll();

Optional<Section> findByName(String sectionName);
}
32 changes: 32 additions & 0 deletions BE/src/main/java/com/team10/banchan/service/ItemDetailService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.team10.banchan.service;

import com.team10.banchan.dto.ItemDetailResponse;
import com.team10.banchan.exception.NotFoundException;
import com.team10.banchan.model.Item;
import com.team10.banchan.repository.ItemRepository;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ItemDetailService {

private final ItemRepository itemRepository;

public ItemDetailService(ItemRepository itemRepository) {
this.itemRepository = itemRepository;
}

public ItemDetailResponse itemDetail(Long itemId) {
Item item = itemRepository.findById(itemId).orElseThrow(() -> new NotFoundException("존재하지 않는 반찬입니다."));
return ItemDetailResponse.of(itemId, item.itemDetail());
}

public List<ItemDetailResponse> itemDetails() {
return itemRepository.findAll().stream()
.map(item -> ItemDetailResponse.of(item.getId(), item.itemDetail()))
.collect(Collectors.toList());
}

}
40 changes: 40 additions & 0 deletions BE/src/main/java/com/team10/banchan/service/SectionService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.team10.banchan.service;

import com.team10.banchan.dto.ItemDetailResponse;
import com.team10.banchan.dto.ItemSummary;
import com.team10.banchan.exception.NotFoundException;
import com.team10.banchan.model.Item;
import com.team10.banchan.model.Section;
import com.team10.banchan.repository.ItemRepository;
import com.team10.banchan.repository.SectionRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.crossstore.ChangeSetPersister;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SectionService {

private final SectionRepository sectionRepository;
private final ItemRepository itemRepository;

public SectionService(SectionRepository sectionRepository, ItemRepository itemRepository) {
this.sectionRepository = sectionRepository;
this.itemRepository = itemRepository;
}

public List<ItemSummary> itemSummaries(String sectionName) {
Section section = sectionRepository.findByName(sectionName).orElseThrow(() -> new NotFoundException("존재하지 않는 섹션입니다"));
return itemRepository.findAllBySection(section.getId()).stream()
.map(Item::itemSummary)
.collect(Collectors.toList());
}

public ItemSummary itemSummary(String sectionName, Long itemId) {
Section section = sectionRepository.findByName(sectionName).orElseThrow(() -> new NotFoundException("존재하지 않는 섹션입니다"));
Item item = itemRepository.findByIdAndSection(itemId, section.getId()).orElseThrow(() -> new NotFoundException("존재하지 않는 아이템입니다"));
return item.itemSummary();
}
}

0 comments on commit b1df945

Please sign in to comment.