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

[feat] post 상세보기 api #27

Merged
merged 7 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,4 @@ gradle-app.setting
# End of https://www.toptal.com/developers/gitignore/api/macos,intellij,windows,java,gradle

src/main/resources/application-secret.yml
src/main/resources/application-test.yml

This file was deleted.

This file was deleted.

This file was deleted.

10 changes: 10 additions & 0 deletions src/main/java/wanted/media/exception/NotFoundException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package wanted.media.exception;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public class NotFoundException extends RuntimeException {
private final ErrorCode errorCode;
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
package wanted.media.exception.handler;

import org.apache.coyote.BadRequestException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import wanted.media.exception.ErrorCode;
import wanted.media.exception.ErrorResponse;
import wanted.media.exception.NotFoundException;

@RestControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler(BadRequestException.class)
public ResponseEntity<ErrorResponse> handleBadRequestException(BadRequestException e) {
return ResponseEntity.badRequest()
.body(new ErrorResponse(400, e.getMessage()));
}
@ExceptionHandler(BadRequestException.class)
public ResponseEntity<ErrorResponse> handleBadRequestException(BadRequestException e) {
return ResponseEntity.badRequest()
.body(new ErrorResponse(400, e.getMessage()));
}

@ExceptionHandler(NotFoundException.class)
public ResponseEntity<ErrorResponse> handlePostNotFound(NotFoundException e) {
ErrorCode errorCode = e.getErrorCode();
ErrorResponse errorResponse = new ErrorResponse(
errorCode.getStatus().value(),
errorCode.getMessage()
);
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(errorResponse);
}
}
44 changes: 44 additions & 0 deletions src/main/java/wanted/media/post/controller/PostController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package wanted.media.post.controller;

import lombok.RequiredArgsConstructor;
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 wanted.media.post.domain.Post;
import wanted.media.post.dto.PostDetailResponse;
import wanted.media.post.service.PostService;

@RestController
@RequestMapping("/api/posts")
@RequiredArgsConstructor
public class PostController {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

불필요한 공백 제거해주세요~!

private final PostService posetService;

/**
* @param postId
* @return PostDetailResponse
*/
@GetMapping("/{postId}")
public ResponseEntity<PostDetailResponse> getPost(@PathVariable String postId) {
Post post = posetService.getPost(postId);
PostDetailResponse result = PostDetailResponse.builder()
.postId(post.getId())
.likeCount(post.getLikeCount())
.type(post.getType())
.title(post.getTitle())
.post(post.getPost())
.hashtags(post.getHashtags())
.viewCount(post.getViewCount())
.shareCount(post.getShareCount())
.updatedAt(post.getUpdatedAt())
.createdAt(post.getCreatedAt())
.userId(post.getUser().getUserId())
.account(post.getUser().getAccount())
.email(post.getUser().getEmail())
.build();
return ResponseEntity.ok(result);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package wanted.media.content.domain;
package wanted.media.post.domain;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.*;
import org.hibernate.annotations.ColumnDefault;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
Expand All @@ -13,19 +14,15 @@

@Entity
@Getter
@Table(name = "contents")
@Table(name = "posts")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
@Builder
@EntityListeners(AuditingEntityListener.class)
public class Content {
public class Post {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "content_id", nullable = false)
private Long id;

@Column(name = "like_count")
private Long likeCount;
@Column(name = "post_id", nullable = false)
private String id;

@Enumerated(EnumType.STRING)
@Column(nullable = false)
Expand All @@ -35,12 +32,16 @@ public class Content {
@Column(nullable = false)
private String title;

private String content;

private String post;
private String hashtags;

@ColumnDefault("0")
private Long likeCount;

@ColumnDefault("0")
private Long viewCount;

@ColumnDefault("0")
private Long shareCount;

@LastModifiedDate
Expand All @@ -54,4 +55,10 @@ public class Content {
@NotNull
private User user;

public void incrementViewCount() {
if (this.viewCount == null) {
this.viewCount = 0L;
}
this.viewCount += 1;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package wanted.media.content.domain;
package wanted.media.post.domain;

public enum Type {
FACEBOOK, TWITTER, INSTAGRAM, THREADS;
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/wanted/media/post/dto/PostDetailResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package wanted.media.post.dto;

import lombok.Builder;
import wanted.media.post.domain.Type;

import java.time.LocalDateTime;
import java.util.UUID;

@Builder
public record PostDetailResponse(
String postId,
Type type,
String title,
String post,
String hashtags,
Long likeCount,
Long viewCount,
Long shareCount,
LocalDateTime updatedAt,
LocalDateTime createdAt,
UUID userId,
String account,
String email
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package wanted.media.post.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import wanted.media.post.domain.Post;

public interface PostRepository extends JpaRepository<Post, String> {
}
24 changes: 24 additions & 0 deletions src/main/java/wanted/media/post/service/PostService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package wanted.media.post.service;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import wanted.media.exception.ErrorCode;
import wanted.media.exception.NotFoundException;
import wanted.media.post.domain.Post;
import wanted.media.post.repository.PostRepository;

@Service
@RequiredArgsConstructor
public class PostService {
private final PostRepository postRepository;

@Transactional
public Post getPost(String postId) {
Post post = postRepository.findById(postId)
.orElseThrow(() -> new NotFoundException(ErrorCode.ENTITY_NOT_FOUND));

post.incrementViewCount();
return post;
}
}
31 changes: 30 additions & 1 deletion src/main/java/wanted/media/user/config/SecurityConfig.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,36 @@
package wanted.media.user.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.security.servlet.PathRequest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
import org.springframework.security.web.SecurityFilterChain;


@EnableWebSecurity
@Configuration
public class SecurityConfig {
}

/**
* 테스트용 메서드
*/
@Bean
@ConditionalOnProperty(name = "spring.h2.console.enabled", havingValue = "true")
public WebSecurityCustomizer configureH2ConsoleEnable() {
return web -> web.ignoring()
.requestMatchers(PathRequest.toH2Console());
}

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http.authorizeHttpRequests(
request -> request.requestMatchers("/**").permitAll()
.anyRequest().authenticated())
.csrf(csrf -> csrf.disable());

return http.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {

private final UserService userService;
}
1 change: 0 additions & 1 deletion src/main/java/wanted/media/user/domain/Code.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
@Entity
@Table(name = "codes")
public class Code {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(nullable = false)
Expand Down
1 change: 0 additions & 1 deletion src/main/java/wanted/media/user/domain/Token.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
@Entity
@Table(name = "tokens")
public class Token {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(nullable = false)
Expand Down
1 change: 0 additions & 1 deletion src/main/java/wanted/media/user/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@
@Service
@RequiredArgsConstructor
public class UserService {

private final UserRepository userRepository;
}
21 changes: 21 additions & 0 deletions src/main/resources/application-dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# application-test
spring:
datasource:
driver-class-name: org.h2.Driver
url: jdbc:h2:mem:db;MODE=MYSQL
username: sa
password:
h2:
console:
enabled: true
path: /h2-console
jpa:
show-sql: true
properties:
hibernate:
format_sql: true
hibernate:
ddl-auto: update

jwt:
secret_key: key
Loading