Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

사용자가 저장한 이미지를 일정 별로 Read, Delete 하는 API 구현 #134

Merged
merged 11 commits into from
Aug 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.isp.backend.domain.scheduleImage.controller;

import com.isp.backend.domain.scheduleImage.dto.SaveScheduleImageRequest;
import com.isp.backend.domain.scheduleImage.dto.SaveScheduleImageResponse;
import com.isp.backend.domain.scheduleImage.service.SaveImageService;
import com.isp.backend.domain.scheduleImage.dto.request.SaveScheduleImageRequest;
import com.isp.backend.domain.scheduleImage.dto.response.ReadScheduleImageResponse;
import com.isp.backend.domain.scheduleImage.dto.response.SaveScheduleImageResponse;
import com.isp.backend.domain.scheduleImage.service.DeleteScheduleImageService;
import com.isp.backend.domain.scheduleImage.service.ReadScheduleImageService;
import com.isp.backend.domain.scheduleImage.service.SaveScheduleImageService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

Expand All @@ -13,12 +17,27 @@
@RequestMapping("/scheduleImages")
public class ScheduleImageController {

private final SaveImageService saveImageService;
private final SaveScheduleImageService saveScheduleImageService;
private final ReadScheduleImageService readScheduleImageService;
private final DeleteScheduleImageService deleteScheduleImageService;

@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public SaveScheduleImageResponse create(@RequestPart(value = "image", required = false) MultipartFile image,
@RequestPart(value = "saveScheduleImageRequest") SaveScheduleImageRequest request) {
return saveImageService.save(request, image);
@RequestPart(value = "saveScheduleImageRequest") SaveScheduleImageRequest request) {
return saveScheduleImageService.save(request, image);
}

@GetMapping("/{scheduleId}")
@ResponseStatus(HttpStatus.OK)
public ReadScheduleImageResponse read(@PathVariable(name = "scheduleId") Long scheduleId) {
return readScheduleImageService.read(scheduleId);
}

@DeleteMapping("/{scheduleImageId}")
@ResponseStatus(HttpStatus.OK)
public ResponseEntity<Void> delete(@PathVariable(name = "scheduleImageId") Long scheduleImageId) {
return deleteScheduleImageService.delete(scheduleImageId);
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.isp.backend.domain.scheduleImage.dto.request;

import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
public class SaveScheduleImageRequest {
private Long scheduleId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.isp.backend.domain.scheduleImage.dto.response;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Map;

@Getter
@AllArgsConstructor
public class ReadScheduleImageResponse {
private Long scheduleId;
private Map<String, String> pathAndSaveDate;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.isp.backend.domain.scheduleImage.dto;
package com.isp.backend.domain.scheduleImage.dto.response;

import com.isp.backend.domain.scheduleImage.entity.ScheduleImage;
import lombok.Getter;
Expand All @@ -8,10 +8,12 @@ public class SaveScheduleImageResponse {
private Long id;
private Long scheduleId;
private String path;
private String saveDate;

public SaveScheduleImageResponse(ScheduleImage scheduleImage) {
this.id = scheduleImage.getId();
this.scheduleId = scheduleImage.getSchedule().getId();
this.path = scheduleImage.getPath();
this.saveDate = scheduleImage.getSaveDate();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Entity
@Getter
Expand All @@ -20,8 +24,16 @@ public class ScheduleImage {
private Schedule schedule;
private String path;

@CreatedDate
private String saveDate;

public ScheduleImage(Schedule schedule, String path) {
this.schedule = schedule;
this.path = path;
}

@PrePersist
public void onPrePersist(){
this.saveDate = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
import com.isp.backend.domain.scheduleImage.entity.ScheduleImage;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface ScheduleImageRepository extends JpaRepository<ScheduleImage, Long> {
List<ScheduleImage> findByScheduleId(Long scheduleId);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.isp.backend.domain.scheduleImage.repository;

import com.isp.backend.domain.scheduleImage.dto.SaveScheduleImageRequest;
import com.isp.backend.domain.scheduleImage.dto.request.SaveScheduleImageRequest;
import io.awspring.cloud.s3.S3Resource;
import io.awspring.cloud.s3.S3Template;
import lombok.AccessLevel;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.isp.backend.domain.scheduleImage.service;

import org.springframework.http.ResponseEntity;

public interface DeleteScheduleImageService {
ResponseEntity<Void> delete(Long scheduleImageId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.isp.backend.domain.scheduleImage.service;

import com.isp.backend.domain.scheduleImage.dto.response.ReadScheduleImageResponse;

public interface ReadScheduleImageService {
ReadScheduleImageResponse read(Long scheduleId);
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.isp.backend.domain.scheduleImage.service;

import com.isp.backend.domain.scheduleImage.dto.request.SaveScheduleImageRequest;
import com.isp.backend.domain.scheduleImage.dto.response.SaveScheduleImageResponse;
import org.springframework.web.multipart.MultipartFile;

public interface SaveScheduleImageService {

SaveScheduleImageResponse save(SaveScheduleImageRequest request, MultipartFile image);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,25 @@

import com.isp.backend.domain.schedule.entity.Schedule;
import com.isp.backend.domain.schedule.repository.ScheduleRepository;
import com.isp.backend.domain.scheduleImage.dto.SaveScheduleImageRequest;
import com.isp.backend.domain.scheduleImage.dto.SaveScheduleImageResponse;
import com.isp.backend.domain.scheduleImage.dto.request.SaveScheduleImageRequest;
import com.isp.backend.domain.scheduleImage.dto.response.ReadScheduleImageResponse;
import com.isp.backend.domain.scheduleImage.dto.response.SaveScheduleImageResponse;
import com.isp.backend.domain.scheduleImage.entity.ScheduleImage;
import com.isp.backend.domain.scheduleImage.repository.ScheduleImageRepository;
import com.isp.backend.domain.scheduleImage.repository.ScheduleImageS3Repository;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class ScheduleImageService implements SaveImageService {
public class ScheduleImageService implements SaveScheduleImageService, ReadScheduleImageService, DeleteScheduleImageService {

private final ScheduleImageS3Repository scheduleImageS3Repository;
private final ScheduleRepository scheduleRepository;
Expand All @@ -28,4 +35,26 @@ public SaveScheduleImageResponse save(SaveScheduleImageRequest request, Multipar
scheduleImageRepository.save(scheduleImage);
return new SaveScheduleImageResponse(scheduleImage);
}

@Override
public ReadScheduleImageResponse read(Long scheduleId) {
List<ScheduleImage> scheduleImages = scheduleImageRepository.findByScheduleId(scheduleId);
Map<String, String> pathSaveDateMap = new HashMap<>();

scheduleImages.forEach(scheduleImage ->
pathSaveDateMap.put(scheduleImage.getSaveDate(), scheduleImage.getPath())
);

return new ReadScheduleImageResponse(scheduleId, pathSaveDateMap);
}

@Override
public ResponseEntity<Void> delete(Long scheduleImageId) {
if (!scheduleImageRepository.existsById(scheduleImageId)) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
}

scheduleImageRepository.deleteById(scheduleImageId);
return ResponseEntity.noContent().build();
}
}
Loading