Skip to content

Commit

Permalink
Merge pull request #14 from UMC-7-Time-Lapse/feature/#5
Browse files Browse the repository at this point in the history
Feature/#5
  • Loading branch information
oxdjww authored Jul 4, 2024
2 parents 63db64e + 02d21ac commit 7b627a4
Show file tree
Hide file tree
Showing 25 changed files with 268 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

@SpringBootApplication
@EnableJpaAuditing
public class TimeLapseApplication {

public static void main(String[] args) {
Expand Down
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,61 @@
package com.hackathon.TimeLapse.article;

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

import org.springframework.http.MediaType;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.hackathon.TimeLapse.apiPayload.ApiResponse;
import com.hackathon.TimeLapse.domain.Article;
import com.hackathon.TimeLapse.image.Image;
import com.hackathon.TimeLapse.image.ImageRequestDTO;
import com.hackathon.TimeLapse.image.ImageService;
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.*;
import lombok.extern.slf4j.Slf4j;

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

private final S3Service s3Service;
private final ArticleService articleService;
private final S3Service s3Service;
private final ImageService imageService;

@PostMapping(value = "", consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.MULTIPART_FORM_DATA_VALUE})
public ApiResponse<ArticleResponseDTO.createArticleResultDTO> createArticle(
@RequestPart @Valid ArticleRequestDTO.createArticleDTO request,
@RequestPart List<MultipartFile> files
) throws IOException {
long memberId = Long.parseLong(SecurityContextHolder.getContext().getAuthentication().getName());
log.info("[*] memberId: " + memberId);
System.out.println(files.get(0));
List<String> uploadedFilesLinks = s3Service.uploadMultipleFiles(files);
Article article = articleService.createArticle(memberId, request, uploadedFilesLinks);

List<Image> images = new ArrayList<>();
for (String url : uploadedFilesLinks) {
Image image = Image.builder()
.image_url(url)
.article(article)
.build();
images.add(image);
}
imageService.addImages(article.getId(), new ImageRequestDTO.addImagesDTO(uploadedFilesLinks));

return ApiResponse.onSuccess(ArticleConverter.toCreateReviewResultDTO(article));
}

@PostMapping("/article")
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
@@ -1,12 +1,13 @@
package com.hackathon.TimeLapse.article;

import java.util.List;

import org.springframework.web.multipart.MultipartFile;

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

import java.util.List;


public class ArticleRequestDTO {

@Getter
Expand All @@ -21,7 +22,5 @@ public static class createArticleDTO {
private String description;
@NotNull
private Long status;
@NotNull
private Long memberId;
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package com.hackathon.TimeLapse.article;
import java.util.List;

import com.hackathon.TimeLapse.apiPayload.exception.MemberNotFoundException;
import com.hackathon.TimeLapse.domain.Image;
import com.hackathon.TimeLapse.image.ImageService;
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 All @@ -20,12 +18,13 @@ public ArticleService(ArticleRepository articleRepository, MemberRepository memb
}

@Transactional
public Article createArticle(Long memberId, ArticleRequestDTO.createArticleDTO request) {
public Article createArticle(Long memberId, ArticleRequestDTO.createArticleDTO request,
List<String> uploadedFilesLinks) {
Article article = ArticleConverter.toArticle(request);

article.setMember(memberRepository.findById(memberId)
.orElseThrow(() -> new MemberNotFoundException("Member not found with id: " + memberId)));

return articleRepository.save(article);
}
}
}

This file was deleted.

30 changes: 30 additions & 0 deletions src/main/java/com/hackathon/TimeLapse/common/BaseEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.hackathon.TimeLapse.common;

import java.time.LocalDateTime;

import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import jakarta.persistence.EntityListeners;
import jakarta.persistence.MappedSuperclass;

@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class BaseEntity {
@CreatedDate
private LocalDateTime createdAt;
@LastModifiedDate
private LocalDateTime updatedAt;

public BaseEntity() {
}

public LocalDateTime getCreatedAt() {
return this.createdAt;
}

public LocalDateTime getUpdatedAt() {
return this.updatedAt;
}
}

This file was deleted.

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> {
}
17 changes: 17 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,17 @@
package com.hackathon.TimeLapse.image;

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

import java.util.List;

public class ImageRequestDTO {
@Getter
@AllArgsConstructor
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;
}

}
Loading

0 comments on commit 7b627a4

Please sign in to comment.