Skip to content

Commit

Permalink
feat: service 코드 url 매핑
Browse files Browse the repository at this point in the history
  • Loading branch information
urinaner committed Jan 22, 2025
1 parent cdc9f13 commit 32276d1
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
import org.example.backend.board.exception.BoardException;
import org.example.backend.board.exception.BoardExceptionType;
import org.example.backend.board.repository.BoardRepository;
import org.example.backend.global.config.aws.S3Uploader;
import org.example.backend.global.config.aws.LocalFileUploader;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
Expand All @@ -27,9 +28,12 @@
@Transactional(readOnly = true)
public class BoardService {
private final BoardRepository boardRepository;
private final S3Uploader s3Uploader;
private final LocalFileUploader localFileUploader;
private static final String dirName = "image";

@Value("${server.url}")
private String serverUrl;

@Transactional
public Long saveBoard(BoardReqDto boardReqDto, List<MultipartFile> multipartFileList) {
fileUpload(boardReqDto, multipartFileList);
Expand Down Expand Up @@ -80,8 +84,8 @@ private void fileUpload(BoardReqDto boardReqDto, List<MultipartFile> multipartFi
if (multipartFile == null || multipartFile.isEmpty()) {
throw new BoardException(BoardExceptionType.REQUIRED_FILE);
}
String uploadImageUrl = s3Uploader.upload(multipartFile, dirName);
updateImageUrlList.add(uploadImageUrl);
String uploadImageUrl = localFileUploader.upload(multipartFile, dirName);
updateImageUrlList.add(serverUrl + uploadImageUrl);
}
boardReqDto.setFileList(updateImageUrlList);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
import static org.example.backend.news.exception.NewsExceptionType.NOT_FOUND_NEWS;

import lombok.RequiredArgsConstructor;
import org.example.backend.global.config.aws.S3Uploader;
import org.example.backend.global.config.aws.LocalFileUploader;
import org.example.backend.news.domain.dto.NewsReqDto;
import org.example.backend.news.domain.dto.NewsResDto;
import org.example.backend.news.domain.entity.News;
import org.example.backend.news.exception.NewsException;
import org.example.backend.news.repository.NewsRepository;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
Expand All @@ -20,15 +21,17 @@
@Transactional(readOnly = true)
public class NewsService {
private final NewsRepository newsRepository;
private final S3Uploader s3Uploader;
private final LocalFileUploader localFileUploader;
private static final String dirName = "news";

@Value("${server.url}")
private String serverUrl;
@Transactional
public Long saveNews(NewsReqDto newsReqDto, MultipartFile multipartFile) {

if (multipartFile != null && !multipartFile.isEmpty()) {
String uploadImageUrl = s3Uploader.upload(multipartFile, dirName);
newsReqDto.setImage(uploadImageUrl);
String uploadImageUrl = localFileUploader.upload(multipartFile, dirName);
newsReqDto.setImage(serverUrl + uploadImageUrl);
}
News news = News.of(newsReqDto);
News savedNews = newsRepository.save(news);
Expand All @@ -43,7 +46,7 @@ public NewsResDto getNews(Long newsId) {
@Transactional
public NewsResDto updateNews(Long newsId, NewsReqDto newsReqDto, MultipartFile multipartFile) {
if (multipartFile != null && !multipartFile.isEmpty()) {
String uploadImageUrl = s3Uploader.upload(multipartFile, dirName);
String uploadImageUrl = localFileUploader.upload(multipartFile, dirName);
newsReqDto.setImage(uploadImageUrl);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import static org.example.backend.professor.exception.ProfessorExceptionType.NOT_FOUND_PROFESSOR;

import lombok.RequiredArgsConstructor;
import org.example.backend.global.config.aws.S3Uploader;
import org.example.backend.global.config.aws.LocalFileUploader;
import org.example.backend.professor.domain.dto.ProfessorReqDto;
import org.example.backend.professor.domain.dto.ProfessorResDto;
import org.example.backend.professor.domain.entity.Professor;
Expand All @@ -12,6 +12,7 @@
import org.example.backend.professor.repository.ProfessorRepository;
import org.example.backend.thesis.domain.dto.ThesisResDto;
import org.example.backend.thesis.service.ThesisService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
Expand All @@ -24,16 +25,18 @@
public class ProfessorService {
private final ProfessorRepository professorRepository;
private final ThesisService thesisService;
private final S3Uploader s3Uploader;
private final LocalFileUploader localFileUploader;
private static final String dirName = "profile";

@Value("${server.url}")
private String serverUrl;
@Transactional
public Long saveProfessor(ProfessorReqDto professorReqDto, MultipartFile multipartFile) {
validateUserUniqueFields(professorReqDto);

if (multipartFile != null && !multipartFile.isEmpty()) {
String uploadImageUrl = s3Uploader.upload(multipartFile, dirName);
professorReqDto.setProfileImage(uploadImageUrl);
String uploadImageUrl = localFileUploader.upload(multipartFile, dirName);
professorReqDto.setProfileImage(uploadImageUrl + serverUrl);
}

Professor professor = Professor.of(professorReqDto);
Expand Down Expand Up @@ -65,7 +68,7 @@ public Page<ProfessorResDto> getAllProfessors(Pageable pageable) {
@Transactional
public ProfessorResDto updateProfessor(Long professorId, ProfessorReqDto professorReqDto, MultipartFile multipartFile) {
if (multipartFile != null && !multipartFile.isEmpty()) {
String uploadImageUrl = s3Uploader.upload(multipartFile, dirName);
String uploadImageUrl = localFileUploader.upload(multipartFile, dirName);
professorReqDto.setProfileImage(uploadImageUrl);
}
Professor professor = findProfessorById(professorId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
import static org.example.backend.thesis.exception.ThesisExceptionType.NOT_FOUND_THESIS;

import lombok.RequiredArgsConstructor;
import org.example.backend.global.config.aws.S3Uploader;
import org.example.backend.global.config.aws.LocalFileUploader;
import org.example.backend.professor.domain.entity.Professor;
import org.example.backend.professor.repository.ProfessorRepository;
import org.example.backend.thesis.domain.dto.ThesisReqDto;
import org.example.backend.thesis.domain.dto.ThesisResDto;
import org.example.backend.thesis.domain.entity.Thesis;
import org.example.backend.thesis.exception.ThesisException;
import org.example.backend.thesis.repository.ThesisRepository;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
Expand All @@ -23,16 +24,18 @@
public class ThesisService {
private final ThesisRepository thesisRepository;
private final ProfessorRepository professorRepository;
private final S3Uploader s3Uploader;
private final LocalFileUploader localFileUploader;
private static final String dirName = "image";

@Value("${server.url}")
private String serverUrl;
@Transactional
public Long saveThesis(ThesisReqDto thesisReqDto, MultipartFile multipartFile) {
Professor professor = findProfessorById(thesisReqDto.getProfessorId());

if (multipartFile != null && !multipartFile.isEmpty()) {
String uploadImageUrl = s3Uploader.upload(multipartFile, dirName);
thesisReqDto.setThesisImage(uploadImageUrl);
String uploadImageUrl = localFileUploader.upload(multipartFile, dirName);
thesisReqDto.setThesisImage(serverUrl + uploadImageUrl);
}

Thesis thesis = Thesis.of(thesisReqDto, professor);
Expand Down Expand Up @@ -60,7 +63,7 @@ public Page<ThesisResDto> getAllTheses(Pageable pageable) {
@Transactional
public ThesisResDto updateThesis(Long thesisId, ThesisReqDto thesisReqDto, MultipartFile multipartFile) {
if (multipartFile != null && !multipartFile.isEmpty()) {
String uploadImageUrl = s3Uploader.upload(multipartFile, dirName);
String uploadImageUrl = localFileUploader.upload(multipartFile, dirName);
thesisReqDto.setThesisImage(uploadImageUrl);
}

Expand Down

0 comments on commit 32276d1

Please sign in to comment.