Skip to content

Commit

Permalink
Merge pull request #4 from PLADI-ALM/feat/PDS-32-search-materials
Browse files Browse the repository at this point in the history
[PDS-32] 아카이빙 자료 조회 API 기능 구현
  • Loading branch information
dangnak2 authored Oct 16, 2023
2 parents 9f64760 + 18d2d10 commit 5856230
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package com.example.PLADIALMArchiving.archiving.controller;

import com.example.PLADIALMArchiving.archiving.dto.request.RegisterProjectReq;
import com.example.PLADIALMArchiving.archiving.dto.request.SearchMaterialReq;
import com.example.PLADIALMArchiving.archiving.dto.request.UploadMaterialReq;
import com.example.PLADIALMArchiving.archiving.dto.response.SearchMaterialRes;
import com.example.PLADIALMArchiving.archiving.service.ArchivingService;
import com.example.PLADIALMArchiving.global.resolver.Account;
import com.example.PLADIALMArchiving.global.response.ResponseCustom;
import com.example.PLADIALMArchiving.user.entity.User;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

@Api(tags = "아카이빙 API")
Expand Down Expand Up @@ -40,10 +44,19 @@ public ResponseCustom<?> uploadMaterial(
archivingService.uploadMaterial(uploadMaterialReq, projectId, user);
return ResponseCustom.OK();
}

/**
* 자료목록을 조회 및 검색한다.
*/

@PostMapping("/projects/{projectId}")
public ResponseCustom<Page<SearchMaterialRes>> searchMaterial(
@PathVariable Long projectId,
@RequestBody SearchMaterialReq searchMaterialReq,
Pageable pageable
)
{
return ResponseCustom.OK(archivingService.searchMaterial(projectId, searchMaterialReq, pageable));
}
/**
* 자료를 삭제한다.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example.PLADIALMArchiving.archiving.dto.request;

import lombok.Getter;

@Getter
public class SearchMaterialReq {
private String cond;
private String category;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
public class UploadMaterialReq {
private String fileKey;
private String name;
private Long size;
private String extension;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.example.PLADIALMArchiving.archiving.dto.response;

import com.example.PLADIALMArchiving.archiving.entity.Material;
import com.example.PLADIALMArchiving.global.Constants;
import lombok.Builder;
import lombok.Data;

import java.time.format.DateTimeFormatter;

@Data
@Builder
public class SearchMaterialRes {
private Long materialId;
private String fileName;
private String createdAt;
private String publisher;
private Long size;

public static SearchMaterialRes toDto(Material material) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(Constants.DATE_PATTERN);
return SearchMaterialRes.builder()
.materialId(material.getMaterialId())
.fileName(material.getName())
.publisher(material.getUser().getName())
.createdAt(material.getCreatedAt().format(dateTimeFormatter))
.size(material.getSize())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.example.PLADIALMArchiving.archiving.entity;

import com.example.PLADIALMArchiving.global.exception.BaseException;
import com.example.PLADIALMArchiving.global.exception.BaseResponseCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.util.Arrays;

@Getter
@RequiredArgsConstructor
public enum Category {

GENERAL("전체"),
IMAGE("이미지"),
VIDEO("비디오"),
DOCS("문서");

private final String value;

public static Category getCategoryByValue(String value) {
return Arrays.stream(Category.values())
.filter(r -> r.getValue().equals(value))
.findAny().orElseThrow(() -> new BaseException(BaseResponseCode.CATEGORY_NOT_FOUND));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public class Material extends BaseEntity {

private String extension;

private Long size;

private String fileKey;

@ManyToOne(fetch = FetchType.LAZY)
Expand All @@ -40,9 +42,10 @@ public class Material extends BaseEntity {
private User user;

@Builder
public Material(String name, String extension, String fileKey, Project project, User user) {
public Material(String name, String extension, Long size, String fileKey, Project project, User user) {
this.name = name;
this.extension = extension;
this.size = size;
this.fileKey = fileKey;
this.project = project;
this.user = user;
Expand All @@ -52,6 +55,7 @@ public static Material toEntity(UploadMaterialReq uploadMaterialReq, Project pro
return Material.builder()
.name(uploadMaterialReq.getName())
.extension(uploadMaterialReq.getExtension())
.size(uploadMaterialReq.getSize())
.fileKey(uploadMaterialReq.getFileKey())
.project(project)
.user(user)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package com.example.PLADIALMArchiving.archiving.repository;

import com.example.PLADIALMArchiving.archiving.entity.Material;
import com.example.PLADIALMArchiving.archiving.entity.Project;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface MaterialRepository extends JpaRepository<Material, Long> {
Page<Material> findByProjectAndExtensionInAndNameContaining(Project project, List<String> extensions, String cond, Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
package com.example.PLADIALMArchiving.archiving.service;

import com.example.PLADIALMArchiving.archiving.dto.request.RegisterProjectReq;
import com.example.PLADIALMArchiving.archiving.dto.request.SearchMaterialReq;
import com.example.PLADIALMArchiving.archiving.dto.request.UploadMaterialReq;
import com.example.PLADIALMArchiving.archiving.dto.response.SearchMaterialRes;
import com.example.PLADIALMArchiving.archiving.entity.Category;
import com.example.PLADIALMArchiving.archiving.entity.Material;
import com.example.PLADIALMArchiving.archiving.entity.Project;
import com.example.PLADIALMArchiving.archiving.repository.MaterialRepository;
import com.example.PLADIALMArchiving.archiving.repository.ProjectRepository;
import com.example.PLADIALMArchiving.global.Constants;
import com.example.PLADIALMArchiving.global.exception.BaseException;
import com.example.PLADIALMArchiving.global.exception.BaseResponseCode;
import com.example.PLADIALMArchiving.user.entity.User;
import com.example.PLADIALMArchiving.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional(readOnly = true)
Expand All @@ -38,4 +44,28 @@ public void uploadMaterial(UploadMaterialReq uploadMaterialReq, Long projectId,
Project project = projectRepository.findById(projectId).orElseThrow(() -> new BaseException(BaseResponseCode.PROJECT_NOT_FOUND));
materialRepository.save(Material.toEntity(uploadMaterialReq, project, user));
}

public Page<SearchMaterialRes> searchMaterial(Long projectId, SearchMaterialReq searchMaterialReq, Pageable pageable) {
Project project = projectRepository.findById(projectId).orElseThrow(() -> new BaseException(BaseResponseCode.PROJECT_NOT_FOUND));
Category category = Category.getCategoryByValue(searchMaterialReq.getCategory());

Page<Material> filteredMaterials;

if (category != Category.GENERAL) {
List<String> extension = new ArrayList<>();
if (category == Category.IMAGE) {
extension = List.of(Constants.EXTENSION.IMAGE.split(" "));
} else if (category == Category.VIDEO) {
extension = List.of(Constants.EXTENSION.VIDEO.split(" "));
} else if (category == Category.DOCS) {
extension = List.of(Constants.EXTENSION.DOCS.split(" "));
}
filteredMaterials = materialRepository.findByProjectAndExtensionInAndNameContaining(project, extension, searchMaterialReq.getCond(), pageable);
} else {
filteredMaterials = materialRepository.findAll(pageable);
}

return filteredMaterials.map(SearchMaterialRes::toDto);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,10 @@ public static class JWT{
public static final String LOGOUT = "logout";
public static final String SIGNOUT = "signout";
}

public static class EXTENSION{
public static final String IMAGE = "jpeg jpg png gif";
public static final String VIDEO = "mp4 mov avi webm html5";
public static final String DOCS = "doc docx txt ppt pptx xls pdf ai psd hwp";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ public enum BaseResponseCode {

SUCCESS("S0001", HttpStatus.OK, "요청에 성공했습니다."),


BAD_REQUEST("G0001", HttpStatus.BAD_REQUEST, "잘못된 요청입니다."),
NO_ATUTHENTIFICATION("G0002", HttpStatus.FORBIDDEN, "접근 권한이 없습니다."),

// User
USER_NOT_FOUND("U0001", HttpStatus.NOT_FOUND, "사용자를 찾을 수 없습니다."),

Expand All @@ -27,22 +27,10 @@ public enum BaseResponseCode {
NOT_ACCESS_HEADER("T0006", HttpStatus.INTERNAL_SERVER_ERROR, "헤더에 접근할 수 없습니다."),
BLACKLIST_TOKEN("T0007", HttpStatus.FORBIDDEN, "로그아웃 혹은 회원 탈퇴된 토큰입니다."),

// Booking
DATE_OR_TIME_IS_NULL("B0001", HttpStatus.BAD_REQUEST, "날짜와 시간을 모두 입력해주세요."),
MEMO_SIZE_OVER("B0002", HttpStatus.BAD_REQUEST, "요청사항은 30자 이하로 작성해주세요."),
START_TIME_MUST_BE_IN_FRONT("B0003", HttpStatus.BAD_REQUEST, "시작시간보다 끝나는 시간이 더 앞에 있습니다."),
DATE_MUST_BE_THE_FUTURE("B0004", HttpStatus.BAD_REQUEST, "미래의 날짜를 선택해주세요."),
ALREADY_BOOKED_TIME("B0005", HttpStatus.CONFLICT, "이미 예약되어 있는 시간입니다."),
BOOKING_NOT_FOUND("B0006", HttpStatus.NOT_FOUND, "존재하지 않는 예약입니다."),
ALREADY_CANCELED_BOOKING("B0007", HttpStatus.CONFLICT, "이미 취소된 예약입니다."),
ALREADY_FINISHED_BOOKING("B0008", HttpStatus.CONFLICT, "이미 사용이 완료된 예약입니다."),

// Office
OFFICE_NOT_FOUND("O0001", HttpStatus.NOT_FOUND, "존재하지 않는 회의실입니다."),

// Archiving
ALREADY_REGISTERED_PROJECT("P0001", HttpStatus.BAD_REQUEST, "이미 등록된 프로젝트입니다."),
PROJECT_NOT_FOUND("P0002", HttpStatus.NOT_FOUND, "존재하지 않는 프로젝트입니다."),
CATEGORY_NOT_FOUND("P0003", HttpStatus.NOT_FOUND, "존재하지 않는 카테고리입니다."),
;

public final String code;
Expand Down

0 comments on commit 5856230

Please sign in to comment.