Changeset 6fe77af for trip-planner


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

Location:
trip-planner/src
Files:
4 added
1 deleted
8 edited

Legend:

Unmodified
Added
Removed
  • trip-planner/src/main/java/finki/diplomska/tripplanner/models/Location.java

    r76712b2 r6fe77af  
    1414@Entity
    1515@Table(name = "locations")
    16 @NoArgsConstructor
    1716@AllArgsConstructor
    1817@Getter
     
    6564    private List<Planner> plannerList;
    6665
     66    @ManyToOne
     67    @JsonBackReference
     68    @JoinColumn(name = "id_user")
     69    private User user;
    6770
    6871    @ManyToMany
     
    7477    private List<Category> categoryList;
    7578
    76 
    7779    @OneToMany(mappedBy = "location", cascade = CascadeType.REMOVE, orphanRemoval = true)
    7880    @JsonManagedReference
    7981    private List<Images> imagesList;
    8082
     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    }
    8195
     96    public Location() {
     97    }
    8298}
  • trip-planner/src/main/java/finki/diplomska/tripplanner/models/User.java

    r76712b2 r6fe77af  
    22
    33import com.fasterxml.jackson.annotation.JsonIgnore;
     4import com.fasterxml.jackson.annotation.JsonManagedReference;
    45import lombok.AllArgsConstructor;
    56import lombok.Getter;
     
    4445    @JsonIgnore
    4546    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<>();;
    4651
    4752    public User() {
  • trip-planner/src/main/java/finki/diplomska/tripplanner/security/SecurityConfig.java

    r76712b2 r6fe77af  
    8181                .antMatchers("/api/all").permitAll()
    8282                .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()
    8388                .anyRequest().authenticated();
    8489        http.addFilterBefore(jwtAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);
  • trip-planner/src/main/java/finki/diplomska/tripplanner/service/LocationService.java

    r76712b2 r6fe77af  
    44import finki.diplomska.tripplanner.models.Location;
    55import finki.diplomska.tripplanner.models.Planner;
     6import finki.diplomska.tripplanner.models.dto.LocationDto;
    67import finki.diplomska.tripplanner.models.dto.PlannerLocationDto;
    78import org.springframework.data.repository.query.Param;
     
    2627    List<Long> getAllLocationIdsForPlanner(Long plannerId);
    2728    List<Location> getAllLocations(String place);
     29    Optional<Location> save (LocationDto locationDto, String username);
    2830}
  • 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}
  • trip-planner/src/main/java/finki/diplomska/tripplanner/web/rest/LocationRestController.java

    r76712b2 r6fe77af  
    22
    33import finki.diplomska.tripplanner.models.Location;
     4import finki.diplomska.tripplanner.models.User;
     5import finki.diplomska.tripplanner.models.dto.LocationDto;
    46import finki.diplomska.tripplanner.models.dto.PlannerLocationDto;
    57import finki.diplomska.tripplanner.service.LocationService;
    68import finki.diplomska.tripplanner.service.PlannerService;
    79import org.springframework.http.ResponseEntity;
     10import org.springframework.security.core.Authentication;
    811import org.springframework.web.bind.annotation.*;
    912
     
    8285    }
    8386
     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    }
    8494}
  • trip-planner/src/main/java/finki/diplomska/tripplanner/web/rest/PlannerRestController.java

    r76712b2 r6fe77af  
    6969    @ResponseStatus(HttpStatus.CREATED)
    7070    public ResponseEntity<?> newPlanner(@Valid @RequestBody PlannerDto plannerDto, Authentication authentication)  {
    71 
    7271        User user = (User) authentication.getPrincipal();
    7372        return this.plannerService.newPlanner(plannerDto, user.getUsername())
  • trip-planner/src/test/api.http

    r76712b2 r6fe77af  
    117117GET http://localhost:8080/api/places
    118118Content-Type: application/json
     119
     120###
     121POST http://localhost:8080/api/add
     122Content-Type: application/json
Note: See TracChangeset for help on using the changeset viewer.