Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---------------------------------------------------------------------------------------------------------------------------
- ------------------------------------------------ USERCONTROLLER -----------------------------------------------------------
- ---------------------------------------------------------------------------------------------------------------------------
- @PutMapping("/{userId}")
- public UserOutDTO update(@RequestHeader HttpHeaders headers,@PathVariable int userId,
- @Valid @RequestBody UserDTO userDTO) {
- try {
- User loggedInUser = authenticationHelper.tryGetUser(headers);
- User userToBeUpdated = mapper.toDTOUpdate(userDTO, userId);
- Optional<String> phone = Optional.ofNullable(userDTO.getPhone());
- int userRole = userDTO.getUserType();
- service.update(loggedInUser, userId, userToBeUpdated, phone, userRole);
- return mapper.fromDTO(userToBeUpdated);
- } catch (EntityNotFoundException e) {
- throw new ResponseStatusException(
- HttpStatus.NOT_FOUND,
- e.getMessage()
- );
- } catch (DuplicateEntityException e) {
- throw new ResponseStatusException(
- HttpStatus.CONFLICT,
- e.getMessage()
- );
- } catch (UnauthorizedOperationException e){
- throw new ResponseStatusException(
- HttpStatus.UNAUTHORIZED,
- e.getMessage()
- );
- }
- }
- ---------------------------------------------------------------------------------------------------------------------------
- ------------------------------------------------ UserService --------------------------------------------------------------
- ---------------------------------------------------------------------------------------------------------------------------
- @Override
- public void update(User userToBeAuthorized, int userId, User userToBeUpdated,
- Optional<String> phoneFromUser, int userRole) {
- boolean sameUser = userToBeAuthorized.getUserId() == userId;
- boolean loggedUserIsAdmin = userToBeAuthorized.getUserType().getUserRole().equalsIgnoreCase("admin");
- boolean userToUpdateIsAdmin = userToBeUpdated.getUserType().getUserRole().equalsIgnoreCase("admin");
- boolean sameRole = userRole <= userToBeAuthorized.getUserType().getUserTypeId();
- boolean duplicateExists = true;
- boolean sameUserInDB = false;
- try {
- User existingUser = repository.getByEmail(userToBeUpdated.getEmail());
- if (existingUser.getUserId() == userToBeUpdated.getUserId()) {
- duplicateExists = false;
- sameUserInDB = true;
- }
- } catch (EntityNotFoundException e) {
- duplicateExists = false;
- }
- if (duplicateExists) {
- throw new DuplicateEntityException("User", "email", userToBeUpdated.getEmail());
- }
- if (!sameUserInDB){
- throw new UnauthorizedOperationException(UPDATE_USER_ERROR_MESSAGE);
- }
- /*Update USER || ADMIN*/
- if (sameUser && sameRole){
- repository.update(userToBeUpdated);
- }
- /*Promote user*/
- if(loggedUserIsAdmin && !sameUser && userRole == 3){
- repository.update(userToBeUpdated);
- }
- Phone existingPhone = phoneRepository.getByUserId(userId);
- /*Admin can add or change its phone*/
- if (phoneFromUser.isPresent() && sameUser && loggedUserIsAdmin){
- Phone phoneToBe = new Phone();
- phoneToBe.setPhone(phoneFromUser.get());
- phoneToBe.setUser(userId);
- if (existingPhone != null){
- phoneRepository.update(phoneToBe);
- if (phoneFromUser.get().equals(existingPhone.getPhone())){
- throw new DuplicateEntityException("Phone", "phoneNumber", phoneFromUser.get());
- }
- }
- else{
- phoneRepository.create(phoneToBe);
- }
- }
- }
- ---------------------------------------------------------------------------------------------------------------------------
- ------------------------------------------------ UserRepository -----------------------------------------------------------
- ---------------------------------------------------------------------------------------------------------------------------
- @Override
- public void update(User user) {
- try(Session session = sessionFactory.openSession()){
- session.beginTransaction();
- session.update(user);
- session.getTransaction().commit();
- }
- }
- ---------------------------------------------------------------------------------------------------------------------------
- ------------------------------------------------ PhoneRepository ----------------------------------------------------------
- ---------------------------------------------------------------------------------------------------------------------------
- @Override
- public Phone getByUserId(int userId) {
- try(Session session = sessionFactory.openSession()){
- Query<Phone> query = session.createQuery
- ("from Phones where userId = :userId", Phone.class);
- query.setParameter("userId", userId);
- Phone phone = null;
- if (!query.list().isEmpty()){
- phone = query.list().get(0);
- }
- return phone;
- }
- }
- @Override
- public void create(Phone phoneToBe) {
- try(Session session = sessionFactory.openSession()){
- session.beginTransaction();
- session.save(phoneToBe);
- session.getTransaction().commit();
- }
- }
- @Override
- public void update(Phone phoneToBe) {
- try(Session session = sessionFactory.openSession()){
- session.beginTransaction();
- session.update(phoneToBe);
- session.getTransaction().commit();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment