Skip to content

Commit

Permalink
Merge pull request #14 from KUIT-Space/12-스페이스-생성
Browse files Browse the repository at this point in the history
Feat 스페이스 생성 api
  • Loading branch information
seongjunnoh authored Jul 17, 2024
2 parents 984257d + d093bb6 commit 47fc76e
Show file tree
Hide file tree
Showing 34 changed files with 608 additions and 92 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package space.space_spring.argument_resolver;
package space.space_spring.argument_resolver.jwtLogin;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
Expand All @@ -7,5 +7,5 @@

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface JwtPreAuth {
public @interface JwtLoginAuth {
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package space.space_spring.argument_resolver;
package space.space_spring.argument_resolver.jwtLogin;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.core.MethodParameter;
Expand All @@ -9,13 +9,12 @@
import org.springframework.web.method.support.ModelAndViewContainer;

@Component
public class JwtAuthHandlerArgumentResolver implements HandlerMethodArgumentResolver {

public class JwtLoginAuthHandlerArgumentResolver implements HandlerMethodArgumentResolver {

@Override
public boolean supportsParameter(MethodParameter parameter) {
// 일단 parameter의 return value type 을 검사하지는 X
return parameter.hasParameterAnnotation(JwtPreAuth.class);
return parameter.hasParameterAnnotation(JwtLoginAuth.class);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package space.space_spring.argument_resolver.jwtUserSpace;

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)
public @interface JwtUserSpaceAuth {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package space.space_spring.argument_resolver.jwtUserSpace;

import jakarta.servlet.http.HttpServletRequest;
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
public class JwtUserSpaceAuthHandlerArgumentResolver implements HandlerMethodArgumentResolver {

@Override
public boolean supportsParameter(MethodParameter parameter) {
// 일단 parameter의 return value type 을 검사하지는 X
return parameter.hasParameterAnnotation(JwtUserSpaceAuth.class);
}

@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
HttpServletRequest request = (HttpServletRequest) webRequest.getNativeRequest();
return request.getAttribute("jwtPayloadDto"); // jwt를 복호화해서 얻은 userId get
}
}
25 changes: 17 additions & 8 deletions src/main/java/space/space_spring/config/WebConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,37 @@
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import space.space_spring.argument_resolver.JwtAuthHandlerArgumentResolver;
import space.space_spring.interceptor.JwtAuthInterceptor;
import space.space_spring.jwt.JwtProvider;
import space.space_spring.argument_resolver.jwtLogin.JwtLoginAuthHandlerArgumentResolver;
import space.space_spring.argument_resolver.jwtUserSpace.JwtUserSpaceAuthHandlerArgumentResolver;
import space.space_spring.interceptor.jwtLogin.JwtLoginAuthInterceptor;
import space.space_spring.interceptor.jwtUserSpace.JwtUserSpaceAuthInterceptor;

import java.util.List;

@Configuration
@RequiredArgsConstructor
public class WebConfig implements WebMvcConfigurer {

private final JwtAuthInterceptor jwtAuthInterceptor;
private final JwtAuthHandlerArgumentResolver authHandlerArgumentResolver;
private final JwtLoginAuthInterceptor jwtLoginAuthInterceptor;
private final JwtUserSpaceAuthInterceptor jwtUserSpaceAuthInterceptor;

private final JwtLoginAuthHandlerArgumentResolver jwtLoginAuthHandlerArgumentResolver;
private final JwtUserSpaceAuthHandlerArgumentResolver jwtUserSpaceAuthHandlerArgumentResolver;

@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(jwtAuthInterceptor)
registry.addInterceptor(jwtLoginAuthInterceptor)
.order(1)
.addPathPatterns("/space/**", "/test/**"); // interceptor 적용되야하는 url enum으로 만들어서 여기에 달면 될듯
.addPathPatterns("/space/**", "/test/**");

registry.addInterceptor(jwtUserSpaceAuthInterceptor)
.order(2)
.addPathPatterns("/test111/**"); // interceptor 적용되야하는 url enum으로 만들어서 여기에 달면 될듯
}

@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
argumentResolvers.add(authHandlerArgumentResolver);
argumentResolvers.add(jwtLoginAuthHandlerArgumentResolver);
argumentResolvers.add(jwtUserSpaceAuthHandlerArgumentResolver);
}
}
45 changes: 45 additions & 0 deletions src/main/java/space/space_spring/controller/SpaceController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package space.space_spring.controller;

import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import space.space_spring.argument_resolver.jwtLogin.JwtLoginAuth;
import space.space_spring.argument_resolver.jwtUserSpace.JwtUserSpaceAuth;
import space.space_spring.dto.jwt.JwtPayloadDto;
import space.space_spring.dto.space.PostSpaceCreateRequest;
import space.space_spring.dto.space.PostSpaceCreateResponse;

import space.space_spring.dto.space.SpaceCreateDto;
import space.space_spring.exception.SpaceException;
import space.space_spring.response.BaseResponse;
import space.space_spring.service.SpaceService;

import static space.space_spring.response.status.BaseExceptionResponseStatus.INVALID_SPACE_CREATE;
import static space.space_spring.util.BindingResultUtils.getErrorMessage;

@RestController
@RequiredArgsConstructor
@RequestMapping("/space")
public class SpaceController {

private final SpaceService spaceService;

@PostMapping("/create")
public BaseResponse<PostSpaceCreateResponse> createSpace(@JwtLoginAuth Long userId, @Validated @RequestBody PostSpaceCreateRequest postSpaceCreateRequest, BindingResult bindingResult, HttpServletResponse response) {
if (bindingResult.hasErrors()) {
throw new SpaceException(INVALID_SPACE_CREATE, getErrorMessage(bindingResult));
}

SpaceCreateDto spaceCreateDto = spaceService.createSpace(userId, postSpaceCreateRequest);
String jwtUserSpace = spaceCreateDto.getJwtUserSpace();
response.setHeader("Authorization", "Bearer " + jwtUserSpace);

return new BaseResponse<>(new PostSpaceCreateResponse(spaceCreateDto.getSpaceId()));
}

}
14 changes: 11 additions & 3 deletions src/main/java/space/space_spring/controller/TestController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import space.space_spring.argument_resolver.JwtPreAuth;
import space.space_spring.argument_resolver.jwtLogin.JwtLoginAuth;
import space.space_spring.argument_resolver.jwtUserSpace.JwtUserSpaceAuth;
import space.space_spring.dto.jwt.JwtPayloadDto;
import space.space_spring.response.BaseResponse;

@RestController
Expand All @@ -16,8 +18,14 @@ public String test(){
}

@GetMapping("/test/jwt")
public BaseResponse<String> jwtTest(@JwtPreAuth Long userId) {
public BaseResponse<String> jwtLoginTest(@JwtLoginAuth Long userId) {
log.info("userId = {}", userId);
return new BaseResponse<>("jwt test 성공");
return new BaseResponse<>("jwt login test 성공");
}

@GetMapping("/test111")
public BaseResponse<String> jwtUserSpaceTest(@JwtUserSpaceAuth JwtPayloadDto jwtPayloadDto) {
log.info("jwtPayloadDto = {}", jwtPayloadDto);
return new BaseResponse<>("jwt user space test 성공");
}
}
18 changes: 9 additions & 9 deletions src/main/java/space/space_spring/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import space.space_spring.dto.PostUserLoginRequest;
import space.space_spring.dto.PostUserLoginResponse;
import space.space_spring.dto.PostUserSignupRequest;
import space.space_spring.dto.PostUserSignupResponse;
import space.space_spring.dto.user.PostUserLoginRequest;
import space.space_spring.dto.user.PostUserLoginResponse;
import space.space_spring.dto.user.PostUserSignupRequest;
import space.space_spring.dto.user.PostUserSignupResponse;
import space.space_spring.exception.UserException;
import space.space_spring.response.BaseResponse;
import space.space_spring.service.UserService;

import static space.space_spring.response.status.BaseExceptionResponseStatus.INVALID_USER_VALUE;
import static space.space_spring.response.status.BaseExceptionResponseStatus.*;
import static space.space_spring.util.BindingResultUtils.getErrorMessage;

@Slf4j
Expand All @@ -34,7 +34,7 @@ public class UserController {
@PostMapping("/signup")
public BaseResponse<PostUserSignupResponse> signup(@Validated @RequestBody PostUserSignupRequest postUserSignupRequest, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
throw new UserException(INVALID_USER_VALUE, getErrorMessage(bindingResult));
throw new UserException(INVALID_USER_SIGNUP, getErrorMessage(bindingResult));
}
return new BaseResponse<>(userService.signup(postUserSignupRequest));
}
Expand All @@ -45,11 +45,11 @@ public BaseResponse<PostUserSignupResponse> signup(@Validated @RequestBody PostU
@PostMapping("/login")
public BaseResponse<PostUserLoginResponse> login(@Validated @RequestBody PostUserLoginRequest postUserLoginRequest, BindingResult bindingResult, HttpServletResponse response) {
if (bindingResult.hasErrors()) {
throw new UserException(INVALID_USER_VALUE, getErrorMessage(bindingResult));
throw new UserException(INVALID_USER_LOGIN, getErrorMessage(bindingResult));
}

String jwt = userService.login(postUserLoginRequest);
response.setHeader("Authorization", "Bearer " + jwt);
String jwtLogin = userService.login(postUserLoginRequest);
response.setHeader("Authorization", "Bearer " + jwtLogin);
return new BaseResponse<>(new PostUserLoginResponse("로그인 성공"));
}

Expand Down
34 changes: 34 additions & 0 deletions src/main/java/space/space_spring/dao/SpaceDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package space.space_spring.dao;

import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import org.springframework.stereotype.Repository;
import space.space_spring.entity.Space;
import space.space_spring.entity.User;
import space.space_spring.entity.UserSpace;
import space.space_spring.dto.space.PostSpaceCreateRequest;

import static space.space_spring.entity.enumStatus.UserSpaceAuth.MANAGER;

@Repository
public class SpaceDao {

@PersistenceContext
private EntityManager em;

public Space saveSpace(PostSpaceCreateRequest postSpaceCreateRequest) {
Space space = new Space();
space.saveSpace(postSpaceCreateRequest.getSpaceName(), postSpaceCreateRequest.getSpaceProfileImg());

em.persist(space);
return space;
}

public UserSpace createUserSpace(User manager, Space saveSpace) {
UserSpace userSpace = new UserSpace();
userSpace.createUserSpace(manager, saveSpace, MANAGER);

em.persist(userSpace);
return userSpace;
}
}
14 changes: 10 additions & 4 deletions src/main/java/space/space_spring/dao/UserDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@
import jakarta.persistence.PersistenceContext;
import jakarta.persistence.TypedQuery;
import org.springframework.stereotype.Repository;
import space.space_spring.domain.User;
import space.space_spring.dto.PostUserSignupRequest;
import space.space_spring.entity.User;
import space.space_spring.dto.user.PostUserSignupRequest;

@Repository
public class UserDao {

@PersistenceContext
private EntityManager em;

public Long saveUser(PostUserSignupRequest postUserSignupRequest) {
public User saveUser(PostUserSignupRequest postUserSignupRequest) {
User user = new User();
user.saveUser(postUserSignupRequest.getEmail(), postUserSignupRequest.getPassword(), postUserSignupRequest.getUserName());

em.persist(user);
return user.getUserId();
return user;
}

public User findUserByEmail(String email) {
Expand All @@ -43,4 +43,10 @@ public User getUserByEmail(String email) {
return null;
}
}

public User findUserByUserId(Long userId) {
TypedQuery<User> query = em.createQuery("SELECT u FROM User u WHERE u.userId = :userId", User.class);
query.setParameter("userId", userId);
return query.getSingleResult();
}
}
30 changes: 30 additions & 0 deletions src/main/java/space/space_spring/dto/jwt/JwtPayloadDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package space.space_spring.dto.jwt;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.util.ArrayList;
import java.util.List;

@Getter
@NoArgsConstructor
@ToString
public class JwtPayloadDto {

private Long userId;

private List<JwtUserSpaceAuthDto> userSpaceList = new ArrayList<>();

public void saveUserIdToJwt(Long userId) {
this.userId = userId;
}

public void saveUserSpaceList(List<JwtUserSpaceAuthDto> userSpaceList) {
this.userSpaceList = userSpaceList;
}

public void addJwtUserSpaceAuth(JwtUserSpaceAuthDto jwtUserSpaceAuthDto) {
this.userSpaceList.add(jwtUserSpaceAuthDto);
}
}
18 changes: 18 additions & 0 deletions src/main/java/space/space_spring/dto/jwt/JwtUserSpaceAuthDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package space.space_spring.dto.jwt;

import lombok.Getter;
import lombok.ToString;

import java.util.HashMap;
import java.util.Map;

@Getter
@ToString
public class JwtUserSpaceAuthDto {

private Map<Long, String> userSpaceAuthMap = new HashMap<>();

public void saveUserSpaceAuth(Long spaceId, String userSpaceAuth) {
userSpaceAuthMap.put(spaceId, userSpaceAuth);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package space.space_spring.dto.space;

import jakarta.validation.constraints.NotBlank;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.validator.constraints.Length;

@Getter
@Setter
@NoArgsConstructor
public class PostSpaceCreateRequest {

@Length(min = 1, max = 10, message = "이름은 10자이내의 문자열이어야 합니다.")
@NotBlank(message = "스페이스 이름은 공백일 수 없습니다.")
private String spaceName;

@NotBlank(message = "스페이스 프로필 이미지는 공백일 수 없습니다.")
private String spaceProfileImg; // 스페이스 프로필 이미지 (썸네일)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package space.space_spring.dto.space;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class PostSpaceCreateResponse {

private Long spaceId;
}
Loading

0 comments on commit 47fc76e

Please sign in to comment.