Skip to content

Commit

Permalink
Merge branch 'feature/blog' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
3un0ia committed May 21, 2024
2 parents c256cef + d335414 commit b298985
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package kr.tgwing.tech.blog.controller;

import kr.tgwing.tech.blog.dto.PostCreationDto;
import kr.tgwing.tech.blog.dto.PostDto;
import kr.tgwing.tech.blog.exception.PathHasNoPostIdException;
import kr.tgwing.tech.blog.service.PostServiceImpl;
Expand Down Expand Up @@ -61,7 +62,7 @@ public ResponseEntity<PostDto> getPost(@PathVariable(required = false, name = "p
}

@PostMapping("/blog") // 블로그 작성 - POST, /tgwing.kr/blog/post
public ResponseEntity<PostDto> post(@RequestBody PostDto requestDto,
public ResponseEntity<PostDto> post(@RequestBody PostCreationDto requestDto,
Principal principal)
{
System.out.println("-- Post new post --");
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/kr/tgwing/tech/blog/dto/PostCreationDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package kr.tgwing.tech.blog.dto;

import kr.tgwing.tech.blog.entity.PostEntity;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;

@Data
@Builder
@AllArgsConstructor
public class PostCreationDto {
private String title;
private String content;
private String thumbnail;

public static PostEntity toEntity(PostCreationDto dto) {
return PostEntity.builder()
.title(dto.title)
.content(dto.content)
.thumbnail(dto.thumbnail)
.build();
}

}
4 changes: 4 additions & 0 deletions src/main/java/kr/tgwing/tech/blog/entity/PostEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,8 @@ public void updateContent(PostDto postDto) {
this.content = postDto.getContent();
this.thumbnail = postDto.getThumbnail();
}

public void setWriter(Long writer) {
this.writer = writer;
}
}
3 changes: 2 additions & 1 deletion src/main/java/kr/tgwing/tech/blog/service/PostService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package kr.tgwing.tech.blog.service;

import kr.tgwing.tech.blog.dto.PostCreationDto;
import kr.tgwing.tech.blog.dto.PostDto;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
Expand All @@ -9,7 +10,7 @@

public interface PostService {
public PostDto getPost(Long postId);
public PostDto createPost(PostDto requestDto, String token);
public PostDto createPost(PostCreationDto requestDto, String token);
public PostDto updatePost(PostDto postDto, Long postId, String utilStudentId);
public void deletePost(Long postId, String utilStudentId);
public Page<PostDto> getPostsInPage(String text, Pageable pageable);
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/kr/tgwing/tech/blog/service/PostServiceImpl.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package kr.tgwing.tech.blog.service;


import kr.tgwing.tech.blog.dto.PostCreationDto;
import kr.tgwing.tech.blog.dto.PostDto;
import kr.tgwing.tech.blog.entity.PostEntity;
import kr.tgwing.tech.blog.exception.PostNotFoundException;
Expand Down Expand Up @@ -55,20 +56,20 @@ private Page<PostEntity> getAllPosts(PageRequest pageRequest) { // 모든 공지
}

@Override
public PostDto createPost(PostDto requestDto, String utilStudentId) // 공지 생성하기
public PostDto createPost(PostCreationDto requestDto, String utilStudentId) // 공지 생성하기
{
// 글을 작성할 user 조회
Optional<UserEntity> userById = userRepository.findById(requestDto.getWriter());
// // userEntity를 받아오지 못한 경우 - 회원을 찾을 수 없음 (Exception)
UserEntity userEntity = userById.orElseThrow(UserNotFoundException::new);
Optional<UserEntity> byStudentId = userRepository.findByStudentId(utilStudentId);
UserEntity userEntity = byStudentId.orElseThrow(UserNotFoundException::new);

// 현재 사용자와 요청 시 들어온 writer가 같은지 확인
if (!Objects.equals(userEntity.getStudentId(), utilStudentId)) {
throw new WrongPostRequestException();
}

// entity에는 저장된 썸네일의 uri만 추가로 넣어서 저장
PostEntity postEntity = toEntity(requestDto);
// entity에 작성자 Id 설정해서 저장하기
PostEntity postEntity = PostCreationDto.toEntity(requestDto);
postEntity.setWriter(userEntity.getId());
PostEntity savedEntity = postRepository.save(postEntity);

return toDto(savedEntity);
Expand Down

0 comments on commit b298985

Please sign in to comment.