-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from GDSC-DGU/feat/3
[feat] #3 Post 생성 API 기능 구현
- Loading branch information
Showing
14 changed files
with
237 additions
and
21 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
src/main/java/org/harang/server/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,32 @@ | ||
package org.harang.server.controller; | ||
|
||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.harang.server.annotation.MemberId; | ||
import org.harang.server.domain.Post; | ||
import org.harang.server.dto.common.ApiResponse; | ||
import org.harang.server.dto.request.PostRequest; | ||
import org.harang.server.dto.type.SuccessMessage; | ||
import org.harang.server.service.PostService; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@Slf4j | ||
@RestController | ||
@RequestMapping("/v1/posts") | ||
@RequiredArgsConstructor | ||
public class PostController { | ||
|
||
private final PostService postService; | ||
|
||
@PostMapping | ||
public ApiResponse<?> createPost(@MemberId Long memberId, @Valid @RequestBody PostRequest request) { | ||
|
||
Post post = postService.createPost(memberId, request); | ||
|
||
return ApiResponse.success(SuccessMessage.CREATED); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/org/harang/server/controller/TestController.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 org.harang.server.controller; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.harang.server.domain.enums.Type; | ||
import org.harang.server.dto.response.JwtTokenResponse; | ||
import org.harang.server.util.JwtUtil; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@Slf4j | ||
@RestController | ||
@RequestMapping("/test") | ||
@RequiredArgsConstructor | ||
public class TestController { | ||
private final JwtUtil jwtUtil; | ||
|
||
@GetMapping("/token/{memberId}") | ||
public ResponseEntity<JwtTokenResponse> getToken(@PathVariable("memberId") Long memberId) { | ||
return ResponseEntity.ok(jwtUtil.generateTokens(memberId, Type.WATERING)); | ||
} | ||
} |
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
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,34 @@ | ||
package org.harang.server.domain; | ||
|
||
import jakarta.persistence.*; | ||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Entity | ||
@Getter | ||
@Table(name = "post_category") | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class PostCategory { | ||
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
@Column(name = "id", nullable = false) | ||
private Long id; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "post_id") | ||
private Post post; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "category_id") | ||
private Category category; | ||
|
||
@Builder | ||
public PostCategory(Post post, Category category) { | ||
this.post = post; | ||
this.category = category; | ||
} | ||
} |
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
29 changes: 29 additions & 0 deletions
29
src/main/java/org/harang/server/dto/request/PostRequest.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,29 @@ | ||
package org.harang.server.dto.request; | ||
|
||
import com.fasterxml.jackson.annotation.JsonFormat; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import jakarta.validation.constraints.NotNull; | ||
import jakarta.validation.constraints.Size; | ||
import org.harang.server.domain.Category; | ||
import org.harang.server.domain.enums.Gender; | ||
import org.harang.server.domain.enums.Status; | ||
|
||
import java.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
|
||
public record PostRequest( | ||
@JsonProperty("title") @NotNull @Size(min = 5, max = 24 ) String title, | ||
@JsonProperty("content") @NotNull(message = "내용을 입력해주세요.") String content, | ||
@JsonProperty("chatLink") @NotNull(message = "오픈채팅방 링크를 첨부해주세요.") String chatLink, | ||
@JsonProperty("preferredGender") Gender preferredGender, | ||
@JsonProperty("preferredAge") int preferredAge, | ||
@JsonProperty("preferredStartAt") @JsonFormat(pattern = "yyyy-MM-dd") LocalDate preferredStartAt, | ||
@JsonProperty("preferredEndAt") @JsonFormat(pattern = "yyyy-MM-dd") LocalDate preferredEndAt, | ||
@JsonProperty("category") List<String> categoryList, | ||
@JsonProperty("preferredLocation") String preferredLocation, | ||
@JsonProperty("x") Double x, | ||
@JsonProperty("y") Double y | ||
) { | ||
|
||
} |
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
8 changes: 8 additions & 0 deletions
8
src/main/java/org/harang/server/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 org.harang.server.repository; | ||
|
||
import org.harang.server.domain.Category; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface CategoryRepository extends JpaRepository<Category, Long> { | ||
Category findByName(String name); | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/org/harang/server/repository/LocationRepository.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,7 @@ | ||
package org.harang.server.repository; | ||
|
||
import org.harang.server.domain.Location; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface LocationRepository extends JpaRepository<Location, Long> { | ||
} |
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
7 changes: 7 additions & 0 deletions
7
src/main/java/org/harang/server/repository/PostCategoryRepository.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,7 @@ | ||
package org.harang.server.repository; | ||
|
||
import org.harang.server.domain.PostCategory; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface PostCategoryRepository extends JpaRepository<PostCategory, Long> { | ||
} |
4 changes: 2 additions & 2 deletions
4
src/main/java/org/harang/server/repository/PostRepository.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 |
---|---|---|
@@ -1,12 +1,12 @@ | ||
package org.harang.server.repository; | ||
|
||
import org.harang.server.domain.Post; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.harang.server.dto.type.ErrorMessage; | ||
import org.harang.server.exception.CustomException; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface PostRepository extends JpaRepository<Post, Long> { | ||
default Post findByIdOrThrow(Long id) { | ||
return findById(id).orElseThrow(() -> new CustomException(ErrorMessage.POST_NOT_FOUND)); | ||
} | ||
} | ||
} |
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,69 @@ | ||
package org.harang.server.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.harang.server.domain.*; | ||
import org.harang.server.domain.enums.Status; | ||
import org.harang.server.dto.request.PostRequest; | ||
import org.harang.server.repository.*; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class PostService { | ||
|
||
private final PostRepository postRepository; | ||
private final MemberRepository memberRepository; | ||
private final CategoryRepository categoryRepository; | ||
private final LocationRepository locationRepository; | ||
private final PostCategoryRepository postCategoryRepository; | ||
|
||
@Transactional | ||
public Post createPost(Long memberId, PostRequest request) { | ||
LocalDateTime createdAt = LocalDateTime.now(); | ||
Status status = Status.WAITING; | ||
|
||
Member member = memberRepository.findByIdOrThrow(memberId); | ||
|
||
Post savedPost = postRepository.save( | ||
Post.builder() | ||
.member(member) | ||
.createdAt(createdAt) | ||
.title(request.title()) | ||
.content(request.content()) | ||
.chatLink(request.chatLink()) | ||
.preferredGender(request.preferredGender()) | ||
.preferredAge(request.preferredAge()) | ||
.preferredStartAt(request.preferredStartAt()) | ||
.preferredEndAt(request.preferredEndAt()) | ||
.status(status) | ||
.build() | ||
); | ||
|
||
Location savedLocation = locationRepository.save( | ||
Location.builder() | ||
.post(savedPost) | ||
.name(request.preferredLocation()) | ||
.xValue(request.x()) | ||
.yValue(request.y()) | ||
.build() | ||
); | ||
|
||
for (String categoryName: request.categoryList()) { | ||
Category category = categoryRepository.findByName(categoryName); | ||
if(category != null) { | ||
PostCategory savedPostCategory = | ||
PostCategory.builder() | ||
.post(savedPost) | ||
.category(category) | ||
.build(); | ||
postCategoryRepository.save(savedPostCategory); | ||
} | ||
} | ||
return savedPost; | ||
} | ||
|
||
} |