Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 슈퍼코딩
- WHATTIMEOFTHEDAY
- Armhole Drop
- 미니마카
- MERN스택
- 고급영어단어
- 자켓실측
- 우레탄지퍼
- 엑셀드래그단축키
- 필터링후복사붙여넣기
- AATCC
- 봉제용어
- 비리짐
- 미국영어연음
- 비슬론지퍼
- 헤이큐
- 엑셀필터복사붙여넣기
- 암홀트롭
- 요척합의
- 지연환가료
- 40HQ컨테이너
- 엑셀자동서식
- 40HQ컨테이너40GP컨테이너차이
- 핸드캐리쿠리어차이점
- 나일론지퍼
- 영어시간읽기
- 클린코드
- 웹API
- TACKING
- 와끼
Archives
- Today
- Total
CASSIE'S BLOG
[슈퍼코딩]95-1강 스프링부트 Service Layer 살펴보기 본문
반응형
Controller가 웹에서 어떻게 들어오는지 어떻게 반환하는지만 신경쓰고 싶어서 비즈니스 로직을 옮기는 행위를 하는거임.
트랜잭션: 데이터 처리 원자성을 보장하기 위한 여러 작업 -> 하나의 작업으로 묶는 것
store_sales sql문 실행
JdbcConfig 설정 바꿔야함
before
package com.github.supercoding.service;
import com.github.supercoding.repository.ElectonicStoreItemRepository;
import com.github.supercoding.repository.ItemEntity;
import com.github.supercoding.web.dto.Item;
import com.github.supercoding.web.dto.ItemBody;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
@Service
public class ElectronicStoreItemService {
private ElectonicStoreItemRepository electonicStoreItemRepository;
public ElectronicStoreItemService(ElectonicStoreItemRepository electonicStoreItemRepository) {
this.electonicStoreItemRepository = electonicStoreItemRepository;
}
public List<Item> findAllItem() {
List<ItemEntity> itemEntities = electonicStoreItemRepository.findAllItems();
return itemEntities.stream().map(Item::new).collect(Collectors.toList());
}
public Integer saveItem(ItemBody itemBody) {
ItemEntity itemEntity = new ItemEntity(null, itemBody.getName(), itemBody.getType(),
itemBody.getPrice(), itemBody.getSpec().getCpu(), itemBody.getSpec().getCapacity());
//이 부분 추가
return electonicStoreItemRepository.saveItem(itemEntity);
}
public Item findItemById(String id) {
Integer idInt = Integer.parseInt(id);
ItemEntity itemEntity = electonicStoreItemRepository.findItemById(idInt);
Item item = new Item(itemEntity);
return item;
}
public List<Item> findItemsByIds(List<String> ids) {
List<ItemEntity> itemEntities = electonicStoreItemRepository.findAllItems();
List<Item> itemsfounded = itemEntities.stream()
.map(Item::new)
.filter((item -> ids.contains(item.getId()))).collect(Collectors.toList());
return itemsfounded;
}
public void deleteItem(String id) {
Integer idInt = Integer.parseInt(id);
electonicStoreItemRepository.deleteItem(idInt);
}
public Item updateItem(String id, ItemBody itemBody) {
Integer idInt = Integer.valueOf(id);
ItemEntity itemEntity = new ItemEntity(idInt, itemBody.getName(), itemBody.getType(), itemBody.getPrice(),
itemBody.getSpec().getCpu(), itemBody.getSpec().getCapacity());
ItemEntity itemEntityUpdated = electonicStoreItemRepository.updateItemEntity(Integer.valueOf(id), itemEntity);
Item itemUpdated = new Item(itemEntityUpdated);
return itemUpdated;
}
}
import com.github.supercoding.repository.ElectonicStoreItemRepository;
import com.github.supercoding.repository.ItemEntity;
import com.github.supercoding.web.dto.Item;
import com.github.supercoding.web.dto.ItemBody;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
@Service
public class ElectronicStoreItemService {
private ElectonicStoreItemRepository electonicStoreItemRepository;
public ElectronicStoreItemService(ElectonicStoreItemRepository electonicStoreItemRepository) {
this.electonicStoreItemRepository = electonicStoreItemRepository;
}
public List<Item> findAllItem() {
List<ItemEntity> itemEntities = electonicStoreItemRepository.findAllItems();
return itemEntities.stream().map(Item::new).collect(Collectors.toList());
}
public Integer saveItem(ItemBody itemBody) {
ItemEntity itemEntity = new ItemEntity(null, itemBody.getName(), itemBody.getType(),
itemBody.getPrice(), itemBody.getSpec().getCpu(), itemBody.getSpec().getCapacity());
//이 부분 추가
return electonicStoreItemRepository.saveItem(itemEntity);
}
public Item findItemById(String id) {
Integer idInt = Integer.parseInt(id);
ItemEntity itemEntity = electonicStoreItemRepository.findItemById(idInt);
Item item = new Item(itemEntity);
return item;
}
public List<Item> findItemsByIds(List<String> ids) {
List<ItemEntity> itemEntities = electonicStoreItemRepository.findAllItems();
List<Item> itemsfounded = itemEntities.stream()
.map(Item::new)
.filter((item -> ids.contains(item.getId()))).collect(Collectors.toList());
return itemsfounded;
}
public void deleteItem(String id) {
Integer idInt = Integer.parseInt(id);
electonicStoreItemRepository.deleteItem(idInt);
}
public Item updateItem(String id, ItemBody itemBody) {
Integer idInt = Integer.valueOf(id);
ItemEntity itemEntity = new ItemEntity(idInt, itemBody.getName(), itemBody.getType(), itemBody.getPrice(),
itemBody.getSpec().getCpu(), itemBody.getSpec().getCapacity());
ItemEntity itemEntityUpdated = electonicStoreItemRepository.updateItemEntity(Integer.valueOf(id), itemEntity);
Item itemUpdated = new Item(itemEntityUpdated);
return itemUpdated;
}
}
after
ElectronicStoreItemServie 추가 +ElectronicStoreItemService 삭제 + private ElectonicStoreItemRepository electonicStoreItemRepository; 이것도 없앰
package com.github.supercoding.service;
import com.github.supercoding.repository.ElectonicStoreItemRepository;
import com.github.supercoding.repository.ItemEntity;
import com.github.supercoding.web.dto.Item;
import com.github.supercoding.web.dto.ItemBody;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;
@Service
public class ElectronicStoreItemService {
private ElectonicStoreItemRepository electonicStoreItemRepository; //이 부분도 없앨 예정
private ElectronicStoreItemService electonicStoreItemService;
//밑에 부분 아예 없앤다고 함.ElectronicStoreItemService
public ElectronicStoreItemService(ElectonicStoreItemRepository electonicStoreItemRepository) {
this.electonicStoreItemRepository = electonicStoreItemRepository;
}
public List<Item> findAllItem() {
List<ItemEntity> itemEntities = electonicStoreItemRepository.findAllItems();
return itemEntities.stream().map(Item::new).collect(Collectors.toList());
}
public Integer saveItem(ItemBody itemBody) {
ItemEntity itemEntity = new ItemEntity(null, itemBody.getName(), itemBody.getType(),
itemBody.getPrice(), itemBody.getSpec().getCpu(), itemBody.getSpec().getCapacity());
//이 부분 추가
return electonicStoreItemRepository.saveItem(itemEntity);
}
public Item findItemById(String id) {
Integer idInt = Integer.parseInt(id);
ItemEntity itemEntity = electonicStoreItemRepository.findItemById(idInt);
Item item = new Item(itemEntity);
return item;
}
public List<Item> findItemsByIds(List<String> ids) {
List<ItemEntity> itemEntities = electonicStoreItemRepository.findAllItems();
List<Item> itemsfounded = itemEntities.stream()
.map(Item::new)
.filter((item -> ids.contains(item.getId()))).collect(Collectors.toList());
return itemsfounded;
}
public void deleteItem(String id) {
Integer idInt = Integer.parseInt(id);
electonicStoreItemRepository.deleteItem(idInt);
}
public Item updateItem(String id, ItemBody itemBody) {
Integer idInt = Integer.valueOf(id);
ItemEntity itemEntity = new ItemEntity(idInt, itemBody.getName(), itemBody.getType(), itemBody.getPrice(),
itemBody.getSpec().getCpu(), itemBody.getSpec().getCapacity());
ItemEntity itemEntityUpdated = electonicStoreItemRepository.updateItemEntity(Integer.valueOf(id), itemEntity);
Item itemUpdated = new Item(itemEntityUpdated);
return itemUpdated;
}
}
import com.github.supercoding.repository.ElectonicStoreItemRepository;
import com.github.supercoding.repository.ItemEntity;
import com.github.supercoding.web.dto.Item;
import com.github.supercoding.web.dto.ItemBody;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;
@Service
public class ElectronicStoreItemService {
private ElectonicStoreItemRepository electonicStoreItemRepository; //이 부분도 없앨 예정
private ElectronicStoreItemService electonicStoreItemService;
//밑에 부분 아예 없앤다고 함.ElectronicStoreItemService
public ElectronicStoreItemService(ElectonicStoreItemRepository electonicStoreItemRepository) {
this.electonicStoreItemRepository = electonicStoreItemRepository;
}
public List<Item> findAllItem() {
List<ItemEntity> itemEntities = electonicStoreItemRepository.findAllItems();
return itemEntities.stream().map(Item::new).collect(Collectors.toList());
}
public Integer saveItem(ItemBody itemBody) {
ItemEntity itemEntity = new ItemEntity(null, itemBody.getName(), itemBody.getType(),
itemBody.getPrice(), itemBody.getSpec().getCpu(), itemBody.getSpec().getCapacity());
//이 부분 추가
return electonicStoreItemRepository.saveItem(itemEntity);
}
public Item findItemById(String id) {
Integer idInt = Integer.parseInt(id);
ItemEntity itemEntity = electonicStoreItemRepository.findItemById(idInt);
Item item = new Item(itemEntity);
return item;
}
public List<Item> findItemsByIds(List<String> ids) {
List<ItemEntity> itemEntities = electonicStoreItemRepository.findAllItems();
List<Item> itemsfounded = itemEntities.stream()
.map(Item::new)
.filter((item -> ids.contains(item.getId()))).collect(Collectors.toList());
return itemsfounded;
}
public void deleteItem(String id) {
Integer idInt = Integer.parseInt(id);
electonicStoreItemRepository.deleteItem(idInt);
}
public Item updateItem(String id, ItemBody itemBody) {
Integer idInt = Integer.valueOf(id);
ItemEntity itemEntity = new ItemEntity(idInt, itemBody.getName(), itemBody.getType(), itemBody.getPrice(),
itemBody.getSpec().getCpu(), itemBody.getSpec().getCapacity());
ItemEntity itemEntityUpdated = electonicStoreItemRepository.updateItemEntity(Integer.valueOf(id), itemEntity);
Item itemUpdated = new Item(itemEntityUpdated);
return itemUpdated;
}
}
ElectronicStoreItemServie 생성자 추가 그러면 밑에 빨간글자로 바뀌는 거 다 바꿔줘야함
package com.github.supercoding.service;
import com.github.supercoding.repository.ElectonicStoreItemRepository;
import com.github.supercoding.repository.ItemEntity;
import com.github.supercoding.web.dto.Item;
import com.github.supercoding.web.dto.ItemBody;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;
@Service
public class ElectronicStoreItemService {
private ElectronicStoreItemService electonicStoreItemService;
public ElectronicStoreItemService(ElectronicStoreItemService electonicStoreItemService) {
this.electonicStoreItemService = electonicStoreItemService;
}
public List<Item> findAllItem() {
List<ItemEntity> itemEntities = electonicStoreItemRepository.findAllItems();
return itemEntities.stream().map(Item::new).collect(Collectors.toList());
}
public Integer saveItem(ItemBody itemBody) {
ItemEntity itemEntity = new ItemEntity(null, itemBody.getName(), itemBody.getType(),
itemBody.getPrice(), itemBody.getSpec().getCpu(), itemBody.getSpec().getCapacity());
//이 부분 추가
return electonicStoreItemRepository.saveItem(itemEntity);
}
public Item findItemById(String id) {
Integer idInt = Integer.parseInt(id);
ItemEntity itemEntity = electonicStoreItemRepository.findItemById(idInt);
Item item = new Item(itemEntity);
return item;
}
public List<Item> findItemsByIds(List<String> ids) {
List<ItemEntity> itemEntities = electonicStoreItemRepository.findAllItems();
List<Item> itemsfounded = itemEntities.stream()
.map(Item::new)
.filter((item -> ids.contains(item.getId()))).collect(Collectors.toList());
return itemsfounded;
}
public void deleteItem(String id) {
Integer idInt = Integer.parseInt(id);
electonicStoreItemRepository.deleteItem(idInt);
}
public Item updateItem(String id, ItemBody itemBody) {
Integer idInt = Integer.valueOf(id);
ItemEntity itemEntity = new ItemEntity(idInt, itemBody.getName(), itemBody.getType(), itemBody.getPrice(),
itemBody.getSpec().getCpu(), itemBody.getSpec().getCapacity());
ItemEntity itemEntityUpdated = electonicStoreItemRepository.updateItemEntity(Integer.valueOf(id), itemEntity);
Item itemUpdated = new Item(itemEntityUpdated);
return itemUpdated;
}
}
import com.github.supercoding.repository.ElectonicStoreItemRepository;
import com.github.supercoding.repository.ItemEntity;
import com.github.supercoding.web.dto.Item;
import com.github.supercoding.web.dto.ItemBody;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;
@Service
public class ElectronicStoreItemService {
private ElectronicStoreItemService electonicStoreItemService;
public ElectronicStoreItemService(ElectronicStoreItemService electonicStoreItemService) {
this.electonicStoreItemService = electonicStoreItemService;
}
public List<Item> findAllItem() {
List<ItemEntity> itemEntities = electonicStoreItemRepository.findAllItems();
return itemEntities.stream().map(Item::new).collect(Collectors.toList());
}
public Integer saveItem(ItemBody itemBody) {
ItemEntity itemEntity = new ItemEntity(null, itemBody.getName(), itemBody.getType(),
itemBody.getPrice(), itemBody.getSpec().getCpu(), itemBody.getSpec().getCapacity());
//이 부분 추가
return electonicStoreItemRepository.saveItem(itemEntity);
}
public Item findItemById(String id) {
Integer idInt = Integer.parseInt(id);
ItemEntity itemEntity = electonicStoreItemRepository.findItemById(idInt);
Item item = new Item(itemEntity);
return item;
}
public List<Item> findItemsByIds(List<String> ids) {
List<ItemEntity> itemEntities = electonicStoreItemRepository.findAllItems();
List<Item> itemsfounded = itemEntities.stream()
.map(Item::new)
.filter((item -> ids.contains(item.getId()))).collect(Collectors.toList());
return itemsfounded;
}
public void deleteItem(String id) {
Integer idInt = Integer.parseInt(id);
electonicStoreItemRepository.deleteItem(idInt);
}
public Item updateItem(String id, ItemBody itemBody) {
Integer idInt = Integer.valueOf(id);
ItemEntity itemEntity = new ItemEntity(idInt, itemBody.getName(), itemBody.getType(), itemBody.getPrice(),
itemBody.getSpec().getCpu(), itemBody.getSpec().getCapacity());
ItemEntity itemEntityUpdated = electonicStoreItemRepository.updateItemEntity(Integer.valueOf(id), itemEntity);
Item itemUpdated = new Item(itemEntityUpdated);
return itemUpdated;
}
}
반응형
'PROGRAMMING > 슈퍼코딩 강의 정리' 카테고리의 다른 글
95-2강 스프링부트 Service Layer 살펴보기 (0) | 2024.01.26 |
---|---|
[슈퍼코딩] 96-1강 스프링 부트 실전 적용하기 v1 (Crud 만들기) (0) | 2024.01.26 |
[슈퍼코딩] 75-1강 SQL 기본문법 익히기 V2 (0) | 2024.01.26 |
[슈퍼코딩] 95-1강 스트링 부트 Service Layer 살펴보기 (0) | 2024.01.23 |
[슈퍼코딩] 94-2강 스트링 부트 Data Access Layer 좀 더 살펴보기 (0) | 2024.01.22 |