Skip to content

Commit

Permalink
Merge pull request #14 from STUDIO-EYE/feat/EPIC-80-post
Browse files Browse the repository at this point in the history
[Feat] 프로젝트 별 파일 생성 및 조회
  • Loading branch information
ibaesuyeon authored Apr 20, 2024
2 parents 1c069f9 + a590888 commit 65f1132
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.mju.management.domain.post.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.mju.management.domain.project.infrastructure.Project;
import jakarta.annotation.Nullable;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
Expand All @@ -24,8 +26,14 @@ public class PostFile {

private String s3key;

@Nullable
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "post_id")
@JsonIgnore
private Post post;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "project_id")
@JsonIgnore
private Project project;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.mju.management.domain.post.domain.PostFile;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;
Expand All @@ -10,4 +11,6 @@
public interface PostFileRepository extends JpaRepository<PostFile, Long> {

List<PostFile> findByPostId(Long postId);
@Query("SELECT pf FROM PostFile pf WHERE pf.project.projectId = :project_Id")
List<PostFile> findByProjectId(Long project_Id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public CommonResult createPost(CreatePostRequestServiceDto dto, List<MultipartFi
.filePath(s3Service.getUrl(s3key))
.s3key(s3key)
.post(post)
.project(project)
.build());
}
postFileRepository.saveAll(postFiles);
Expand Down Expand Up @@ -157,6 +158,7 @@ public CommonResult updatePost(UpdatePostRequestServiceDto dto, List<MultipartFi
.filePath(s3Service.getUrl(s3key))
.s3key(s3key)
.post(post)
.project(project)
.build());
}
postFileRepository.saveAll(postFiles);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Tag(name = "프로젝트 CRUD API", description = "프로젝트 CRUD API")
Expand Down Expand Up @@ -54,6 +56,13 @@ public CommonResult getProject(@PathVariable Long projectId) {
return responseService.getSingleResult(project);
}

@Operation(summary = "프로젝트 파일 생성")
@PostMapping("/{projectId}/files")
public CommonResult uploadProjectFiles(@PathVariable Long projectId,
@ModelAttribute List<MultipartFile> files) throws IOException {
projectService.createProjectFiles(projectId, files);
return responseService.getSuccessfulResult();
}
// 프로젝트 별 파일리스트 조회
@Operation(summary = "프로젝트 별 파일 리스트 조회")
@GetMapping("/{projectId}/files")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.mju.management.domain.project.infrastructure;

import com.mju.management.domain.post.domain.Post;
import com.mju.management.domain.post.domain.PostFile;
import com.mju.management.domain.project.dto.reqeust.CreateProjectRequestDto;
import com.mju.management.domain.schedule.infrastructure.Schedule;
import com.mju.management.domain.todo.infrastructure.ToDoEntity;
Expand Down Expand Up @@ -56,6 +57,9 @@ public Project(String name, LocalDate startDate, LocalDate finishDate, String de
@OneToMany(mappedBy = "project", cascade = ALL, orphanRemoval = true)
private List<Post> postList = new ArrayList<>();

@OneToMany(mappedBy = "project", cascade = ALL, orphanRemoval = true)
private List<PostFile> postFileList = new ArrayList<>();

@OneToMany(mappedBy = "project", cascade = ALL, orphanRemoval = true)
private List<Schedule> scheduleList = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import com.mju.management.domain.project.dto.reqeust.CreateProjectRequestDto;
import com.mju.management.domain.project.dto.response.GetProjectListResponseDto;
import com.mju.management.domain.project.dto.response.GetProjectResponseDto;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

public interface ProjectService {
Expand All @@ -18,4 +20,5 @@ public interface ProjectService {
void finishProject(Long projectIndex);

List<PostFile> getProjectFiles(Long projectId);
void createProjectFiles(Long projectId, List<MultipartFile> files) throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.mju.management.domain.post.domain.PostFile;
import com.mju.management.domain.post.infrastructure.PostFileRepository;
import com.mju.management.domain.post.infrastructure.PostRepository;
import com.mju.management.domain.project.dto.reqeust.CreateProjectRequestDto;
import com.mju.management.domain.project.dto.response.GetProjectListResponseDto;
import com.mju.management.domain.project.dto.response.GetProjectResponseDto;
import com.mju.management.domain.project.dto.response.GetProjectUserResponseDto;
Expand All @@ -13,17 +14,18 @@
import com.mju.management.global.config.jwtInterceptor.JwtContextHolder;
import com.mju.management.global.model.Exception.ExceptionList;
import com.mju.management.global.model.Exception.NonExistentException;
import com.mju.management.domain.project.dto.reqeust.CreateProjectRequestDto;
import com.mju.management.global.model.Exception.StartDateAfterEndDateException;
import com.mju.management.global.model.Exception.UnauthorizedAccessException;
import com.mju.management.global.service.S3Service;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

Expand All @@ -34,6 +36,7 @@ public class ProjectServiceImpl implements ProjectService{
private final UserServiceImpl userService;
private final PostRepository postRepository;
private final PostFileRepository postFileRepository;
private final S3Service s3Service;

@Override
@Transactional
Expand Down Expand Up @@ -141,14 +144,34 @@ public List<PostFile> getProjectFiles(Long projectId) {
throw new UnauthorizedAccessException(ExceptionList.UNAUTHORIZED_ACCESS);
// List<GetProjectUserResponseDto> getProjectUserResponseDtoList =
// getProjectUserResponseDtoList(project.getProjectUserList());
List<Post> posts = postRepository.findByProject(project);
List<PostFile> project_files = new ArrayList<>();
for(Post post : posts){
project_files.addAll(postFileRepository.findByPostId(post.getId()));
}
List<PostFile> project_files = postFileRepository.findByProjectId(projectId);
return project_files;
}

@Override
public void createProjectFiles(Long projectId, List<MultipartFile> files) throws IOException {
Project project = projectRepository.findByIdWithProjectUserList(projectId)
.orElseThrow(()->new NonExistentException(ExceptionList.NON_EXISTENT_PROJECT));
if(!project.isLeaderOrMember(JwtContextHolder.getUserId()))
throw new UnauthorizedAccessException(ExceptionList.UNAUTHORIZED_ACCESS);
// 파일 업로드
List<PostFile> projectFiles = new ArrayList<>();

if (files != null) {
for (MultipartFile file : files) {
String s3key = s3Service.uploadFile(file);

projectFiles.add(PostFile.builder()
.fileName(file.getOriginalFilename())
.filePath(s3Service.getUrl(s3key))
.s3key(s3key)
.project(project)
.build());
}
postFileRepository.saveAll(projectFiles);
}
}

public void validateProjectPeriod(CreateProjectRequestDto createProjectRequestDto){
LocalDate startDate = createProjectRequestDto.startDateAsLocalDateType();
LocalDate endDate = createProjectRequestDto.finishDateAsLocalDateType();
Expand Down

0 comments on commit 65f1132

Please sign in to comment.