-
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.
- Loading branch information
Showing
20 changed files
with
650 additions
and
6 deletions.
There are no files selected for viewing
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
43 changes: 43 additions & 0 deletions
43
src/main/java/net/skhu/likelion12thteam03be/category/api/CategoryController.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,43 @@ | ||
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; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/categories") | ||
public class CategoryController { | ||
|
||
private final CategoryService categoryService; | ||
|
||
@PostMapping | ||
public ResponseEntity<String> categorySave(@RequestBody @Valid CategorySaveReqDto categorySaveReqDto) { | ||
categoryService.categorySave(categorySaveReqDto); | ||
return new ResponseEntity<>("Successful Category Save!", HttpStatus.CREATED); | ||
} | ||
|
||
@GetMapping() | ||
public ResponseEntity<CategoryListResDto> categoryFindAll() { | ||
CategoryListResDto categoryListResDto = categoryService.categoryFindAll(); | ||
return new ResponseEntity<>(categoryListResDto, HttpStatus.OK); | ||
} | ||
|
||
@PatchMapping("/{categoryId}") | ||
public ResponseEntity<String> 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<String> categoryDelete(@PathVariable Long categoryId) { | ||
categoryService.categoryDelete(categoryId); | ||
return new ResponseEntity<>("Successful Category Delete! categoryId = " + categoryId, HttpStatus.OK); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/net/skhu/likelion12thteam03be/category/api/dto/request/CategorySaveReqDto.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,6 @@ | ||
package net.skhu.likelion12thteam03be.category.api.dto.request; | ||
|
||
public record CategorySaveReqDto( | ||
String name | ||
) { | ||
} |
6 changes: 6 additions & 0 deletions
6
...ain/java/net/skhu/likelion12thteam03be/category/api/dto/request/CategoryUpdateReqDto.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,6 @@ | ||
package net.skhu.likelion12thteam03be.category.api.dto.request; | ||
|
||
public record CategoryUpdateReqDto( | ||
String name | ||
) { | ||
} |
16 changes: 16 additions & 0 deletions
16
...main/java/net/skhu/likelion12thteam03be/category/api/dto/response/CategoryInfoResDto.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 net.skhu.likelion12thteam03be.category.api.dto.response; | ||
|
||
import lombok.Builder; | ||
import net.skhu.likelion12thteam03be.category.domain.Category; | ||
|
||
@Builder | ||
public record CategoryInfoResDto( | ||
Long categoryId, | ||
String name | ||
) { | ||
public static CategoryInfoResDto from(Category category) { | ||
return CategoryInfoResDto.builder() | ||
.name(category.getName()) | ||
.build(); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...main/java/net/skhu/likelion12thteam03be/category/api/dto/response/CategoryListResDto.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 net.skhu.likelion12thteam03be.category.api.dto.response; | ||
|
||
import lombok.Builder; | ||
|
||
import java.util.List; | ||
|
||
@Builder | ||
public record CategoryListResDto( | ||
List<CategoryInfoResDto> categoryList | ||
) { | ||
public static CategoryListResDto from(List<CategoryInfoResDto> categoryList) { | ||
return CategoryListResDto.builder() | ||
.categoryList(categoryList) | ||
.build(); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
src/main/java/net/skhu/likelion12thteam03be/category/application/CategoryService.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,61 @@ | ||
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; | ||
|
||
import java.util.List; | ||
|
||
@Service | ||
@Transactional | ||
@RequiredArgsConstructor | ||
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<Category> categories = categoryRepository.findAll(); | ||
|
||
List<CategoryInfoResDto> categoryInfoResDtoList = categories.stream() | ||
.map(CategoryInfoResDto::from) | ||
.toList(); | ||
|
||
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); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/net/skhu/likelion12thteam03be/category/domain/Category.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,38 @@ | ||
package net.skhu.likelion12thteam03be.category.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 Category { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
@Column(name = "category_id") | ||
private Long categoryId; | ||
|
||
private String name; | ||
|
||
@JsonIgnore | ||
@OneToMany(mappedBy = "category", cascade = CascadeType.ALL, orphanRemoval = true) | ||
private List<Post> postList = new ArrayList<>(); | ||
|
||
@Builder | ||
public Category(String name) { | ||
this.name = name; | ||
} | ||
|
||
public void update(String name) { | ||
this.name = name; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...ain/java/net/skhu/likelion12thteam03be/category/domain/repository/CategoryRepository.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 net.skhu.likelion12thteam03be.category.domain.repository; | ||
|
||
import net.skhu.likelion12thteam03be.category.domain.Category; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface CategoryRepository extends JpaRepository<Category, Long> { | ||
boolean existsByName(String name); | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/net/skhu/likelion12thteam03be/config/S3Config.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 net.skhu.likelion12thteam03be.config; | ||
|
||
import com.amazonaws.auth.AWSCredentials; | ||
import com.amazonaws.auth.AWSStaticCredentialsProvider; | ||
import com.amazonaws.auth.BasicAWSCredentials; | ||
import com.amazonaws.services.s3.AmazonS3; | ||
import com.amazonaws.services.s3.AmazonS3ClientBuilder; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class S3Config { | ||
|
||
@Value("${cloud.aws.credentials.access-key}") | ||
private String accessKey; | ||
|
||
@Value("${cloud.aws.credentials.secret-key}") | ||
private String secretKey; | ||
|
||
@Value("${cloud.aws.region.static}") | ||
private String region; | ||
|
||
// AmazonS3 클라이언트를 생성하는 Bean을 정의 | ||
@Bean | ||
public AmazonS3 s3Builder() { | ||
// AWS 자격 증명 생성 | ||
AWSCredentials basicAWSCredentials = new BasicAWSCredentials(accessKey, secretKey); | ||
|
||
// AmazonS3 클라이언트 생성, 자격 증명 및 region 설정 | ||
return AmazonS3ClientBuilder | ||
.standard() | ||
.withCredentials(new AWSStaticCredentialsProvider(basicAWSCredentials)) | ||
.withRegion(region) | ||
.build(); | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
src/main/java/net/skhu/likelion12thteam03be/post/api/dto/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,70 @@ | ||
package net.skhu.likelion12thteam03be.post.api.dto; | ||
|
||
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.application.PostService; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RestController | ||
@RequestMapping("/posts") | ||
public class PostController { | ||
private final PostService postService; | ||
|
||
public PostController(PostService postService) { this.postService = postService; } | ||
|
||
// 글 저장 | ||
@PostMapping() | ||
public ResponseEntity<String> postSave(@RequestBody PostSaveReqDto postSaveReqDto) { | ||
postService.postSave(postSaveReqDto); | ||
return new ResponseEntity<>("Successful Post Save", HttpStatus.CREATED); | ||
} | ||
|
||
// 글 전체 조회 | ||
@GetMapping() | ||
public ResponseEntity<PostListResDto> postFindAll() { | ||
PostListResDto postListResDto = postService.postFindAll(); | ||
return new ResponseEntity<>(postListResDto, HttpStatus.OK); | ||
} | ||
|
||
// 글 개별 조회 | ||
@GetMapping("/{postId}") | ||
public ResponseEntity<PostInfoResDto> postFindById(@PathVariable Long postId) { | ||
PostInfoResDto postInfoResDto = postService.postFindById(postId); | ||
return new ResponseEntity<>(postInfoResDto, HttpStatus.OK); | ||
} | ||
|
||
// 글 카테고리별 조회 | ||
@GetMapping("/categories/{categoryId}") | ||
public ResponseEntity<PostListResDto> postFindByCategoryId(@PathVariable("categoryId") Long categoryId) { | ||
PostListResDto postListResDto = postService.postFindByCategoryId(categoryId); | ||
return new ResponseEntity<>(postListResDto, HttpStatus.OK); | ||
} | ||
|
||
// 글 작성자별 조회(내 글 조회) | ||
/*@GetMapping("/users/{userId}") | ||
public ResponseEntity<PostListResDto> postFindByUserId(@PathVariable("userId") Long userId) { | ||
PostListResDto postListResDto = postService.postFindByUserId(userId); | ||
return new ResponseEntity<>(postListResDto, HttpStatus.OK); | ||
}*/ | ||
|
||
// 글 검색 조회 | ||
|
||
|
||
// 글 수정 | ||
@PatchMapping("/{postId}") | ||
public ResponseEntity<String> postUpdate(@PathVariable("postId") Long postId, @RequestBody PostUpdateReqDto postUpdateReqDto) { | ||
postService.postUpdate(postId, postUpdateReqDto); | ||
return new ResponseEntity<>("Successful Post Update", HttpStatus.OK); | ||
} | ||
|
||
// 글 삭제 | ||
@DeleteMapping("/{postId}") | ||
public ResponseEntity<String> postDelete(@PathVariable("postId") Long postId) { | ||
postService.postDelete(postId); | ||
return new ResponseEntity<>("Successful Post Delete", HttpStatus.OK); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/net/skhu/likelion12thteam03be/post/api/dto/request/PostSaveReqDto.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,14 @@ | ||
package net.skhu.likelion12thteam03be.post.api.dto.request; | ||
|
||
public record PostSaveReqDto( | ||
String title, | ||
String content, | ||
String imgUrl, | ||
String locationId, | ||
Integer time, | ||
Integer price, | ||
Long categoryId, | ||
String emotionId, | ||
String colorId | ||
) { | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/net/skhu/likelion12thteam03be/post/api/dto/request/PostUpdateReqDto.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,14 @@ | ||
package net.skhu.likelion12thteam03be.post.api.dto.request; | ||
|
||
public record PostUpdateReqDto( | ||
String title, | ||
String content, | ||
String imgUrl, | ||
String locationId, | ||
Integer time, | ||
Integer price, | ||
Long categoryId, | ||
String emotionId, | ||
String colorId | ||
) { | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/net/skhu/likelion12thteam03be/post/api/dto/response/PostInfoResDto.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 net.skhu.likelion12thteam03be.post.api.dto.response; | ||
|
||
import lombok.Builder; | ||
import net.skhu.likelion12thteam03be.category.domain.Category; | ||
import net.skhu.likelion12thteam03be.post.domain.Post; | ||
|
||
@Builder | ||
public record PostInfoResDto( | ||
String title, | ||
String content, | ||
String imgUrl, | ||
String locationId, | ||
Integer time, | ||
Integer price, | ||
Category category, | ||
String emotionId, | ||
String colorId | ||
) { | ||
public static PostInfoResDto from(Post post) { | ||
return PostInfoResDto.builder() | ||
.title(post.getTitle()) | ||
.content(post.getContent()) | ||
.imgUrl(post.getImgUrl()) | ||
.locationId(post.getLocationId()) | ||
.time(post.getTime()) | ||
.price(post.getPrice()) | ||
.category(post.getCategory()) | ||
.emotionId(post.getEmotionId()) | ||
.colorId(post.getColorId()) | ||
.build(); | ||
} | ||
} |
Oops, something went wrong.