Created
August 20, 2021 11:57
-
-
Save JuliaKrivonos/8ece37f188f10cc3fe514f96a737fc04 to your computer and use it in GitHub Desktop.
InventoryService implementation
This file contains hidden or 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
| package org.jazzteam.fitnesclub.service.impl; | |
| import lombok.Builder; | |
| import org.jazzteam.fitnesclub.dto.InventoryDto; | |
| import org.jazzteam.fitnesclub.dto.InventoryTypeDto; | |
| import org.jazzteam.fitnesclub.model.Inventory; | |
| import org.jazzteam.fitnesclub.model.InventoryType; | |
| import org.jazzteam.fitnesclub.repository.RoomRepository; | |
| import org.jazzteam.fitnesclub.repository.addictional.InventoryRepository; | |
| import org.jazzteam.fitnesclub.repository.addictional.InventoryTypeRepository; | |
| import org.jazzteam.fitnesclub.service.InventoryService; | |
| import org.jazzteam.fitnesclub.validators.InventoryValidator; | |
| import java.util.List; | |
| import java.util.function.Predicate; | |
| @Builder | |
| public class InventoryServiceImpl implements InventoryService { | |
| private final InventoryRepository inventoryRepository; | |
| private final RoomRepository roomRepository; | |
| private final InventoryTypeRepository inventoryTypeRepository; | |
| private final InventoryValidator inventoryValidator; | |
| public InventoryServiceImpl(InventoryRepository inventoryRepository, RoomRepository roomRepository, InventoryTypeRepository inventoryTypeRepository, InventoryValidator inventoryValidator) { | |
| this.inventoryRepository = inventoryRepository; | |
| this.roomRepository = roomRepository; | |
| this.inventoryTypeRepository = inventoryTypeRepository; | |
| this.inventoryValidator = inventoryValidator; | |
| } | |
| @Override | |
| public Inventory saveInventory(InventoryDto inventoryDto) { | |
| inventoryValidator.validate(inventoryDto); | |
| Inventory inventory = new Inventory(); | |
| inventory.setInventoryType(inventoryTypeRepository.findById(inventoryDto.getInventoryType().getId())); | |
| inventory.setAmount(inventoryDto.getAmount()); | |
| return inventoryRepository.save(inventory); | |
| } | |
| @Override | |
| public void deleteInventory(InventoryDto inventoryDto) { | |
| Inventory inventoryToDelete = inventoryRepository.findById(inventoryDto.getId()); | |
| inventoryRepository.delete(inventoryToDelete); | |
| } | |
| @Override | |
| public InventoryType saveInventoryType(InventoryTypeDto inventoryTypeDto) { | |
| InventoryType inventoryType = new InventoryType(); | |
| inventoryType.setType(inventoryTypeDto.getType()); | |
| return inventoryTypeRepository.save(inventoryType); | |
| } | |
| @Override | |
| public void deleteInventoryType(InventoryTypeDto inventoryTypeDto) { | |
| InventoryType inventoryTypeToDelete = inventoryTypeRepository.findById(inventoryTypeDto.getId()); | |
| inventoryTypeRepository.delete(inventoryTypeToDelete); | |
| } | |
| @Override | |
| public Inventory updateInventory(InventoryDto inventoryDto) { | |
| inventoryValidator.validate(inventoryDto); | |
| Inventory inventory = inventoryRepository.findById(inventoryDto.getId()); | |
| inventory.setInventoryType(inventoryTypeRepository.findById(inventoryDto.getInventoryType().getId())); | |
| inventory.setAmount(inventoryDto.getAmount()); | |
| return inventoryRepository.save(inventory); | |
| } | |
| @Override | |
| public InventoryType updateInventoryType(InventoryTypeDto inventoryTypeDto) { | |
| InventoryType inventoryType = inventoryTypeRepository.findById(inventoryTypeDto.getId()); | |
| inventoryType.setType(inventoryTypeDto.getType()); | |
| return inventoryTypeRepository.save(inventoryType); | |
| } | |
| @Override | |
| public List<Inventory> findAllInventory(Predicate<Inventory> predicate) { | |
| return inventoryRepository.findAll(predicate); | |
| } | |
| @Override | |
| public List<Inventory> findAllInventory() { | |
| return inventoryRepository.findAll(); | |
| } | |
| @Override | |
| public List<InventoryType> findAllInventoryListType() { | |
| return inventoryTypeRepository.findAll(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment