Skip to content
Snippets Groups Projects
Verified Commit 66a3dd9c authored by Adam Krídl's avatar Adam Krídl
Browse files

Add CityFacade and its implementation

parent 5aba44b1
No related branches found
No related tags found
No related merge requests found
package cz.muni.fi.pa165.core.facade.city;
import cz.muni.fi.pa165.core.model.CityDto;
import cz.muni.fi.pa165.core.model.NewCityDtoRequest;
import java.util.List;
import java.util.Optional;
/**
* @param <K> Key
*/
public interface CityFacade<K> {
CityDto save(NewCityDtoRequest newCityDtoRequest);
Optional<CityDto> findById(K id);
List<CityDto> findAll();
void deleteById(K id);
void deleteAll();
CityDto update(K id, NewCityDtoRequest newCityDtoRequest);
Optional<CityDto> findByName(String name);
}
package cz.muni.fi.pa165.core.facade.city;
import cz.muni.fi.pa165.core.data.domain.City;
import cz.muni.fi.pa165.core.exceptions.ResourceNotFoundException;
import cz.muni.fi.pa165.core.mapper.CityMapper;
import cz.muni.fi.pa165.core.model.CityDto;
import cz.muni.fi.pa165.core.model.NewCityDtoRequest;
import cz.muni.fi.pa165.core.service.city.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
public class CityFacadeImpl implements CityFacade<Long> {
private final CityService cityService;
private final CityMapper cityMapper;
@Autowired
public CityFacadeImpl(CityService cityService, CityMapper cityMapper) {
this.cityService = cityService;
this.cityMapper = cityMapper;
}
@Override
public CityDto save(NewCityDtoRequest newCityDtoRequest) {
City entityToSave = cityMapper.toEntityFromNewRequest(newCityDtoRequest);
City savedEntity = cityService.save(entityToSave);
return cityMapper.toDto(savedEntity);
}
@Override
public Optional<CityDto> findById(Long id) {
City foundEntity = cityService.findById(id)
.orElseThrow(ResourceNotFoundException::new);
return Optional.of(cityMapper.toDto(foundEntity));
}
@Override
public List<CityDto> findAll() {
List<City> foundEntities = cityService.findAll();
return foundEntities.stream()
.map(cityMapper::toDto)
.toList();
}
@Override
public void deleteById(Long id) {
if (!cityService.existsById(id)) {
throw new ResourceNotFoundException();
}
cityService.deleteById(id);
}
@Override
public void deleteAll() {
cityService.deleteAll();
}
@Override
public CityDto update(Long id, NewCityDtoRequest newCityDtoRequest) {
if (!cityService.existsById(id)) {
throw new ResourceNotFoundException();
}
City newEntity = cityMapper.toEntityFromNewRequest(newCityDtoRequest);
City updatedEntity = cityService.update(id, newEntity);
return cityMapper.toDto(updatedEntity);
}
@Override
public Optional<CityDto> findByName(String name) {
Optional<City> foundEntity = cityService.findByName(name);
return foundEntity.map(cityMapper::toDto);
}
}
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