Feat: 워크스페이스 초기 세팅 추가 - S11P21S002-25

This commit is contained in:
kimtaesoo7 2024-08-27 17:45:17 +09:00
parent d928af889e
commit fa580341bd
6 changed files with 187 additions and 7 deletions

View File

@ -0,0 +1,71 @@
package com.worlabel.domain.workspace.controller;
import com.worlabel.domain.workspace.entity.Workspace;
import com.worlabel.domain.workspace.entity.dto.WorkspaceRequest;
import com.worlabel.domain.workspace.service.WorkspaceService;
import com.worlabel.global.config.swagger.SwaggerApiError;
import com.worlabel.global.config.swagger.SwaggerApiSuccess;
import com.worlabel.global.exception.ErrorCode;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Tag(name = "워크스페이스 관련 API")
@RestController
@RequestMapping("/api/workspaces")
@RequiredArgsConstructor
public class WorkspaceController {
private final WorkspaceService workspaceService;
@Operation(summary = "워크스페이스 생성", description = "새로운 워크스페이스를 생성합니다.")
@SwaggerApiSuccess(description = "워크스페이스를 성공적으로 생성합니다.")
@SwaggerApiError({ErrorCode.EMPTY_REQUEST_PARAMETER, ErrorCode.SERVER_ERROR})
@PostMapping
public ResponseEntity<Workspace> createWorkspace(final Integer memberId, @RequestBody final WorkspaceRequest workspaceRequest) {
Workspace createdWorkspace = workspaceService.createWorkspace(memberId, workspaceRequest);
return ResponseEntity.ok(createdWorkspace);
}
@Operation(summary = "특정 워크스페이스 조회", description = "특정 워크스페이스를 조회합니다.")
@SwaggerApiSuccess(description = "특정 워크스페이스를 성공적으로 조회합니다.")
@SwaggerApiError({ErrorCode.BAD_REQUEST, ErrorCode.NOT_AUTHOR, ErrorCode.SERVER_ERROR})
@GetMapping("/{workspace_id}")
public ResponseEntity<Workspace> getWorkspace(@PathVariable("workspace_id") Integer workspaceId) {
Workspace workspace = workspaceService.getWorkspaceById(workspaceId);
return ResponseEntity.ok(workspace);
}
@Operation(summary = "전체 워크스페이스 조회", description = "모든 워크스페이스를 조회합니다.")
@SwaggerApiSuccess(description = "전체 워크스페이스를 성공적으로 조회합니다.")
@SwaggerApiError({ErrorCode.SERVER_ERROR})
@GetMapping
public ResponseEntity<List<Workspace>> getAllWorkspaces() {
List<Workspace> workspaces = workspaceService.getAllWorkspaces();
return ResponseEntity.ok(workspaces);
}
@Operation(summary = "워크스페이스 수정", description = "특정 워크스페이스를 수정합니다.")
@SwaggerApiSuccess(description = "특정 워크스페이스를 성공적으로 수정합니다.")
@SwaggerApiError({ErrorCode.BAD_REQUEST, ErrorCode.NOT_AUTHOR, ErrorCode.SERVER_ERROR})
@PutMapping("/{workspace_id}")
public ResponseEntity<Workspace> updateWorkspace(
@PathVariable("workspace_id") Integer workspaceId,
@RequestBody Workspace updatedWorkspace) {
Workspace workspace = workspaceService.updateWorkspace(workspaceId, updatedWorkspace);
return ResponseEntity.ok(workspace);
}
@Operation(summary = "워크스페이스 삭제", description = "특정 워크스페이스를 삭제합니다.")
@SwaggerApiSuccess(description = "특정 워크스페이스를 성공적으로 삭제합니다.")
@SwaggerApiError({ErrorCode.BAD_REQUEST, ErrorCode.NOT_AUTHOR, ErrorCode.SERVER_ERROR})
@DeleteMapping("/{workspace_id}")
public ResponseEntity<Void> deleteWorkspace(@PathVariable("workspace_id") Integer workspaceId) {
workspaceService.deleteWorkspace(workspaceId);
return ResponseEntity.noContent().build();
}
}

View File

@ -1,11 +1,15 @@
package com.worlabel.domain.workspace.entity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.worlabel.domain.member.entity.Member;
import com.worlabel.domain.project.entity.Project;
import com.worlabel.global.common.BaseEntity;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
import java.util.ArrayList;
import java.util.List;
@ -24,22 +28,33 @@ public class Workspace extends BaseEntity {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
/**
* 만든 사용자
*/
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
private Member member;
/**
* 워크 스페이스 제목
*/
@Column(name = "title",nullable = false, length = 50)
@Column(name = "title", nullable = false, length = 50)
private String title;
/**
* 워크 스페이스 설명
*/
@Column(name = "description", nullable = false,length = 255)
@Column(name = "description", nullable = false, length = 255)
private String description;
/**
* 워크 스페이스에 속한 프로젝트
*/
@OneToMany(mappedBy = "workspace", fetch = FetchType.LAZY,cascade = CascadeType.ALL, orphanRemoval = true)
private List<Project> projectList = new ArrayList<>();
public Workspace(final Member member, final String title, final String description) {
this.member = member;
this.title = title;
this.description = description;
}
public static Workspace of(final Member member, final String title, final String description) {
return new Workspace(member, title, description);
}
}

View File

@ -0,0 +1,23 @@
package com.worlabel.domain.workspace.entity.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotEmpty;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
@Schema(name = "워크스페이스 request dto", description = "워크스페이스 작성시 필요한 정보")
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@AllArgsConstructor
@Getter
public class WorkspaceRequest {
@Schema(description = "제목", example = "title1")
@NotEmpty(message = "제목을 입력하세요.")
private String title;
@Schema(description = "내용", example = "content1")
@NotEmpty(message = "내용을 입력하세요.")
private String content;
}

View File

@ -0,0 +1,9 @@
package com.worlabel.domain.workspace.repository;
import com.worlabel.domain.workspace.entity.Workspace;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface WorkspaceRepository extends JpaRepository<Workspace, Integer> {
}

View File

@ -0,0 +1,59 @@
package com.worlabel.domain.workspace.service;
import com.worlabel.domain.workspace.entity.Workspace;
import com.worlabel.domain.workspace.entity.dto.WorkspaceRequest;
import com.worlabel.domain.workspace.repository.WorkspaceRepository;
import com.worlabel.global.exception.CustomException;
import com.worlabel.global.exception.ErrorCode;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
@RequiredArgsConstructor
public class WorkspaceService {
private final WorkspaceRepository workspaceRepository;
/**
* 새로운 워크스페이스 생성
*/
public Workspace createWorkspace(Integer memberId, WorkspaceRequest workspace) {
// return workspaceRepository.save();
}
/**
* 특정 워크스페이스 조회
*/
public Workspace getWorkspaceById(Integer id) {
return workspaceRepository.findById(id)
.orElseThrow(() -> new CustomException(ErrorCode.WORKSPACE_NOT_FOUND, "Workspace not found"));
}
/**
* 전체 워크스페이스 조회
*/
public List<Workspace> getAllWorkspaces() {
return workspaceRepository.findAll();
}
/**
* 워크스페이스 수정
*/
public Workspace updateWorkspace(Integer id, Workspace updatedWorkspace) {
Workspace workspace = getWorkspaceById(id);
// workspace.setTitle(updatedWorkspace.getTitle());
// workspace.setDescription(updatedWorkspace.getDescription());
return workspaceRepository.save(workspace);
}
/**
* 워크스페이스 삭제
*/
public void deleteWorkspace(Integer id) {
Workspace workspace = getWorkspaceById(id);
workspaceRepository.delete(workspace);
}
}

View File

@ -23,6 +23,9 @@ public enum ErrorCode {
INVALID_REFRESH_TOKEN(HttpStatus.BAD_REQUEST, 2004, "유효하지 않은 리프레시 토큰입니다."),
UNAUTHORIZED(HttpStatus.UNAUTHORIZED, 2005, "인증에 실패하였습니다."),
// Workspace - 3000
NOT_AUTHOR(HttpStatus.FORBIDDEN, 3001, "작성자가 아닙니다. 이 작업을 수행할 권한이 없습니다."),
WORKSPACE_NOT_FOUND(HttpStatus.BAD_REQUEST, 3002, "해당 워크스페이스는 존재하지 않습니다."),
;
private final HttpStatus status;