Skip to content

Commit

Permalink
Merge pull request #29 from tukcom2023CD/BE/feat/#22
Browse files Browse the repository at this point in the history
[BE/feat/#22] Post Create api 구현
  • Loading branch information
kjeongh authored Jan 5, 2023
2 parents 768f5c5 + 6f3b8f4 commit baab8ca
Show file tree
Hide file tree
Showing 11 changed files with 180 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.rising.backend.domain.comment.domain.controller;

import com.rising.backend.domain.comment.dto.request.CommentRequest;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "USER API")
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/comments")
public class CommentController {


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.rising.backend.domain.comment.dto.request;

import lombok.Getter;

@Getter
public class CommentRequest {

public static class CreateDto {
private Long postId;

private Long userId;

private String content;

private Long parentId;

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.rising.backend.domain.comment.repository;

import com.rising.backend.domain.comment.domain.Comment;
import org.springframework.data.jpa.repository.JpaRepository;

public interface CommentRepository extends JpaRepository<Comment, Long> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.rising.backend.domain.comment.service;

import com.rising.backend.domain.comment.repository.CommentRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class CommentService {

private final CommentRepository commentRepository;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.rising.backend.domain.post.controller;

import com.rising.backend.domain.post.dto.request.PostCreateRequestDto;
import com.rising.backend.domain.post.service.PostService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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;

@Tag(name = "POST API")
@RestController
@RequiredArgsConstructor
@RequestMapping("api/v1/posts")
public class PostController {

private final PostService postService;

@PostMapping
public ResponseEntity<String> create(@RequestBody PostCreateRequestDto dto) {
postService.createPost(dto);

return ResponseEntity.status(HttpStatus.CREATED)
.body("글 등록 성공");
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
package com.rising.backend.domain.post.domain;

import com.fasterxml.jackson.annotation.JsonCreator;

public enum PostType {
QUESTION,
MENTORING
MENTORING;

@JsonCreator
public static PostType from(String s) {
return PostType.valueOf(s.toUpperCase());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.rising.backend.domain.post.dto.mapper;

import com.rising.backend.domain.post.domain.Post;
import com.rising.backend.domain.post.dto.request.PostCreateRequestDto;
import com.rising.backend.domain.post.repository.PostRepository;
import com.rising.backend.domain.user.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class PostMapper {

private final PostRepository postRepository;
private final UserService userService;

public Post toPostEntity(PostCreateRequestDto dto) {
return Post.builder()
.user(userService.getUser(dto.getUserId()))
.content(dto.getContent())
.title(dto.getTitle())
.memo(null)
.videoUrl(null)
.type(dto.getType()).build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.rising.backend.domain.post.dto.request;

import com.rising.backend.domain.post.domain.PostType;
import lombok.Getter;
import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.NotEmpty;

@Getter
public class PostCreateRequestDto {

@NotEmpty
private Long userId;

@NotEmpty
@Length(max = 100)
private String title;

@NotEmpty
private String content;

@NotEmpty
private PostType type;


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.rising.backend.domain.post.repository;

import com.rising.backend.domain.post.domain.Post;
import org.springframework.data.jpa.repository.JpaRepository;

public interface PostRepository extends JpaRepository<Post, Long> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.rising.backend.domain.post.service;

import com.rising.backend.domain.post.domain.Post;
import com.rising.backend.domain.post.dto.mapper.PostMapper;
import com.rising.backend.domain.post.dto.request.PostCreateRequestDto;
import com.rising.backend.domain.post.repository.PostRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class PostService {

private final PostRepository postRepository;
private final PostMapper postMapper;

public Post createPost(PostCreateRequestDto dto) {
return postRepository.save(postMapper.toPostEntity(dto));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@ public User register(UserRequest.CreateDto requestDto) {
user.setEncryptedPassword(passwordEncoder.encode(user.getPassword()));
return userRepository.save(user);
}

public User getUser(Long userId) {
return userRepository.findById(userId).orElseThrow();
}
}

0 comments on commit baab8ca

Please sign in to comment.