Skip to content
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: 0 additions & 5 deletions src/main/java/com/blog/domain/comment/Comment.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.blog.domain.comment.controller;

import com.blog.domain.comment.dto.CommentRequest;
import com.blog.domain.comment.dto.CommentResponse;
import com.blog.domain.comment.service.CommentService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/comments")
public class CommentController {
private final CommentService commentService;

public CommentController(CommentService commentService) {
this.commentService = commentService;
}

@PostMapping
public ResponseEntity<Map<String, String>> createComment(@RequestBody CommentRequest request) {
commentService.createComment(request);
Map<String, String> response = new HashMap<>();
response.put("message", "댓글 생성 성공");
return ResponseEntity.ok(response);
}

@GetMapping("/{id}")
public ResponseEntity<CommentResponse> getComment(@PathVariable UUID id) {
return ResponseEntity.ok(commentService.getComment(id));
}

@GetMapping("/post/{postId}")
public ResponseEntity<List<CommentResponse>> getCommentsByPostId(@PathVariable UUID postId) {
return ResponseEntity.ok(commentService.getCommentsByPostId(postId));
}

@PutMapping("/{id}")
public ResponseEntity<Map<String, String>> updateComment(@PathVariable UUID id, @RequestBody CommentRequest request) {
commentService.updateComment(id, request);
Map<String, String> response = new HashMap<>();
response.put("message", "댓글 수정 성공");
return ResponseEntity.ok(response);
}

@DeleteMapping("/{id}")
public ResponseEntity<Map<String, String>> deleteComment(@PathVariable UUID id) {
commentService.deleteComment(id);
Map<String, String> response = new HashMap<>();
response.put("message", "댓글 삭제 성공");
return ResponseEntity.ok(response);
}
}
51 changes: 51 additions & 0 deletions src/main/java/com/blog/domain/comment/domain/Comment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.blog.domain.comment.domain;

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

public class Comment {
private UUID id;
private String content;
private UUID authorId;
private UUID postId;
private LocalDateTime createdAt;
private LocalDateTime updatedAt;

public Comment(UUID id, String content, UUID authorId, UUID postId, LocalDateTime createdAt, LocalDateTime updatedAt) {
this.id = id;
this.content = content;
this.authorId = authorId;
this.postId = postId;
this.createdAt = createdAt;
this.updatedAt = updatedAt;
}

public UUID getId() {
return id;
}

public String getContent() {
return content;
}

public UUID getAuthorId() {
return authorId;
}

public UUID getPostId() {
return postId;
}

public LocalDateTime getCreatedAt() {
return createdAt;
}

public LocalDateTime getUpdatedAt() {
return updatedAt;
}

public void updateContent(String content) {
this.content = content;
this.updatedAt = LocalDateTime.now();
}
}
21 changes: 21 additions & 0 deletions src/main/java/com/blog/domain/comment/dto/CommentRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.blog.domain.comment.dto;

import java.util.UUID;

public class CommentRequest {
private String content;
private UUID postId;
private UUID authorId;

public String getContent() {
return content;
}

public UUID getPostId() {
return postId;
}

public UUID getAuthorId() {
return authorId;
}
}
58 changes: 58 additions & 0 deletions src/main/java/com/blog/domain/comment/dto/CommentResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.blog.domain.comment.dto;

import com.blog.domain.comment.domain.Comment;
import java.time.LocalDateTime;
import java.util.UUID;

public class CommentResponse {
private UUID id;
private String content;
private UUID authorId;
private UUID postId;
private LocalDateTime createdAt;
private LocalDateTime updatedAt;

public CommentResponse(UUID id, String content, UUID authorId, UUID postId, LocalDateTime createdAt, LocalDateTime updatedAt) {
this.id = id;
this.content = content;
this.authorId = authorId;
this.postId = postId;
this.createdAt = createdAt;
this.updatedAt = updatedAt;
}

public static CommentResponse from(Comment comment) {
return new CommentResponse(
comment.getId(),
comment.getContent(),
comment.getAuthorId(),
comment.getPostId(),
comment.getCreatedAt(),
comment.getUpdatedAt()
);
}

public UUID getId() {
return id;
}

public String getContent() {
return content;
}

public UUID getAuthorId() {
return authorId;
}

public UUID getPostId() {
return postId;
}

public LocalDateTime getCreatedAt() {
return createdAt;
}

public LocalDateTime getUpdatedAt() {
return updatedAt;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.blog.domain.comment.repository;

import com.blog.domain.comment.domain.Comment;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Repository
public class CommentRepository {
private final JdbcTemplate jdbcTemplate;

public CommentRepository(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}

public void save(Comment comment) {
String sql = "INSERT INTO comment (id, content, author_id, post_id, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?)";
jdbcTemplate.update(sql,
comment.getId().toString(),
comment.getContent(),
comment.getAuthorId().toString(),
comment.getPostId().toString(),
Timestamp.valueOf(comment.getCreatedAt()),
Timestamp.valueOf(comment.getUpdatedAt())
);
}

public Optional<Comment> findById(UUID id) {
String sql = "SELECT * FROM comment WHERE id = ?";
List<Comment> result = jdbcTemplate.query(sql, commentRowMapper(), id.toString());
return result.stream().findFirst();
}

public List<Comment> findByPostId(UUID postId) {
String sql = "SELECT * FROM comment WHERE post_id = ?";
return jdbcTemplate.query(sql, commentRowMapper(), postId.toString());
}

public void update(Comment comment) {
String sql = "UPDATE comment SET content = ?, updated_at = ? WHERE id = ?";
jdbcTemplate.update(sql,
comment.getContent(),
Timestamp.valueOf(comment.getUpdatedAt()),
comment.getId().toString()
);
}

public void deleteById(UUID id) {
String sql = "DELETE FROM comment WHERE id = ?";
jdbcTemplate.update(sql, id.toString());
}

private RowMapper<Comment> commentRowMapper() {
return (rs, rowNum) -> new Comment(
UUID.fromString(rs.getString("id")),
rs.getString("content"),
UUID.fromString(rs.getString("author_id")),
UUID.fromString(rs.getString("post_id")),
rs.getTimestamp("created_at").toLocalDateTime(),
rs.getTimestamp("updated_at").toLocalDateTime()
);
}

public List<Comment> findByAuthorId(UUID authorId) {
String sql = "SELECT * FROM comment WHERE author_id = ?";
return jdbcTemplate.query(sql, commentRowMapper(), authorId.toString());
}

}
65 changes: 65 additions & 0 deletions src/main/java/com/blog/domain/comment/service/CommentService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.blog.domain.comment.service;

import com.blog.domain.comment.domain.Comment;
import com.blog.domain.comment.dto.CommentRequest;
import com.blog.domain.comment.dto.CommentResponse;
import com.blog.domain.comment.repository.CommentRepository;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
public class CommentService {
private final CommentRepository commentRepository;

public CommentService(CommentRepository commentRepository) {
this.commentRepository = commentRepository;
}

public void createComment(CommentRequest request) {
Comment comment = new Comment(
UUID.randomUUID(),
request.getContent(),
request.getAuthorId(),
request.getPostId(),
LocalDateTime.now(),
LocalDateTime.now()
);
commentRepository.save(comment);
}

public CommentResponse getComment(UUID id) {
Comment comment = commentRepository.findById(id)
.orElseThrow(() -> new RuntimeException("댓글을 찾을 수 없습니다."));
return CommentResponse.from(comment);
}

public List<CommentResponse> getCommentsByPostId(UUID postId) {
return commentRepository.findByPostId(postId)
.stream()
.map(CommentResponse::from)
.collect(Collectors.toList());
}

public void updateComment(UUID id, CommentRequest request) {
Comment comment = commentRepository.findById(id)
.orElseThrow(() -> new RuntimeException("댓글을 찾을 수 없습니다."));
comment.updateContent(request.getContent());
commentRepository.update(comment);
}

public void deleteComment(UUID id) {
commentRepository.deleteById(id);
}

public List<CommentResponse> getCommentsByAuthor(UUID authorId) {
return commentRepository.findByAuthorId(authorId)
.stream()
.map(CommentResponse::from)
.collect(Collectors.toList());
}

}
5 changes: 0 additions & 5 deletions src/main/java/com/blog/domain/image/Image.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.blog.domain.image.controller;

import com.blog.domain.image.dto.ImageResponse;
import com.blog.domain.image.service.ImageService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/images")
public class ImageController {
private final ImageService imageService;

public ImageController(ImageService imageService) {
this.imageService = imageService;
}

@PostMapping("/upload")
public ResponseEntity<ImageResponse> upload(@RequestParam("file") MultipartFile file) {
ImageResponse response = imageService.upload(file);
return ResponseEntity.ok(response);
}
}
Loading