feat : update mvc implement

This commit is contained in:
minseokkang
2022-09-23 13:14:37 +09:00
parent aa39f98495
commit 4b73bbeaf0
7 changed files with 70 additions and 19 deletions

View File

@@ -10,6 +10,7 @@ import com.io.realworld.domain.aggregate.user.service.UserServiceImpl;
import lombok.AllArgsConstructor;
import lombok.Value;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
@@ -23,7 +24,7 @@ public class UserController {
private final UserServiceImpl userService;
public UserController(UserServiceImpl userService){
public UserController(UserServiceImpl userService) {
this.userService = userService;
}
@@ -33,7 +34,7 @@ public class UserController {
}
@PutMapping
public UserResponse updateUser(@Valid @RequestBody UserUpdate userUpdate){
return userService.updateUser(userUpdate);
public UserResponse updateUser(@Valid @RequestBody UserUpdate userUpdate, @AuthenticationPrincipal UserAuth userAuth) {
return userService.updateUser(userUpdate, userAuth);
}
}

View File

@@ -4,18 +4,20 @@ import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeName;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
@Builder
@Getter
@Setter
@ToString
@JsonTypeName("user")
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.WRAPPER_OBJECT)
public class UserUpdate {
private Long id;
@Email
private String email;
private String username;

View File

@@ -1,5 +1,6 @@
package com.io.realworld.domain.aggregate.user.entity;
import com.io.realworld.domain.aggregate.user.dto.UserUpdate;
import lombok.Builder;
import lombok.Getter;
import org.springframework.security.core.GrantedAuthority;
@@ -69,4 +70,20 @@ public class User implements UserDetails {
public boolean isEnabled() {
return true;
}
//비즈니스
public void changeUsername(String username){
this.username = username;
}
public void changeEmail(String email){
this.email = email;
}
public void update(UserUpdate userUpdate) {
this.password = userUpdate.getPassword();
this.bio = userUpdate.getBio();
this.image = userUpdate.getImage();
}
}

View File

@@ -1,11 +1,19 @@
package com.io.realworld.domain.aggregate.user.repository;
import com.io.realworld.domain.aggregate.user.entity.User;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import java.util.List;
import java.util.Optional;
public interface UserRepository extends CrudRepository<User, Long> {
User save(User user);
public interface UserRepository extends JpaRepository<User, Long> {
User findByEmail(String email);
Iterable<User> findAll();
List<User> findAllByEmail(String email);
Optional<User> findById(Long id);
Optional<User> findByUsername(String username);
List<User> findAll();
}

View File

@@ -10,5 +10,5 @@ public interface UserService {
UserResponse getCurrentUser(UserAuth userAuth);
UserResponse updateUser(UserUpdate userUpdate);
UserResponse updateUser(UserUpdate userUpdate, UserAuth userAuth);
}

View File

@@ -8,12 +8,14 @@ import com.io.realworld.domain.aggregate.user.entity.User;
import com.io.realworld.domain.aggregate.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@RequiredArgsConstructor
@Log4j2
@Transactional(readOnly = true)
public class UserServiceImpl implements UserService {
@@ -55,20 +57,39 @@ public class UserServiceImpl implements UserService {
if (findUser == null) {
throw new CustomException(Error.EMAIL_NULL_OR_INVALID);
} else {
System.out.println(findUser);
return convertUser(findUser);
}
}
@Override
public UserResponse updateUser(UserUpdate userUpdate){
System.out.println(userUpdate.toString());
userRepository.save(User.builder()
.username(userUpdate.getUsername())
.image(userUpdate.getImage())
.bio(userUpdate.getBio())
.password(userUpdate.getPassword())
.email(userUpdate.getEmail()).build());
return convertUser();
@Transactional
public UserResponse updateUser(UserUpdate userUpdate, UserAuth userAuth){
User user = userRepository.findById(userAuth.getId()).orElseThrow(() -> new CustomException(Error.USER_NOT_FOUND));
if(userUpdate.getUsername() != null){
userRepository.findByUsername(userUpdate.getUsername())
.filter(found -> !found.getId().equals(userRepository.findById(user.getId())))
.ifPresent(found -> new CustomException(Error.DUPLICATE_USER));
System.out.println(user.getUsername() + userUpdate.getUsername());
user.changeUsername(userUpdate.getUsername());
System.out.println("!!" + user.getUsername() );
}
if(userUpdate.getEmail() != null){
userRepository.findAllByEmail(userUpdate.getEmail())
.stream().filter(found -> !found.getId().equals(userRepository.findById(user.getId())))
.findAny().ifPresent(found -> new CustomException(Error.DUPLICATE_USER));
user.changeEmail(userUpdate.getEmail());
}
userUpdate.setId(user.getId());
user.update(userUpdate);
log.info(" {} , {} {}",userUpdate.hashCode(), user.hashCode() , user.getId());
User test = userRepository.save(user);
log.info("test : {}",test.getBio());
return convertUser(test);
}

View File

@@ -7,7 +7,9 @@ import org.springframework.http.HttpStatus;
public enum Error {
DUPLICATE_USER("duplicate user", HttpStatus.CONFLICT),
SIGNUP_NULL_DATA("request body include null",HttpStatus.BAD_REQUEST),
EMAIL_NULL_OR_INVALID("email is blank or invalid check plz",HttpStatus.BAD_REQUEST);
EMAIL_NULL_OR_INVALID("email is blank or invalid check plz",HttpStatus.BAD_REQUEST),
USER_NOT_FOUND("user not found check your info",HttpStatus.NOT_FOUND);
private final String message;
private final HttpStatus status;