Skip to content

Commit

Permalink
Merge pull request #144 from JNU-econovation/feat-cookie
Browse files Browse the repository at this point in the history
닉네임 자리수 8글자 제한
  • Loading branch information
pyg410 authored Jan 24, 2024
2 parents 893b612 + 65ca6a3 commit 163940f
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 3 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'io.jsonwebtoken:jjwt:0.9.1'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.h2database:h2'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.timcooki.jnuwiki.domain.member.DTO.request;

import com.timcooki.jnuwiki.domain.member.valid.Nickname;
import lombok.Builder;
@Builder

public record JoinReqDTO(
String email,
String nickName,
@Nickname String nickName,
String password
) {

@Builder
public JoinReqDTO{}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public ResponseEntity<ApiResult<AccessTokenResDTO>> refreshToken(@CookieValue(va
}

@PostMapping("/join")
public ResponseEntity<?> join(@RequestBody JoinReqDTO joinReqDTO) {
public ResponseEntity<?> join(@RequestBody @Valid JoinReqDTO joinReqDTO) {
memberWriteService.join(joinReqDTO);
return ResponseEntity.status(HttpStatus.CREATED).body(ApiUtils.success(null));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.timcooki.jnuwiki.domain.member.valid;

import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = NicknameValidator.class)
public @interface Nickname {

String message() default "닉네임은 최대 8글자까지 가능합니다.";

Class<?>[] groups() default {};

Class<? extends Payload>[] payload() default {};

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.timcooki.jnuwiki.domain.member.valid;

import lombok.extern.slf4j.Slf4j;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;


@Slf4j
public class NicknameValidator implements ConstraintValidator<Nickname, String> {

@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
log.info("벨리데이션 진행중");
if (value == null) {
return false;
}
return value.matches(".{1,8}");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {
Expand Down Expand Up @@ -42,10 +44,20 @@ public ResponseEntity<?> serverError(Exception500 e){
return new ResponseEntity<>(e.body(), e.status());
}

@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<?> validationError(
MethodArgumentNotValidException e) {
log.error("[VALIDATION ERROR] {}", e.getMessage());
ApiResult<?> apiResult = ApiUtils.error("[VALIDATION ERROR]", HttpStatus.BAD_REQUEST);
return ResponseEntity.badRequest().body(apiResult);
}
@ExceptionHandler(Exception.class)
public ResponseEntity<?> unknownServerError(Exception e){
ApiResult<?> apiResult = ApiUtils.error(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
log.error("[UNKNOWN SERVER ERROR] {}", e.getMessage());
return new ResponseEntity<>(apiResult, HttpStatus.INTERNAL_SERVER_ERROR);
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//package com.timcooki.jnuwiki.domain.member.validation;
//
//
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.timcooki.jnuwiki.domain.member.DTO.request.JoinReqDTO;
//import org.junit.jupiter.api.DisplayName;
//import org.junit.jupiter.api.Test;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
//import org.springframework.boot.test.context.SpringBootTest;
//import org.springframework.http.MediaType;
//import org.springframework.test.web.servlet.MockMvc;
//import org.springframework.test.web.servlet.ResultActions;
//import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
//import java.nio.charset.Charset;
//
//import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
//import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
//
//@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK)
//@AutoConfigureMockMvc
//public class NicknameValidatorTest {
//
// @Autowired
// protected MockMvc mockMvc;
//
// @Autowired
// protected ObjectMapper om;
//
// @DisplayName("닉네임 자리수 실패 테스트")
// @Test
// public void nickname_8자리_테스트() throws Exception{
// // given
// JoinReqDTO joinReqDTO = JoinReqDTO.builder()
// .email("[email protected]")
// .nickName("11asdfasdfsadfsa")
// .password("asdfasdf")
// .build();
//
//
// // when
// ResultActions resultActions = mockMvc.perform(
// MockMvcRequestBuilders
// .post("/members/join")
// .contentType(MediaType.APPLICATION_JSON)
// .content(om.writeValueAsString(joinReqDTO))
// );
//
// String responseBody = resultActions.andReturn().getResponse().getContentAsString(Charset.forName("UTF-8"));
// System.out.println("테스트 : " + responseBody);
//
// // then
// resultActions.andExpectAll(
// status().is4xxClientError(), jsonPath("$.error.status").value("400"));
//
// }
//}

0 comments on commit 163940f

Please sign in to comment.