Changes in / [dd7a2b6:ae61ea4]
- Location:
- ReserveNGo-backend/src/main/java/mk/ukim/finki/it/reservengo
- Files:
-
- 1 added
- 5 deleted
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
ReserveNGo-backend/src/main/java/mk/ukim/finki/it/reservengo/config/WebSecurityConfig.java
rdd7a2b6 rae61ea4 46 46 "/uploads/**") 47 47 .permitAll() 48 .requestMatchers("/api/user/**").authenticated()49 48 .requestMatchers("/api/customer/**").hasRole("CUSTOMER") 50 49 .requestMatchers("/api/admin/**").hasRole("ADMIN") -
ReserveNGo-backend/src/main/java/mk/ukim/finki/it/reservengo/service/impl/UserServiceImpl.java
rdd7a2b6 rae61ea4 1 1 package mk.ukim.finki.it.reservengo.service.impl; 2 2 3 import mk.ukim.finki.it.reservengo.dto.userDTO. *;3 import mk.ukim.finki.it.reservengo.dto.userDTO.CreateUserDTO; 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;7 6 import mk.ukim.finki.it.reservengo.model.exceptions.UserIdNotFoundException; 8 7 import mk.ukim.finki.it.reservengo.repository.UserRepository; 9 import mk.ukim.finki.it.reservengo.service.intf.JWTService;10 8 import mk.ukim.finki.it.reservengo.service.intf.UserService; 11 9 import org.springframework.security.crypto.password.PasswordEncoder; … … 16 14 17 15 private final UserRepository userRepository; 18 private final JWTService jwtService;19 16 private final PasswordEncoder passwordEncoder; 20 17 21 public UserServiceImpl(UserRepository userRepository, PasswordEncoder passwordEncoder , JWTService jwtService) {18 public UserServiceImpl(UserRepository userRepository, PasswordEncoder passwordEncoder) { 22 19 this.userRepository = userRepository; 23 this.jwtService = jwtService;24 20 this.passwordEncoder = passwordEncoder; 25 21 } … … 41 37 42 38 @Override 43 public DisplayUserEmailDTO changeEmail(Long userId, EditUserEmailDTO editUserEmailDTO) {39 public User editUserProfile(Long userId, CreateUserDTO createUserDTO) { 44 40 User user = userRepository.findById(userId).orElseThrow(() -> new UserIdNotFoundException(userId)); 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); 41 updateUserFromDto(user, createUserDTO); 42 return userRepository.save(user); 58 43 } 59 44 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"); 45 private void updateUserFromDto(User user, CreateUserDTO createUserDTO) { 46 if (createUserDTO.firstName() != null) { 47 user.setFirstName(createUserDTO.firstName()); 66 48 } 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); 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 } 84 61 } 85 62 } -
ReserveNGo-backend/src/main/java/mk/ukim/finki/it/reservengo/service/intf/UserService.java
rdd7a2b6 rae61ea4 1 1 package mk.ukim.finki.it.reservengo.service.intf; 2 2 3 import mk.ukim.finki.it.reservengo.dto.userDTO. *;3 import mk.ukim.finki.it.reservengo.dto.userDTO.CreateUserDTO; 4 4 import mk.ukim.finki.it.reservengo.model.domain.User; 5 5 … … 7 7 User getUserById(Long userId); 8 8 9 DisplayUserDTO editUser(Long userId, EditUserProfileDTO editUserProfileDTO);9 User editUserProfile(Long userId, CreateUserDTO createUserDTO); 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);18 14 } -
ReserveNGo-backend/src/main/java/mk/ukim/finki/it/reservengo/web/advice/GlobalExceptionHandler.java
rdd7a2b6 rae61ea4 24 24 @ExceptionHandler(AuthenticationException.class) 25 25 public ResponseEntity<String> handleAuthenticationError(AuthenticationException ex) { 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()); 26 return ResponseEntity.status(HttpStatus.NOT_FOUND).body(ex.getMessage()); 32 27 } 33 28 -
ReserveNGo-backend/src/main/java/mk/ukim/finki/it/reservengo/web/controller/AdminController.java
rdd7a2b6 rae61ea4 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;6 5 import org.springframework.http.ResponseEntity; 7 6 import org.springframework.security.access.prepost.PreAuthorize; … … 22 21 public ResponseEntity<?> addLocal(@RequestBody CreateLocalDTO localDTO) { 23 22 adminService.addLocal(localDTO); 24 return new ResponseEntity<>(HttpStatus.OK);23 return ResponseEntity.ok().build(); 25 24 } 26 25 … … 28 27 public ResponseEntity<?> deleteLocal(@PathVariable Long id) { 29 28 adminService.deleteLocal(id); 30 return new ResponseEntity<>(HttpStatus.OK);29 return ResponseEntity.ok().build(); 31 30 } 32 31 } -
ReserveNGo-backend/src/main/java/mk/ukim/finki/it/reservengo/web/controller/CustomerController.java
rdd7a2b6 rae61ea4 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;9 8 import org.springframework.http.ResponseEntity; 10 9 import org.springframework.security.access.prepost.PreAuthorize; … … 30 29 public ResponseEntity<?> getCustomerReservations(@AuthenticationPrincipal User user) { 31 30 List<Reservation> customerReservations = reservationService.getCustomerReservations(user.getId()); 32 return new ResponseEntity<>(customerReservations, HttpStatus.OK);31 return ResponseEntity.ok(customerReservations); 33 32 } 34 33 … … 36 35 public ResponseEntity<?> listFavouriteLocals(@AuthenticationPrincipal User user) { 37 36 List<Local> favouriteLocals = customerService.listFavouriteLocals(user.getId()); 38 return new ResponseEntity<>(favouriteLocals, HttpStatus.OK);37 return ResponseEntity.ok(favouriteLocals); 39 38 } 40 39 … … 42 41 public ResponseEntity<?> addFavouriteLocal(@AuthenticationPrincipal User user, @PathVariable Long id) { 43 42 customerService.addFavouriteLocal(user.getId(), id); 44 return new ResponseEntity<>(HttpStatus.OK);43 return ResponseEntity.ok().build(); 45 44 } 46 45 … … 48 47 public ResponseEntity<?> removeFavouriteLocal(@AuthenticationPrincipal User user, @PathVariable Long id) { 49 48 customerService.removeFavouriteLocal(user.getId(), id); 50 return new ResponseEntity<>(HttpStatus.OK);49 return ResponseEntity.ok().build(); 51 50 } 52 51 } -
ReserveNGo-backend/src/main/java/mk/ukim/finki/it/reservengo/web/controller/LocalController.java
rdd7a2b6 rae61ea4 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;6 5 import org.springframework.http.ResponseEntity; 7 6 import org.springframework.web.bind.annotation.*; … … 19 18 @GetMapping 20 19 public ResponseEntity<?> getLocals() { 21 return new ResponseEntity<>(localService.listAll(), HttpStatus.OK);20 return ResponseEntity.ok(localService.listAll()); 22 21 } 23 22 … … 25 24 public ResponseEntity<?> getLocalInformation(@PathVariable Long id) { 26 25 Local local = localService.getLocalById(id); 27 return new ResponseEntity<>(local, HttpStatus.OK);26 return ResponseEntity.ok(local); 28 27 } 29 28 } -
ReserveNGo-backend/src/main/java/mk/ukim/finki/it/reservengo/web/controller/UserController.java
rdd7a2b6 rae61ea4 1 1 package mk.ukim.finki.it.reservengo.web.controller; 2 2 3 import mk.ukim.finki.it.reservengo.dto.userDTO. *;3 import mk.ukim.finki.it.reservengo.dto.userDTO.CreateUserDTO; 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;7 6 import org.springframework.http.ResponseEntity; 8 7 import org.springframework.security.core.annotation.AuthenticationPrincipal; … … 21 20 22 21 @GetMapping("/profile") 23 public ResponseEntity< DisplayUserDTO> getProfile(@AuthenticationPrincipal User user) {24 DisplayUserDTO displayUserDTO = DisplayUserDTO.fromUser(userService.getUserById(user.getId()));25 return new ResponseEntity<>(displayUserDTO, HttpStatus.OK);22 public ResponseEntity<?> getProfile(@AuthenticationPrincipal User user) { 23 User u = userService.getUserById(user.getId()); 24 return ResponseEntity.ok(u); 26 25 } 27 26 28 27 @PutMapping("/edit") 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); 28 public ResponseEntity<?> editProfile(@AuthenticationPrincipal User user, @RequestBody CreateUserDTO createUserDTO) { 29 return ResponseEntity.ok(userService.editUserProfile(user.getId(), createUserDTO)); 41 30 } 42 31 }
Note:
See TracChangeset
for help on using the changeset viewer.