Skip to content

Commit

Permalink
게시물 CRUD 기능구현 [완료]
Browse files Browse the repository at this point in the history
  • Loading branch information
softwareyong committed Nov 26, 2023
1 parent fd33360 commit dfc78af
Show file tree
Hide file tree
Showing 27 changed files with 784 additions and 0 deletions.
6 changes: 6 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ repositories {
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-mustache'
implementation 'org.springframework.boot:spring-boot-starter-web'

implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'com.h2database:h2'

testImplementation 'junit:junit:4.13.1'
testImplementation 'junit:junit:4.13.1'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/book/book_aws/BookAwsApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

@EnableJpaAuditing // JPA Auditing 활성화
@SpringBootApplication
public class BookAwsApplication {

Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/book/book_aws/controller/HelloController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.book.book_aws.controller;


import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.service.annotation.GetExchange;

@RestController
public class HelloController {

@GetMapping("/hello")
public String hello(){
return "hello";
}
}
35 changes: 35 additions & 0 deletions src/main/java/com/book/book_aws/controller/IndexController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.book.book_aws.controller;

import com.book.book_aws.dto.PostsResponseDto;
import com.book.book_aws.service.PostsService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@RequiredArgsConstructor
@Controller
public class IndexController {

private final PostsService postsService;

@GetMapping("/")
public String index(Model model) {
model.addAttribute("posts", postsService.findAllDesc());
return "index";
}

@GetMapping("/posts/save")
public String postsSave() {
return "posts-save";
}

@GetMapping("/posts/update/{id}")
public String postsUpdate(@PathVariable Long id, Model model) {
PostsResponseDto dto = postsService.findById(id);
model.addAttribute("post", dto);

return "posts-update";
}
}
37 changes: 37 additions & 0 deletions src/main/java/com/book/book_aws/controller/PostsApiController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.book.book_aws.controller;

import com.book.book_aws.dto.PostsResponseDto;
import com.book.book_aws.dto.PostsSaveRequestDto;
import com.book.book_aws.dto.PostsUpdateRequestDto;
import com.book.book_aws.service.PostsService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RequiredArgsConstructor
@RestController
public class PostsApiController {

private final PostsService postsService;

@PostMapping("/api/v1/posts")
public Long save(@RequestBody PostsSaveRequestDto requestDto) {
return postsService.save(requestDto);
}

@PutMapping("/api/v1/posts/{id}")
public Long update(@PathVariable Long id, @RequestBody PostsUpdateRequestDto requestDto) {
return postsService.update(id, requestDto);
}

@GetMapping("/api/v1/posts/{id}")
public PostsResponseDto findById(@PathVariable Long id) {
return postsService.findById(id);
}

@DeleteMapping("/api/v1/posts/{id}")
public Long delete(@PathVariable Long id) {
postsService.delete(id);
return id;
}

}
23 changes: 23 additions & 0 deletions src/main/java/com/book/book_aws/domain/BaseTimeEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.book.book_aws.domain;

import jakarta.persistence.EntityListeners;
import jakarta.persistence.MappedSuperclass;
import lombok.Getter;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.LocalDateTime;

@Getter
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class BaseTimeEntity {

@CreatedDate
private LocalDateTime createdDate;

@LastModifiedDate
private LocalDateTime modifiedDate;

}
50 changes: 50 additions & 0 deletions src/main/java/com/book/book_aws/domain/posts/Posts.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.book.book_aws.domain.posts;

import com.book.book_aws.domain.BaseTimeEntity;
import jakarta.persistence.*;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
@Entity
public class Posts extends BaseTimeEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY) // 오토 인크리션
private Long id;

@Column(length = 500, nullable = false) // 사용하지 않아도 되지만
private String title;

@Column(columnDefinition = "TEXT", nullable = false)
private String content;

private String author;

@Builder
public Posts(String title, String content, String author) {
this.title = title;
this.content = content;
this.author = author;
}

public void update(String title, String content) {
this.title = title;
this.content = content;
}

}












13 changes: 13 additions & 0 deletions src/main/java/com/book/book_aws/domain/posts/PostsRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.book.book_aws.domain.posts;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface PostsRepository extends JpaRepository<Posts, Long> {

@Query("SELECT p FROM Posts p ORDER BY p.id DESC")
List<Posts> findAllDesc();

}
13 changes: 13 additions & 0 deletions src/main/java/com/book/book_aws/dto/HelloResponseDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.book.book_aws.dto;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public class HelloResponseDto {

private final String name;
private final int amount;

}
21 changes: 21 additions & 0 deletions src/main/java/com/book/book_aws/dto/PostsListResponseDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.book.book_aws.dto;

import com.book.book_aws.domain.posts.Posts;
import lombok.Getter;

import java.time.LocalDateTime;

@Getter
public class PostsListResponseDto {
private Long id;
private String title;
private String author;
private LocalDateTime modifiedDate;

public PostsListResponseDto(Posts entity) {
this.id = entity.getId();
this.title = entity.getTitle();
this.author = entity.getAuthor();
this.modifiedDate = entity.getModifiedDate();
}
}
20 changes: 20 additions & 0 deletions src/main/java/com/book/book_aws/dto/PostsResponseDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.book.book_aws.dto;

import com.book.book_aws.domain.posts.Posts;
import lombok.Getter;

@Getter
public class PostsResponseDto {

private Long id;
private String title;
private String content;
private String author;

public PostsResponseDto(Posts entity) {
this.id = entity.getId();
this.title = entity.getTitle();
this.content = entity.getContent();
this.author = entity.getAuthor();
}
}
29 changes: 29 additions & 0 deletions src/main/java/com/book/book_aws/dto/PostsSaveRequestDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.book.book_aws.dto;

import com.book.book_aws.domain.posts.Posts;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
public class PostsSaveRequestDto {
private String title;
private String content;
private String author;

@Builder
public PostsSaveRequestDto(String title, String content, String author) {
this.title = title;
this.content = content;
this.author = author;
}

public Posts toEntity() {
return Posts.builder()
.title(title)
.content(content)
.author(author)
.build();
}
}
20 changes: 20 additions & 0 deletions src/main/java/com/book/book_aws/dto/PostsUpdateRequestDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.book.book_aws.dto;

import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
public class PostsUpdateRequestDto {
private String title;
private String content;


@Builder
public PostsUpdateRequestDto(String title, String content) {
this.title = title;
this.content = content;
}

}
64 changes: 64 additions & 0 deletions src/main/java/com/book/book_aws/service/PostsService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.book.book_aws.service;

import com.book.book_aws.domain.posts.Posts;
import com.book.book_aws.domain.posts.PostsRepository;
import com.book.book_aws.dto.PostsListResponseDto;
import com.book.book_aws.dto.PostsResponseDto;
import com.book.book_aws.dto.PostsSaveRequestDto;
import com.book.book_aws.dto.PostsUpdateRequestDto;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Service
public class PostsService {

private final PostsRepository postsRepository;

@Transactional
public Long save(PostsSaveRequestDto requestDto) {
return postsRepository.save(requestDto.toEntity()).getId();
}

@Transactional
public Long update(Long id, PostsUpdateRequestDto requestDto) {
Posts posts = postsRepository.findById(id)
.orElseThrow(() -> new IllegalArgumentException("해당 사용자가 없습니다. id=" + id));

posts.update(requestDto.getTitle(), requestDto.getContent());

return id;
}


@Transactional(readOnly = true)
public PostsResponseDto findById(Long id) {
Posts entity = postsRepository.findById(id)
.orElseThrow(() -> new IllegalArgumentException("해당 사용자가 없습니다. id=" + id));

return new PostsResponseDto(entity);
}


@Transactional
public void delete (Long id) {
Posts posts = postsRepository.findById(id)
.orElseThrow(() -> new IllegalArgumentException("해당 게시글이 없습니다. id=" + id));

postsRepository.delete(posts);
}

@Transactional(readOnly = true)
public List<PostsListResponseDto> findAllDesc() {
return postsRepository.findAllDesc().stream()
.map(PostsListResponseDto::new)
.collect(Collectors.toList());
}



}
6 changes: 6 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
spring.jpa.show-sql=true
#spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
spring.datasource.url=jdbc:h2:mem:testdb

server.servlet.encoding.force-response=true
Loading

0 comments on commit dfc78af

Please sign in to comment.