Changes in / [dd7a2b6:ae61ea4]


Ignore:
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  
    4646                                "/uploads/**")
    4747                        .permitAll()
    48                         .requestMatchers("/api/user/**").authenticated()
    4948                        .requestMatchers("/api/customer/**").hasRole("CUSTOMER")
    5049                        .requestMatchers("/api/admin/**").hasRole("ADMIN")
  • ReserveNGo-backend/src/main/java/mk/ukim/finki/it/reservengo/service/impl/UserServiceImpl.java

    rdd7a2b6 rae61ea4  
    11package mk.ukim.finki.it.reservengo.service.impl;
    22
    3 import mk.ukim.finki.it.reservengo.dto.userDTO.*;
     3import mk.ukim.finki.it.reservengo.dto.userDTO.CreateUserDTO;
    44import mk.ukim.finki.it.reservengo.model.domain.User;
    55import mk.ukim.finki.it.reservengo.model.exceptions.EmailNotFoundException;
    6 import mk.ukim.finki.it.reservengo.model.exceptions.UserEmailAlreadyExistsException;
    76import mk.ukim.finki.it.reservengo.model.exceptions.UserIdNotFoundException;
    87import mk.ukim.finki.it.reservengo.repository.UserRepository;
    9 import mk.ukim.finki.it.reservengo.service.intf.JWTService;
    108import mk.ukim.finki.it.reservengo.service.intf.UserService;
    119import org.springframework.security.crypto.password.PasswordEncoder;
     
    1614
    1715    private final UserRepository userRepository;
    18     private final JWTService jwtService;
    1916    private final PasswordEncoder passwordEncoder;
    2017
    21     public UserServiceImpl(UserRepository userRepository, PasswordEncoder passwordEncoder, JWTService jwtService) {
     18    public UserServiceImpl(UserRepository userRepository, PasswordEncoder passwordEncoder) {
    2219        this.userRepository = userRepository;
    23         this.jwtService = jwtService;
    2420        this.passwordEncoder = passwordEncoder;
    2521    }
     
    4137
    4238    @Override
    43     public DisplayUserEmailDTO changeEmail(Long userId, EditUserEmailDTO editUserEmailDTO) {
     39    public User editUserProfile(Long userId, CreateUserDTO createUserDTO) {
    4440        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);
    5843    }
    5944
    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());
    6648        }
    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        }
    8461    }
    8562}
  • ReserveNGo-backend/src/main/java/mk/ukim/finki/it/reservengo/service/intf/UserService.java

    rdd7a2b6 rae61ea4  
    11package mk.ukim.finki.it.reservengo.service.intf;
    22
    3 import mk.ukim.finki.it.reservengo.dto.userDTO.*;
     3import mk.ukim.finki.it.reservengo.dto.userDTO.CreateUserDTO;
    44import mk.ukim.finki.it.reservengo.model.domain.User;
    55
     
    77    User getUserById(Long userId);
    88
    9     DisplayUserDTO editUser(Long userId, EditUserProfileDTO editUserProfileDTO);
     9    User editUserProfile(Long userId, CreateUserDTO createUserDTO);
    1010
    1111    User getUserByEmail(String email);
    1212
    1313    boolean emailExists(String email);
    14 
    15     DisplayUserEmailDTO changeEmail(Long userId, EditUserEmailDTO editUserEmailDTO);
    16 
    17     String changePassword(Long userId, EditUserPasswordDTO editUserPasswordDTO);
    1814}
  • ReserveNGo-backend/src/main/java/mk/ukim/finki/it/reservengo/web/advice/GlobalExceptionHandler.java

    rdd7a2b6 rae61ea4  
    2424    @ExceptionHandler(AuthenticationException.class)
    2525    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());
    3227    }
    3328
  • ReserveNGo-backend/src/main/java/mk/ukim/finki/it/reservengo/web/controller/AdminController.java

    rdd7a2b6 rae61ea4  
    33import mk.ukim.finki.it.reservengo.dto.localDTO.CreateLocalDTO;
    44import mk.ukim.finki.it.reservengo.service.intf.AdminService;
    5 import org.springframework.http.HttpStatus;
    65import org.springframework.http.ResponseEntity;
    76import org.springframework.security.access.prepost.PreAuthorize;
     
    2221    public ResponseEntity<?> addLocal(@RequestBody CreateLocalDTO localDTO) {
    2322        adminService.addLocal(localDTO);
    24         return new ResponseEntity<>(HttpStatus.OK);
     23        return ResponseEntity.ok().build();
    2524    }
    2625
     
    2827    public ResponseEntity<?> deleteLocal(@PathVariable Long id) {
    2928        adminService.deleteLocal(id);
    30         return new ResponseEntity<>(HttpStatus.OK);
     29        return ResponseEntity.ok().build();
    3130    }
    3231}
  • ReserveNGo-backend/src/main/java/mk/ukim/finki/it/reservengo/web/controller/CustomerController.java

    rdd7a2b6 rae61ea4  
    66import mk.ukim.finki.it.reservengo.service.intf.CustomerService;
    77import mk.ukim.finki.it.reservengo.service.intf.ReservationService;
    8 import org.springframework.http.HttpStatus;
    98import org.springframework.http.ResponseEntity;
    109import org.springframework.security.access.prepost.PreAuthorize;
     
    3029    public ResponseEntity<?> getCustomerReservations(@AuthenticationPrincipal User user) {
    3130        List<Reservation> customerReservations = reservationService.getCustomerReservations(user.getId());
    32         return new ResponseEntity<>(customerReservations, HttpStatus.OK);
     31        return ResponseEntity.ok(customerReservations);
    3332    }
    3433
     
    3635    public ResponseEntity<?> listFavouriteLocals(@AuthenticationPrincipal User user) {
    3736        List<Local> favouriteLocals = customerService.listFavouriteLocals(user.getId());
    38         return new ResponseEntity<>(favouriteLocals, HttpStatus.OK);
     37        return ResponseEntity.ok(favouriteLocals);
    3938    }
    4039
     
    4241    public ResponseEntity<?> addFavouriteLocal(@AuthenticationPrincipal User user, @PathVariable Long id) {
    4342        customerService.addFavouriteLocal(user.getId(), id);
    44         return new ResponseEntity<>(HttpStatus.OK);
     43        return ResponseEntity.ok().build();
    4544    }
    4645
     
    4847    public ResponseEntity<?> removeFavouriteLocal(@AuthenticationPrincipal User user, @PathVariable Long id) {
    4948        customerService.removeFavouriteLocal(user.getId(), id);
    50         return new ResponseEntity<>(HttpStatus.OK);
     49        return ResponseEntity.ok().build();
    5150    }
    5251}
  • ReserveNGo-backend/src/main/java/mk/ukim/finki/it/reservengo/web/controller/LocalController.java

    rdd7a2b6 rae61ea4  
    33import mk.ukim.finki.it.reservengo.model.domain.Local;
    44import mk.ukim.finki.it.reservengo.service.intf.LocalService;
    5 import org.springframework.http.HttpStatus;
    65import org.springframework.http.ResponseEntity;
    76import org.springframework.web.bind.annotation.*;
     
    1918    @GetMapping
    2019    public ResponseEntity<?> getLocals() {
    21         return new ResponseEntity<>(localService.listAll(), HttpStatus.OK);
     20        return ResponseEntity.ok(localService.listAll());
    2221    }
    2322
     
    2524    public ResponseEntity<?> getLocalInformation(@PathVariable Long id) {
    2625        Local local = localService.getLocalById(id);
    27         return new ResponseEntity<>(local, HttpStatus.OK);
     26        return ResponseEntity.ok(local);
    2827    }
    2928}
  • ReserveNGo-backend/src/main/java/mk/ukim/finki/it/reservengo/web/controller/UserController.java

    rdd7a2b6 rae61ea4  
    11package mk.ukim.finki.it.reservengo.web.controller;
    22
    3 import mk.ukim.finki.it.reservengo.dto.userDTO.*;
     3import mk.ukim.finki.it.reservengo.dto.userDTO.CreateUserDTO;
    44import mk.ukim.finki.it.reservengo.model.domain.User;
    55import mk.ukim.finki.it.reservengo.service.intf.UserService;
    6 import org.springframework.http.HttpStatus;
    76import org.springframework.http.ResponseEntity;
    87import org.springframework.security.core.annotation.AuthenticationPrincipal;
     
    2120
    2221    @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);
    2625    }
    2726
    2827    @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));
    4130    }
    4231}
Note: See TracChangeset for help on using the changeset viewer.