Skip to content

Commit

Permalink
스페이스 생성 API 기능 구현 완료 (#168)
Browse files Browse the repository at this point in the history
* feat : AddSpace 엔티티에 스페이스 생성 시 필요한 representativeName 컬럼 추가 (#165)

* feat : AddSpaceRepository에 저장 메소드 생성 (#165)

* modify : ExceptionHandler 메소드에 에러로그 추가 (#165)

* chore : FileBlock 메소드간 개행 추가 (#165)

* feat : SpaceController에 저장메소드 구현 및 토큰에서 memberId 가져오도록 수정 (#165)

* feat : SpaceSaveRequest 생성 (#165)

* feat : SpaceSaveResponse 생성 (#165)

* feat : SpaceService에 스페이스 저장 메소드 구현 (#165)

* feat : SpaceType에 String에서 Enum타입으로 변환하는 메소드 추가 (#165)

* modify : 더미데이터 수정 (#165)

- member테이블에 password를 인코딩된 값으로 수정
- add_space테이블에 representative_name 컬럼 추가
  • Loading branch information
miyounlee authored Oct 18, 2023
1 parent 54979f9 commit cb4c073
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,19 @@ protected ResponseEntity<ApiResponse.InvalidResponse> handleApplicationException

@ExceptionHandler(MissingServletRequestParameterException.class)
public ResponseEntity<ApiResponse.InvalidResponse> handleMissingServletRequestParameterException(MissingServletRequestParameterException e) {
log.error(e.getMessage());
return ApiResponse.invalidResponse(ApiStatus.OBJECT_EMPTY, e.getMessage());
}

@ExceptionHandler(MissingPathVariableException.class)
public ResponseEntity<ApiResponse.InvalidResponse> handleServletRequestBindingException (MissingPathVariableException e) {
log.error(e.getMessage());
return ApiResponse.invalidResponse(ApiStatus.OBJECT_EMPTY, e.getMessage());
}

@ExceptionHandler(MethodArgumentTypeMismatchException.class)
public ResponseEntity<ApiResponse.InvalidResponse> handleMethodArgumentTypeMismatchException (MethodArgumentTypeMismatchException e) {
log.error(e.getMessage());
return ApiResponse.invalidResponse(ApiStatus.OBJECT_EMPTY, e.getMessage());
}
}
2 changes: 2 additions & 0 deletions src/main/java/com/javajober/fileBlock/domain/FileBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public FileBlock(final String fileTitle, final String fileDescription, final Str
this.fileDescription = fileDescription;
this.fileName = fileName;
}

public void update(final String fileTitle, final String fileDescription, final String fileName, final String file) {
this.fileTitle = fileTitle;
this.fileDescription = fileDescription;
Expand All @@ -72,6 +73,7 @@ public void fileUpdate(final String fileTitle, final String fileDescription, fin
this.fileDescription = fileDescription;
this.fileName = fileName;
}

public void setDeletedAt() {
this.deletedAt = LocalDateTime.now();
}
Expand Down
33 changes: 27 additions & 6 deletions src/main/java/com/javajober/space/controller/SpaceController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,50 @@
import com.javajober.core.util.ApiResponse;
import com.javajober.exception.ApiStatus;
import com.javajober.exception.ApplicationException;
import com.javajober.space.service.SpaceService;
import com.javajober.security.JwtTokenizer;
import com.javajober.space.domain.SpaceType;
import com.javajober.space.dto.request.SpaceSaveRequest;
import com.javajober.space.dto.response.SpaceResponse;
import com.javajober.space.dto.response.SpaceSaveResponse;
import com.javajober.space.service.SpaceService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/api/employee")
@RequestMapping("/api")
@RestController
public class SpaceController {

private final SpaceService spaceService;
private final JwtTokenizer jwtTokenizer;

public SpaceController(final SpaceService spaceService) {
public SpaceController(final SpaceService spaceService, final JwtTokenizer jwtTokenizer) {
this.spaceService = spaceService;
this.jwtTokenizer = jwtTokenizer;
}

@PostMapping("/space")
public ResponseEntity<ApiResponse.Response<SpaceSaveResponse>> save(
@RequestBody final SpaceSaveRequest request, @RequestHeader("Authorization") String token){

if (!request.getSpaceType().equals(SpaceType.ORGANIZATION.getEngTitle())){
throw new ApplicationException(ApiStatus.INVALID_DATA, "스페이스 생성은 organization만 가능");
}
Long memberId = jwtTokenizer.getUserIdFromToken(token);
SpaceSaveResponse data = spaceService.save(request, memberId);

return ApiResponse.response(ApiStatus.OK, "스페이스 저장을 성공했습니다.", data);
}

@GetMapping("/{memberId}/{addSpaceId}")
@GetMapping("/employee/{addSpaceId}")
public ResponseEntity<ApiResponse.Response<SpaceResponse>> find(
@PathVariable final Long memberId, @PathVariable final Long addSpaceId, @RequestParam final String spaceType) {
@PathVariable final Long addSpaceId, @RequestParam final String spaceType, @RequestHeader("Authorization") String token) {

if (spaceType.isEmpty()) {
throw new ApplicationException(ApiStatus.OBJECT_EMPTY, "spaceType 값이 요청되지 않았습니다.");
}
SpaceResponse data = spaceService.find(memberId, addSpaceId, spaceType);

Long memberId = jwtTokenizer.getUserIdFromToken(token);
SpaceResponse data = spaceService.find(addSpaceId, spaceType, memberId);

return ApiResponse.response(ApiStatus.OK, "스페이스 조회를 성공했습니다.", data);
}
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/com/javajober/space/domain/AddSpace.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.javajober.space.domain;

import com.javajober.member.domain.Member;
import lombok.Builder;
import lombok.Getter;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
Expand All @@ -26,6 +27,9 @@ public class AddSpace {
@Column(name = "space_type", nullable = false)
private SpaceType spaceType;

@Column(name = "representative_name", nullable = false)
private String representativeName;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="member_id", nullable = false)
private Member member;
Expand All @@ -45,9 +49,11 @@ protected AddSpace() {

}

public AddSpace (final String spaceTitle, final SpaceType spaceType, Member member) {
@Builder
public AddSpace (final String spaceTitle, final SpaceType spaceType, final Member member, final String representativeName) {
this.spaceTitle = spaceTitle;
this.spaceType = spaceType;
this.member = member;
this.representativeName= representativeName;
}
}
11 changes: 11 additions & 0 deletions src/main/java/com/javajober/space/domain/SpaceType.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

import com.fasterxml.jackson.annotation.JsonValue;

import com.javajober.exception.ApiStatus;
import com.javajober.exception.ApplicationException;
import lombok.Getter;

import java.util.Arrays;

@Getter
public enum SpaceType {

Expand All @@ -22,4 +26,11 @@ public enum SpaceType {
public String getEngTitle() {
return engTitle;
}

public static SpaceType findSpaceTypeByString(final String type) {
return Arrays.stream(values())
.filter(spaceType -> spaceType.getEngTitle().equals(type))
.findAny()
.orElseThrow(() -> new ApplicationException(ApiStatus.INVALID_DATA, "존재하지 않는 space type입니다."));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.javajober.space.dto.request;

import com.javajober.member.domain.Member;
import com.javajober.space.domain.AddSpace;
import com.javajober.space.domain.SpaceType;
import lombok.Getter;

@Getter
public class SpaceSaveRequest {

private Long memberId;
private String spaceTitle;
private String representativeName;
private String spaceType;

public SpaceSaveRequest() {
}

public static AddSpace toEntity(SpaceSaveRequest request, Member member) {
return AddSpace.builder()
.spaceTitle(request.getSpaceTitle())
.spaceType(SpaceType.findSpaceTypeByString(request.getSpaceType()))
.representativeName(request.getRepresentativeName())
.member(member)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.javajober.space.dto.response;

import lombok.Getter;

@Getter
public class SpaceSaveResponse {

private Long spaceId;

public SpaceSaveResponse() {
}

public SpaceSaveResponse(Long spaceId) {
this.spaceId = spaceId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@

public interface AddSpaceRepository extends Repository<AddSpace, Long> {

@Query("SELECT a FROM AddSpace a JOIN FETCH a.member WHERE a.member.id = :memberId AND a.spaceType IN :spaceTypes")
List<AddSpace> findSpacesByMemberIdAndSpaceTypes(final Long memberId, final List<SpaceType> spaceTypes);
AddSpace save(final AddSpace addSpace);

Optional<AddSpace> findById(final Long id);

Optional<AddSpace> findBySpaceTypeAndId(final SpaceType spaceType, final Long memberId);

@Query("SELECT a FROM AddSpace a JOIN FETCH a.member WHERE a.member.id = :memberId AND a.spaceType IN :spaceTypes")
List<AddSpace> findSpacesByMemberIdAndSpaceTypes(final Long memberId, final List<SpaceType> spaceTypes);

@Query("SELECT s.id FROM AddSpace s WHERE s.spaceType = :spaceType AND s.member.id = :memberId")
List<Long> findAddSpaceIdBySpaceTypeAndMemberId(@Param("spaceType") final SpaceType spaceType, @Param("memberId") final Long memberId);

Expand Down
25 changes: 19 additions & 6 deletions src/main/java/com/javajober/space/service/SpaceService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@

import com.javajober.exception.ApiStatus;
import com.javajober.exception.ApplicationException;
import com.javajober.member.domain.Member;
import com.javajober.member.repository.MemberRepository;
import com.javajober.memberGroup.repository.MemberGroupRepository;
import com.javajober.space.domain.AddSpace;
import com.javajober.space.domain.SpaceType;
import com.javajober.space.dto.request.SpaceSaveRequest;
import com.javajober.space.dto.response.SpaceSaveResponse;
import com.javajober.space.repository.AddSpaceRepository;
import com.javajober.space.dto.response.SpaceResponse;
import com.javajober.space.dto.response.MemberGroupResponse;
import com.javajober.spaceWall.domain.FlagType;
import com.javajober.spaceWall.repository.SpaceWallRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
Expand All @@ -19,21 +23,31 @@
@Service
public class SpaceService {

private final MemberRepository memberRepository;
private final MemberGroupRepository memberGroupRepository;
private final AddSpaceRepository addSpaceRepository;
private final SpaceWallRepository spaceWallRepository;

public SpaceService(final MemberGroupRepository memberGroupRepository, final AddSpaceRepository addSpaceRepository, final SpaceWallRepository spaceWallRepository) {
public SpaceService(final MemberRepository memberRepository, final MemberGroupRepository memberGroupRepository,
final AddSpaceRepository addSpaceRepository, final SpaceWallRepository spaceWallRepository) {

this.memberRepository = memberRepository;
this.memberGroupRepository = memberGroupRepository;
this.addSpaceRepository = addSpaceRepository;
this.spaceWallRepository = spaceWallRepository;
}

@Transactional
public SpaceResponse find(final Long memberId, final Long addSpaceId, final String spaceTypeString) {
public SpaceSaveResponse save(SpaceSaveRequest request, Long memberId) {
Member member = memberRepository.findMember(memberId);
AddSpace space = SpaceSaveRequest.toEntity(request, member);
Long spaceId = addSpaceRepository.save(space).getId();

return new SpaceSaveResponse(spaceId);
}

public SpaceResponse find(Long addSpaceId, String spaceTypeString, Long memberId) {
SpaceType spaceType = SpaceType.valueOf(spaceTypeString.toUpperCase());
List<Long> addSpaceIds = addSpaceRepository.findAddSpaceIds(spaceType, memberId); // 단체스페이스면 여러개 나옴
List<Long> addSpaceIds = addSpaceRepository.findAddSpaceIds(spaceType, memberId);
existsAddSpace(addSpaceId, addSpaceIds);

List<MemberGroupResponse> memberGroupResponses = memberGroupRepository.findMemberGroup(addSpaceId);
Expand All @@ -45,7 +59,6 @@ public SpaceResponse find(final Long memberId, final Long addSpaceId, final Stri
}

private static void existsAddSpace(Long addSpaceId, List<Long> addSpaceIds) {

boolean containsId = addSpaceIds.stream().anyMatch(id -> Objects.equals(id, addSpaceId));
if(!containsId) {
throw new ApplicationException(ApiStatus.NOT_FOUND, "존재하지 않는 스페이스 입니다.");
Expand Down
83 changes: 42 additions & 41 deletions src/main/resources/db/temporary.sql
Original file line number Diff line number Diff line change
@@ -1,47 +1,48 @@
INSERT INTO member(member_name, member_email, password, phone_number, member_ship, created_at, updated_at, deleted_at)
VALUES ('변백현', '[email protected]', '1234', '010-1175-1312', 'FREE', NOW(), NOW(), NULL),
('이수민', '[email protected]', '1234', '010-1135-2486', 'FREE', NOW(), NOW(), NULL),
('김영희', '[email protected]', '1234', '010-1158-3741', 'FREE', NOW(), NOW(), NULL),
('정민준', '[email protected]', '1234', '010-1179-4285', 'STANDARD', NOW(), NOW(), NULL),
('박지영', '[email protected]', '1234', '010-1136-5279', 'FREE', NOW(), NOW(), NULL),
('최우진', '[email protected]', '1234', '010-1157-6384', 'FREE', NOW(), NOW(), NULL),
('한지훈', '[email protected]', '1234', '010-1178-7942', 'STANDARD', NOW(), NOW(), NULL),
('서예진', '[email protected]', '1234', '010-1139-8425', 'FREE', NOW(), NOW(), NULL),
('윤다미', '[email protected]', '1234', '010-6012-4904', 'FREE', NOW(), NOW(), NULL),
('이하빈', '[email protected]', '1234', '010-6123-6015', 'STANDARD', NOW(), NOW(), NULL),
('박서인', '[email protected]', '1234', '010-6234-7126', 'FREE', NOW(), NOW(), NULL),
('최지안', '[email protected]', '1234', '010-6345-8237', 'FREE', NOW(), NOW(), NULL),
('김하늘', '[email protected]', '1234', '010-6456-9348', 'FREE', NOW(), NOW(), NULL),
('윤하나', '[email protected]', '1234', '010-6567-0459', 'FREE', NOW(), NOW(), NULL),
('이서하', '[email protected]', '1234', '010-6678-1560', 'FREE', NOW(), NOW(), NULL),
('박다은', '[email protected]', '1234', '010-6789-2671', 'FREE', NOW(), NOW(), NULL),
('최수하', '[email protected]', '1234', '010-6890-3782', 'FREE', NOW(), NOW(), NULL),
('김지하', '[email protected]', '1234', '010-6901-4893', 'FREE', NOW(), NOW(), NULL);
VALUES ('변백현', '[email protected]', '$2a$10$RdYW3w/9DZ/hU.Nklx1RyOy5RC7yNqQk0JqIZXtw6HorY3Trkch86', '010-1175-1312', 'FREE', NOW(), NOW(), NULL),
('이수민', '[email protected]', '$2a$10$RdYW3w/9DZ/hU.Nklx1RyOy5RC7yNqQk0JqIZXtw6HorY3Trkch86', '010-1135-2486', 'FREE', NOW(), NOW(), NULL),
('김영희', '[email protected]', '$2a$10$RdYW3w/9DZ/hU.Nklx1RyOy5RC7yNqQk0JqIZXtw6HorY3Trkch86', '010-1158-3741', 'FREE', NOW(), NOW(), NULL),
('정민준', '[email protected]', '$2a$10$RdYW3w/9DZ/hU.Nklx1RyOy5RC7yNqQk0JqIZXtw6HorY3Trkch86', '010-1179-4285', 'STANDARD', NOW(), NOW(), NULL),
('박지영', '[email protected]', '$2a$10$RdYW3w/9DZ/hU.Nklx1RyOy5RC7yNqQk0JqIZXtw6HorY3Trkch86', '010-1136-5279', 'FREE', NOW(), NOW(), NULL),
('최우진', '[email protected]', '$2a$10$RdYW3w/9DZ/hU.Nklx1RyOy5RC7yNqQk0JqIZXtw6HorY3Trkch86', '010-1157-6384', 'FREE', NOW(), NOW(), NULL),
('한지훈', '[email protected]', '$2a$10$RdYW3w/9DZ/hU.Nklx1RyOy5RC7yNqQk0JqIZXtw6HorY3Trkch86', '010-1178-7942', 'STANDARD', NOW(), NOW(), NULL),
('서예진', '[email protected]', '$2a$10$RdYW3w/9DZ/hU.Nklx1RyOy5RC7yNqQk0JqIZXtw6HorY3Trkch86', '010-1139-8425', 'FREE', NOW(), NOW(), NULL),
('윤다미', '[email protected]', '$2a$10$RdYW3w/9DZ/hU.Nklx1RyOy5RC7yNqQk0JqIZXtw6HorY3Trkch86', '010-6012-4904', 'FREE', NOW(), NOW(), NULL),
('이하빈', '[email protected]', '$2a$10$RdYW3w/9DZ/hU.Nklx1RyOy5RC7yNqQk0JqIZXtw6HorY3Trkch86', '010-6123-6015', 'STANDARD', NOW(), NOW(), NULL),
('박서인', '[email protected]', '$2a$10$RdYW3w/9DZ/hU.Nklx1RyOy5RC7yNqQk0JqIZXtw6HorY3Trkch86', '010-6234-7126', 'FREE', NOW(), NOW(), NULL),
('최지안', '[email protected]', '$2a$10$RdYW3w/9DZ/hU.Nklx1RyOy5RC7yNqQk0JqIZXtw6HorY3Trkch86', '010-6345-8237', 'FREE', NOW(), NOW(), NULL),
('김하늘', '[email protected]', '$2a$10$RdYW3w/9DZ/hU.Nklx1RyOy5RC7yNqQk0JqIZXtw6HorY3Trkch86', '010-6456-9348', 'FREE', NOW(), NOW(), NULL),
('윤하나', '[email protected]', '$2a$10$RdYW3w/9DZ/hU.Nklx1RyOy5RC7yNqQk0JqIZXtw6HorY3Trkch86', '010-6567-0459', 'FREE', NOW(), NOW(), NULL),
('이서하', '[email protected]', '$2a$10$RdYW3w/9DZ/hU.Nklx1RyOy5RC7yNqQk0JqIZXtw6HorY3Trkch86', '010-6678-1560', 'FREE', NOW(), NOW(), NULL),
('박다은', '[email protected]', '$2a$10$RdYW3w/9DZ/hU.Nklx1RyOy5RC7yNqQk0JqIZXtw6HorY3Trkch86', '010-6789-2671', 'FREE', NOW(), NOW(), NULL),
('최수하', '[email protected]', '$2a$10$RdYW3w/9DZ/hU.Nklx1RyOy5RC7yNqQk0JqIZXtw6HorY3Trkch86', '010-6890-3782', 'FREE', NOW(), NOW(), NULL),
('김지하', '[email protected]', '$2a$10$RdYW3w/9DZ/hU.Nklx1RyOy5RC7yNqQk0JqIZXtw6HorY3Trkch86', '010-6901-4893', 'FREE', NOW(), NOW(), NULL);
// password : abcd1234@

INSERT INTO add_space (space_title, space_type, representative_name, member_id, created_at, updated_at, deleted_at)
VALUES ('변백현', 'PERSONAL','변백현', 1, NOW(), NOW(), NULL),
('임시회사명', 'ORGANIZATION','변백현', 1, NOW(), NOW(), NULL),
('이수민', 'PERSONAL','이수민', 2, NOW(), NOW(), NULL),
('임시회사명', 'ORGANIZATION','이수민', 2, NOW(), NOW(), NULL),
('김영희', 'PERSONAL','김영희', 3, NOW(), NOW(), NULL),
('임시회사명', 'ORGANIZATION','김영희', 3, NOW(), NOW(), NULL),
('정민준', 'PERSONAL', '정민준', 4, NOW(), NOW(), NULL),
('임시회사명', 'ORGANIZATION','정민준', 4, NOW(), NOW(), NULL),
('박지영', 'PERSONAL', '박지영', 5, NOW(), NOW(), NULL),
('임시회사명', 'ORGANIZATION','박지영', 5, NOW(), NOW(), NULL),
('최우진', 'PERSONAL', '최우진', 6, NOW(), NOW(), NULL),
('임시회사명', 'ORGANIZATION', '최우진', 6, NOW(), NOW(), NULL),
('한지훈', 'PERSONAL', '한지훈', 7, NOW(), NOW(), NULL),
('임시회사명', 'ORGANIZATION', '한지훈', 7, NOW(), NOW(), NULL),
('서예진', 'PERSONAL', '서예진', 8, NOW(), NOW(), NULL),
('임시회사명', 'ORGANIZATION', '서예진', 8, NOW(), NOW(), NULL),
('윤다미', 'PERSONAL', '윤다미', 9, NOW(), NOW(), NULL),
('임시회사명', 'ORGANIZATION', '윤다미', 9, NOW(), NOW(), NULL),
('이하빈', 'PERSONAL','이하빈', 10, NOW(), NOW(), NULL),
('임시회사명', 'ORGANIZATION','이하빈', 10, NOW(), NOW(), NULL),
('패스트캠퍼스 X Jober', 'ORGANIZATION','변백현', 1, NOW(), NOW(), NULL),
('근로계약서', 'ORGANIZATION','이수민', 2, NOW(), NOW(), NULL);

INSERT INTO add_space (space_title, space_type, member_id, created_at, updated_at, deleted_at)
VALUES ('변백현', 'PERSONAL', 1, NOW(), NOW(), NULL),
('임시회사명', 'ORGANIZATION', 1, NOW(), NOW(), NULL),
('이수민', 'PERSONAL', 2, NOW(), NOW(), NULL),
('임시회사명', 'ORGANIZATION', 2, NOW(), NOW(), NULL),
('김영희', 'PERSONAL', 3, NOW(), NOW(), NULL),
('임시회사명', 'ORGANIZATION', 3, NOW(), NOW(), NULL),
('정민준', 'PERSONAL', 4, NOW(), NOW(), NULL),
('임시회사명', 'ORGANIZATION', 4, NOW(), NOW(), NULL),
('박지영', 'PERSONAL', 5, NOW(), NOW(), NULL),
('임시회사명', 'ORGANIZATION', 5, NOW(), NOW(), NULL),
('최우진', 'PERSONAL', 6, NOW(), NOW(), NULL),
('임시회사명', 'ORGANIZATION', 6, NOW(), NOW(), NULL),
('한지훈', 'PERSONAL', 7, NOW(), NOW(), NULL),
('임시회사명', 'ORGANIZATION', 7, NOW(), NOW(), NULL),
('서예진', 'PERSONAL', 8, NOW(), NOW(), NULL),
('임시회사명', 'ORGANIZATION', 8, NOW(), NOW(), NULL),
('윤다미', 'PERSONAL', 9, NOW(), NOW(), NULL),
('임시회사명', 'ORGANIZATION', 9, NOW(), NOW(), NULL),
('이하빈', 'PERSONAL', 10, NOW(), NOW(), NULL),
('임시회사명', 'ORGANIZATION', 10, NOW(), NOW(), NULL),
('패스트캠퍼스 X Jober', 'ORGANIZATION', 1, NOW(), NOW(), NULL),
('근로계약서', 'ORGANIZATION', 1, NOW(), NOW(), NULL);

INSERT INTO member_group (member_hashtag_type, account_type, add_space_id, member_id, created_at, updated_at,
deleted_at)
Expand Down

0 comments on commit cb4c073

Please sign in to comment.