-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from KUIT-Space/12-스페이스-생성
Feat 스페이스 생성 api
- Loading branch information
Showing
34 changed files
with
608 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/main/java/space/space_spring/argument_resolver/jwtUserSpace/JwtUserSpaceAuth.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} |
25 changes: 25 additions & 0 deletions
25
.../space_spring/argument_resolver/jwtUserSpace/JwtUserSpaceAuthHandlerArgumentResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/main/java/space/space_spring/controller/SpaceController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/main/java/space/space_spring/dto/jwt/JwtPayloadDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
18
src/main/java/space/space_spring/dto/jwt/JwtUserSpaceAuthDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/space/space_spring/dto/space/PostSpaceCreateRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; // 스페이스 프로필 이미지 (썸네일) | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/space/space_spring/dto/space/PostSpaceCreateResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.