Skip to content

Commit

Permalink
Merge pull request #31 from Favor-Gift-Reminder/juwon/#29
Browse files Browse the repository at this point in the history
[Feat] 기념일 API 구현 및 AuthenticationEntryPoint 오류 수정 확인
  • Loading branch information
eunki96 authored May 25, 2023
2 parents 46e557a + 101927d commit 31469b6
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 19 deletions.
1 change: 1 addition & 0 deletions favor/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ dependencies {
implementation 'io.springfox:springfox-swagger-ui:2.9.2'

implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'com.googlecode.json-simple:json-simple:1.1.1'
testImplementation 'org.springframework.boot:spring-boot-starter-test'

compileOnly 'org.projectlombok:lombok'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,31 @@
package com.favor.favor.exception;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.json.simple.JSONObject;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.OutputStream;

import static com.favor.favor.exception.ExceptionCode.UNAUTHORIZED_USER;

@Component("customAuthenticationEntryPoint")
public class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint {

private static CustomException errorResponse =
new CustomException(new RuntimeException(), UNAUTHORIZED_USER);

@Override
public void commence(HttpServletRequest request,
HttpServletResponse response,
AuthenticationException ex) throws IOException, ServletException {
AuthenticationException ex) throws IOException {

// [commence] 인증 실패로 response.sendError 발생
ObjectMapper objectMapper = new ObjectMapper();

response.setStatus(401);
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
try (OutputStream os = response.getOutputStream()) {
objectMapper.writeValue(os, errorResponse);
os.flush();
}
response.setStatus(401);

JSONObject responseJson = new JSONObject();
responseJson.put("ResponseCode", "AUTHENTICATION_FAILED");
responseJson.put("ResponseMessage", "인증에 실패하였습니다.");

response.getWriter().print(responseJson);
}
}
34 changes: 32 additions & 2 deletions favor/src/main/java/com/favor/favor/user/UserController.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.favor.favor.user;


import com.favor.favor.anniversary.AnniversaryResponseDto;
import com.favor.favor.common.DefaultResponseDto;
import com.favor.favor.common.enums.Category;
import com.favor.favor.common.enums.Emotion;
Expand Down Expand Up @@ -353,7 +354,7 @@ public ResponseEntity<DefaultResponseDto<Object>> readGiftList(
@ApiOperation("회원의 친구 전체 조회")
@ApiResponses(value={
@ApiResponse(code = 200,
message = "FRIENDSS_FOUND",
message = "FRIENDS_FOUND",
response = UserResponseDto.class),
@ApiResponse(code = 401,
message = "UNAUTHORIZED_USER"),
Expand All @@ -373,12 +374,41 @@ public ResponseEntity<DefaultResponseDto<Object>> readFriendList(@PathVariable L

return ResponseEntity.status(200)
.body(DefaultResponseDto.builder()
.responseCode("FRIENDSS_FOUND")
.responseCode("FRIENDS_FOUND")
.responseMessage("회원의 친구 전체 조회 완료")
.data(friends)
.build());
}

@ApiOperation("회원의 기념일 전체 조회")
@ApiResponses(value={
@ApiResponse(code = 200,
message = "ANNIVERSARY_FOUND",
response = UserResponseDto.class),
@ApiResponse(code = 401,
message = "UNAUTHORIZED_USER"),
@ApiResponse(code = 404,
message = "USER_NOT_FOUND"),
@ApiResponse(code = 500,
message = "SERVER_ERROR")
})
@ResponseStatus(HttpStatus.OK)
@Transactional
@GetMapping("/anniversary-list/{userNo}")
public ResponseEntity<DefaultResponseDto<Object>> readAnniversaryList(@PathVariable Long userNo){

userService.isExistingUserNo(userNo);

List<AnniversaryResponseDto> friends = userService.readAnniversaryList(userNo);

return ResponseEntity.status(200)
.body(DefaultResponseDto.builder()
.responseCode("ANNIVERSARY_FOUND")
.responseMessage("회원의 기념일 전체 조회 완료")
.data(friends)
.build());
}


@ApiOperation("전체 회원 조회")
@ApiResponses(value={
Expand Down
12 changes: 12 additions & 0 deletions favor/src/main/java/com/favor/favor/user/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,18 @@ public List<ReminderResponseDto> readReminderList(Long userNo){
return r_List;
}

@Transactional
public List<AnniversaryResponseDto> readAnniversaryList(Long userNo){
User user = findUserByUserNo(userNo);

List<AnniversaryResponseDto> r_List = new ArrayList<>();
for(Anniversary r : user.getAnniversaryList()){
AnniversaryResponseDto dto = new AnniversaryResponseDto(r);
r_List.add(dto);
}
return r_List;
}

@Transactional
public List<GiftResponseDto> readGiftList(Long userNo){
User user = findUserByUserNo(userNo);
Expand Down

0 comments on commit 31469b6

Please sign in to comment.