-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BE] feat: 방 신청 구현(#32) #58
Changes from all commits
e33a4ea
bbefc3d
754acf8
163344e
9f0fb9a
0a3b25d
9a156a7
aa2bddc
d5d87d9
f837f9b
1909dd2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -1,12 +1,20 @@ | ||||||||
package corea; | ||||||||
|
||||||||
import corea.auth.resolver.LoginMemberArgumentResolver; | ||||||||
import lombok.RequiredArgsConstructor; | ||||||||
import org.springframework.context.annotation.Configuration; | ||||||||
import org.springframework.web.method.support.HandlerMethodArgumentResolver; | ||||||||
import org.springframework.web.servlet.config.annotation.CorsRegistry; | ||||||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||||||||
|
||||||||
import java.util.List; | ||||||||
|
||||||||
@Configuration | ||||||||
@RequiredArgsConstructor | ||||||||
public class WebConfig implements WebMvcConfigurer { | ||||||||
|
||||||||
private final LoginMemberArgumentResolver loginMemberArgumentResolver; | ||||||||
|
||||||||
@Override | ||||||||
public void addCorsMappings(CorsRegistry registry) { | ||||||||
registry.addMapping("/**") | ||||||||
|
@@ -15,4 +23,9 @@ public void addCorsMappings(CorsRegistry registry) { | |||||||
.allowCredentials(true) | ||||||||
.maxAge(3000); | ||||||||
} | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
|
||||||||
@Override | ||||||||
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) { | ||||||||
resolvers.add(loginMemberArgumentResolver); | ||||||||
} | ||||||||
} |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,14 @@ | ||||||||
package corea.auth; | ||||||||
|
||||||||
import jakarta.servlet.http.HttpServletRequest; | ||||||||
import org.springframework.stereotype.Component; | ||||||||
|
||||||||
@Component | ||||||||
public class RequestHandler { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
|
||||||||
private static final String AUTHORIZATION_HEADER = "Authorization"; | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
개행하세요 ^^ |
||||||||
|
||||||||
public String extract(HttpServletRequest request) { | ||||||||
return request.getHeader(AUTHORIZATION_HEADER); | ||||||||
} | ||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package corea.auth.annotation; | ||
|
||
import io.swagger.v3.oas.annotations.Hidden; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target(ElementType.PARAMETER) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Hidden() | ||
public @interface LoginUser { | ||
} |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,34 @@ | ||||||||
package corea.auth.domain; | ||||||||
|
||||||||
import corea.member.domain.Member; | ||||||||
import lombok.Getter; | ||||||||
import lombok.RequiredArgsConstructor; | ||||||||
|
||||||||
import java.util.Objects; | ||||||||
|
||||||||
@RequiredArgsConstructor | ||||||||
@Getter | ||||||||
public class AuthInfo { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
|
||||||||
private final Long id; | ||||||||
|
||||||||
private final String name; | ||||||||
|
||||||||
private final String email; | ||||||||
|
||||||||
public static AuthInfo from(Member member){ | ||||||||
return new AuthInfo(member.getId(), member.getUserName(), member.getEmail()); | ||||||||
} | ||||||||
|
||||||||
@Override | ||||||||
public boolean equals(final Object o) { | ||||||||
if (this == o) return true; | ||||||||
if (!(o instanceof AuthInfo authInfo)) return false; | ||||||||
return Objects.equals(id, authInfo.id); | ||||||||
} | ||||||||
|
||||||||
@Override | ||||||||
public int hashCode() { | ||||||||
return Objects.hashCode(id); | ||||||||
} | ||||||||
} |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,41 @@ | ||||||||
package corea.auth.resolver; | ||||||||
|
||||||||
import corea.auth.RequestHandler; | ||||||||
import corea.auth.annotation.LoginUser; | ||||||||
import corea.auth.domain.AuthInfo; | ||||||||
import corea.exception.CoreaException; | ||||||||
import corea.exception.ExceptionType; | ||||||||
import corea.member.domain.Member; | ||||||||
import corea.member.repository.MemberRepository; | ||||||||
import jakarta.servlet.http.HttpServletRequest; | ||||||||
import lombok.RequiredArgsConstructor; | ||||||||
import org.springframework.core.MethodParameter; | ||||||||
import org.springframework.stereotype.Component; | ||||||||
import org.springframework.web.bind.support.WebDataBinderFactory; | ||||||||
import org.springframework.web.context.request.NativeWebRequest; | ||||||||
import org.springframework.web.method.support.HandlerMethodArgumentResolver; | ||||||||
import org.springframework.web.method.support.ModelAndViewContainer; | ||||||||
|
||||||||
@Component | ||||||||
@RequiredArgsConstructor | ||||||||
public class LoginMemberArgumentResolver implements HandlerMethodArgumentResolver { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
|
||||||||
private final RequestHandler requestHandler; | ||||||||
private final MemberRepository memberRepository; | ||||||||
|
||||||||
@Override | ||||||||
public boolean supportsParameter(MethodParameter parameter) { | ||||||||
return parameter.hasParameterAnnotation(LoginUser.class); | ||||||||
} | ||||||||
|
||||||||
@Override | ||||||||
public AuthInfo resolveArgument(MethodParameter parameter, | ||||||||
ModelAndViewContainer mavContainer, | ||||||||
NativeWebRequest webRequest, | ||||||||
WebDataBinderFactory binderFactory) { | ||||||||
HttpServletRequest request = (HttpServletRequest) webRequest.getNativeRequest(); | ||||||||
Member member = memberRepository.findByEmail(requestHandler.extract(request)) | ||||||||
.orElseThrow(()-> new CoreaException(ExceptionType.AUTHORIZATION_ERROR)); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
return AuthInfo.from(member); | ||||||||
} | ||||||||
} |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -1,9 +1,18 @@ | ||||||||
package corea.exception; | ||||||||
|
||||||||
public class CoreaException extends Exception { | ||||||||
import org.springframework.http.HttpStatus; | ||||||||
|
||||||||
public class CoreaException extends RuntimeException { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
|
||||||||
private final ExceptionType exceptionType; | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
|
||||||||
public CoreaException(ExceptionType exceptionType) { | ||||||||
super(exceptionType.getMessage()); | ||||||||
this.exceptionType = exceptionType; | ||||||||
} | ||||||||
|
||||||||
public CoreaException(ExceptionType exceptionType, String message) { | ||||||||
super(message); | ||||||||
this.exceptionType = exceptionType; | ||||||||
} | ||||||||
|
||||||||
|
@@ -12,7 +21,12 @@ public CoreaException(ExceptionType exceptionType, Throwable cause) { | |||||||
this.exceptionType = exceptionType; | ||||||||
} | ||||||||
|
||||||||
public ExceptionType getExceptionType() { | ||||||||
return exceptionType; | ||||||||
public HttpStatus getHttpStatus() { | ||||||||
return exceptionType.getHttpStatus(); | ||||||||
} | ||||||||
|
||||||||
@Override | ||||||||
public String getMessage() { | ||||||||
return exceptionType.getMessage(); | ||||||||
} | ||||||||
} |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,25 @@ | ||||||||
package corea.exception; | ||||||||
|
||||||||
import lombok.extern.slf4j.Slf4j; | ||||||||
import org.springframework.http.ResponseEntity; | ||||||||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||||||||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||||||||
|
||||||||
@Slf4j | ||||||||
@ControllerAdvice | ||||||||
public class ExceptionResponseHandler { | ||||||||
|
||||||||
@ExceptionHandler(CoreaException.class) | ||||||||
public ResponseEntity<ErrorResponse> handleCoreaException(final CoreaException e) { | ||||||||
log.debug("Corea exception [statusCode = {}, errorMessage = {}, cause = {}]", e.getHttpStatus(), e.getMessage(), e.getCause()); | ||||||||
return ResponseEntity.status(e.getHttpStatus()) | ||||||||
.body(new ErrorResponse(e.getMessage())); | ||||||||
} | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
|
||||||||
@ExceptionHandler(Exception.class) | ||||||||
public ResponseEntity<ErrorResponse> handleException(final Exception e) { | ||||||||
log.debug("Server exception [errorMessage = {}, cause = {}]", e.getMessage(), e.getCause()); | ||||||||
return ResponseEntity.internalServerError() | ||||||||
.body(new ErrorResponse(e.getMessage())); | ||||||||
} | ||||||||
} |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,27 @@ | ||||||||
package corea.matching.controller; | ||||||||
|
||||||||
import corea.auth.annotation.LoginUser; | ||||||||
import corea.auth.domain.AuthInfo; | ||||||||
import corea.matching.dto.ParticipationRequest; | ||||||||
import corea.matching.service.ParticipationService; | ||||||||
import lombok.RequiredArgsConstructor; | ||||||||
import org.springframework.http.ResponseEntity; | ||||||||
import org.springframework.web.bind.annotation.PathVariable; | ||||||||
import org.springframework.web.bind.annotation.PostMapping; | ||||||||
import org.springframework.web.bind.annotation.RequestMapping; | ||||||||
import org.springframework.web.bind.annotation.RestController; | ||||||||
|
||||||||
@RestController | ||||||||
@RequestMapping("/participate") | ||||||||
@RequiredArgsConstructor | ||||||||
public class ParticipateController implements ParticipationControllerSpecification { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
|
||||||||
private final ParticipationService participationService; | ||||||||
|
||||||||
@PostMapping("/{id}") | ||||||||
public ResponseEntity<Void> participate(@PathVariable long id, @LoginUser AuthInfo authInfo) { | ||||||||
participationService.participate(new ParticipationRequest(id, authInfo.getId())); | ||||||||
return ResponseEntity.ok() | ||||||||
.build(); | ||||||||
} | ||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package corea.matching.controller; | ||
|
||
import corea.auth.domain.AuthInfo; | ||
import io.swagger.v3.oas.annotations.media.Content; | ||
import io.swagger.v3.oas.annotations.media.ExampleObject; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponses; | ||
import org.springframework.http.ResponseEntity; | ||
|
||
public interface ParticipationControllerSpecification { | ||
|
||
@ApiResponses( | ||
value = { | ||
@ApiResponse(responseCode = "404", content = @Content(mediaType = "application/json", examples = { | ||
@ExampleObject(name = "해당하는 방이 없는 경우", value = """ | ||
{ | ||
"message": "1에 해당하는 방 없습니다." | ||
} | ||
""") | ||
})), | ||
} | ||
) | ||
ResponseEntity<Void> participate(long id, AuthInfo authInfo); | ||
} |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -6,5 +6,8 @@ | |||||||
import java.util.List; | ||||||||
|
||||||||
public interface ParticipationRepository extends JpaRepository<Participation, Long> { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
|
||||||||
List<Participation> findAllByRoomId(long roomId); | ||||||||
|
||||||||
boolean existsByRoomIdAndMemberId(long roomId, long memberId); | ||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.