Changes in / [ae61ea4:dd7a2b6]
- Location:
- ReserveNGo-backend/src/main/java/mk/ukim/finki/it/reservengo
- Files:
-
- 5 added
- 1 deleted
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
ReserveNGo-backend/src/main/java/mk/ukim/finki/it/reservengo/config/WebSecurityConfig.java
rae61ea4 rdd7a2b6 46 46 "/uploads/**") 47 47 .permitAll() 48 .requestMatchers("/api/user/**").authenticated() 48 49 .requestMatchers("/api/customer/**").hasRole("CUSTOMER") 49 50 .requestMatchers("/api/admin/**").hasRole("ADMIN") -
ReserveNGo-backend/src/main/java/mk/ukim/finki/it/reservengo/service/impl/UserServiceImpl.java
rae61ea4 rdd7a2b6 1 1 package mk.ukim.finki.it.reservengo.service.impl; 2 2 3 import mk.ukim.finki.it.reservengo.dto.userDTO. CreateUserDTO;3 import mk.ukim.finki.it.reservengo.dto.userDTO.*; 4 4 import mk.ukim.finki.it.reservengo.model.domain.User; 5 5 import mk.ukim.finki.it.reservengo.model.exceptions.EmailNotFoundException; 6 import mk.ukim.finki.it.reservengo.model.exceptions.UserEmailAlreadyExistsException; 6 7 import mk.ukim.finki.it.reservengo.model.exceptions.UserIdNotFoundException; 7 8 import mk.ukim.finki.it.reservengo.repository.UserRepository; 9 import mk.ukim.finki.it.reservengo.service.intf.JWTService; 8 10 import mk.ukim.finki.it.reservengo.service.intf.UserService; 9 11 import org.springframework.security.crypto.password.PasswordEncoder; … … 14 16 15 17 private final UserRepository userRepository; 18 private final JWTService jwtService; 16 19 private final PasswordEncoder passwordEncoder; 17 20 18 public UserServiceImpl(UserRepository userRepository, PasswordEncoder passwordEncoder ) {21 public UserServiceImpl(UserRepository userRepository, PasswordEncoder passwordEncoder, JWTService jwtService) { 19 22 this.userRepository = userRepository; 23 this.jwtService = jwtService; 20 24 this.passwordEncoder = passwordEncoder; 21 25 } … … 37 41 38 42 @Override 39 public User editUserProfile(Long userId, CreateUserDTO createUserDTO) {43 public DisplayUserEmailDTO changeEmail(Long userId, EditUserEmailDTO editUserEmailDTO) { 40 44 User user = userRepository.findById(userId).orElseThrow(() -> new UserIdNotFoundException(userId)); 41 updateUserFromDto(user, createUserDTO); 42 return userRepository.save(user); 45 46 if (editUserEmailDTO.newEmail().equals(user.getEmail())) { 47 throw new IllegalArgumentException("New email must be different from the current email."); 48 } 49 if (emailExists(editUserEmailDTO.newEmail())) { 50 throw new UserEmailAlreadyExistsException(editUserEmailDTO.newEmail()); 51 } 52 user.setEmail(editUserEmailDTO.newEmail()); 53 54 userRepository.save(user); 55 String jwt = jwtService.generateToken(user); 56 57 return DisplayUserEmailDTO.fromUser(user, jwt); 43 58 } 44 59 45 private void updateUserFromDto(User user, CreateUserDTO createUserDTO) { 46 if (createUserDTO.firstName() != null) { 47 user.setFirstName(createUserDTO.firstName()); 60 @Override 61 public String changePassword(Long userId, EditUserPasswordDTO editUserPasswordDTO) { 62 User user = userRepository.findById(userId).orElseThrow(() -> new UserIdNotFoundException(userId)); 63 64 if (!passwordEncoder.matches(editUserPasswordDTO.currentPassword(), user.getPassword())) { 65 throw new IllegalArgumentException("Current password is incorrect"); 48 66 } 49 if (createUserDTO.lastName() != null) { 50 user.setLastName(createUserDTO.lastName()); 51 } 52 if (createUserDTO.email() != null) { 53 user.setEmail(createUserDTO.email()); 54 } 55 if (createUserDTO.password() != null && !createUserDTO.password().isEmpty()) { 56 user.setPassword(passwordEncoder.encode(createUserDTO.password())); 57 } 58 if (createUserDTO.phoneNumber() != null) { 59 user.setPhoneNumber(createUserDTO.phoneNumber()); 60 } 67 68 user.setPassword(passwordEncoder.encode(editUserPasswordDTO.newPassword())); 69 userRepository.save(user); 70 71 return "Password updated successfully!"; 72 } 73 74 @Override 75 public DisplayUserDTO editUser(Long userId, EditUserProfileDTO editUserProfileDTO) { 76 User user = userRepository.findById(userId).orElseThrow(() -> new UserIdNotFoundException(userId)); 77 78 user.setFirstName(editUserProfileDTO.firstName()); 79 user.setLastName(editUserProfileDTO.lastName()); 80 user.setPhoneNumber(editUserProfileDTO.phoneNumber()); 81 userRepository.save(user); 82 83 return DisplayUserDTO.fromUser(user); 61 84 } 62 85 } -
ReserveNGo-backend/src/main/java/mk/ukim/finki/it/reservengo/service/intf/UserService.java
rae61ea4 rdd7a2b6 1 1 package mk.ukim.finki.it.reservengo.service.intf; 2 2 3 import mk.ukim.finki.it.reservengo.dto.userDTO. CreateUserDTO;3 import mk.ukim.finki.it.reservengo.dto.userDTO.*; 4 4 import mk.ukim.finki.it.reservengo.model.domain.User; 5 5 … … 7 7 User getUserById(Long userId); 8 8 9 User editUserProfile(Long userId, CreateUserDTO createUserDTO);9 DisplayUserDTO editUser(Long userId, EditUserProfileDTO editUserProfileDTO); 10 10 11 11 User getUserByEmail(String email); 12 12 13 13 boolean emailExists(String email); 14 15 DisplayUserEmailDTO changeEmail(Long userId, EditUserEmailDTO editUserEmailDTO); 16 17 String changePassword(Long userId, EditUserPasswordDTO editUserPasswordDTO); 14 18 } -
ReserveNGo-backend/src/main/java/mk/ukim/finki/it/reservengo/web/advice/GlobalExceptionHandler.java
rae61ea4 rdd7a2b6 24 24 @ExceptionHandler(AuthenticationException.class) 25 25 public ResponseEntity<String> handleAuthenticationError(AuthenticationException ex) { 26 return ResponseEntity.status(HttpStatus.NOT_FOUND).body(ex.getMessage()); 26 return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ex.getMessage()); 27 } 28 29 @ExceptionHandler(IllegalArgumentException.class) 30 public ResponseEntity<String> handleIllegalArgumentException(IllegalArgumentException ex) { 31 return ResponseEntity.status(HttpStatus.NOT_ACCEPTABLE).body(ex.getMessage()); 27 32 } 28 33 -
ReserveNGo-backend/src/main/java/mk/ukim/finki/it/reservengo/web/controller/AdminController.java
rae61ea4 rdd7a2b6 3 3 import mk.ukim.finki.it.reservengo.dto.localDTO.CreateLocalDTO; 4 4 import mk.ukim.finki.it.reservengo.service.intf.AdminService; 5 import org.springframework.http.HttpStatus; 5 6 import org.springframework.http.ResponseEntity; 6 7 import org.springframework.security.access.prepost.PreAuthorize; … … 21 22 public ResponseEntity<?> addLocal(@RequestBody CreateLocalDTO localDTO) { 22 23 adminService.addLocal(localDTO); 23 return ResponseEntity.ok().build();24 return new ResponseEntity<>(HttpStatus.OK); 24 25 } 25 26 … … 27 28 public ResponseEntity<?> deleteLocal(@PathVariable Long id) { 28 29 adminService.deleteLocal(id); 29 return ResponseEntity.ok().build();30 return new ResponseEntity<>(HttpStatus.OK); 30 31 } 31 32 } -
ReserveNGo-backend/src/main/java/mk/ukim/finki/it/reservengo/web/controller/CustomerController.java
rae61ea4 rdd7a2b6 6 6 import mk.ukim.finki.it.reservengo.service.intf.CustomerService; 7 7 import mk.ukim.finki.it.reservengo.service.intf.ReservationService; 8 import org.springframework.http.HttpStatus; 8 9 import org.springframework.http.ResponseEntity; 9 10 import org.springframework.security.access.prepost.PreAuthorize; … … 29 30 public ResponseEntity<?> getCustomerReservations(@AuthenticationPrincipal User user) { 30 31 List<Reservation> customerReservations = reservationService.getCustomerReservations(user.getId()); 31 return ResponseEntity.ok(customerReservations);32 return new ResponseEntity<>(customerReservations, HttpStatus.OK); 32 33 } 33 34 … … 35 36 public ResponseEntity<?> listFavouriteLocals(@AuthenticationPrincipal User user) { 36 37 List<Local> favouriteLocals = customerService.listFavouriteLocals(user.getId()); 37 return ResponseEntity.ok(favouriteLocals);38 return new ResponseEntity<>(favouriteLocals, HttpStatus.OK); 38 39 } 39 40 … … 41 42 public ResponseEntity<?> addFavouriteLocal(@AuthenticationPrincipal User user, @PathVariable Long id) { 42 43 customerService.addFavouriteLocal(user.getId(), id); 43 return ResponseEntity.ok().build();44 return new ResponseEntity<>(HttpStatus.OK); 44 45 } 45 46 … … 47 48 public ResponseEntity<?> removeFavouriteLocal(@AuthenticationPrincipal User user, @PathVariable Long id) { 48 49 customerService.removeFavouriteLocal(user.getId(), id); 49 return ResponseEntity.ok().build();50 return new ResponseEntity<>(HttpStatus.OK); 50 51 } 51 52 } -
ReserveNGo-backend/src/main/java/mk/ukim/finki/it/reservengo/web/controller/LocalController.java
rae61ea4 rdd7a2b6 3 3 import mk.ukim.finki.it.reservengo.model.domain.Local; 4 4 import mk.ukim.finki.it.reservengo.service.intf.LocalService; 5 import org.springframework.http.HttpStatus; 5 6 import org.springframework.http.ResponseEntity; 6 7 import org.springframework.web.bind.annotation.*; … … 18 19 @GetMapping 19 20 public ResponseEntity<?> getLocals() { 20 return ResponseEntity.ok(localService.listAll());21 return new ResponseEntity<>(localService.listAll(), HttpStatus.OK); 21 22 } 22 23 … … 24 25 public ResponseEntity<?> getLocalInformation(@PathVariable Long id) { 25 26 Local local = localService.getLocalById(id); 26 return ResponseEntity.ok(local);27 return new ResponseEntity<>(local, HttpStatus.OK); 27 28 } 28 29 } -
ReserveNGo-backend/src/main/java/mk/ukim/finki/it/reservengo/web/controller/UserController.java
rae61ea4 rdd7a2b6 1 1 package mk.ukim.finki.it.reservengo.web.controller; 2 2 3 import mk.ukim.finki.it.reservengo.dto.userDTO. CreateUserDTO;3 import mk.ukim.finki.it.reservengo.dto.userDTO.*; 4 4 import mk.ukim.finki.it.reservengo.model.domain.User; 5 5 import mk.ukim.finki.it.reservengo.service.intf.UserService; 6 import org.springframework.http.HttpStatus; 6 7 import org.springframework.http.ResponseEntity; 7 8 import org.springframework.security.core.annotation.AuthenticationPrincipal; … … 20 21 21 22 @GetMapping("/profile") 22 public ResponseEntity< ?> getProfile(@AuthenticationPrincipal User user) {23 User u = userService.getUserById(user.getId());24 return ResponseEntity.ok(u);23 public ResponseEntity<DisplayUserDTO> getProfile(@AuthenticationPrincipal User user) { 24 DisplayUserDTO displayUserDTO = DisplayUserDTO.fromUser(userService.getUserById(user.getId())); 25 return new ResponseEntity<>(displayUserDTO, HttpStatus.OK); 25 26 } 26 27 27 28 @PutMapping("/edit") 28 public ResponseEntity<?> editProfile(@AuthenticationPrincipal User user, @RequestBody CreateUserDTO createUserDTO) { 29 return ResponseEntity.ok(userService.editUserProfile(user.getId(), createUserDTO)); 29 public ResponseEntity<DisplayUserDTO> editProfile(@AuthenticationPrincipal User user, @RequestBody EditUserProfileDTO editUserProfileDTO) { 30 return new ResponseEntity<>(userService.editUser(user.getId(), editUserProfileDTO), HttpStatus.OK); 31 } 32 33 @PatchMapping("/change-email") 34 public ResponseEntity<DisplayUserEmailDTO> changeEmail(@AuthenticationPrincipal User user, @RequestBody EditUserEmailDTO editUserEmailDTO) { 35 return new ResponseEntity<>(userService.changeEmail(user.getId(), editUserEmailDTO), HttpStatus.OK); 36 } 37 38 @PatchMapping("/change-password") 39 public ResponseEntity<String> changePassword(@AuthenticationPrincipal User user, @RequestBody EditUserPasswordDTO editUserPasswordDTO) { 40 return new ResponseEntity<>(userService.changePassword(user.getId(), editUserPasswordDTO), HttpStatus.OK); 30 41 } 31 42 }
Note:
See TracChangeset
for help on using the changeset viewer.