Skip to content
Snippets Groups Projects
Commit 4f31dfb6 authored by Bruno Teixeira's avatar Bruno Teixeira
Browse files

added Responseentitys

parent fbbcb0aa
No related branches found
No related tags found
No related merge requests found
...@@ -3,8 +3,11 @@ package com.mobilecomputing.pecunia.controller; ...@@ -3,8 +3,11 @@ package com.mobilecomputing.pecunia.controller;
import com.mobilecomputing.pecunia.model.Trip; import com.mobilecomputing.pecunia.model.Trip;
import com.mobilecomputing.pecunia.model.User; import com.mobilecomputing.pecunia.model.User;
import com.mobilecomputing.pecunia.repository.TripRepository; import com.mobilecomputing.pecunia.repository.TripRepository;
import com.mobilecomputing.pecunia.repository.UserRepository;
import org.bson.types.ObjectId; import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -17,50 +20,69 @@ public class TripController { ...@@ -17,50 +20,69 @@ public class TripController {
@Autowired @Autowired
TripRepository tripRepository; TripRepository tripRepository;
@Autowired
UserRepository userRepository;
@GetMapping("/getTripById") @GetMapping("/getTripById")
public String getTripById(@RequestParam String id) { public ResponseEntity getTripById(@RequestParam String id) {
Trip response = tripRepository.findById(id).get();
if(response==null){
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
}
return String.valueOf(tripRepository.findById(id)); return ResponseEntity.ok(tripRepository.findById(id).get());
} }
@GetMapping("/getAllTrips") @GetMapping("/getAllTrips")
public ArrayList<Trip> getAllTrips() { public ResponseEntity getAllTrips() {
ArrayList<Trip> response = new ArrayList<>(); ArrayList<Trip> response = new ArrayList<>();
tripRepository.findAll().forEach(trip -> { tripRepository.findAll().forEach(trip -> {
response.add(trip); response.add(trip);
}); });
return response;
if(response.size()==0){
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
}
return ResponseEntity.ok(response);
} }
@PostMapping("/addTrip") @PostMapping("/addTrip")
public String addTrip(@RequestParam String tripName, @RequestParam String startOfTrip, public ResponseEntity addTrip(@RequestBody Trip trip) {
@RequestParam String endOfTrip, @RequestParam List<User> tripParticipants) {
Trip temp = new Trip(tripName, null, null, tripParticipants, null); return ResponseEntity.ok(tripRepository.save(trip));
tripRepository.save(temp).getTripId();
return tripRepository.save(temp).getTripId();
} }
@GetMapping("/getTripByUser") @GetMapping("/getTripsByUser")
public List<Trip> getTripsByUser(@RequestParam String eMail) { public ResponseEntity getTripsByUser(@RequestParam String eMail) {
if(userRepository.findById(eMail).get()==null){
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("User not found");
}
ArrayList<Trip> temp = new ArrayList<>(); ArrayList<Trip> temp = new ArrayList<>();
ArrayList<Trip> response = new ArrayList<>(); ArrayList<Trip> response = new ArrayList<>();
tripRepository.findAll().forEach(trip -> { tripRepository.findAll().forEach(trip -> {
temp.add(trip); temp.add(trip);
}); });
for (Trip t : temp) {
boolean eMailIsEqual = false; for (Trip t: temp) {
for (User u : t.getTripParticipants()) { boolean userIsParticipant = false;
if (u.geteMail().equals(eMail)) { for(String tempEmail: t.getTripParticipants()){
eMailIsEqual = true; if(tempEmail.equals(eMail)){
break; userIsParticipant=true;
} }
} }
if (eMailIsEqual) {
if(userIsParticipant){
response.add(t); response.add(t);
eMailIsEqual = false;
} }
} }
return response;
if(response.size()==0){
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("User has no trip");
}
return ResponseEntity.ok(response);
} }
} }
...@@ -6,6 +6,7 @@ import com.mobilecomputing.pecunia.repository.TripRepository; ...@@ -6,6 +6,7 @@ import com.mobilecomputing.pecunia.repository.TripRepository;
import com.mobilecomputing.pecunia.repository.UserRepository; import com.mobilecomputing.pecunia.repository.UserRepository;
import org.bson.types.ObjectId; import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
...@@ -14,7 +15,7 @@ import java.util.List; ...@@ -14,7 +15,7 @@ import java.util.List;
import java.util.Optional; import java.util.Optional;
@RestController @RestController
@RequestMapping(value = "/user", method = RequestMethod.GET) @RequestMapping(value = "/user")
public class UserController { public class UserController {
@Autowired @Autowired
UserRepository userRepository; UserRepository userRepository;
...@@ -22,45 +23,67 @@ public class UserController { ...@@ -22,45 +23,67 @@ public class UserController {
TripRepository tripRepository; TripRepository tripRepository;
@GetMapping("/getByEMail") @GetMapping("/getByEMail")
public String getUserByEmail(@RequestParam String eMail){ public ResponseEntity getUserByEmail(@RequestParam String eMail) {
return String.valueOf(userRepository.findById(eMail)); User response = userRepository.findById(eMail).get();
if (response != null) {
return ResponseEntity.ok(response);
}
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
} }
@GetMapping("/getAll") @GetMapping("/getAll")
public List<User> getAllUsers(){ public ResponseEntity getAllUsers() {
ArrayList<User> response = new ArrayList<>(); ArrayList<User> response = new ArrayList<>();
userRepository.findAll().forEach(user -> { userRepository.findAll().forEach(user -> {
response.add(user); response.add(user);
}); });
return response; if (response.size() > 0) {
return ResponseEntity.ok(response);
}
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
} }
@PostMapping("/registrateUser") @PostMapping("/registrateUser")
public String registrateUser(@RequestParam String eMail, @RequestParam String name, @RequestParam String surname, public ResponseEntity registrateUser(@RequestBody User user) {
@RequestParam String password){
if (userRepository.findById(eMail).isEmpty()) {
User user = new User();
user.seteMail(eMail); // überprüfen ob email schon vergeben ist
user.setName(name);
user.setSurname(surname);
user.setPassword(password);
if (userRepository.findById(user.geteMail()).isEmpty()) {
userRepository.save(user); userRepository.save(user);
return "ok"; return ResponseEntity.ok(HttpStatus.OK);
} }
return "error"; return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("User already registered");
} }
@PostMapping("/addUserToTrip") @PostMapping("/addUserToTrip")
public void addUserToTrip(@RequestParam String eMail,@RequestParam String tripId){ public ResponseEntity addUserToTrip(@RequestParam String eMail, @RequestParam String tripId) {
Optional<Trip> t = tripRepository.findById(tripId); boolean userAlreadyexists = false;
t.get().getTripParticipants().add(userRepository.findById(eMail).get()); Trip trip = tripRepository.findById(tripId).get();
User user = userRepository.findById(eMail).get();
for (String currentUserEMail : trip.getTripParticipants()) {
if (currentUserEMail.equals(eMail)) {
userAlreadyexists = true;
}
}
if (userAlreadyexists) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("User already exists");
}
if (trip == null || user == null) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
}
trip.getTripParticipants().add(eMail);
tripRepository.save(trip);
return ResponseEntity.ok(HttpStatus.OK);
} }
@DeleteMapping("/deleteUser") @DeleteMapping("/deleteUser")
public void deleteUser(@RequestParam String eMail){ public ResponseEntity deleteUser(@RequestParam String eMail) {
if(userRepository.findById(eMail).get()==null){
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
}
userRepository.deleteById(eMail); userRepository.deleteById(eMail);
return ResponseEntity.ok(HttpStatus.OK);
} }
} }
...@@ -5,12 +5,12 @@ import org.springframework.data.annotation.Id; ...@@ -5,12 +5,12 @@ import org.springframework.data.annotation.Id;
public class Transaction { public class Transaction {
@Id @Id
String transactionId; String transactionId;
private User debtor; //Schuldner private String debtor; //Schuldner
private User creditor; // Gläubiger private String creditor; // Gläubiger
private String currency; private String currency; // vllt Enum?
private double loan; private double loan;
public Transaction(User debtor, User creditor, String currency, double loan) { public Transaction(String debtor, String creditor, String currency, double loan) {
this.debtor = debtor; this.debtor = debtor;
this.creditor = creditor; this.creditor = creditor;
this.currency = currency; this.currency = currency;
...@@ -25,19 +25,19 @@ public class Transaction { ...@@ -25,19 +25,19 @@ public class Transaction {
this.transactionId = transactionId; this.transactionId = transactionId;
} }
public User getDebtor() { public String getDebtor() {
return debtor; return debtor;
} }
public void setDebtor(User debtor) { public void setDebtor(String debtor) {
this.debtor = debtor; this.debtor = debtor;
} }
public User getCreditor() { public String getCreditor() {
return creditor; return creditor;
} }
public void setCreditor(User creditor) { public void setCreditor(String creditor) {
this.creditor = creditor; this.creditor = creditor;
} }
......
...@@ -11,11 +11,11 @@ public class Trip { ...@@ -11,11 +11,11 @@ public class Trip {
private String tripName; private String tripName;
private Date startOfTrip; private Date startOfTrip;
private Date endOfTrip; private Date endOfTrip;
private List<User> tripParticipants; private List<String> tripParticipants;
private List<Transaction> transactions; private List<String> transactions;
public Trip(String tripName, Date startOfTrip, Date endOfTrip, List<User> tripParticipants, public Trip(String tripName, Date startOfTrip, Date endOfTrip, List<String> tripParticipants,
List<Transaction> transactions) { List<String> transactions) {
this.tripName = tripName; this.tripName = tripName;
this.startOfTrip = startOfTrip; this.startOfTrip = startOfTrip;
this.endOfTrip = endOfTrip; this.endOfTrip = endOfTrip;
...@@ -55,11 +55,11 @@ public class Trip { ...@@ -55,11 +55,11 @@ public class Trip {
this.endOfTrip = endOfTrip; this.endOfTrip = endOfTrip;
} }
public List<User> getTripParticipants() { public List<String> getTripParticipants() {
return tripParticipants; return tripParticipants;
} }
public void setTripParticipants(List<User> tripParticipants) { public void setTripParticipants(List<String> tripParticipants) {
this.tripParticipants = tripParticipants; this.tripParticipants = tripParticipants;
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment