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

Adding new Mappers for Flight and Steward

parent 8abeb952
No related branches found
No related tags found
No related merge requests found
......@@ -8,7 +8,8 @@ import lombok.NoArgsConstructor;
import java.util.Collection;
import java.util.Objects;
@Entity(name = "stewards")
@Entity
@Table(name = "stewards")
@Data
@NoArgsConstructor
public class Steward extends DomainEntity {
......
......@@ -2,9 +2,15 @@ package cz.muni.fi.pa165.core.data.repository.steward;
import cz.muni.fi.pa165.core.data.domain.Steward;
import cz.muni.fi.pa165.core.data.repository.common.BaseRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.util.Optional;
@Repository
public interface StewardRepository extends BaseRepository<Steward, Long> {
@Query("SELECT s FROM Steward s JOIN FETCH s.flightStewards fs WHERE s.id = :id")
Optional<Steward> findByIdWithFlights(@Param("id") Long id);
}
package cz.muni.fi.pa165.core.facade.flight;
import cz.muni.fi.pa165.core.data.domain.Flight;
import cz.muni.fi.pa165.core.exceptions.ResourceNotFoundException;
import cz.muni.fi.pa165.core.mapper.FlightMapper;
import cz.muni.fi.pa165.core.model.FlightDto;
import cz.muni.fi.pa165.core.model.NewFlightDtoRequest;
import cz.muni.fi.pa165.core.model.StewardDto;
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;
......@@ -18,21 +15,19 @@ import java.util.Optional;
public class FlightFacadeImpl implements FlightFacade<Long> {
private final FlightService flightService;
private final ModelMapper modelMapper;
private final FlightMapper flightMapper;
@Autowired
public FlightFacadeImpl(FlightService flightService, ModelMapper modelMapper, FlightMapper flightMapper) {
public FlightFacadeImpl(FlightService flightService, FlightMapper flightMapper) {
this.flightService = flightService;
this.modelMapper = modelMapper;
this.flightMapper = flightMapper;
}
@Override
public FlightDto save(NewFlightDtoRequest newFlightDtoRequest) {
var entityToSave = modelMapper.map(newFlightDtoRequest, Flight.class);
var entityToSave = flightMapper.toEntityFromNewRequest(newFlightDtoRequest);
var savedEntity = flightService.save(entityToSave);
return modelMapper.map(savedEntity, FlightDto.class);
return flightMapper.toDto(savedEntity);
}
@Override
......@@ -41,14 +36,6 @@ public class FlightFacadeImpl implements FlightFacade<Long> {
.orElseThrow(() -> new ResourceNotFoundException("Not Found."));
return Optional.ofNullable(flightMapper.toDto(foundFlightEntity));
// var flightDto = modelMapper.map(foundFlightEntity, FlightDto.class);
//
// for (var flightSteward: foundFlightEntity.getFlightStewards()) {
// flightDto.addStewardsItem(modelMapper.map(flightSteward.getSteward(), StewardDto.class));
// }
//
// return Optional.ofNullable(flightDto);
}
@Override
......
......@@ -3,6 +3,8 @@ 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.mapper.StewardMapper;
import cz.muni.fi.pa165.core.model.FlightDto;
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;
......@@ -19,20 +21,20 @@ public class StewardFacadeImpl implements StewardFacade<Long> {
private final StewardService stewardService;
private final FlightService flightService;
private final ModelMapper modelMapper;
private final StewardMapper stewardMapper;
@Autowired
public StewardFacadeImpl(StewardService stewardService, FlightService flightService, ModelMapper modelMapper) {
public StewardFacadeImpl(StewardService stewardService, FlightService flightService, StewardMapper stewardMapper) {
this.stewardService = stewardService;
this.flightService = flightService;
this.modelMapper = modelMapper;
this.stewardMapper = stewardMapper;
}
@Override
public StewardDto save(NewStewardDtoRequest newStewardDtoRequest) {
var entityToSave = modelMapper.map(newStewardDtoRequest, Steward.class);
var entityToSave = stewardMapper.toEntityFromNewRequest(newStewardDtoRequest);
var savedEntity = stewardService.save(entityToSave);
return modelMapper.map(savedEntity, StewardDto.class);
return stewardMapper.toDto(savedEntity);
}
@Override
......@@ -57,25 +59,20 @@ public class StewardFacadeImpl implements StewardFacade<Long> {
@Override
public StewardDto assignStewardFlight(Long stewardId, Long flightId) {
var stewardEntityOpt = stewardService.findById(stewardId);
var flightEntityOpt = flightService.findById(flightId);
var stewardEntity = stewardService.findByIdWithFlights(stewardId)
.orElse(stewardService.findById(stewardId)
.orElseThrow(() -> new ResourceNotFoundException("Not Found.")));
if (stewardEntityOpt.isEmpty() || flightEntityOpt.isEmpty()) {
throw new ResourceNotFoundException("Not Found.");
}
var stewardEntity = stewardEntityOpt.get();
var flightEntity = flightEntityOpt.get();
var flightEntity = flightService.findById(flightId)
.orElseThrow(() -> new ResourceNotFoundException("Not Found."));
var flightSteward = new FlightSteward();
flightSteward.setSteward(stewardEntity);
flightSteward.setFlight(flightEntity);
stewardService.saveFlightStewards(flightSteward);
// Check that Flight members were eagerly fetched
var flightEntityOptEagerlyFetched = flightService.findByIdWithStewards(flightId);
var stewardDto = stewardMapper.toDto(stewardEntity);
//TODO create DTO without nested members
return null;
return stewardDto;
}
}
package cz.muni.fi.pa165.core.mapper;
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 org.mapstruct.*;
@Mapper(componentModel = "spring")
public interface FlightMapper {
Flight toEntity(FlightDto flightDto);
Flight toEntityFromNewRequest(NewFlightDtoRequest newFlightDtoRequest);
@Mapping(target = "stewards", source = "flightStewards")
FlightDto toDto(Flight flight);
@BeanMapping(nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE)
Flight partialUpdate(FlightDto flightDto, @MappingTarget Flight flight);
}
package cz.muni.fi.pa165.core.mapper;
import cz.muni.fi.pa165.core.data.domain.Steward;
import cz.muni.fi.pa165.core.model.NewStewardDtoRequest;
import cz.muni.fi.pa165.core.model.StewardDto;
import org.mapstruct.Mapper;
@Mapper(componentModel = "spring")
public interface StewardMapper {
Steward toEntity(StewardDto stewardDto);
Steward toEntityFromNewRequest(NewStewardDtoRequest newStewardDtoRequest);
StewardDto toDto(Steward steward);
}
......@@ -4,6 +4,8 @@ import cz.muni.fi.pa165.core.data.domain.FlightSteward;
import cz.muni.fi.pa165.core.data.domain.Steward;
import cz.muni.fi.pa165.core.service.common.BaseService;
import java.util.Optional;
public interface StewardService extends BaseService<Steward, Long> {
/**
......@@ -12,4 +14,6 @@ public interface StewardService extends BaseService<Steward, Long> {
* @return FlightSteward connection table stored in DB
*/
FlightSteward saveFlightStewards(FlightSteward flightSteward);
Optional<Steward> findByIdWithFlights(Long id);
}
......@@ -8,6 +8,8 @@ import cz.muni.fi.pa165.core.service.common.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Optional;
@Service
public class StewardServiceImpl extends BaseServiceImpl<Steward, Long> implements StewardService {
......@@ -25,4 +27,9 @@ public class StewardServiceImpl extends BaseServiceImpl<Steward, Long> implement
public FlightSteward saveFlightStewards(FlightSteward flightSteward) {
return flightStewardRepository.save(flightSteward);
}
@Override
public Optional<Steward> findByIdWithFlights(Long id) {
return stewardRepository.findByIdWithFlights(id);
}
}
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