Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#63 - 게시글 기능 구현 #64

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,16 @@ repositories {
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'

implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'com.google.guava:guava:28.2-jre'
implementation 'org.apache.commons:commons-lang3:3.9'
implementation 'org.springframework.boot:spring-boot-starter-mail'
compileOnly group: 'javax.servlet', name: 'javax.servlet-api', version: '4.0.1'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'org.mariadb.jdbc:mariadb-java-client'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'jakarta.persistence:jakarta.persistence-api'
implementation 'jakarta.persistence:jakarta.persistence-api:3.1.0'
implementation 'io.jsonwebtoken:jjwt-api:0.11.5'
implementation 'org.springframework.security:spring-security-oauth2-core:5.7.3'
implementation 'io.jsonwebtoken:jjwt-impl:0.11.5'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2023-01-27T23:07:40+0900",
comments = "version: 1.4.2.Final, compiler: javac, environment: Java 17.0.5 (Amazon.com Inc.)"
date = "2023-01-29T22:01:20+0900",
comments = "version: 1.4.2.Final, compiler: javac, environment: Java 19.0.2 (Oracle Corporation)"
)
public class CommentInfoMapperImpl implements CommentInfoMapper {

Expand All @@ -20,6 +20,7 @@ public CommentInfo toCommentInfo(Comment comment) {

commentInfo.setCommentId( comment.getId() );
commentInfo.setCreatedAt( comment.getCreatedAt() );
commentInfo.setUserName( comment.getUserName() );
commentInfo.setContent( comment.getContent() );
commentInfo.setIsDeleted( comment.getIsDeleted() );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import site.moasis.monolithicbe.domain.article.entity.Article;
import site.moasis.monolithicbe.domain.article.repository.ArticleRepository;
import site.moasis.monolithicbe.domain.useraccount.UserRole;
import site.moasis.monolithicbe.domain.useraccount.entity.UserAccount;
import site.moasis.monolithicbe.domain.useraccount.repository.UserAccountRepository;
import site.moasis.monolithicbe.infrastructure.article.ArticleRepository;

@EnableJpaAuditing
@SpringBootApplication
Expand Down Expand Up @@ -43,6 +43,7 @@ public void run(ApplicationArguments args) {
.build());

articleRepository.save(Article.builder()
.title("게시글 예시 제목")
.content("게시글 예시 내용")
.build());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,28 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.SecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.DefaultSecurityFilterChain;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.web.filter.CorsFilter;
import site.moasis.monolithicbe.domain.useraccount.AccessTokenManager;
import site.moasis.monolithicbe.domain.useraccount.JwtAccessDeniedHandler;
import site.moasis.monolithicbe.domain.useraccount.JwtAuthenticationEntryPoint;
import site.moasis.monolithicbe.domain.useraccount.service.PrincipalOauth2UserService;

import site.moasis.monolithicbe.domain.useraccount.AccessTokenManager;

@RequiredArgsConstructor
@EnableWebSecurity
@Configuration
public class SecurityConfig {

public class SecurityConfig extends SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSecurity> {
private final CorsFilter corsFilter;
private final JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint;
private final JwtAccessDeniedHandler jwtAccessDeniedHandler;
private final AccessTokenManager accessTokenManager;
private final PrincipalOauth2UserService principalOauth2UserService;

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
Expand All @@ -44,19 +42,12 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)

.and().authorizeHttpRequests()
.requestMatchers(HttpMethod.POST, "/users").permitAll()
.requestMatchers(HttpMethod.POST,"/users").permitAll()
.requestMatchers("/users/signin").permitAll()
.requestMatchers("/users/**").permitAll()
.anyRequest().permitAll()

.and()
.apply(new JwtSecurityConfig(accessTokenManager))

.and()
.oauth2Login()
.defaultSuccessUrl("/users/authorization/success")
.userInfoEndpoint()
.userService(principalOauth2UserService);
.apply(new JwtSecurityConfig(accessTokenManager));

return http.build();
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package site.moasis.monolithicbe.controller.article;


import io.swagger.v3.oas.annotations.Operation;
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.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import site.moasis.monolithicbe.controller.common.CommonResponse;
import site.moasis.monolithicbe.domain.article.service.ArticleReadService;

import java.util.UUID;


@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1")

@Tag(name = "ArticleReadController", description = "게시글을 각 속성 값으로 조회 가능하다")
public class ArticleReadController {

private final ArticleReadService articleReadService;

@GetMapping("/articles")
@Operation(summary = "게시글 전체 조회")
public ResponseEntity<CommonResponse<?>> searchAll() {
var articleInfo = articleReadService.searchArticles();
return ResponseEntity
.status(HttpStatus.OK)
.body(CommonResponse.success(articleInfo, "게시글 검색 결과"));
}

@GetMapping("/articles/{articleId}")
@Operation(summary = "article_id로 게시글 단건 조회")
public ResponseEntity<CommonResponse<?>> searchOne(@PathVariable UUID articleId) {
var articleInfo = articleReadService.searchOne(articleId);
return ResponseEntity
.status(HttpStatus.OK)
.body(CommonResponse.success(articleInfo, "게시글 검색 결과"));
}

@GetMapping("/articles/userId/{userId}")
@Operation(summary = "user_id로 게시글 조회")
public ResponseEntity<CommonResponse<?>> searchByUserId(@PathVariable UUID userId) {
var articleInfo = articleReadService.searchByUserId(userId);
return ResponseEntity
.status(HttpStatus.OK)
.body(CommonResponse.success(articleInfo, userId + "검색 결과"));
}

@GetMapping("/articles/title/{title}")
@Operation(summary = "제목으로 게시글 조회")
public ResponseEntity<CommonResponse<?>> searchByTitle(@PathVariable String title) {
var articleInfo = articleReadService.searchBytitle(title);
return ResponseEntity
.status(HttpStatus.OK)
.body(CommonResponse.success(articleInfo, title + "검색 결과"));
}

@GetMapping("/articles/content/{content}")
@Operation(summary = "내용으로 게시글 조회")
public ResponseEntity<CommonResponse<?>> searchByContent(@PathVariable String content) {
var articleInfo = articleReadService.searchBycontent(content);
return ResponseEntity
.status(HttpStatus.OK)
.body(CommonResponse.success(articleInfo, content + "검색 결과"));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package site.moasis.monolithicbe.controller.article;


import io.swagger.v3.oas.annotations.Operation;
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.*;
import site.moasis.monolithicbe.controller.common.CommonResponse;
import site.moasis.monolithicbe.domain.article.dto.ArticleRequest;
import site.moasis.monolithicbe.domain.article.service.ArticleWriteService;

import java.util.UUID;

import static site.moasis.monolithicbe.domain.article.service.ArticleCommand.CreateCommand;


@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1")

@Tag(name = "ArticleWriteController", description = "게시글을 각 속성 값으로 생성, 삭제, 수정 가능하다")
public class ArticleWriteController {

private final ArticleWriteService articleWriteService;

@PostMapping("/article")
@Operation(summary = "게시글 생성")
public ResponseEntity<CommonResponse<?>> createArticle(
@RequestBody ArticleRequest articleRequest
) {
var command = CreateCommand
.builder()
.title(articleRequest.title())
.content(articleRequest.content())
.build();
var articleInfo = articleWriteService.createArticle(command);

return ResponseEntity.status(HttpStatus.CREATED).body(CommonResponse.success(articleInfo, "게시글 생성 완료"));
}

@PostMapping("/article/{articleId}")
@Operation(summary = "게시글 업데이트")
public ResponseEntity<CommonResponse<String>> updateArticle(
@PathVariable UUID articleId,
@RequestBody ArticleRequest articleRequest
) {
articleWriteService.updateArticle(articleId, articleRequest);
return ResponseEntity.status(HttpStatus.OK).body(CommonResponse.success("게시글 업데이트 완료"));
}

@DeleteMapping("/article/{articleId}")
@Operation(summary = "게시글 삭제")
public ResponseEntity<CommonResponse<?>> deleteArticle(@PathVariable UUID articleId) {
articleWriteService.deleteOne(articleId);
return ResponseEntity.status(HttpStatus.OK).body(CommonResponse.success("게시글 삭제 완료"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,33 @@
import org.springframework.web.bind.annotation.RestController;
import site.moasis.monolithicbe.controller.common.CommonResponse;
import site.moasis.monolithicbe.service.CommentReadService;

import site.moasis.monolithicbe.service.CommentWriteService;
import java.util.UUID;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/articles/{articleId}/comments")
@RequestMapping("/api/v1/users/{userId}/articles/{articleId}") // todo: 스프링 시큐리티 기능 완성 후 유저 정보 가져올 수 있을 때 "/users/{userId}" 삭제
@Tag(name = "CommentController", description = "댓글 조회 기능")
public class CommentReadController {

private final CommentReadService readService;
private final CommentWriteService writeService;

@GetMapping("/users/{userId}")
@Operation(summary = "user_id를 통한 댓글 조회")
public ResponseEntity<CommonResponse<?>> findByUser(@PathVariable UUID userId) {
var commentInfo = readService.selectByUser(userId);
@GetMapping("/comments")
@Operation(summary = "댓글 전체 조회")
public ResponseEntity<CommonResponse<?>> findAll() {
var commentInfo = readService.selectAll();
return ResponseEntity
.status(HttpStatus.OK)
.body(CommonResponse.success(commentInfo, "Id가 " + userId + "인 유저가 작성한 댓글 조회 성공"));
.body(CommonResponse.success(commentInfo, "전체 댓글 조회 성공"));
}

@GetMapping
@Operation(summary = "article_id를 통한 댓글 조회")
public ResponseEntity<CommonResponse<?>> findByArticle(@PathVariable UUID articleId) {
var commentInfo = readService.selectByArticle(articleId);
@GetMapping("/comments/{commentId}")
@Operation(summary = "댓글 단건 조회")
public ResponseEntity<CommonResponse<?>> findOne(@PathVariable("commentId") UUID commentId) {
var commentInfo = readService.selectOne(commentId);
return ResponseEntity
.status(HttpStatus.OK)
.body(CommonResponse.success(commentInfo, articleId + "번 게시글 댓글 조회 성공"));
.body(CommonResponse.success(commentInfo, "댓글 조회 성공"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,31 @@
import org.springframework.web.bind.annotation.*;
import site.moasis.monolithicbe.controller.common.CommonResponse;
import site.moasis.monolithicbe.service.CommentCommand;
import site.moasis.monolithicbe.service.CommentReadService;
import site.moasis.monolithicbe.service.CommentWriteService;

import java.util.UUID;

import static site.moasis.monolithicbe.service.CommentCommand.RegisterCommentCommand;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/articles/{articleId}/comments")
@RequestMapping("/api/v1/users/{userId}/articles/{articleId}") // todo: 스프링 시큐리티 기능 완성 후 유저 정보 가져올 수 있을 때 "/users/{userId}" 삭제
@Tag(name = "CommentController", description = "댓글 생성, 삭제 기능")
public class CommentWriteController {
private final CommentReadService readService;
private final CommentWriteService writeService;

@PostMapping
@PostMapping("/comments")
@Operation(summary = "댓글 생성")
public ResponseEntity<CommonResponse<?>> registerComment(
@PathVariable UUID userId,
@PathVariable UUID articleId,
@RequestBody RegisterCommentRequest registerCommentRequest) {

UUID loginUserId = userId;

var command = RegisterCommentCommand
.builder()
.userId(loginUserId)
.articleId(articleId)
.content(registerCommentRequest.getContent())
.build();
Expand All @@ -43,13 +47,17 @@ public ResponseEntity<CommonResponse<?>> registerComment(
.body(CommonResponse.success(commentInfo, "댓글 생성 완료"));
}

@DeleteMapping("/{commentId}")
@DeleteMapping("/comments/{commentId}")
@Operation(summary = "댓글 삭제")
public ResponseEntity<CommonResponse<?>> deleteComment(
@PathVariable("userId") UUID userId,
@PathVariable("articleId") UUID articleId,
@PathVariable("commentId") UUID commentId) {

UUID loginUserId = userId;

CommentCommand.DeleteCommentCommand command = CommentCommand.DeleteCommentCommand.builder()
.userId(loginUserId)
.articleId(articleId)
.commentId(commentId)
.build();
Expand All @@ -60,18 +68,6 @@ public ResponseEntity<CommonResponse<?>> deleteComment(
.body(CommonResponse.success(command, command.getCommentId() + "댓글 삭제 성공"));
}

@PostMapping("/{commentId}")
@Operation(summary = "댓글 좋아요 또는 취소")
public ResponseEntity<CommonResponse<?>> likeComment(
@PathVariable("articleId") UUID articleId,
@PathVariable("commentId") UUID commentId
) {
Boolean commentInfo = writeService.likeComment(articleId, commentId);
return ResponseEntity
.status(HttpStatus.OK)
.body(CommonResponse.success(commentInfo, "현재 상태 : " + commentInfo));
}

@Getter
@EqualsAndHashCode
@NoArgsConstructor
Expand Down
Loading