Skip to content

Commit

Permalink
Task/Added update email function (#44)
Browse files Browse the repository at this point in the history
* Added change login function

* Fix style issues
  • Loading branch information
Nikname2303 authored Jun 7, 2024
1 parent a53a098 commit 923b704
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
14 changes: 14 additions & 0 deletions src/main/java/com/ua/accommodation/controller/UserController.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.ua.accommodation.controller;

import com.ua.accommodation.dto.user.UserResponseDto;
import com.ua.accommodation.dto.user.UserUpdateEmailDto;
import com.ua.accommodation.dto.user.UserUpdateProfileDto;
import com.ua.accommodation.dto.user.UserUpdateRoleDto;
import com.ua.accommodation.model.User;
Expand Down Expand Up @@ -59,4 +60,17 @@ public UserResponseDto updateProfile(
@RequestBody @Valid UserUpdateProfileDto updateProfileDto) {
return userService.updateProfile(user.getId(), updateProfileDto);
}

@PatchMapping("/newEmail")
@PreAuthorize("hasRole('USER')")
@Operation(
summary = "Update email",
description = "You can update your email, "
+ "but after that you need to log in with new email"
)
public UserResponseDto updateEmail(
@AuthenticationPrincipal User user,
@RequestBody @Valid UserUpdateEmailDto updateDto) {
return userService.updateEmail(user.getEmail(), updateDto);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.ua.accommodation.dto.user;

import jakarta.validation.constraints.NotBlank;
import lombok.Data;

@Data
public class UserUpdateEmailDto {
@NotBlank
private String newEmail;
}
3 changes: 3 additions & 0 deletions src/main/java/com/ua/accommodation/service/UserService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.ua.accommodation.service;

import com.ua.accommodation.dto.user.UserResponseDto;
import com.ua.accommodation.dto.user.UserUpdateEmailDto;
import com.ua.accommodation.dto.user.UserUpdateProfileDto;
import com.ua.accommodation.dto.user.UserUpdateRoleDto;

Expand All @@ -10,4 +11,6 @@ public interface UserService {
UserResponseDto updateRoles(Long userId, UserUpdateRoleDto updateRoleDto);

UserResponseDto updateProfile(Long userId, UserUpdateProfileDto updateProfileDto);

UserResponseDto updateEmail(String email, UserUpdateEmailDto updateDto);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.ua.accommodation.service.impl;

import com.ua.accommodation.dto.user.UserResponseDto;
import com.ua.accommodation.dto.user.UserUpdateEmailDto;
import com.ua.accommodation.dto.user.UserUpdateProfileDto;
import com.ua.accommodation.dto.user.UserUpdateRoleDto;
import com.ua.accommodation.mapper.UserMapper;
Expand All @@ -19,9 +20,7 @@ public class UserServiceImpl implements UserService {

@Override
public UserResponseDto getUser(String email) {
User user = userRepository.findByEmail(email).orElseThrow(
() -> new EntityNotFoundException("Can`t find user with email: " + email)
);
User user = getUserByEmail(email);
return userMapper.toResponseDto(user);
}

Expand All @@ -42,6 +41,20 @@ public UserResponseDto updateProfile(Long userId, UserUpdateProfileDto updatePro
return userMapper.toResponseDto(user);
}

@Override
public UserResponseDto updateEmail(String email, UserUpdateEmailDto updateDto) {
User user = getUserByEmail(email);
user.setEmail(updateDto.getNewEmail());
userRepository.save(user);
return userMapper.toResponseDto(user);
}

private User getUserByEmail(String email) {
return userRepository.findByEmail(email).orElseThrow(
() -> new EntityNotFoundException("Can`t find user with email: " + email)
);
}

private User getUserById(Long userId) {
return userRepository.findById(userId).orElseThrow(
() -> new EntityNotFoundException("Can`t find user with id: " + userId)
Expand Down

0 comments on commit 923b704

Please sign in to comment.