Skip to content

Commit

Permalink
Merge pull request #30 from wjdgml/be
Browse files Browse the repository at this point in the history
๋กœ๊ทธ์ธ, ์‚ฌ์ง„ ์ฒจ๋ถ€ ์ฝ”๋“œ ์ˆ˜์ •
  • Loading branch information
wjdgml authored Sep 12, 2023
2 parents 19f2fe4 + 3e1e9cf commit 04e86b5
Show file tree
Hide file tree
Showing 17 changed files with 116 additions and 98 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.green.greenEarthForUs.Exception;

public class ImageDeletionException extends Throwable{
public ImageDeletionException() {
super();
}

public ImageDeletionException(String message) {
super(message);
}

public ImageDeletionException(String message, Throwable cause) {
super(message, cause);
}

public ImageDeletionException(Throwable cause) {
super(cause);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.green.greenEarthForUs.Exception;

public class ImageUploadException extends RuntimeException {

public ImageUploadException(String message) {
super(message);
}

public ImageUploadException(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;



@Configuration
public class AmazonS3Config {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.net.MalformedURLException;

import java.net.URL;
import java.io.IOException;
import java.io.InputStream;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.green.greenEarthForUs.post.Controller;

import com.green.greenEarthForUs.Exception.ImageDeletionException;
import com.green.greenEarthForUs.Image.Service.ImageService;
import com.green.greenEarthForUs.post.DTO.PostPatchDto;
import com.green.greenEarthForUs.post.DTO.PostPostDto;
import com.green.greenEarthForUs.post.DTO.PostResponseDto;
import com.green.greenEarthForUs.post.Entity.Post;
import com.green.greenEarthForUs.post.Mapper.PostMapper;
import com.green.greenEarthForUs.post.Service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
Expand All @@ -24,30 +27,25 @@ public class PostController {
final PostService postService;
private final PostMapper mapper;

public PostController(PostService postService, PostMapper mapper) {
private final ImageService imageService;
@Autowired
public PostController(PostService postService, PostMapper mapper, ImageService imageService) {
this.postService = postService;
this.mapper = mapper;
this.imageService = imageService;
}

// ๊ฒŒ์‹œ๊ธ€ ์ƒ์„ฑ
@PostMapping("/{user-id}")
public ResponseEntity<PostResponseDto> createPost(@PathVariable(value = "user-id") Long userId,
@ModelAttribute PostPostDto postPostDto,
MultipartHttpServletRequest request) throws IOException {

// ํŒŒ์ผ
MultipartFile multipartFile = request.getFile("image");
if (multipartFile != null && !multipartFile.isEmpty()) {
// ํŒŒ์ผ ์ด๋ฆ„ ๊ฐ€์ ธ์˜ค๊ธฐ
String originalFileName = multipartFile.getOriginalFilename();
// ํŒŒ์ผ ๋‚ด์šฉ(byte ๋ฐฐ์—ด) ๊ฐ€์ ธ์˜ค๊ธฐ
byte[] fileContent = multipartFile.getBytes();

postPostDto.setBodyImageFileName(originalFileName);
postPostDto.setBodyImage(fileContent);
}
@RequestParam("image") MultipartFile image,
@RequestBody PostPostDto postPostDto) throws IOException {

String imageUrl = imageService.uploadImage(image); // ์ด๋ฏธ์ง€ ์—…๋กœ๋“œํ•˜๊ณ 

Post createdPost = postService.createPost(userId, postPostDto, image);
createdPost.setImageUrl(imageUrl);

Post createdPost = postService.createPost(userId, postPostDto);
PostResponseDto responseDto = mapper.postToPostResponseDto(createdPost);

return ResponseEntity.status(HttpStatus.CREATED).body(responseDto);
Expand Down Expand Up @@ -99,7 +97,7 @@ public ResponseEntity<PostResponseDto> updatePost(@PathVariable(value = "user-id
// ๊ฒŒ์‹œ๊ธ€ ์‚ญ์ œ
@DeleteMapping("/{user-id}/{post-id}")
public ResponseEntity<Void> deletePost(@PathVariable(value = "user-id") Long userId,
@PathVariable(value = "post-id") Long postId) {
@PathVariable(value = "post-id") Long postId) throws ImageDeletionException {
postService.deletePost(postId, userId);

return ResponseEntity.noContent().build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ public class PostDto { // ์—”ํ‹ฐํ‹ฐ ์กฐํšŒ, ๋‚ด์šฉ ์ˆ˜์ •

private String body;

private String bodyImageFileName;

private byte[] bodyImage;
private String imageUrl;

private Boolean open;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ public class PostPatchDto {

private String title;

private String bodyImageFileName;

private byte[] bodyImage;
private String imageUrl;

private String body;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ public class PostPostDto { // ์ƒ์„ฑ

private Boolean open;

private String bodyImageFileName;

private byte[] bodyImage;
private String imageUrl;

public Boolean isOpen() {
return open != null && open; // open ํ•„๋“œ๊ฐ€ null์ธ ๊ฒฝ์šฐ๋‚˜ false์ธ ๊ฒฝ์šฐ์— ๊ตฌํ˜„ ์ด๋ ‡๊ฒŒ ํ•ด์ฃผ์–ด์•ผํ•จ
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ public class PostResponseDto {

private String open;

private String bodyImageFileName;

private byte[] bodyImage;
private String imageUrl;

private LocalDateTime createdAt;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
import javax.persistence.*;
import java.time.LocalDateTime;

@Getter @Setter
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entity
Expand All @@ -25,22 +26,16 @@ public class Post {
private Boolean open;

@Column
private String writer;
private String imageUrl;

@Column
private String type; // type ๊ฒŒ์‹œ๊ธ€์˜ ์œ ํ˜•

@Column
private String title;

@Column
private String bodyImageFileName;

@Lob
@Column
private byte[] bodyImage;

@Column(name= "created_at")
@Column(name = "created_at")
private LocalDateTime createdAt;

@ManyToOne
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.green.greenEarthForUs.post.Service;

import com.green.greenEarthForUs.Exception.ImageDeletionException;
import com.green.greenEarthForUs.Exception.UnauthorizedException;
import com.green.greenEarthForUs.Image.Service.ImageService;
import com.green.greenEarthForUs.post.DTO.PostPatchDto;
import com.green.greenEarthForUs.post.DTO.PostPostDto;
import com.green.greenEarthForUs.post.DTO.PostResponseDto;
Expand All @@ -13,6 +15,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.persistence.EntityNotFoundException;
import java.io.IOException;
Expand All @@ -30,23 +33,30 @@ public class PostService {
private final PostMapper mapper;
private final UserService userService;

public PostService(PostRepository postsRepository, UserRepository userRepository, PostMapper mapper, UserService userService) {
private final ImageService imageService;
@Autowired
public PostService(PostRepository postsRepository, UserRepository userRepository, PostMapper mapper,
UserService userService, ImageService imageService) {
this.postsRepository = postsRepository;
this.userRepository = userRepository;
this.mapper = mapper;
this.userService = userService;
this.imageService = imageService;
}

// ๊ฒŒ์‹œ๊ธ€ ์ƒ์„ฑ
@Transactional
public Post createPost(Long userId, PostPostDto postPostDto) throws IOException { // ์œ ์ €, ๊ฒŒ์‹œ๊ธ€

public Post createPost(Long userId, PostPostDto postPostDto, MultipartFile image) throws IOException { // ์œ ์ €, ๊ฒŒ์‹œ๊ธ€
User user = userRepository.findById(userId)
.orElseThrow(() -> new EntityNotFoundException("User not found with ID: " + userId)); // ์œ ์ € ์กฐํšŒ

String imageUrl = imageService.uploadImage(image);

Post post = mapper.postPostDtoToPost(postPostDto);
post.setUser(user);
post.setCreatedAt(LocalDateTime.now()); // ๊ฒŒ์‹œ๊ธ€ ์ƒ์„ฑ
post.setCreatedAt(LocalDateTime.now()); // ๊ฒŒ์‹œ๊ธ€ ์ƒ์„ฑํ•˜๊ณ 

post.setImageUrl(imageUrl); // ์ด๋ฏธ์ง€ url ๋„ฃ๊ณ 

post.setOpen(postPostDto.isOpen());

Expand Down Expand Up @@ -121,10 +131,20 @@ public PostResponseDto updatePost(Long userId, Long postId, PostPatchDto postPat

// ๊ฒŒ์‹œ๊ธ€ ์‚ญ์ œ
@Transactional
public void deletePost(Long userId, Long postId) {
public void deletePost(Long userId, Long postId) throws ImageDeletionException {
Post existingPost = postsRepository.findById(postId)
.orElseThrow(() -> new EntityNotFoundException("Post not found with ID: " + postId));

//์ด๋ฏธ์ง€ ์‚ญ์ œํ•˜๊ธฐ
String imageUrl = existingPost.getImageUrl();
if(imageUrl != null) {
try {
imageService.deleteImage(imageUrl);
} catch (Exception e) {
throw new ImageDeletionException("Failed to delete image: " + imageUrl, e);
}
}

// ์ž‘์„ฑ์ž ์š”์ฒญ์ž ์ผ์น˜ํ•˜๋Š”์ง€
User user = existingPost.getUser();
if (!user.getUserId().equals(userId)) {
Expand All @@ -134,16 +154,5 @@ public void deletePost(Long userId, Long postId) {
postsRepository.delete(existingPost);
}


// ํŽ˜์ด์ง€๋„ค์ด์…˜
// @Transactional
// public Page<Post> getLatestPosts(int page) {
//
// int pageNumber = page > 0 ? page - 1 : 0;
//
// Pageable pageable = PageRequest.of(pageNumber, 10, Sort.by("createdAt").descending());
//
// return postsRepository.findAll(pageable);
// }
}

Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class User {
@Enumerated(EnumType.STRING) // Post ํšŸ์ˆ˜์— ๋”ฐ๋ผ์„œ ๋•…(0๊ฐœ) โ†’ ์ƒˆ์‹น(1๊ฐœ) โ†’ ์กฐ๊ธˆ ๋” ์ž๋ž€ ์ƒˆ์‹น(5๊ฐœ) โ†’ ๊ฝƒ๋ด‰์˜ค๋ฆฌ(10๊ฐœ) โ†’ ๊ฝƒ(20๊ฐœ)
private UserGrade grade; // ์”จ์•— -> ์ƒˆ์‹น -> ์ ์  ์ž๋ผ๋Š” ๋Š๋‚Œ ๋ˆ„์  ๊ฒŒ์‹œ๊ธ€ ๊ธฐ์ค€ ..


private String imageUrl; // ์ด๋ฏธ์ง€ ์ €์žฅํ•  ํ•„๋“œ ์ถ”๊ฐ€ํ•˜๊ธฐ

@Column
Expand All @@ -49,12 +50,6 @@ public class User {
@Column
private LocalDateTime createAt;

@Column
private String profileImageFileName;

@Column
private byte[] profileImage;

@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
// ์‚ฌ์šฉ์ž๋ฅผ ์‚ญ์ œํ•  ๋•Œ ๊ด€๋ จ ๊ฒŒ์‹œ๋ฌผ, ์ด๋ฏธ์ง€๋„ ์ž๋™์ ์œผ๋กœ ์‚ญ์ œ๋จ
private List<Post> posts;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.green.greenEarthForUs.user.controller;


import com.green.greenEarthForUs.Image.Service.ImageService;
import com.green.greenEarthForUs.user.Entity.User;
import com.green.greenEarthForUs.user.dto.UserPatchDto;
import com.green.greenEarthForUs.user.dto.UserPostDto;
import com.green.greenEarthForUs.user.dto.UserResponseDto;
import com.green.greenEarthForUs.user.mapper.UserMapper;
import com.green.greenEarthForUs.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
Expand All @@ -22,27 +24,24 @@ public class UserController { // ์ด๋ฏธ์ง€ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ”์ด๋„ˆ๋ฆฌ ํ˜•ํƒœ๋กœ

private UserService userService;
private UserMapper mapper;

public UserController(UserService userService, UserMapper mapper) {
private final ImageService imageService;
@Autowired
public UserController(UserService userService, UserMapper mapper, ImageService imageService) {
this.userService = userService;
this.mapper = mapper;
this.imageService = imageService;
}

// ์‚ฌ์šฉ์ž ๋“ฑ๋ก
@PostMapping("/")
public ResponseEntity<UserResponseDto> createUser(@ModelAttribute UserPostDto userPostDto,
@RequestParam("image") MultipartFile imageFile) throws IOException {
// ์ด๋ฏธ์ง€ ํŒŒ์ผ ์—…๋กœ๋“œ
if (imageFile != null && !imageFile.isEmpty()) {
String originalFileName = imageFile.getOriginalFilename();
byte[] fileContent = imageFile.getBytes();

userPostDto.setImageFileName(originalFileName);
userPostDto.setImage(fileContent);
}
@PostMapping()
public ResponseEntity<UserResponseDto> createUser(@RequestParam("image") MultipartFile image,
@RequestBody UserPostDto userPostDto) throws IOException {

String imageUrl = imageService.uploadImage(image);

User createUser = userService.createUser(userPostDto, imageUrl);

User createdUser = userService.createUser(userPostDto, imageFile);
UserResponseDto responseDto = mapper.userToUserResponseDto(createdUser);
UserResponseDto responseDto = mapper.userToUserResponseDto(createUser);

return ResponseEntity.status(HttpStatus.CREATED).body(responseDto);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,5 @@ public class UserPatchDto {

private String passwordAnswer;

private String ImageFileName;

private byte[] fileImage;
private String imageUrl;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import lombok.Getter;
import lombok.Setter;

@Getter @Setter
@Getter
@Setter
public class UserPostDto { // ๊ฐ€์ž…

private Long userId;
Expand All @@ -16,8 +17,6 @@ public class UserPostDto { // ๊ฐ€์ž…

private String passwordAnswer;

private String ImageFileName;

private byte[] Image;
private String imageUrl;

}
Loading

0 comments on commit 04e86b5

Please sign in to comment.