Skip to content

Commit

Permalink
수정 및 삭제 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
HeeSung98 committed Nov 18, 2023
1 parent a4fb766 commit 9d8fb34
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 13 deletions.
19 changes: 9 additions & 10 deletions user/src/main/java/com/weather/user/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

Expand Down Expand Up @@ -49,17 +46,19 @@ public ResponseEntity<UserDTO> profile(@RequestBody UserDTO userDTO) {
}

@PatchMapping("/user/profile/modify")
public ResponseEntity modify(@RequestBody UserDTO userDTO) {
public ResponseEntity<UserDTO> modify(@RequestBody UserDTO userDTO) {
log.info("userDTO: " + userDTO);

return new ResponseEntity(HttpStatus.OK);
UserDTO result = userService.modify(userDTO);

return new ResponseEntity<>(result, HttpStatus.OK);
}

@DeleteMapping("/user/profile/remove")
public ResponseEntity remove(@RequestBody UserDTO userDTO) {
log.info("userDTO: " + userDTO);
@DeleteMapping("/user/profile/remove/{email}")
public ResponseEntity remove(@PathVariable String email) {
log.info("email: " + email);

userService.remove(userDTO.getEmail());
userService.remove(email);

return new ResponseEntity(HttpStatus.OK);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
import java.util.Optional;

public interface UserRepository extends JpaRepository<User, Long> {
@Query("select m from User m where m.fromSocial = :social and m.email = :email")
@Query("select u from User u where u.fromSocial = :social and u.email = :email")
Optional<User> findByEmail(@Param("email") String email, @Param("social") boolean social);


@Query("select m from User m where m.email = :email")
@Query("select u from User u where u.email = :email")
Optional<User> findByEmail(@Param("email") String email);
}
2 changes: 2 additions & 0 deletions user/src/main/java/com/weather/user/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public interface UserService {

UserDTO profile(String email);

UserDTO modify(UserDTO userDTO);

void remove(String email);

default User dtoToEntity(UserDTO userDTO) {
Expand Down
26 changes: 25 additions & 1 deletion user/src/main/java/com/weather/user/service/UserServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public void signup(UserDTO userDTO) {
}

userDTO.setPassword(passwordEncoder.encode(userDTO.getPassword()));
userDTO.setStatus(true);
User user = dtoToEntity(userDTO);
userRepository.save(user);
}
Expand Down Expand Up @@ -63,14 +64,36 @@ public UserDTO profile(String email) {
}
User user = optionalUser.get();

if(user.isStatus()) {
if(!user.isStatus()) {
throw new Error("탈퇴 대기중인 유저입니다.");
}

UserDTO result = entityToDTO(user);
return result;
}

@Override
public UserDTO modify(UserDTO userDTO) {
Optional<User> optionalUser = userRepository.findByEmail(userDTO.getEmail());
log.info("optionalUser: " + optionalUser);

if(optionalUser.isEmpty()) {
throw new Error("존재하지 않는 유저입니다.");
}
User user = optionalUser.get();

if(user.isStatus()) {
user.changeNickname(userDTO.getNickname());
user.changeImage(userDTO.getImage());
userRepository.save(user);

UserDTO result = entityToDTO(user);
return result;
} else {
throw new Error("잘못 된 접근입니다.");
}
}

@Override
public void remove(String email) {
Optional<User> optionalUser = userRepository.findByEmail(email);
Expand All @@ -83,6 +106,7 @@ public void remove(String email) {

if(user.isStatus()) {
user.changeStatus();
userRepository.save(user);
} else {
throw new Error("잘못 된 접근입니다.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ void insertUser() {
.name("USER" + i)
.nickname("테스터")
.fromSocial(false)
.status(true)
.build();

userRepository.save(user);
Expand Down

0 comments on commit 9d8fb34

Please sign in to comment.