Skip to content
Snippets Groups Projects
Commit 1897494d authored by Martin Slovík's avatar Martin Slovík
Browse files

Adding Flight and Steward facades

parent 94441e8e
No related branches found
No related tags found
No related merge requests found
package cz.muni.fi.pa165.core.facade.flight;
import cz.muni.fi.pa165.core.model.FlightDto;
import cz.muni.fi.pa165.core.model.NewFlightDtoRequest;
import java.util.List;
import java.util.Optional;
/**
* @param <K> Key
* @author martinslovik
*/
public interface FlightFacade<K> {
FlightDto save(NewFlightDtoRequest newFlightDtoRequest);
Optional<FlightDto> findById(K id);
List<FlightDto> findAll();
void deleteById(K id);
void deleteAll();
}
package cz.muni.fi.pa165.core.facade.flight;
import cz.muni.fi.pa165.core.data.domain.Flight;
import cz.muni.fi.pa165.core.model.FlightDto;
import cz.muni.fi.pa165.core.model.NewFlightDtoRequest;
import cz.muni.fi.pa165.core.service.flight.FlightService;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
public class FlightFacadeImpl implements FlightFacade<Long> {
private final FlightService flightService;
private final ModelMapper modelMapper;
@Autowired
public FlightFacadeImpl(FlightService flightService, ModelMapper modelMapper) {
this.flightService = flightService;
this.modelMapper = modelMapper;
}
@Override
public FlightDto save(NewFlightDtoRequest newFlightDtoRequest) {
var entityToSave = modelMapper.map(newFlightDtoRequest, Flight.class);
var savedEntity = flightService.save(entityToSave);
return modelMapper.map(savedEntity, FlightDto.class);
}
@Override
public Optional<FlightDto> findById(Long id) {
var foundEntity = flightService.findById(id);
return foundEntity.map(e -> modelMapper.map(e, FlightDto.class));
}
@Override
public List<FlightDto> findAll() {
return null;
}
@Override
public void deleteById(Long id) {
}
@Override
public void deleteAll() {
}
}
package cz.muni.fi.pa165.core.facade.steward;
import cz.muni.fi.pa165.core.model.NewStewardDtoRequest;
import cz.muni.fi.pa165.core.model.StewardDto;
import java.util.List;
import java.util.Optional;
/**
* @param <K> Key
* @author martinslovik
*/
public interface StewardFacade<K> {
StewardDto save(NewStewardDtoRequest newStewardDtoRequest);
Optional<StewardDto> findById(K id);
List<StewardDto> findAll();
void deleteById(K id);
void deleteAll();
StewardDto assignStewardFlight(Long stewardId, Long flightId);
}
package cz.muni.fi.pa165.core.facade.steward;
import cz.muni.fi.pa165.core.data.domain.FlightSteward;
import cz.muni.fi.pa165.core.data.domain.Steward;
import cz.muni.fi.pa165.core.exceptions.ResourceNotFoundException;
import cz.muni.fi.pa165.core.model.NewStewardDtoRequest;
import cz.muni.fi.pa165.core.model.StewardDto;
import cz.muni.fi.pa165.core.service.flight.FlightService;
import cz.muni.fi.pa165.core.service.steward.StewardService;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
public class StewardFacadeImpl implements StewardFacade<Long> {
private final StewardService stewardService;
private final FlightService flightService;
private final ModelMapper modelMapper;
@Autowired
public StewardFacadeImpl(StewardService stewardService, FlightService flightService, ModelMapper modelMapper) {
this.stewardService = stewardService;
this.flightService = flightService;
this.modelMapper = modelMapper;
}
@Override
public StewardDto save(NewStewardDtoRequest newStewardDtoRequest) {
var entityToSave = modelMapper.map(newStewardDtoRequest, Steward.class);
var savedEntity = stewardService.save(entityToSave);
return modelMapper.map(savedEntity, StewardDto.class);
}
@Override
public Optional<StewardDto> findById(Long id) {
return Optional.empty();
}
@Override
public List<StewardDto> findAll() {
return null;
}
@Override
public void deleteById(Long id) {
}
@Override
public void deleteAll() {
}
@Override
public StewardDto assignStewardFlight(Long stewardId, Long flightId) {
var stewardEntityOpt = stewardService.findById(stewardId);
var flightEntityOpt = flightService.findById(flightId);
if (stewardEntityOpt.isEmpty() || flightEntityOpt.isEmpty()) {
throw new ResourceNotFoundException("Not Found.");
}
var stewardEntity = stewardEntityOpt.get();
var flightEntity = flightEntityOpt.get();
var flightSteward = new FlightSteward();
flightSteward.setStewardId(stewardId);
flightSteward.setSteward(stewardEntity);
flightSteward.setFlightId(flightId);
flightSteward.setFlight(flightEntity);
flightEntity.setFlightSteward(flightSteward);
flightService.save(flightEntity);
stewardEntity.setFlightSteward(flightSteward);
stewardService.save(stewardEntity);
//TODO create DTO without nested members
return null;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment