Changeset 6fe77af for trip-planner/src/main
- Timestamp:
- 02/06/22 18:15:51 (3 years ago)
- Branches:
- master
- Children:
- 571e0df
- Parents:
- 76712b2
- Location:
- trip-planner/src/main/java/finki/diplomska/tripplanner
- Files:
-
- 4 added
- 1 deleted
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
trip-planner/src/main/java/finki/diplomska/tripplanner/models/Location.java
r76712b2 r6fe77af 14 14 @Entity 15 15 @Table(name = "locations") 16 @NoArgsConstructor17 16 @AllArgsConstructor 18 17 @Getter … … 65 64 private List<Planner> plannerList; 66 65 66 @ManyToOne 67 @JsonBackReference 68 @JoinColumn(name = "id_user") 69 private User user; 67 70 68 71 @ManyToMany … … 74 77 private List<Category> categoryList; 75 78 76 77 79 @OneToMany(mappedBy = "location", cascade = CascadeType.REMOVE, orphanRemoval = true) 78 80 @JsonManagedReference 79 81 private List<Images> imagesList; 80 82 83 public Location(String name, String description, String address, String priority, int duration, String trivia, byte[] photo, Region region, City city, User user) { 84 this.name = name; 85 this.description = description; 86 this.address = address; 87 this.priority = priority; 88 this.duration = duration; 89 this.trivia = trivia; 90 this.photo = photo; 91 this.region = region; 92 this.city = city; 93 this.user = user; 94 } 81 95 96 public Location() { 97 } 82 98 } -
trip-planner/src/main/java/finki/diplomska/tripplanner/models/User.java
r76712b2 r6fe77af 2 2 3 3 import com.fasterxml.jackson.annotation.JsonIgnore; 4 import com.fasterxml.jackson.annotation.JsonManagedReference; 4 5 import lombok.AllArgsConstructor; 5 6 import lombok.Getter; … … 44 45 @JsonIgnore 45 46 private List<Planner> planners = new ArrayList<>(); 47 48 @OneToMany(cascade = CascadeType.REFRESH, mappedBy = "user", orphanRemoval = true) 49 @JsonIgnore 50 private List<Location> locationList = new ArrayList<>();; 46 51 47 52 public User() { -
trip-planner/src/main/java/finki/diplomska/tripplanner/security/SecurityConfig.java
r76712b2 r6fe77af 81 81 .antMatchers("/api/all").permitAll() 82 82 .antMatchers("/api/places").permitAll() 83 .antMatchers("/create/planner").permitAll() 84 .antMatchers("/create/planner/{id}").permitAll() 85 .antMatchers("/create/locations").permitAll() 86 .antMatchers("/create/trip/locations").permitAll() 87 .antMatchers("/create").permitAll() 83 88 .anyRequest().authenticated(); 84 89 http.addFilterBefore(jwtAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class); -
trip-planner/src/main/java/finki/diplomska/tripplanner/service/LocationService.java
r76712b2 r6fe77af 4 4 import finki.diplomska.tripplanner.models.Location; 5 5 import finki.diplomska.tripplanner.models.Planner; 6 import finki.diplomska.tripplanner.models.dto.LocationDto; 6 7 import finki.diplomska.tripplanner.models.dto.PlannerLocationDto; 7 8 import org.springframework.data.repository.query.Param; … … 26 27 List<Long> getAllLocationIdsForPlanner(Long plannerId); 27 28 List<Location> getAllLocations(String place); 29 Optional<Location> save (LocationDto locationDto, String username); 28 30 } -
trip-planner/src/main/java/finki/diplomska/tripplanner/service/impl/LocationServiceImpl.java
r76712b2 r6fe77af 1 1 package finki.diplomska.tripplanner.service.impl; 2 2 3 import finki.diplomska.tripplanner.models. Location;4 import finki.diplomska.tripplanner.models. Planner;3 import finki.diplomska.tripplanner.models.*; 4 import finki.diplomska.tripplanner.models.dto.LocationDto; 5 5 import finki.diplomska.tripplanner.models.dto.PlannerLocationDto; 6 import finki.diplomska.tripplanner.models.exceptions.CityNotFoundException; 7 import finki.diplomska.tripplanner.models.exceptions.CompanionNotFoundException; 6 8 import finki.diplomska.tripplanner.models.exceptions.LocationNotFoundException; 7 import finki.diplomska.tripplanner. repository.jpa.JpaLocationRepository;8 import finki.diplomska.tripplanner.repository.jpa. JpaPlannerRepository;9 import finki.diplomska.tripplanner.models.exceptions.RegionNotFoundException; 10 import finki.diplomska.tripplanner.repository.jpa.*; 9 11 import finki.diplomska.tripplanner.service.LocationService; 10 12 import org.springframework.stereotype.Service; … … 19 21 private final JpaLocationRepository locationRepository; 20 22 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) { 23 30 this.locationRepository = locationRepository; 24 31 this.plannerRepository = plannerRepository; 32 this.regionRepository = regionRepository; 33 this.cityRepository = cityRepository; 34 this.userRepository = userRepository; 35 this.categoryRepository = categoryRepository; 36 this.companionRepository = companionRepository; 25 37 } 26 38 … … 151 163 } 152 164 } 153 154 165 return newList; 155 166 } … … 189 200 } 190 201 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 } 191 218 192 219 } -
trip-planner/src/main/java/finki/diplomska/tripplanner/web/rest/LocationRestController.java
r76712b2 r6fe77af 2 2 3 3 import finki.diplomska.tripplanner.models.Location; 4 import finki.diplomska.tripplanner.models.User; 5 import finki.diplomska.tripplanner.models.dto.LocationDto; 4 6 import finki.diplomska.tripplanner.models.dto.PlannerLocationDto; 5 7 import finki.diplomska.tripplanner.service.LocationService; 6 8 import finki.diplomska.tripplanner.service.PlannerService; 7 9 import org.springframework.http.ResponseEntity; 10 import org.springframework.security.core.Authentication; 8 11 import org.springframework.web.bind.annotation.*; 9 12 … … 82 85 } 83 86 87 @PostMapping("/add") 88 public ResponseEntity<Location> save(@RequestBody LocationDto locationDto, Authentication authentication) { 89 User user = (User) authentication.getPrincipal(); 90 return this.locationService.save(locationDto, user.getUsername()) 91 .map(location -> ResponseEntity.ok().body(location)) 92 .orElseGet(() -> ResponseEntity.badRequest().build()); 93 } 84 94 } -
trip-planner/src/main/java/finki/diplomska/tripplanner/web/rest/PlannerRestController.java
r76712b2 r6fe77af 69 69 @ResponseStatus(HttpStatus.CREATED) 70 70 public ResponseEntity<?> newPlanner(@Valid @RequestBody PlannerDto plannerDto, Authentication authentication) { 71 72 71 User user = (User) authentication.getPrincipal(); 73 72 return this.plannerService.newPlanner(plannerDto, user.getUsername())
Note:
See TracChangeset
for help on using the changeset viewer.