-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from ghis22130/service
[BE] Service 구현 issue: closes #5
- Loading branch information
Showing
7 changed files
with
108 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
BE/src/main/java/com/team10/banchan/exception/NotFoundException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
BE/src/main/java/com/team10/banchan/service/ItemDetailService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
40
BE/src/main/java/com/team10/banchan/service/SectionService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |