Skip to content

Commit

Permalink
Get poi list and add a poi to the user
Browse files Browse the repository at this point in the history
  • Loading branch information
sefadegirmenci committed Jul 21, 2022
1 parent 7794eb2 commit 3aeab90
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/main/java/com/eistgeist/flightsystem/rest/UserController.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.eistgeist.flightsystem.rest;

import com.eistgeist.flightsystem.model.Journey;
import com.eistgeist.flightsystem.model.POI;
import com.eistgeist.flightsystem.model.User;
import com.eistgeist.flightsystem.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
Expand All @@ -25,20 +26,31 @@ public ResponseEntity<User> addUser(@RequestBody User user) {
if(user == null) return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
return new ResponseEntity<>(userService.addUser(user), HttpStatus.CREATED);
}

@GetMapping("")
@Operation(description = "Get users")
public ResponseEntity<List<User>> getUsers() {
return ResponseEntity.ok(userService.getUsers());
}
@GetMapping("{username}/journeys")
@Operation(description = "Get journeys of the user")
public ResponseEntity<List<Journey>> getJourneys(@PathVariable String username) {
return ResponseEntity.ok(userService.getJourneys(username));
}
@PostMapping("{username}/addJourney")
@PostMapping("{username}/journeys")
@Operation(description = "Add a journey to the user")
public ResponseEntity<User> addJourney(@RequestBody Journey journey, @PathVariable String username) {
return ResponseEntity.ok(userService.addJourney(journey, username));
}
@GetMapping("{username}/pois")
@Operation(description = "Get POIs of the user")
public ResponseEntity<List<POI>> getPOIs(@PathVariable String username) {
return ResponseEntity.ok(userService.getPOIs(username));
}
@PostMapping("{username}/pois")
@Operation(description = "Add a journey to the user")
public ResponseEntity<User> addPOI(@RequestBody POI poi, @PathVariable String username) {
return ResponseEntity.ok(userService.addPOI(poi, username));
}



Expand Down
18 changes: 18 additions & 0 deletions src/main/java/com/eistgeist/flightsystem/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.eistgeist.flightsystem.exception.FlightNotFoundException;
import com.eistgeist.flightsystem.model.Journey;
import com.eistgeist.flightsystem.model.POI;
import com.eistgeist.flightsystem.model.User;
import com.eistgeist.flightsystem.repository.FlightRepository;
import com.eistgeist.flightsystem.repository.UserRepository;
Expand Down Expand Up @@ -45,4 +46,21 @@ public List<Journey> getJourneys(String userName) {
User user = userRepository.findUserByUserNameIgnoreCase(userName).orElseThrow(() -> new IllegalStateException("User not found"));
return user.getJourneys();
}

public List<POI> getPOIs(String userName) {
User user = userRepository.findUserByUserNameIgnoreCase(userName).orElseThrow(() -> new IllegalStateException("User not found"));
return user.getPOIList();
}
public User addPOI(POI poi, String userName) {
User user = userRepository.findUserByUserNameIgnoreCase(userName).orElseThrow(() -> new IllegalStateException("User not found"));
List<POI> pois = user.getPOIList();
if (pois == null) {
pois = new ArrayList<>();
}
pois.add(poi);
user.setPOIList(pois);
return userRepository.save(user);
}


}

0 comments on commit 3aeab90

Please sign in to comment.