Changes in / [ae61ea4:dd7a2b6]


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

    rae61ea4 rdd7a2b6  
    11package mk.ukim.finki.it.reservengo.service.impl;
    22
    3 import mk.ukim.finki.it.reservengo.dto.userDTO.CreateUserDTO;
     3import mk.ukim.finki.it.reservengo.dto.userDTO.*;
    44import mk.ukim.finki.it.reservengo.model.domain.User;
    55import mk.ukim.finki.it.reservengo.model.exceptions.EmailNotFoundException;
     6import mk.ukim.finki.it.reservengo.model.exceptions.UserEmailAlreadyExistsException;
    67import mk.ukim.finki.it.reservengo.model.exceptions.UserIdNotFoundException;
    78import mk.ukim.finki.it.reservengo.repository.UserRepository;
     9import mk.ukim.finki.it.reservengo.service.intf.JWTService;
    810import mk.ukim.finki.it.reservengo.service.intf.UserService;
    911import org.springframework.security.crypto.password.PasswordEncoder;
     
    1416
    1517    private final UserRepository userRepository;
     18    private final JWTService jwtService;
    1619    private final PasswordEncoder passwordEncoder;
    1720
    18     public UserServiceImpl(UserRepository userRepository, PasswordEncoder passwordEncoder) {
     21    public UserServiceImpl(UserRepository userRepository, PasswordEncoder passwordEncoder, JWTService jwtService) {
    1922        this.userRepository = userRepository;
     23        this.jwtService = jwtService;
    2024        this.passwordEncoder = passwordEncoder;
    2125    }
     
    3741
    3842    @Override
    39     public User editUserProfile(Long userId, CreateUserDTO createUserDTO) {
     43    public DisplayUserEmailDTO changeEmail(Long userId, EditUserEmailDTO editUserEmailDTO) {
    4044        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);
    4358    }
    4459
    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");
    4866        }
    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);
    6184    }
    6285}
  • ReserveNGo-backend/src/main/java/mk/ukim/finki/it/reservengo/service/intf/UserService.java

    rae61ea4 rdd7a2b6  
    11package mk.ukim.finki.it.reservengo.service.intf;
    22
    3 import mk.ukim.finki.it.reservengo.dto.userDTO.CreateUserDTO;
     3import mk.ukim.finki.it.reservengo.dto.userDTO.*;
    44import mk.ukim.finki.it.reservengo.model.domain.User;
    55
     
    77    User getUserById(Long userId);
    88
    9     User editUserProfile(Long userId, CreateUserDTO createUserDTO);
     9    DisplayUserDTO editUser(Long userId, EditUserProfileDTO editUserProfileDTO);
    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);
    1418}
  • ReserveNGo-backend/src/main/java/mk/ukim/finki/it/reservengo/web/advice/GlobalExceptionHandler.java

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

    rae61ea4 rdd7a2b6  
    33import mk.ukim.finki.it.reservengo.dto.localDTO.CreateLocalDTO;
    44import mk.ukim.finki.it.reservengo.service.intf.AdminService;
     5import org.springframework.http.HttpStatus;
    56import org.springframework.http.ResponseEntity;
    67import org.springframework.security.access.prepost.PreAuthorize;
     
    2122    public ResponseEntity<?> addLocal(@RequestBody CreateLocalDTO localDTO) {
    2223        adminService.addLocal(localDTO);
    23         return ResponseEntity.ok().build();
     24        return new ResponseEntity<>(HttpStatus.OK);
    2425    }
    2526
     
    2728    public ResponseEntity<?> deleteLocal(@PathVariable Long id) {
    2829        adminService.deleteLocal(id);
    29         return ResponseEntity.ok().build();
     30        return new ResponseEntity<>(HttpStatus.OK);
    3031    }
    3132}
  • ReserveNGo-backend/src/main/java/mk/ukim/finki/it/reservengo/web/controller/CustomerController.java

    rae61ea4 rdd7a2b6  
    66import mk.ukim.finki.it.reservengo.service.intf.CustomerService;
    77import mk.ukim.finki.it.reservengo.service.intf.ReservationService;
     8import org.springframework.http.HttpStatus;
    89import org.springframework.http.ResponseEntity;
    910import org.springframework.security.access.prepost.PreAuthorize;
     
    2930    public ResponseEntity<?> getCustomerReservations(@AuthenticationPrincipal User user) {
    3031        List<Reservation> customerReservations = reservationService.getCustomerReservations(user.getId());
    31         return ResponseEntity.ok(customerReservations);
     32        return new ResponseEntity<>(customerReservations, HttpStatus.OK);
    3233    }
    3334
     
    3536    public ResponseEntity<?> listFavouriteLocals(@AuthenticationPrincipal User user) {
    3637        List<Local> favouriteLocals = customerService.listFavouriteLocals(user.getId());
    37         return ResponseEntity.ok(favouriteLocals);
     38        return new ResponseEntity<>(favouriteLocals, HttpStatus.OK);
    3839    }
    3940
     
    4142    public ResponseEntity<?> addFavouriteLocal(@AuthenticationPrincipal User user, @PathVariable Long id) {
    4243        customerService.addFavouriteLocal(user.getId(), id);
    43         return ResponseEntity.ok().build();
     44        return new ResponseEntity<>(HttpStatus.OK);
    4445    }
    4546
     
    4748    public ResponseEntity<?> removeFavouriteLocal(@AuthenticationPrincipal User user, @PathVariable Long id) {
    4849        customerService.removeFavouriteLocal(user.getId(), id);
    49         return ResponseEntity.ok().build();
     50        return new ResponseEntity<>(HttpStatus.OK);
    5051    }
    5152}
  • ReserveNGo-backend/src/main/java/mk/ukim/finki/it/reservengo/web/controller/LocalController.java

    rae61ea4 rdd7a2b6  
    33import mk.ukim.finki.it.reservengo.model.domain.Local;
    44import mk.ukim.finki.it.reservengo.service.intf.LocalService;
     5import org.springframework.http.HttpStatus;
    56import org.springframework.http.ResponseEntity;
    67import org.springframework.web.bind.annotation.*;
     
    1819    @GetMapping
    1920    public ResponseEntity<?> getLocals() {
    20         return ResponseEntity.ok(localService.listAll());
     21        return new ResponseEntity<>(localService.listAll(), HttpStatus.OK);
    2122    }
    2223
     
    2425    public ResponseEntity<?> getLocalInformation(@PathVariable Long id) {
    2526        Local local = localService.getLocalById(id);
    26         return ResponseEntity.ok(local);
     27        return new ResponseEntity<>(local, HttpStatus.OK);
    2728    }
    2829}
  • ReserveNGo-backend/src/main/java/mk/ukim/finki/it/reservengo/web/controller/UserController.java

    rae61ea4 rdd7a2b6  
    11package mk.ukim.finki.it.reservengo.web.controller;
    22
    3 import mk.ukim.finki.it.reservengo.dto.userDTO.CreateUserDTO;
     3import mk.ukim.finki.it.reservengo.dto.userDTO.*;
    44import mk.ukim.finki.it.reservengo.model.domain.User;
    55import mk.ukim.finki.it.reservengo.service.intf.UserService;
     6import org.springframework.http.HttpStatus;
    67import org.springframework.http.ResponseEntity;
    78import org.springframework.security.core.annotation.AuthenticationPrincipal;
     
    2021
    2122    @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);
    2526    }
    2627
    2728    @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);
    3041    }
    3142}
Note: See TracChangeset for help on using the changeset viewer.