From d9337b0bfc899ac5a6f9ffa6f75fd425a6226242 Mon Sep 17 00:00:00 2001 From: firefox1234123 Date: Tue, 30 Jul 2024 18:08:20 +0900 Subject: [PATCH] - --- .../LikeLion12thTeam03BeApplication.java | 2 + .../category/api/CategoryController.java | 22 ----- .../category/application/CategoryService.java | 33 ------- .../category/domain/Category.java | 6 +- .../domain/repository/CategoryRepository.java | 1 - .../location/api/dto/LocationController.java | 21 ----- .../api/dto/request/LocationSaveReqDto.java | 6 -- .../api/dto/request/LocationUpdateReqDto.java | 6 -- .../location/application/LocationService.java | 30 ------- .../location/domain/Location.java | 6 +- .../domain/repository/LocationRepository.java | 1 - .../mood/api/dto/MoodController.java | 23 +++++ .../mood/api/dto/response/MoodInfoResDto.java | 16 ++++ .../mood/api/dto/response/MoodListResDto.java | 16 ++++ .../mood/application/MoodService.java | 29 +++++++ .../mood/domain/Mood.java | 34 ++++++++ .../domain/repository/MoodRepository.java | 7 ++ .../post/api/dto/PostController.java | 30 ++++--- .../post/api/dto/request/PostSaveReqDto.java | 4 +- .../api/dto/request/PostUpdateReqDto.java | 4 +- .../post/api/dto/response/PostInfoResDto.java | 15 ++-- .../post/application/PostService.java | 87 ++++++++++++++----- .../post/domain/Post.java | 32 +++---- .../post/domain/Time.java | 22 +++++ .../likelion12thteam03be/s3/S3Service.java | 8 ++ src/main/resources/application.yml | 5 +- 26 files changed, 275 insertions(+), 191 deletions(-) delete mode 100644 src/main/java/net/skhu/likelion12thteam03be/location/api/dto/request/LocationSaveReqDto.java delete mode 100644 src/main/java/net/skhu/likelion12thteam03be/location/api/dto/request/LocationUpdateReqDto.java create mode 100644 src/main/java/net/skhu/likelion12thteam03be/mood/api/dto/MoodController.java create mode 100644 src/main/java/net/skhu/likelion12thteam03be/mood/api/dto/response/MoodInfoResDto.java create mode 100644 src/main/java/net/skhu/likelion12thteam03be/mood/api/dto/response/MoodListResDto.java create mode 100644 src/main/java/net/skhu/likelion12thteam03be/mood/application/MoodService.java create mode 100644 src/main/java/net/skhu/likelion12thteam03be/mood/domain/Mood.java create mode 100644 src/main/java/net/skhu/likelion12thteam03be/mood/domain/repository/MoodRepository.java create mode 100644 src/main/java/net/skhu/likelion12thteam03be/post/domain/Time.java diff --git a/src/main/java/net/skhu/likelion12thteam03be/LikeLion12thTeam03BeApplication.java b/src/main/java/net/skhu/likelion12thteam03be/LikeLion12thTeam03BeApplication.java index 431ddc1..b0e5598 100644 --- a/src/main/java/net/skhu/likelion12thteam03be/LikeLion12thTeam03BeApplication.java +++ b/src/main/java/net/skhu/likelion12thteam03be/LikeLion12thTeam03BeApplication.java @@ -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 LikeLion12thTeam03BeApplication { public static void main(String[] args) { diff --git a/src/main/java/net/skhu/likelion12thteam03be/category/api/CategoryController.java b/src/main/java/net/skhu/likelion12thteam03be/category/api/CategoryController.java index b53c078..5859be4 100644 --- a/src/main/java/net/skhu/likelion12thteam03be/category/api/CategoryController.java +++ b/src/main/java/net/skhu/likelion12thteam03be/category/api/CategoryController.java @@ -1,9 +1,6 @@ package net.skhu.likelion12thteam03be.category.api; -import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; -import net.skhu.likelion12thteam03be.category.api.dto.request.CategorySaveReqDto; -import net.skhu.likelion12thteam03be.category.api.dto.request.CategoryUpdateReqDto; import net.skhu.likelion12thteam03be.category.api.dto.response.CategoryListResDto; import net.skhu.likelion12thteam03be.category.application.CategoryService; import org.springframework.http.HttpStatus; @@ -14,30 +11,11 @@ @RequiredArgsConstructor @RequestMapping("/categories") public class CategoryController { - private final CategoryService categoryService; - @PostMapping - public ResponseEntity categorySave(@RequestBody @Valid CategorySaveReqDto categorySaveReqDto) { - categoryService.categorySave(categorySaveReqDto); - return new ResponseEntity<>("Successful Category Save!", HttpStatus.CREATED); - } - @GetMapping() public ResponseEntity categoryFindAll() { CategoryListResDto categoryListResDto = categoryService.categoryFindAll(); return new ResponseEntity<>(categoryListResDto, HttpStatus.OK); } - - @PatchMapping("/{categoryId}") - public ResponseEntity categoryUpdate(@PathVariable Long categoryId, @RequestBody @Valid CategoryUpdateReqDto categoryUpdateReqDto) { - categoryService.categoryUpdate(categoryId, categoryUpdateReqDto); - return new ResponseEntity<>("Successful Category Update! categoryId = " + categoryId, HttpStatus.OK); - } - - @DeleteMapping("/{categoryId}") - public ResponseEntity categoryDelete(@PathVariable Long categoryId) { - categoryService.categoryDelete(categoryId); - return new ResponseEntity<>("Successful Category Delete! categoryId = " + categoryId, HttpStatus.OK); - } } diff --git a/src/main/java/net/skhu/likelion12thteam03be/category/application/CategoryService.java b/src/main/java/net/skhu/likelion12thteam03be/category/application/CategoryService.java index 70168b9..e18752c 100644 --- a/src/main/java/net/skhu/likelion12thteam03be/category/application/CategoryService.java +++ b/src/main/java/net/skhu/likelion12thteam03be/category/application/CategoryService.java @@ -1,15 +1,10 @@ package net.skhu.likelion12thteam03be.category.application; import lombok.RequiredArgsConstructor; -import net.skhu.likelion12thteam03be.category.api.dto.request.CategorySaveReqDto; -import net.skhu.likelion12thteam03be.category.api.dto.request.CategoryUpdateReqDto; import net.skhu.likelion12thteam03be.category.api.dto.response.CategoryInfoResDto; import net.skhu.likelion12thteam03be.category.api.dto.response.CategoryListResDto; import net.skhu.likelion12thteam03be.category.domain.Category; import net.skhu.likelion12thteam03be.category.domain.repository.CategoryRepository; -import net.skhu.likelion12thteam03be.post.api.dto.response.PostInfoResDto; -import net.skhu.likelion12thteam03be.post.api.dto.response.PostListResDto; -import net.skhu.likelion12thteam03be.post.domain.Post; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -21,18 +16,6 @@ public class CategoryService { private final CategoryRepository categoryRepository; - @Transactional - public void categorySave(CategorySaveReqDto categorySaveReqDto) { - if (categoryRepository.existsByName(categorySaveReqDto.name())) { - throw new IllegalArgumentException("해당 카테고리가 이미 존재합니다."); - } - Category category = Category.builder() - .name(categorySaveReqDto.name()) - .build(); - - categoryRepository.save(category); - } - public CategoryListResDto categoryFindAll() { List categories = categoryRepository.findAll(); @@ -42,20 +25,4 @@ public CategoryListResDto categoryFindAll() { return CategoryListResDto.from(categoryInfoResDtoList); } - - @Transactional - public void categoryUpdate(Long categoryId, CategoryUpdateReqDto categoryUpdateReqDto) { - Category category = categoryRepository.findById(categoryId).orElseThrow( - () -> new IllegalArgumentException("해당 카테고리가 존재하지 않습니다.") - ); - category.update(categoryUpdateReqDto.name()); - } - - @Transactional - public void categoryDelete(Long categoryId) { - Category category = categoryRepository.findById(categoryId).orElseThrow( - () -> new IllegalArgumentException("해당 카테고리가 존재하지 않습니다.") - ); - categoryRepository.deleteById(categoryId); - } } diff --git a/src/main/java/net/skhu/likelion12thteam03be/category/domain/Category.java b/src/main/java/net/skhu/likelion12thteam03be/category/domain/Category.java index 5d58e4e..62969c1 100644 --- a/src/main/java/net/skhu/likelion12thteam03be/category/domain/Category.java +++ b/src/main/java/net/skhu/likelion12thteam03be/category/domain/Category.java @@ -18,7 +18,7 @@ public class Category { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - @Column(name = "category_id") + @Column(name = "categoryId") private Long categoryId; private String name; @@ -31,8 +31,4 @@ public class Category { public Category(String name) { this.name = name; } - - public void update(String name) { - this.name = name; - } } diff --git a/src/main/java/net/skhu/likelion12thteam03be/category/domain/repository/CategoryRepository.java b/src/main/java/net/skhu/likelion12thteam03be/category/domain/repository/CategoryRepository.java index 0cd3229..d81e831 100644 --- a/src/main/java/net/skhu/likelion12thteam03be/category/domain/repository/CategoryRepository.java +++ b/src/main/java/net/skhu/likelion12thteam03be/category/domain/repository/CategoryRepository.java @@ -4,5 +4,4 @@ import org.springframework.data.jpa.repository.JpaRepository; public interface CategoryRepository extends JpaRepository { - boolean existsByName(String name); } diff --git a/src/main/java/net/skhu/likelion12thteam03be/location/api/dto/LocationController.java b/src/main/java/net/skhu/likelion12thteam03be/location/api/dto/LocationController.java index 4e08ea5..117b5f2 100644 --- a/src/main/java/net/skhu/likelion12thteam03be/location/api/dto/LocationController.java +++ b/src/main/java/net/skhu/likelion12thteam03be/location/api/dto/LocationController.java @@ -1,9 +1,6 @@ package net.skhu.likelion12thteam03be.location.api.dto; import lombok.RequiredArgsConstructor; -import net.skhu.likelion12thteam03be.category.api.dto.response.CategoryListResDto; -import net.skhu.likelion12thteam03be.location.api.dto.request.LocationSaveReqDto; -import net.skhu.likelion12thteam03be.location.api.dto.request.LocationUpdateReqDto; import net.skhu.likelion12thteam03be.location.api.dto.response.LocationListResDto; import net.skhu.likelion12thteam03be.location.application.LocationService; import org.springframework.http.HttpStatus; @@ -16,27 +13,9 @@ public class LocationController { private final LocationService locationService; - @PostMapping() - public ResponseEntity locationSave(@RequestBody LocationSaveReqDto locationSaveReqDto) { - locationService.locationSave(locationSaveReqDto); - return new ResponseEntity<>("Successful Location Save!", HttpStatus.CREATED); - } - @GetMapping() public ResponseEntity locationFindAll() { LocationListResDto locationListResDto = locationService.locationFindAll(); return new ResponseEntity<>(locationListResDto, HttpStatus.OK); } - - @PatchMapping("/{locationId}") - public ResponseEntity locationUpdate(@PathVariable Long locationId, @RequestBody LocationUpdateReqDto locationUpdateReqDto) { - locationService.locationUpdate(locationId, locationUpdateReqDto); - return new ResponseEntity<>("Successful Location Update! locationId = " + locationId, HttpStatus.OK); - } - - @DeleteMapping("/{locationId}") - public ResponseEntity locationDelete(@PathVariable Long locationId) { - locationService.locationDelete(locationId); - return new ResponseEntity<>("Successful Location Delete! locationId = " + locationId, HttpStatus.OK); - } } diff --git a/src/main/java/net/skhu/likelion12thteam03be/location/api/dto/request/LocationSaveReqDto.java b/src/main/java/net/skhu/likelion12thteam03be/location/api/dto/request/LocationSaveReqDto.java deleted file mode 100644 index 4809cf6..0000000 --- a/src/main/java/net/skhu/likelion12thteam03be/location/api/dto/request/LocationSaveReqDto.java +++ /dev/null @@ -1,6 +0,0 @@ -package net.skhu.likelion12thteam03be.location.api.dto.request; - -public record LocationSaveReqDto( - String name -) { -} diff --git a/src/main/java/net/skhu/likelion12thteam03be/location/api/dto/request/LocationUpdateReqDto.java b/src/main/java/net/skhu/likelion12thteam03be/location/api/dto/request/LocationUpdateReqDto.java deleted file mode 100644 index 85dec0d..0000000 --- a/src/main/java/net/skhu/likelion12thteam03be/location/api/dto/request/LocationUpdateReqDto.java +++ /dev/null @@ -1,6 +0,0 @@ -package net.skhu.likelion12thteam03be.location.api.dto.request; - -public record LocationUpdateReqDto( - String name -) { -} diff --git a/src/main/java/net/skhu/likelion12thteam03be/location/application/LocationService.java b/src/main/java/net/skhu/likelion12thteam03be/location/application/LocationService.java index 2c4d2c0..0bac4a1 100644 --- a/src/main/java/net/skhu/likelion12thteam03be/location/application/LocationService.java +++ b/src/main/java/net/skhu/likelion12thteam03be/location/application/LocationService.java @@ -1,8 +1,6 @@ package net.skhu.likelion12thteam03be.location.application; import lombok.RequiredArgsConstructor; -import net.skhu.likelion12thteam03be.location.api.dto.request.LocationSaveReqDto; -import net.skhu.likelion12thteam03be.location.api.dto.request.LocationUpdateReqDto; import net.skhu.likelion12thteam03be.location.api.dto.response.LocationInfoResDto; import net.skhu.likelion12thteam03be.location.api.dto.response.LocationListResDto; import net.skhu.likelion12thteam03be.location.domain.Location; @@ -18,18 +16,6 @@ public class LocationService { private final LocationRepository locationRepository; - @Transactional - public void locationSave(LocationSaveReqDto locationSaveReqDto) { - if (locationRepository.existsByName(locationSaveReqDto.name())) { - throw new IllegalArgumentException("해당 위치가 이미 존재합니다."); - } - Location location = Location.builder() - .name(locationSaveReqDto.name()) - .build(); - - locationRepository.save(location); - } - public LocationListResDto locationFindAll() { List locations = locationRepository.findAll(); @@ -39,20 +25,4 @@ public LocationListResDto locationFindAll() { return LocationListResDto.from(locationInfoResDtoList); } - - @Transactional - public void locationUpdate(Long locationId, LocationUpdateReqDto locationUpdateReqDto) { - Location location = locationRepository.findById(locationId).orElseThrow( - () -> new IllegalArgumentException("해당 위치가 존재하지 않습니다. 새 위치로 등록해주세요.") - ); - location.update(locationUpdateReqDto.name()); - } - - @Transactional - public void locationDelete(Long locationId) { - Location location = locationRepository.findById(locationId).orElseThrow( - () -> new IllegalArgumentException("해당 위치가 존재하지 않습니다.") - ); - locationRepository.delete(location); - } } diff --git a/src/main/java/net/skhu/likelion12thteam03be/location/domain/Location.java b/src/main/java/net/skhu/likelion12thteam03be/location/domain/Location.java index fccb0c9..4f89310 100644 --- a/src/main/java/net/skhu/likelion12thteam03be/location/domain/Location.java +++ b/src/main/java/net/skhu/likelion12thteam03be/location/domain/Location.java @@ -18,7 +18,7 @@ public class Location { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - @Column(name = "location_id") + @Column(name = "locationId") private Long locationId; private String name; @@ -31,8 +31,4 @@ public class Location { public Location(String name) { this.name = name; } - - public void update(String name) { - this.name = name; - } } diff --git a/src/main/java/net/skhu/likelion12thteam03be/location/domain/repository/LocationRepository.java b/src/main/java/net/skhu/likelion12thteam03be/location/domain/repository/LocationRepository.java index fd97dbd..5135742 100644 --- a/src/main/java/net/skhu/likelion12thteam03be/location/domain/repository/LocationRepository.java +++ b/src/main/java/net/skhu/likelion12thteam03be/location/domain/repository/LocationRepository.java @@ -4,5 +4,4 @@ import org.springframework.data.jpa.repository.JpaRepository; public interface LocationRepository extends JpaRepository { - boolean existsByName(String name); } diff --git a/src/main/java/net/skhu/likelion12thteam03be/mood/api/dto/MoodController.java b/src/main/java/net/skhu/likelion12thteam03be/mood/api/dto/MoodController.java new file mode 100644 index 0000000..64fcec0 --- /dev/null +++ b/src/main/java/net/skhu/likelion12thteam03be/mood/api/dto/MoodController.java @@ -0,0 +1,23 @@ +package net.skhu.likelion12thteam03be.mood.api.dto; + +import lombok.RequiredArgsConstructor; +import net.skhu.likelion12thteam03be.mood.api.dto.response.MoodListResDto; +import net.skhu.likelion12thteam03be.mood.application.MoodService; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequiredArgsConstructor +@RequestMapping("/moods") +public class MoodController { + private final MoodService moodService; + + @GetMapping + public ResponseEntity moodFindAll() { + MoodListResDto moodListResDto = moodService.moodFindAll(); + return new ResponseEntity<>(moodListResDto, HttpStatus.OK); + } +} diff --git a/src/main/java/net/skhu/likelion12thteam03be/mood/api/dto/response/MoodInfoResDto.java b/src/main/java/net/skhu/likelion12thteam03be/mood/api/dto/response/MoodInfoResDto.java new file mode 100644 index 0000000..5229dde --- /dev/null +++ b/src/main/java/net/skhu/likelion12thteam03be/mood/api/dto/response/MoodInfoResDto.java @@ -0,0 +1,16 @@ +package net.skhu.likelion12thteam03be.mood.api.dto.response; + +import lombok.Builder; +import net.skhu.likelion12thteam03be.mood.domain.Mood; + +@Builder +public record MoodInfoResDto( + Long moodId, + String name +) { + public static MoodInfoResDto from(Mood mood) { + return MoodInfoResDto.builder() + .name(mood.getName()) + .build(); + } +} diff --git a/src/main/java/net/skhu/likelion12thteam03be/mood/api/dto/response/MoodListResDto.java b/src/main/java/net/skhu/likelion12thteam03be/mood/api/dto/response/MoodListResDto.java new file mode 100644 index 0000000..eee6044 --- /dev/null +++ b/src/main/java/net/skhu/likelion12thteam03be/mood/api/dto/response/MoodListResDto.java @@ -0,0 +1,16 @@ +package net.skhu.likelion12thteam03be.mood.api.dto.response; + +import lombok.Builder; + +import java.util.List; + +@Builder +public record MoodListResDto( + List moodList +) { + public static MoodListResDto from(List moodList) { + return MoodListResDto.builder() + .moodList(moodList) + .build(); + } +} diff --git a/src/main/java/net/skhu/likelion12thteam03be/mood/application/MoodService.java b/src/main/java/net/skhu/likelion12thteam03be/mood/application/MoodService.java new file mode 100644 index 0000000..c6c28b7 --- /dev/null +++ b/src/main/java/net/skhu/likelion12thteam03be/mood/application/MoodService.java @@ -0,0 +1,29 @@ +package net.skhu.likelion12thteam03be.mood.application; + +import lombok.RequiredArgsConstructor; +import net.skhu.likelion12thteam03be.mood.api.dto.response.MoodInfoResDto; +import net.skhu.likelion12thteam03be.mood.api.dto.response.MoodListResDto; +import net.skhu.likelion12thteam03be.mood.domain.Mood; +import net.skhu.likelion12thteam03be.mood.domain.repository.MoodRepository; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; + +@Service +@Transactional +@RequiredArgsConstructor +public class MoodService { + private final MoodRepository moodRepository; + + @Transactional + public MoodListResDto moodFindAll() { + List moods = moodRepository.findAll(); + + List moodInfoResDtoList = moods.stream() + .map(MoodInfoResDto::from) + .toList(); + + return MoodListResDto.from(moodInfoResDtoList); + } +} diff --git a/src/main/java/net/skhu/likelion12thteam03be/mood/domain/Mood.java b/src/main/java/net/skhu/likelion12thteam03be/mood/domain/Mood.java new file mode 100644 index 0000000..d7e9be7 --- /dev/null +++ b/src/main/java/net/skhu/likelion12thteam03be/mood/domain/Mood.java @@ -0,0 +1,34 @@ +package net.skhu.likelion12thteam03be.mood.domain; + +import com.fasterxml.jackson.annotation.JsonIgnore; +import jakarta.persistence.*; +import lombok.AccessLevel; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import net.skhu.likelion12thteam03be.post.domain.Post; + +import java.util.ArrayList; +import java.util.List; + +@Entity +@Getter +@NoArgsConstructor(access = AccessLevel.PROTECTED) +public class Mood { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "moodId") + private Long moodId; + + private String name; + + @JsonIgnore + @OneToMany(mappedBy = "mood", cascade = CascadeType.ALL, orphanRemoval = true) + private List postList = new ArrayList<>(); + + @Builder + public Mood(String name) { + this.name = name; + } +} diff --git a/src/main/java/net/skhu/likelion12thteam03be/mood/domain/repository/MoodRepository.java b/src/main/java/net/skhu/likelion12thteam03be/mood/domain/repository/MoodRepository.java new file mode 100644 index 0000000..f9d649b --- /dev/null +++ b/src/main/java/net/skhu/likelion12thteam03be/mood/domain/repository/MoodRepository.java @@ -0,0 +1,7 @@ +package net.skhu.likelion12thteam03be.mood.domain.repository; + +import net.skhu.likelion12thteam03be.mood.domain.Mood; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface MoodRepository extends JpaRepository { +} diff --git a/src/main/java/net/skhu/likelion12thteam03be/post/api/dto/PostController.java b/src/main/java/net/skhu/likelion12thteam03be/post/api/dto/PostController.java index b58bc6c..ceedf9e 100644 --- a/src/main/java/net/skhu/likelion12thteam03be/post/api/dto/PostController.java +++ b/src/main/java/net/skhu/likelion12thteam03be/post/api/dto/PostController.java @@ -6,20 +6,27 @@ import net.skhu.likelion12thteam03be.post.api.dto.response.PostListResDto; import net.skhu.likelion12thteam03be.post.application.PostService; import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; + +import java.io.IOException; +import java.security.Principal; @RestController @RequestMapping("/posts") public class PostController { private final PostService postService; - public PostController(PostService postService) { this.postService = postService; } + public PostController(PostService postService) { + this.postService = postService; + } // 글 저장 - @PostMapping() - public ResponseEntity postSave(@RequestBody PostSaveReqDto postSaveReqDto) { - postService.postSave(postSaveReqDto); + @PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE) + public ResponseEntity postSave(@RequestPart("post") PostSaveReqDto postSaveReqDto, @RequestPart("imgUrl") MultipartFile imgUrl, Principal principal) throws IOException { + postService.postSave(postSaveReqDto, imgUrl, principal); return new ResponseEntity<>("Successful Post Save", HttpStatus.CREATED); } @@ -51,9 +58,12 @@ public ResponseEntity postFindByCategoryId(@PathVariable("catego return new ResponseEntity<>(postListResDto, HttpStatus.OK); } - // 글 감정별 조회 - - // 글 색상별 조회 + // 글 분위기별 조회 + @GetMapping("/moods/{moodId}") + public ResponseEntity postFindByMoodId(@PathVariable("moodId") Long moodId) { + PostListResDto postListResDto = postService.postFindByMoodId(moodId); + return new ResponseEntity<>(postListResDto, HttpStatus.OK); + } // 글 작성자별 조회(내 글 조회) /*@GetMapping("/users/{userId}") @@ -67,14 +77,14 @@ public ResponseEntity postFindByUserId(@PathVariable("userId") L // 글 수정 @PatchMapping("/{postId}") - public ResponseEntity postUpdate(@PathVariable("postId") Long postId, @RequestBody PostUpdateReqDto postUpdateReqDto) { - postService.postUpdate(postId, postUpdateReqDto); + public ResponseEntity postUpdate(@PathVariable("postId") Long postId, @RequestPart("post") PostUpdateReqDto postUpdateReqDto, @RequestPart("imgUrl") MultipartFile imgUrl) throws IOException { + postService.postUpdate(postId, postUpdateReqDto, imgUrl); return new ResponseEntity<>("Successful Post Update", HttpStatus.OK); } // 글 삭제 @DeleteMapping("/{postId}") - public ResponseEntity postDelete(@PathVariable("postId") Long postId) { + public ResponseEntity postDelete(@PathVariable("postId") Long postId) throws IOException { postService.postDelete(postId); return new ResponseEntity<>("Successful Post Delete", HttpStatus.OK); } diff --git a/src/main/java/net/skhu/likelion12thteam03be/post/api/dto/request/PostSaveReqDto.java b/src/main/java/net/skhu/likelion12thteam03be/post/api/dto/request/PostSaveReqDto.java index e016ae9..51dd5bd 100644 --- a/src/main/java/net/skhu/likelion12thteam03be/post/api/dto/request/PostSaveReqDto.java +++ b/src/main/java/net/skhu/likelion12thteam03be/post/api/dto/request/PostSaveReqDto.java @@ -3,12 +3,10 @@ public record PostSaveReqDto( String title, String content, - String imgUrl, Long locationId, Integer time, Integer price, Long categoryId, - Long emotionId, - Long colorId + Long moodId ) { } diff --git a/src/main/java/net/skhu/likelion12thteam03be/post/api/dto/request/PostUpdateReqDto.java b/src/main/java/net/skhu/likelion12thteam03be/post/api/dto/request/PostUpdateReqDto.java index 892d2ee..5c095ad 100644 --- a/src/main/java/net/skhu/likelion12thteam03be/post/api/dto/request/PostUpdateReqDto.java +++ b/src/main/java/net/skhu/likelion12thteam03be/post/api/dto/request/PostUpdateReqDto.java @@ -3,12 +3,10 @@ public record PostUpdateReqDto( String title, String content, - String imgUrl, Long locationId, Integer time, Integer price, Long categoryId, - Long emotionId, - Long colorId + Long moodId ) { } diff --git a/src/main/java/net/skhu/likelion12thteam03be/post/api/dto/response/PostInfoResDto.java b/src/main/java/net/skhu/likelion12thteam03be/post/api/dto/response/PostInfoResDto.java index 5c0f042..5653d68 100644 --- a/src/main/java/net/skhu/likelion12thteam03be/post/api/dto/response/PostInfoResDto.java +++ b/src/main/java/net/skhu/likelion12thteam03be/post/api/dto/response/PostInfoResDto.java @@ -3,8 +3,11 @@ import lombok.Builder; import net.skhu.likelion12thteam03be.category.domain.Category; import net.skhu.likelion12thteam03be.location.domain.Location; +import net.skhu.likelion12thteam03be.mood.domain.Mood; import net.skhu.likelion12thteam03be.post.domain.Post; +import java.time.LocalDateTime; + @Builder public record PostInfoResDto( String title, @@ -14,20 +17,22 @@ public record PostInfoResDto( Integer time, Integer price, Category category, - Long emotionId, - Long colorId + Mood mood, + LocalDateTime createDate, + LocalDateTime modifiedDate ) { public static PostInfoResDto from(Post post) { return PostInfoResDto.builder() .title(post.getTitle()) .content(post.getContent()) - .imgUrl(post.getImgUrl()) .location(post.getLocation()) .time(post.getTime()) .price(post.getPrice()) .category(post.getCategory()) - .emotionId(post.getEmotionId()) - .colorId(post.getColorId()) + .mood(post.getMood()) + .imgUrl(post.getImgUrl()) + .createDate(post.getCreateDate()) + .modifiedDate(post.getModifiedDate()) .build(); } } diff --git a/src/main/java/net/skhu/likelion12thteam03be/post/application/PostService.java b/src/main/java/net/skhu/likelion12thteam03be/post/application/PostService.java index b819ff9..bf0817d 100644 --- a/src/main/java/net/skhu/likelion12thteam03be/post/application/PostService.java +++ b/src/main/java/net/skhu/likelion12thteam03be/post/application/PostService.java @@ -5,15 +5,21 @@ import net.skhu.likelion12thteam03be.category.domain.repository.CategoryRepository; import net.skhu.likelion12thteam03be.location.domain.Location; import net.skhu.likelion12thteam03be.location.domain.repository.LocationRepository; +import net.skhu.likelion12thteam03be.mood.domain.Mood; +import net.skhu.likelion12thteam03be.mood.domain.repository.MoodRepository; import net.skhu.likelion12thteam03be.post.api.dto.request.PostSaveReqDto; import net.skhu.likelion12thteam03be.post.api.dto.request.PostUpdateReqDto; import net.skhu.likelion12thteam03be.post.api.dto.response.PostInfoResDto; import net.skhu.likelion12thteam03be.post.api.dto.response.PostListResDto; import net.skhu.likelion12thteam03be.post.domain.Post; import net.skhu.likelion12thteam03be.post.domain.repository.PostRepository; +import net.skhu.likelion12thteam03be.s3.S3Service; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.multipart.MultipartFile; +import java.io.IOException; +import java.security.Principal; import java.util.List; import java.util.Optional; @@ -24,25 +30,32 @@ public class PostService { private final PostRepository postRepository; private final CategoryRepository categoryRepository; private final LocationRepository locationRepository; + private final S3Service s3Service; + private final MoodRepository moodRepository; @Transactional - public void postSave(PostSaveReqDto postSaveReqDto) { - Category category = categoryRepository.findById(postSaveReqDto.categoryId()) - .orElseThrow(() -> new IllegalArgumentException("해당 카테고리가 존재하지 않습니다. categoryId = " + postSaveReqDto.categoryId())); + public void postSave(PostSaveReqDto postSaveReqDto, MultipartFile multipartFile, Principal principal) throws IOException { + String imgUrl = s3Service.upload(multipartFile, "post"); + Long id = Long.parseLong(principal.getName()); Location location = locationRepository.findById(postSaveReqDto.locationId()) .orElseThrow(() -> new IllegalArgumentException("해당 위치가 존재하지 않습니다. locationId = " + postSaveReqDto.locationId())); + Category category = categoryRepository.findById(postSaveReqDto.categoryId()) + .orElseThrow(() -> new IllegalArgumentException("해당 카테고리가 존재하지 않습니다. categoryId = " + postSaveReqDto.categoryId())); + + Mood mood = moodRepository.findById(postSaveReqDto.moodId()) + .orElseThrow(() -> new IllegalArgumentException("해당 분위기가 존재하지 않습니다. moodId = " + postSaveReqDto.moodId())); + Post post = Post.builder() .title(postSaveReqDto.title()) .content(postSaveReqDto.content()) - .imgUrl(postSaveReqDto.imgUrl()) .location(location) .time(postSaveReqDto.time()) .price(postSaveReqDto.price()) .category(category) - .emotionId(postSaveReqDto.emotionId()) - .colorId(postSaveReqDto.colorId()) + .mood(mood) + .imgUrl(imgUrl) .build(); postRepository.save(post); @@ -69,7 +82,9 @@ public PostInfoResDto postFindById(Long postId) { // 글 위치별 조회 public PostListResDto postFindByLocationId(Long locationId) { - Optional post = postRepository.findById(locationId); + Optional post = Optional.ofNullable(postRepository.findById(locationId).orElseThrow( + () -> new IllegalArgumentException("해당 위치의 글을 조회할 수 없습니다. locationId = " + locationId) + )); List postInfoResDtoList = post.stream() .map(PostInfoResDto::from) @@ -80,7 +95,9 @@ public PostListResDto postFindByLocationId(Long locationId) { // 글 카테고리별 조회 public PostListResDto postFindByCategoryId(Long categoryId) { - Optional posts = postRepository.findById(categoryId); + Optional posts = Optional.ofNullable(postRepository.findById(categoryId).orElseThrow( + () -> new IllegalArgumentException("해당 카테고리의 글을 찾을 수 없습니다. categoryId = " + categoryId) + )); List postInfoResDtoList = posts.stream() .map(PostInfoResDto::from) @@ -89,9 +106,11 @@ public PostListResDto postFindByCategoryId(Long categoryId) { return PostListResDto.from(postInfoResDtoList); } - // 글 작성자별 조회(내 글 조회) - public PostListResDto postFindByUserId(Long userId) { - Optional posts = postRepository.findById(userId); + // 글 분위기별 조회 + public PostListResDto postFindByMoodId(Long moodId) { + Optional posts = Optional.ofNullable(postRepository.findById(moodId).orElseThrow( + () -> new IllegalArgumentException("해당 분위기의 글을 찾을 수 없습니다. moodId = " + moodId) + )); List postInfoResDtoList = posts.stream() .map(PostInfoResDto::from) @@ -100,38 +119,64 @@ public PostListResDto postFindByUserId(Long userId) { return PostListResDto.from(postInfoResDtoList); } - // 글 감정별 조회 + // 글 작성자별 조회(내 글 조회) + public PostListResDto postFindByUserId(Long userId) { + Optional posts = Optional.ofNullable(postRepository.findById(userId).orElseThrow( + () -> new IllegalArgumentException("해당 사용자가 작성한 글을 찾을 수 없습니다. userId = " + userId) + )); - // 글 색상별 조회 + List postInfoResDtoList = posts.stream() + .map(PostInfoResDto::from) + .toList(); - // 글 검색 조회 + return PostListResDto.from(postInfoResDtoList); + } + // 글 검색 조회 // 글 수정 @Transactional - public void postUpdate(Long postId, PostUpdateReqDto postUpdateReqDto) { + public void postUpdate(Long postId, PostUpdateReqDto postUpdateReqDto, MultipartFile multipartFile) throws IOException { Post post = postRepository.findById(postId).orElseThrow( () -> new IllegalArgumentException("해당 글을 수정할 수 없습니다. postId = " + postId) ); + Location location = locationRepository.findById(postUpdateReqDto.locationId()) + .orElseThrow(() -> new IllegalArgumentException("해당 위치가 존재하지 않습니다. locationId = " + postUpdateReqDto.locationId())); + Category category = categoryRepository.findById(postUpdateReqDto.categoryId()) .orElseThrow(() -> new IllegalArgumentException("해당 카테고리가 존재하지 않습니다. categoryId = " + postUpdateReqDto.categoryId())); - Location location = locationRepository.findById(postUpdateReqDto.locationId()) - .orElseThrow(() -> new IllegalArgumentException("해당 위치가 존재하지 않습니다. locationId = " + postUpdateReqDto.locationId())); + Mood mood = moodRepository.findById(postUpdateReqDto.moodId()) + .orElseThrow(() -> new IllegalArgumentException("해당 분위기가 존재하지 않습니다. moodId = " + postUpdateReqDto.moodId())); - post.update(location, category, postUpdateReqDto); + String imgUrl = s3Service.upload(multipartFile, "post"); + + post.update(location, category, postUpdateReqDto, mood, imgUrl); PostInfoResDto.from(post); } // 글 삭제 @Transactional - public void postDelete(Long postId) { + public void postDelete(Long postId) throws IOException { Post post = postRepository.findById(postId).orElseThrow( () -> new IllegalArgumentException("해당 글을 삭제할 수 없습니다. postId = " + postId) ); - postRepository.delete(post); + Optional imgUrl = Optional.ofNullable(post.getImgUrl()); + + imgUrl.ifPresentOrElse( + url -> { + try { + s3Service.delete(url, "post"); + } catch (IllegalArgumentException e) { + throw new IllegalArgumentException("이미지 삭제 중 오류 발생", e); + } + postRepository.delete(post); + }, + () -> { + throw new IllegalArgumentException("이미지 URL이 존재하지 않습니다. postId = " + postId); + } + ); } - } diff --git a/src/main/java/net/skhu/likelion12thteam03be/post/domain/Post.java b/src/main/java/net/skhu/likelion12thteam03be/post/domain/Post.java index ba0781a..9505a60 100644 --- a/src/main/java/net/skhu/likelion12thteam03be/post/domain/Post.java +++ b/src/main/java/net/skhu/likelion12thteam03be/post/domain/Post.java @@ -7,63 +7,63 @@ import lombok.NoArgsConstructor; import net.skhu.likelion12thteam03be.category.domain.Category; import net.skhu.likelion12thteam03be.location.domain.Location; +import net.skhu.likelion12thteam03be.mood.domain.Mood; import net.skhu.likelion12thteam03be.post.api.dto.request.PostUpdateReqDto; @Entity @Getter @NoArgsConstructor(access = AccessLevel.PROTECTED) -public class Post { +public class Post extends Time { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - @Column(name = "post_id") + @Column(name = "postId") private Long postId; private String title; // 제목 private String content; // 내용 - private String imgUrl; // 사진 @ManyToOne - @JoinColumn(name = "location") + @JoinColumn(name = "locationId") private Location location; // 거래 장소 - private Integer time; // 거래 시간 private Integer price; // 가격 @ManyToOne - @JoinColumn(name = "category") + @JoinColumn(name = "categoryId") private Category category; - private Long emotionId; // 감정 키워드 - private Long colorId; // 색상 코드 + @ManyToOne + @JoinColumn(name = "moodId") + private Mood mood; // 감정 키워드 + + private String imgUrl; // 사진 /* @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "user_id") private User user;*/ @Builder - public Post(String title, String content, String imgUrl, Location location, Integer time, Integer price, Category category, Long emotionId, Long colorId) { + public Post(String title, String content, Location location, Integer time, Integer price, Category category, Mood mood, String imgUrl) { this.title = title; this.content = content; - this.imgUrl = imgUrl; this.location = location; this.time = time; this.price = price; this.category = category; - this.emotionId = emotionId; - this.colorId = colorId; + this.mood = mood; + this.imgUrl = imgUrl; } - public void update(Location location, Category category, PostUpdateReqDto postUpdateReqDto) { + public void update(Location location, Category category, PostUpdateReqDto postUpdateReqDto, Mood mood, String imgUrl) { this.title = postUpdateReqDto.title(); this.content = postUpdateReqDto.content(); - this.imgUrl = postUpdateReqDto.imgUrl(); this.location = location; this.time = postUpdateReqDto.time(); this.price = postUpdateReqDto.price(); this.category = category; - this.emotionId = postUpdateReqDto.emotionId(); - this.colorId = postUpdateReqDto.colorId(); + this.mood = mood; + this.imgUrl = imgUrl; } } diff --git a/src/main/java/net/skhu/likelion12thteam03be/post/domain/Time.java b/src/main/java/net/skhu/likelion12thteam03be/post/domain/Time.java new file mode 100644 index 0000000..0d8c0b5 --- /dev/null +++ b/src/main/java/net/skhu/likelion12thteam03be/post/domain/Time.java @@ -0,0 +1,22 @@ +package net.skhu.likelion12thteam03be.post.domain; + +import jakarta.persistence.EntityListeners; +import jakarta.persistence.MappedSuperclass; +import lombok.Getter; +import org.springframework.data.annotation.CreatedDate; +import org.springframework.data.annotation.LastModifiedDate; +import org.springframework.data.jpa.domain.support.AuditingEntityListener; + +import java.time.LocalDateTime; + +@Getter +@MappedSuperclass +@EntityListeners(AuditingEntityListener.class) +public class Time { + + @CreatedDate + private LocalDateTime createDate; + + @LastModifiedDate + private LocalDateTime modifiedDate; +} diff --git a/src/main/java/net/skhu/likelion12thteam03be/s3/S3Service.java b/src/main/java/net/skhu/likelion12thteam03be/s3/S3Service.java index 8f80583..28a3b0e 100644 --- a/src/main/java/net/skhu/likelion12thteam03be/s3/S3Service.java +++ b/src/main/java/net/skhu/likelion12thteam03be/s3/S3Service.java @@ -1,6 +1,7 @@ package net.skhu.likelion12thteam03be.s3; import com.amazonaws.services.s3.AmazonS3; +import com.amazonaws.services.s3.model.DeleteObjectRequest; import com.amazonaws.services.s3.model.PutObjectRequest; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -68,4 +69,11 @@ public Optional convert(MultipartFile multipartFile) throws IOException { return Optional.empty(); } + + public void delete(String imgUrl, String dirName) { + String imgName = imgUrl.substring(imgUrl.lastIndexOf("/") + 1); + String fileName = dirName + "/" + imgName; + + amazonS3.deleteObject(new DeleteObjectRequest(bucket, fileName)); + } } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 164d6f2..fc3a5a7 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -5,12 +5,11 @@ spring: url: ${spring.datasource.url} username: ${spring.datasource.username} password: ${spring.datasource.password} - driver-class-name: ${spring.datasource.driver-class-name} + driver-class-name: com.mysql.cj.jdbc.Driver jpa: database: mysql - database-platform: org.hibernate.dialect.MySQL8Dialect hibernate: - ddl-auto: update + ddl-auto: create show-sql: true properties: hibernate: