Skip to content

Commit

Permalink
feature: add image #5
Browse files Browse the repository at this point in the history
  • Loading branch information
ujiiin committed Jul 4, 2024
1 parent 63db64e commit 2417b62
Show file tree
Hide file tree
Showing 20 changed files with 174 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.hackathon.TimeLapse.apiPayload.exception;

public class ArticleNotFoundException extends RuntimeException {
public ArticleNotFoundException(String message){
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.hackathon.TimeLapse.domain;
package com.hackathon.TimeLapse.article;

import com.hackathon.TimeLapse.domain.common.BaseEntity;
import com.hackathon.TimeLapse.common.BaseEntity;
import com.hackathon.TimeLapse.image.Image;
import com.hackathon.TimeLapse.member.Member;
import jakarta.persistence.*;
import lombok.*;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.util.ArrayList;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
package com.hackathon.TimeLapse.article;

import com.hackathon.TimeLapse.apiPayload.ApiResponse;
import com.hackathon.TimeLapse.domain.Article;
import com.hackathon.TimeLapse.s3.S3Service;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@Validated
@RequestMapping("/api")
@RequestMapping("/api/article")
public class ArticleController {
private final ArticleService articleService;

private final S3Service s3Service;

@PostMapping("/article")
@PostMapping("/")
public ApiResponse<ArticleResponseDTO.createArticleResultDTO> createArticle(@RequestBody @Valid ArticleRequestDTO.createArticleDTO request,
@RequestParam(name = "memberId") Long memberId) {
Article article = articleService.createArticle(memberId, request);
return ApiResponse.onSuccess(ArticleConverter.toCreateReviewResultDTO(article));
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.hackathon.TimeLapse.article;

import com.hackathon.TimeLapse.domain.Article;

import java.time.LocalDateTime;

public class ArticleConverter {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.hackathon.TimeLapse.article;

import com.hackathon.TimeLapse.domain.Article;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ArticleRepository extends JpaRepository<Article, Long> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
import jakarta.validation.constraints.NotNull;
import lombok.Getter;

import java.util.List;


public class ArticleRequestDTO {

@Getter
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
package com.hackathon.TimeLapse.article;
import com.hackathon.TimeLapse.apiPayload.exception.MemberNotFoundException;
import com.hackathon.TimeLapse.domain.Image;
import com.hackathon.TimeLapse.member.MemberRepository;
import com.hackathon.TimeLapse.domain.Article;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class ArticleService {
private final ArticleRepository articleRepository;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.hackathon.TimeLapse.domain.common;
package com.hackathon.TimeLapse.common;

import jakarta.persistence.EntityListeners;
import jakarta.persistence.MappedSuperclass;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.hackathon.TimeLapse.domain;
package com.hackathon.TimeLapse.image;

import com.hackathon.TimeLapse.domain.common.BaseEntity;
import com.hackathon.TimeLapse.article.Article;
import com.hackathon.TimeLapse.common.BaseEntity;
import jakarta.persistence.*;
import lombok.*;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

@Entity
@Getter
Expand All @@ -16,7 +16,7 @@ public class Image extends BaseEntity {
@GeneratedValue(
strategy = GenerationType.IDENTITY
)
private long id;
private Long id;

@Column(
nullable = false,
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/com/hackathon/TimeLapse/image/ImageController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.hackathon.TimeLapse.image;

import com.hackathon.TimeLapse.apiPayload.ApiResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequiredArgsConstructor
@Validated
@RequestMapping("/api/images")
public class ImageController {
private final ImageService imageService;

@PostMapping("/")
public ApiResponse<ImageResponseDTO.addImageResultDTO> addImages(@RequestBody @Valid ImageRequestDTO.addImagesDTO request,
@RequestHeader("articleId") Long articleId) {
List<Image> images = imageService.addImages(articleId, request);
return ApiResponse.onSuccess(ImageConverter.toAddImageResultDTO(images));
}
}
15 changes: 15 additions & 0 deletions src/main/java/com/hackathon/TimeLapse/image/ImageConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.hackathon.TimeLapse.image;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

public class ImageConverter {

public static ImageResponseDTO.addImageResultDTO toAddImageResultDTO(List<Image> images){
List<String> imageUrlList = images.stream()
.map(Image::getImage_url)
.collect(Collectors.toList());
return new ImageResponseDTO.addImageResultDTO(imageUrlList);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.hackathon.TimeLapse.image;

import com.hackathon.TimeLapse.article.Article;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ImageRepository extends JpaRepository<Image, Long> {
}
14 changes: 14 additions & 0 deletions src/main/java/com/hackathon/TimeLapse/image/ImageRequestDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.hackathon.TimeLapse.image;

import jakarta.validation.constraints.NotNull;
import lombok.Getter;

import java.util.List;

public class ImageRequestDTO {
@Getter
public static class addImagesDTO {

private List<String> imageUrlList;
}
}
20 changes: 20 additions & 0 deletions src/main/java/com/hackathon/TimeLapse/image/ImageResponseDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.hackathon.TimeLapse.image;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.util.List;

public class ImageResponseDTO {
@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class addImageResultDTO{
private List<String> imageUrlList;
}

}
37 changes: 37 additions & 0 deletions src/main/java/com/hackathon/TimeLapse/image/ImageService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.hackathon.TimeLapse.image;
import com.hackathon.TimeLapse.apiPayload.exception.ArticleNotFoundException;
import com.hackathon.TimeLapse.article.Article;
import com.hackathon.TimeLapse.article.ArticleRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class ImageService {
private final ImageRepository imageRepository;
private final ArticleRepository articleRepository;

public ImageService(ImageRepository imageRepository, ArticleRepository articleRepository) {
this.imageRepository = imageRepository;
this.articleRepository = articleRepository;
}

@Transactional
public List<Image> addImages(Long articleId, ImageRequestDTO.addImagesDTO request) {
Article article = articleRepository.findById(articleId)
.orElseThrow(() -> new ArticleNotFoundException("Article not found with id: " + articleId));

List<Image> images = new ArrayList<>();
for (String imageUrl : request.getImageUrlList()) {
Image image = Image.builder()
.image_url(imageUrl)
.article(article)
.build();
images.add(image);
}

return imageRepository.saveAll(images);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.hackathon.TimeLapse.domain;
package com.hackathon.TimeLapse.member;

import java.util.ArrayList;
import java.util.List;

import com.hackathon.TimeLapse.domain.common.BaseEntity;
import com.hackathon.TimeLapse.article.Article;
import com.hackathon.TimeLapse.common.BaseEntity;
import com.hackathon.TimeLapse.oauth.OAuthProvider;

import jakarta.persistence.CascadeType;
Expand All @@ -27,7 +28,7 @@
public class Member extends BaseEntity {
@Id
@GeneratedValue(
strategy = GenerationType.IDENTITY
strategy = GenerationType.IDENTITY
)
private Long id;

Expand All @@ -39,9 +40,9 @@ public class Member extends BaseEntity {
private OAuthProvider oAuthProvider;

@OneToMany(
mappedBy = "member",
cascade = {CascadeType.ALL},
fetch = FetchType.EAGER
mappedBy = "member",
cascade = {CascadeType.ALL},
fetch = FetchType.EAGER
)
private List<Article> articleList = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

import org.springframework.data.jpa.repository.JpaRepository;

import com.hackathon.TimeLapse.domain.Member;
import com.hackathon.TimeLapse.member.Member;

public interface MemberRepository extends JpaRepository<Member, Long> {
Optional<Member> findByEmail(String email);
Optional<Member> findByEmail(String email);
}
7 changes: 7 additions & 0 deletions src/main/java/com/hackathon/TimeLapse/s3/S3Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import lombok.RequiredArgsConstructor;

import java.util.List;

@RestController
@RequestMapping("/api/files")
@RequiredArgsConstructor
Expand All @@ -20,6 +22,11 @@ public ResponseEntity<String> uploadFile(@RequestParam("file") MultipartFile fil
return s3Service.uploadFile(file);
}

@PostMapping(value = "/uploadMultiple", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<List<String>> uploadMultipleFiles(@RequestParam("files") MultipartFile[] files) {
return s3Service.uploadMultipleFiles(files);
}

@GetMapping("/url")
public String getFileUrl(@RequestParam("fileName") String fileName) {
return s3Service.getFileUrl(fileName);
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/com/hackathon/TimeLapse/s3/S3Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import java.net.URL;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
Expand Down Expand Up @@ -44,6 +46,25 @@ public ResponseEntity<String> uploadFile(@RequestParam("file") MultipartFile fil
}
}

public ResponseEntity<List<String>> uploadMultipleFiles(@RequestParam("files") MultipartFile[] files) {
List<String> fileUrls = new ArrayList<>();
for (MultipartFile file : files) {
try {
String fileName = file.getOriginalFilename();
String fileUrl = "https://" + bucket + "/test" + fileName;
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentType(file.getContentType());
metadata.setContentLength(file.getSize());
amazonS3Client.putObject(bucket, fileName, file.getInputStream(), metadata);
fileUrls.add(fileUrl);
} catch (IOException e) {
e.printStackTrace();
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
}
return ResponseEntity.ok(fileUrls);
}

public void deleteFile(String fileName) {
amazonS3Client.deleteObject(bucket, fileName);
}
Expand Down

0 comments on commit 2417b62

Please sign in to comment.