-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'refs/heads/main' into feature-#20
- Loading branch information
Showing
26 changed files
with
698 additions
and
10 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package leets.weeth.domain.file.entity; | ||
|
||
import jakarta.persistence.*; | ||
import lombok.*; | ||
|
||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@ToString | ||
@Entity | ||
@Getter | ||
public class File { | ||
public File(String url) { | ||
this.url = url; | ||
} | ||
|
||
@Id //엔티티의 대푯값 지정 | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
@Column(name = "file_id") | ||
private Long id; | ||
|
||
private String url; | ||
|
||
|
||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/leets/weeth/domain/file/repository/FileRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package leets.weeth.domain.file.repository; | ||
|
||
import leets.weeth.domain.file.entity.File; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface FileRepository extends JpaRepository<File, Long> { | ||
|
||
} |
71 changes: 71 additions & 0 deletions
71
src/main/java/leets/weeth/domain/file/service/FileService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package leets.weeth.domain.file.service; | ||
|
||
import com.amazonaws.services.s3.AmazonS3; | ||
import com.amazonaws.services.s3.model.PutObjectRequest; | ||
import leets.weeth.domain.file.repository.FileRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import leets.weeth.domain.file.entity.File; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
@Slf4j | ||
@Service | ||
@RequiredArgsConstructor | ||
public class FileService { | ||
|
||
private final FileRepository fileRepository; | ||
|
||
@Value("${cloud.aws.s3.bucket}") | ||
private String bucketName; | ||
|
||
private final AmazonS3 s3Client; | ||
|
||
public List<File> uploadFiles(List<MultipartFile> files) { | ||
// 다중 업로드 && 리스트 ","을 기준으로 하나의 문자열 반환 | ||
// files 갯수 0 이면 반환 "" | ||
if(files == null || files.isEmpty()) | ||
return List.of(); | ||
|
||
List<File> results = new ArrayList<>(); | ||
|
||
for (MultipartFile file : files) { | ||
java.io.File fileObj = convertMultiPartFileToFile(file); | ||
String originalFilename = file.getOriginalFilename(); | ||
String extension = getFileExtension(originalFilename); | ||
String fileName = UUID.randomUUID() + "." + extension; | ||
|
||
log.info("uploadFile fileName: {}", fileName); | ||
s3Client.putObject(new PutObjectRequest(bucketName, fileName, fileObj)); | ||
fileObj.delete(); | ||
File newFile = new File(s3Client.getUrl(bucketName, fileName).toString()); | ||
|
||
|
||
fileRepository.save(newFile); | ||
results.add(newFile); | ||
} | ||
return results; | ||
} | ||
|
||
private java.io.File convertMultiPartFileToFile(MultipartFile file) { | ||
java.io.File convertedFile = new java.io.File(file.getOriginalFilename()); | ||
try (FileOutputStream fos = new FileOutputStream(convertedFile)) { | ||
fos.write(file.getBytes()); | ||
} catch (IOException e) { | ||
log.error("Error converting multipartFile to file", e); | ||
} | ||
return convertedFile; | ||
} | ||
|
||
private static String getFileExtension(String originalFileName) { | ||
return originalFileName.substring(originalFileName.lastIndexOf(".") + 1); | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/leets/weeth/domain/post/controller/CommentController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package leets.weeth.domain.post.controller; | ||
|
||
import leets.weeth.domain.post.dto.RequestCommentDTO; | ||
import leets.weeth.domain.post.dto.ResponseCommentDTO; | ||
import leets.weeth.domain.post.service.CommentService; | ||
import leets.weeth.global.common.response.CommonResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
import org.springframework.security.core.userdetails.User; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.List; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/articles/{articleId}/comments") | ||
public class CommentController { | ||
@Autowired | ||
private CommentService commentService; | ||
@PostMapping() | ||
public CommonResponse<String> create(@PathVariable Long articleId, @RequestBody RequestCommentDTO dto, | ||
@AuthenticationPrincipal User user){ | ||
commentService.create(user.getUsername(), articleId, dto); | ||
return CommonResponse.createSuccess(); | ||
} | ||
@GetMapping() | ||
public CommonResponse<List<ResponseCommentDTO>> show(@PathVariable Long articleId){ | ||
List<ResponseCommentDTO> comments = commentService.comments(articleId); | ||
return CommonResponse.createSuccess(comments); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
src/main/java/leets/weeth/domain/post/controller/PostController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package leets.weeth.domain.post.controller; | ||
|
||
import leets.weeth.domain.post.dto.RequestPostDTO; | ||
import leets.weeth.domain.post.dto.ResponsePostDTO; | ||
import leets.weeth.domain.post.service.PostService; | ||
import leets.weeth.global.auth.annotation.CurrentUser; | ||
import leets.weeth.global.common.error.exception.custom.InvalidAccessException; | ||
import leets.weeth.global.common.response.CommonResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.web.bind.annotation.*; | ||
import org.springframework.web.multipart.MultipartFile; | ||
import java.util.List; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/posts") | ||
public class PostController { | ||
@Autowired | ||
private final PostService postService; | ||
@PostMapping(value = {"/{postId}",""}) | ||
public CommonResponse<String> createOrUpdate(@RequestPart(value = "requestPostDTO") RequestPostDTO requestPostDTO, | ||
@RequestPart(value = "files", required = false) List<MultipartFile> files, | ||
@CurrentUser Long userId, @PathVariable(required = false) Long postId) throws InvalidAccessException { | ||
|
||
postService.create(userId, requestPostDTO, files, postId); | ||
return CommonResponse.createSuccess(); | ||
} | ||
|
||
@GetMapping("") | ||
public CommonResponse<List<ResponsePostDTO>> findAllPosts(){ | ||
List<ResponsePostDTO> posts = postService.findAllPosts(); | ||
return CommonResponse.createSuccess(posts); | ||
} | ||
|
||
@GetMapping("/myPosts") | ||
public CommonResponse<List<ResponsePostDTO>> showMyPost(@CurrentUser Long userId){ | ||
List<ResponsePostDTO> myPost = postService.myPosts(userId); | ||
return CommonResponse.createSuccess(myPost); | ||
} | ||
|
||
@GetMapping("/{postId}") | ||
public CommonResponse<ResponsePostDTO> showPost(@PathVariable Long postId){ | ||
ResponsePostDTO newPost = postService.show(postId); | ||
return CommonResponse.createSuccess(newPost); | ||
} | ||
|
||
@DeleteMapping("/{postId}") | ||
public CommonResponse<String> delete(@PathVariable Long postId, @CurrentUser Long userId) throws InvalidAccessException { | ||
postService.delete(postId, userId); | ||
return CommonResponse.createSuccess(); | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/leets/weeth/domain/post/dto/RequestCommentDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package leets.weeth.domain.post.dto; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.ToString; | ||
|
||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Getter | ||
@ToString | ||
public class RequestCommentDTO { | ||
|
||
@NotBlank | ||
private String content; | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/leets/weeth/domain/post/dto/RequestPostDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package leets.weeth.domain.post.dto; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
import lombok.*; | ||
|
||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Getter | ||
@ToString | ||
@Setter | ||
public class RequestPostDTO { | ||
@NotBlank | ||
private String title; | ||
@NotBlank | ||
private String content; | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/leets/weeth/domain/post/dto/ResponseCommentDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package leets.weeth.domain.post.dto; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
import leets.weeth.domain.post.entity.Comment; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.ToString; | ||
import java.time.LocalDateTime; | ||
|
||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Getter | ||
@ToString | ||
public class ResponseCommentDTO { | ||
private Long id; | ||
@NotBlank | ||
private String content; | ||
private LocalDateTime time; | ||
|
||
public static ResponseCommentDTO createResponseCommentDto(Comment comment) { | ||
return new ResponseCommentDTO(comment.getId(), comment.getContent(), | ||
comment.getTime()); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/leets/weeth/domain/post/dto/ResponsePostDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package leets.weeth.domain.post.dto; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
import leets.weeth.domain.file.entity.File; | ||
import leets.weeth.domain.post.entity.Post; | ||
import lombok.*; | ||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
|
||
@AllArgsConstructor | ||
@Builder | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@Getter | ||
@ToString | ||
public class ResponsePostDTO { | ||
|
||
private Long id; | ||
@NotBlank | ||
private String name; | ||
@NotBlank | ||
private String title; | ||
@NotBlank | ||
private String content; | ||
private LocalDateTime time; | ||
private List<File> fileUrls; | ||
|
||
public static ResponsePostDTO createResponsePostDTO(Post post) { | ||
return ResponsePostDTO.builder() | ||
.id(post.getId()) | ||
.name(post.getUser().getName()) | ||
.title(post.getTitle()) | ||
.content(post.getContent()) | ||
.time(post.getTime()) | ||
.fileUrls(post.getFileUrls()) | ||
.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package leets.weeth.domain.post.entity; | ||
|
||
import jakarta.persistence.*; | ||
import jakarta.validation.constraints.NotEmpty; | ||
import leets.weeth.domain.post.dto.RequestCommentDTO; | ||
import leets.weeth.domain.user.entity.User; | ||
import leets.weeth.global.common.entity.BaseEntity; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.ToString; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
|
||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@ToString | ||
@Entity | ||
@Getter | ||
@Table | ||
public class Comment extends BaseEntity { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
@Column(name = "comment_id") | ||
private Long id; | ||
|
||
@ManyToOne | ||
@JoinColumn(name="post_id") | ||
private Post post; | ||
|
||
@ManyToOne | ||
@JoinColumn(name = "user_id") | ||
private User user; | ||
|
||
@NotEmpty | ||
@Column | ||
private String content; | ||
LocalDateTime time; | ||
public static Comment createComment(RequestCommentDTO dto, Post post, User user){ | ||
|
||
Comment newComment = new Comment( | ||
null, | ||
post, | ||
user, | ||
dto.getContent(), | ||
null | ||
); | ||
return newComment; | ||
} | ||
|
||
@PrePersist | ||
@PreUpdate | ||
public void setTime() { | ||
this.time = this.getModifiedAt() == null ? this.getCreatedAt() : this.getModifiedAt(); | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.