|
@@ -1,11 +1,14 @@
|
|
|
package com.deliveryproject.easydelivery;
|
|
package com.deliveryproject.easydelivery;
|
|
|
|
|
|
|
|
import com.deliveryproject.easydelivery.Models.AppUser;
|
|
import com.deliveryproject.easydelivery.Models.AppUser;
|
|
|
|
|
+import com.deliveryproject.easydelivery.Models.Delivery;
|
|
|
import com.deliveryproject.easydelivery.Repository.AppUserRepository;
|
|
import com.deliveryproject.easydelivery.Repository.AppUserRepository;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.http.HttpStatus;
|
|
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.*;
|
|
|
|
|
+
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
import java.util.Optional;
|
|
import java.util.Optional;
|
|
|
|
|
|
|
@@ -22,9 +25,9 @@ public class AppUserController {
|
|
|
return new ResponseEntity<>(savedUser, HttpStatus.CREATED);
|
|
return new ResponseEntity<>(savedUser, HttpStatus.CREATED);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- @GetMapping("/{id}")
|
|
|
|
|
- public ResponseEntity<AppUser> getUserById(@PathVariable Long id) {
|
|
|
|
|
- Optional<AppUser> userOptional = userRepository.findById(id);
|
|
|
|
|
|
|
+ @GetMapping("/{name}")
|
|
|
|
|
+ public ResponseEntity<AppUser> getUserByName(@PathVariable String name) {
|
|
|
|
|
+ Optional<AppUser> userOptional = userRepository.findByName(name);
|
|
|
return userOptional.map(user -> new ResponseEntity<>(user, HttpStatus.OK))
|
|
return userOptional.map(user -> new ResponseEntity<>(user, HttpStatus.OK))
|
|
|
.orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND));
|
|
.orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND));
|
|
|
}
|
|
}
|
|
@@ -46,6 +49,21 @@ public class AppUserController {
|
|
|
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
|
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ @PutMapping("/{name}")
|
|
|
|
|
+ public ResponseEntity<AppUser> addDeliveryToTheUser(@PathVariable String name, @RequestBody Delivery delivery) {
|
|
|
|
|
+ Optional<AppUser> userOptional = userRepository.findByName(name);
|
|
|
|
|
+ if (userOptional.isPresent()) {
|
|
|
|
|
+ AppUser appUser = userOptional.get();
|
|
|
|
|
+ List<Delivery> newListOfDeliveries = appUser.getDeliveries();
|
|
|
|
|
+ newListOfDeliveries.add(delivery);
|
|
|
|
|
+ appUser.setDeliveries(newListOfDeliveries);
|
|
|
|
|
+ System.out.println("abcd " + appUser.toString());
|
|
|
|
|
+ userRepository.save(appUser);
|
|
|
|
|
+ return new ResponseEntity<>(appUser, HttpStatus.OK);
|
|
|
|
|
+ }
|
|
|
|
|
+ return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
@DeleteMapping("/{id}")
|
|
@DeleteMapping("/{id}")
|
|
|
public ResponseEntity<HttpStatus> deleteUser(@PathVariable Long id) {
|
|
public ResponseEntity<HttpStatus> deleteUser(@PathVariable Long id) {
|
|
|
try {
|
|
try {
|