Skip to content

Commit

Permalink
✨ feat: add nickname change api (#162)
Browse files Browse the repository at this point in the history
* ✨ add nickname change api

* ✨ feat: refactoring Change Nickname API
  • Loading branch information
yunyoung1819 authored Jul 3, 2023
1 parent f010623 commit 63da3c2
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ public enum ErrorCode {

ALREADY_ITEM_REPORTED_ERROR(HttpStatus.CONFLICT, "C-0008", "User Already Item reported"),

INVALID_USER_EXCEPTION(HttpStatus.FORBIDDEN, "C-0009", "Modify or Delete Not Permitted");
INVALID_USER_EXCEPTION(HttpStatus.FORBIDDEN, "C-0009", "Modify or Delete Not Permitted"),

INVALID_INPUT_EXCEPTION(HttpStatus.BAD_REQUEST, "C-0015", "Nickname must be at least 1 character and not more than 10 characters");

private final HttpStatus status;
private final String code;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
import com.depromeet.user.User;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/users")
Expand All @@ -29,4 +29,14 @@ public ResponseEntity<UserResponseDto> getUserInfo(
return ResponseDto.ok(response);
}

@Operation(summary = "닉네임 변경하기")
@PatchMapping("/me/nickname")
public ResponseEntity<UserResponseDto> changeNickname(
@ReqUser User user,
@RequestParam("nickname") @NotNull(message = "Nickname is required")
@Size(min = 1, max = 10, message = "닉네임은 한글자 이상 10글자 이하입니다.") String nickname
) {
var response = userService.changeNickname(user, nickname);
return ResponseDto.ok(response);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.depromeet.domains.user.service;

import com.depromeet.common.error.dto.ErrorCode;
import com.depromeet.common.error.exception.common.BusinessException;
import com.depromeet.common.error.exception.common.NotFoundException;
import com.depromeet.domains.user.dto.response.UserResponseDto;
import com.depromeet.user.User;
import com.depromeet.domains.user.repository.UserRepository;
Expand All @@ -14,6 +17,7 @@
@RequiredArgsConstructor
@Service
public class UserService {

private final UserRepository userRepository;

@Transactional(readOnly = true)
Expand Down Expand Up @@ -64,4 +68,13 @@ private String generateDefaultNickname() {
return preName.get((int) (Math.random() * preName.size()))
+ " " + postName.get((int) (Math.random() * postName.size()));
}

@Transactional
public UserResponseDto changeNickname(User user, String nickname) {
var changedUser = user.changeNickname(nickname);

userRepository.save(changedUser);

return new UserResponseDto(user);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
.and().cors()
.and().csrf().disable()
.authorizeHttpRequests()
.requestMatchers("/users/me").authenticated()
.requestMatchers("/users/*").authenticated()
.requestMatchers(HttpMethod.POST, "/items").authenticated()
.requestMatchers(HttpMethod.PATCH, "/items/*").authenticated()
.requestMatchers(HttpMethod.DELETE, "/items/*").authenticated()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,9 @@ public User(String nickname, String idfv) {
this.nickname = nickname;
this.idfv = idfv;
}

public User changeNickname(String nickname) {
this.nickname = nickname;
return this;
}
}

0 comments on commit 63da3c2

Please sign in to comment.