source: trip-planner/src/main/java/finki/diplomska/tripplanner/service/impl/LocationServiceImpl.java@ eed0bf8

Last change on this file since eed0bf8 was eed0bf8, checked in by Ema <ema_spirova@…>, 3 years ago

editing location-form on frontend and backend

  • Property mode set to 100644
File size: 3.9 KB
Line 
1package finki.diplomska.tripplanner.service.impl;
2
3import finki.diplomska.tripplanner.models.Location;
4import finki.diplomska.tripplanner.models.exceptions.LocationNotFoundException;
5import finki.diplomska.tripplanner.repository.jpa.JpaLocationRepository;
6import finki.diplomska.tripplanner.service.LocationService;
7import org.springframework.stereotype.Service;
8
9import java.util.*;
10import java.util.stream.Collectors;
11
12
13@Service
14public class LocationServiceImpl implements LocationService {
15
16 private final JpaLocationRepository locationRepository;
17
18 public LocationServiceImpl(JpaLocationRepository locationRepository) {
19 this.locationRepository = locationRepository;
20 }
21
22 @Override
23 public List<Location> findLocationsFromCity(String locName, String companion, List<String> categories) {
24 return this.locationRepository.findLocationsFromCity(locName, companion, categories);
25 }
26
27 @Override
28 public List<Location> findLocationsFromCountry(String locName, String companion, String region, List<String> categories) {
29 return this.locationRepository.findLocationsFromCountry(locName, companion, region, categories);
30 }
31
32 @Override
33 public List<Location> findAll() {
34 return this.locationRepository.findAll();
35 }
36
37 @Override
38 public Location getById(Long id) {
39 Optional<Location> location = this.locationRepository.findById(id);
40 if (!location.isPresent()) {
41 throw new LocationNotFoundException(id);
42 }
43 return location.get();
44 }
45
46 @Override
47 public Optional<Location> findById(Long id) {
48 return this.locationRepository.findById(id);
49 }
50
51 @Override
52 public List<Location> findLocations(Long locationId, Long companionId, Long lengthOfStay, String categoryIds) {
53 List<Long> categories = null;
54 if(categoryIds != null && !categoryIds.isEmpty()){
55 List<String> ids = Arrays.asList(categoryIds.split(","));
56 categories = ids.stream().map(Long::valueOf).collect(Collectors.toList());
57 }
58 List<Location> foundLocations = locationRepository.findLocationsFromForm(locationId, companionId, categories);
59 return foundLocations;
60 }
61
62 @Override
63 public List<Location> scheduleLocations(String locName, String companion, String region, List<String> categories, int numberOfDays) {
64 int maxMinutesPerDay = numberOfDays *6 * 60;
65 int minutesPerDay = 0;
66 List<Location> locations = this.locationRepository.findLocationsFromCity(locName, companion, categories);
67 List<Location> countryLocations = this.locationRepository.findLocationsFromCountry(locName, companion, region, categories);
68 List<Location> newList = new ArrayList<>();
69 int listSize = locations.size();
70 int listCountrySize = countryLocations.size();
71
72 if(locName.equals("Macedonia")){
73 while(minutesPerDay < maxMinutesPerDay){
74 for(Location l: countryLocations) {
75 if (minutesPerDay < maxMinutesPerDay && l.getDuration() + minutesPerDay <= maxMinutesPerDay && listCountrySize != 0) {
76 newList.add(l);
77 listCountrySize --;
78 }
79 minutesPerDay += l.getDuration();
80 if (minutesPerDay > maxMinutesPerDay) {
81 break;
82 }
83 }
84 }
85 }else{
86 while(minutesPerDay < maxMinutesPerDay ){
87 for(Location l: locations) {
88 if (minutesPerDay < maxMinutesPerDay && l.getDuration() + minutesPerDay <= maxMinutesPerDay && listSize != 0) {
89 newList.add(l);
90 listSize --;
91 }
92 minutesPerDay += l.getDuration();
93 if (minutesPerDay > maxMinutesPerDay) {
94 break;
95 }
96 }
97 }
98 }
99
100 return newList;
101 }
102
103}
Note: See TracBrowser for help on using the repository browser.