Ignore:
Timestamp:
02/06/22 18:15:51 (3 years ago)
Author:
Ema <ema_spirova@…>
Branches:
master
Children:
571e0df
Parents:
76712b2
Message:

add location feature

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trip-planner/src/main/java/finki/diplomska/tripplanner/service/impl/LocationServiceImpl.java

    r76712b2 r6fe77af  
    11package finki.diplomska.tripplanner.service.impl;
    22
    3 import finki.diplomska.tripplanner.models.Location;
    4 import finki.diplomska.tripplanner.models.Planner;
     3import finki.diplomska.tripplanner.models.*;
     4import finki.diplomska.tripplanner.models.dto.LocationDto;
    55import finki.diplomska.tripplanner.models.dto.PlannerLocationDto;
     6import finki.diplomska.tripplanner.models.exceptions.CityNotFoundException;
     7import finki.diplomska.tripplanner.models.exceptions.CompanionNotFoundException;
    68import finki.diplomska.tripplanner.models.exceptions.LocationNotFoundException;
    7 import finki.diplomska.tripplanner.repository.jpa.JpaLocationRepository;
    8 import finki.diplomska.tripplanner.repository.jpa.JpaPlannerRepository;
     9import finki.diplomska.tripplanner.models.exceptions.RegionNotFoundException;
     10import finki.diplomska.tripplanner.repository.jpa.*;
    911import finki.diplomska.tripplanner.service.LocationService;
    1012import org.springframework.stereotype.Service;
     
    1921    private final JpaLocationRepository locationRepository;
    2022    private final JpaPlannerRepository plannerRepository;
    21 
    22     public LocationServiceImpl(JpaLocationRepository locationRepository, JpaPlannerRepository plannerRepository) {
     23    private final JpaRegionRepository regionRepository;
     24    private final JpaCityRepository cityRepository;
     25    private final JpaUserRepository userRepository;
     26    private final JpaCategoryRepository categoryRepository;
     27    private final JpaCompanionRepository companionRepository;
     28
     29    public LocationServiceImpl(JpaLocationRepository locationRepository, JpaPlannerRepository plannerRepository, JpaRegionRepository regionRepository, JpaCityRepository cityRepository, JpaUserRepository userRepository, JpaCategoryRepository categoryRepository, JpaCompanionRepository companionRepository) {
    2330        this.locationRepository = locationRepository;
    2431        this.plannerRepository = plannerRepository;
     32        this.regionRepository = regionRepository;
     33        this.cityRepository = cityRepository;
     34        this.userRepository = userRepository;
     35        this.categoryRepository = categoryRepository;
     36        this.companionRepository = companionRepository;
    2537    }
    2638
     
    151163            }
    152164        }
    153 
    154165        return newList;
    155166    }
     
    189200    }
    190201
     202    @Override
     203    public Optional<Location> save(LocationDto locationDto, String username) {
     204        City city = new City();
     205        Region region = this.regionRepository.findById(locationDto.getRegion())
     206                .orElseThrow(() -> new RegionNotFoundException(locationDto.getRegion()));
     207        if(locationDto.getCity() != null){
     208             city = this.cityRepository.findById(locationDto.getCity())
     209                    .orElseThrow(() -> new CityNotFoundException(locationDto.getCity()));
     210        }else{
     211            city = null;
     212        }
     213        User user = this.userRepository.findByUsername(username);
     214        locationDto.setUser(user.getUsername());
     215        return Optional.of(this.locationRepository.save(new Location(locationDto.getName(), locationDto.getDescription(), locationDto.getAddress(), locationDto.getPriority(),
     216                locationDto.getDuration(), locationDto.getTrivia(), locationDto.getPhoto(), region, city, user)));
     217    }
    191218
    192219}
Note: See TracChangeset for help on using the changeset viewer.