Created
August 20, 2021 12:52
-
-
Save JuliaKrivonos/ec0e5d84927f58024dda831498ab827f to your computer and use it in GitHub Desktop.
delete Lists.newArrayList()
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 com.google.common.collect.Lists; | |
| import lombok.Builder; | |
| import org.jazzteam.fitnesclub.dto.OfficeDto; | |
| import org.jazzteam.fitnesclub.dto.RoomDto; | |
| import org.jazzteam.fitnesclub.model.Office; | |
| import org.jazzteam.fitnesclub.model.Room; | |
| import org.jazzteam.fitnesclub.repository.OfficeRepository; | |
| import org.jazzteam.fitnesclub.repository.RoomRepository; | |
| import org.jazzteam.fitnesclub.repository.addictional.FitnessCategoryRepository; | |
| import org.jazzteam.fitnesclub.repository.addictional.InventoryRepository; | |
| import org.jazzteam.fitnesclub.repository.addictional.impl.FitnessCategoryRepositoryImpl; | |
| import org.jazzteam.fitnesclub.repository.addictional.impl.InventoryRepositoryImpl; | |
| import org.jazzteam.fitnesclub.repository.impl.OfficeRepositoryImpl; | |
| import org.jazzteam.fitnesclub.repository.impl.RoomRepositoryImpl; | |
| import org.jazzteam.fitnesclub.service.OfficeService; | |
| import org.jazzteam.fitnesclub.validators.RoomValidator; | |
| import java.util.List; | |
| import java.util.function.Predicate; | |
| import java.util.stream.Collectors; | |
| @Builder | |
| public class OfficeServiceImpl implements OfficeService { | |
| private final OfficeRepository officeRepository = new OfficeRepositoryImpl(); | |
| private final RoomValidator roomValidator; | |
| private final RoomRepository roomRepository = new RoomRepositoryImpl(); | |
| private final FitnessCategoryRepository fitnessCategoryRepository = new FitnessCategoryRepositoryImpl(); | |
| private final InventoryRepository inventoryRepository = new InventoryRepositoryImpl(); | |
| @Override | |
| public Office addOffice(OfficeDto officeDto) { | |
| Office office = new Office(); | |
| office.setAddress(officeDto.getAddress()); | |
| return officeRepository.save(office); | |
| } | |
| @Override | |
| public Office updateOffice(OfficeDto officeDto) { | |
| Office office = officeRepository.findById(officeDto.getId()); | |
| office.setAddress(officeDto.getAddress()); | |
| return officeRepository.save(office); | |
| } | |
| @Override | |
| public void deleteOffice(OfficeDto officeDto) { | |
| Office officeToDelete = officeRepository.findById(officeDto.getId()); | |
| officeRepository.delete(officeToDelete); | |
| } | |
| @Override | |
| public Room addRoom(RoomDto roomDto, Long officeId) { | |
| Room room = new Room(); | |
| addFitnessCategoriesToRoom(roomDto, room); | |
| // TODO: 20.08.2021 validate that inventories in db enough to add in room | |
| addInventoryToRoom(roomDto, room); | |
| Office officeFromDb = officeRepository.findById(officeId); | |
| room.setOffice(officeFromDb); | |
| return roomRepository.save(room); | |
| } | |
| @Override | |
| public Room updateRoom(RoomDto roomDto) { | |
| Room room = roomRepository.findById(roomDto.getId()); | |
| addFitnessCategoriesToRoom(roomDto, room); | |
| // TODO: 20.08.2021 validate that inventories in db enough to add in room | |
| addInventoryToRoom(roomDto, room); | |
| return roomRepository.save(room); | |
| } | |
| @Override | |
| public void deleteRoom(RoomDto roomDto) { | |
| Room roomToDelete = roomRepository.findById(roomDto.getId()); | |
| roomRepository.delete(roomToDelete); | |
| } | |
| @Override | |
| public List<Office> findAllOffices(Predicate<Office> predicate) { | |
| return officeRepository.findAll(predicate); | |
| } | |
| @Override | |
| public List<Room> findAllRooms(Predicate<Room> predicate) { | |
| return roomRepository.findAll(predicate); | |
| } | |
| private void addFitnessCategoriesToRoom(RoomDto roomDto, Room room) { | |
| room.getFitnessCategories().addAll( | |
| roomDto.getFitnessCategories().stream() | |
| .map(fitnessDto -> fitnessCategoryRepository.findById(fitnessDto.getId())) | |
| .collect(Collectors.toList())); | |
| } | |
| private void addInventoryToRoom(RoomDto roomDto, Room room) { | |
| room.getInventories().addAll(roomDto.getInventories().stream() | |
| .map(inventoryDto -> inventoryRepository.findById(inventoryDto.getId())) | |
| .collect(Collectors.toList())); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment