From 3a217df20dd3f3b0aa61d412d52ba46c0050235a Mon Sep 17 00:00:00 2001 From: SongJungwoo Date: Sat, 13 Jun 2020 15:54:51 +0900 Subject: [PATCH 1/3] feat:Make Jpa Configuration at config package I make Jpa Configuration explicitly at config package for using JPA --- .../java/pitapat/party/dongdong/config/JpaConfig.java | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 src/main/java/pitapat/party/dongdong/config/JpaConfig.java diff --git a/src/main/java/pitapat/party/dongdong/config/JpaConfig.java b/src/main/java/pitapat/party/dongdong/config/JpaConfig.java new file mode 100644 index 0000000..7ffdc30 --- /dev/null +++ b/src/main/java/pitapat/party/dongdong/config/JpaConfig.java @@ -0,0 +1,9 @@ +package pitapat.party.dongdong.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.data.jpa.repository.config.EnableJpaAuditing; + +@Configuration +@EnableJpaAuditing +public class JpaConfig { +} \ No newline at end of file From 9d407c4043608d94a0774b212beecc315e9cb126 Mon Sep 17 00:00:00 2001 From: SongJungwoo Date: Sat, 13 Jun 2020 15:57:36 +0900 Subject: [PATCH 2/3] feat: Make Posts schema with basetime entity I make Posts schema with JPA repository and basetime entity. You can see the blank for some columns, i will add it later. --- .../party/dongdong/domain/BaseTimeEntity.java | 21 +++++++++ .../party/dongdong/domain/posts/Posts.java | 44 +++++++++++++++++++ .../domain/posts/PostsRepository.java | 11 +++++ 3 files changed, 76 insertions(+) create mode 100644 src/main/java/pitapat/party/dongdong/domain/BaseTimeEntity.java create mode 100644 src/main/java/pitapat/party/dongdong/domain/posts/Posts.java create mode 100644 src/main/java/pitapat/party/dongdong/domain/posts/PostsRepository.java diff --git a/src/main/java/pitapat/party/dongdong/domain/BaseTimeEntity.java b/src/main/java/pitapat/party/dongdong/domain/BaseTimeEntity.java new file mode 100644 index 0000000..98f91fb --- /dev/null +++ b/src/main/java/pitapat/party/dongdong/domain/BaseTimeEntity.java @@ -0,0 +1,21 @@ +package pitapat.party.dongdong.domain; + +import lombok.Getter; +import org.springframework.data.annotation.CreatedDate; +import org.springframework.data.annotation.LastModifiedDate; +import org.springframework.data.jpa.domain.support.AuditingEntityListener; + +import javax.persistence.EntityListeners; +import javax.persistence.MappedSuperclass; +import java.time.LocalDateTime; + +@Getter +@MappedSuperclass +@EntityListeners(AuditingEntityListener.class) +public abstract class BaseTimeEntity { + @CreatedDate + private LocalDateTime createdDate; + + @LastModifiedDate + private LocalDateTime modifiedDate; +} diff --git a/src/main/java/pitapat/party/dongdong/domain/posts/Posts.java b/src/main/java/pitapat/party/dongdong/domain/posts/Posts.java new file mode 100644 index 0000000..83cb308 --- /dev/null +++ b/src/main/java/pitapat/party/dongdong/domain/posts/Posts.java @@ -0,0 +1,44 @@ +package pitapat.party.dongdong.domain.posts; + +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import pitapat.party.dongdong.domain.BaseTimeEntity; + +import javax.persistence.*; + +@Getter +@NoArgsConstructor +@Entity +public class Posts extends BaseTimeEntity { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column(length=50, nullable = false) + private String name; + + @Column(columnDefinition = "TEXT", nullable = false) + private String description; + + @Column(columnDefinition = "TEXT", nullable = false) + private String location; + + @Column(length=255, nullable = false) + private String owner; + + @Column(columnDefinition = "TEXT") + private String saleinfo; + + // To-Do + // Section, Image, category 추가 + + @Builder + public Posts(String name, String description, String location, String owner, String saleinfo) { + this.name = name; + this.description = description; + this.location = location; + this.owner = owner; + this.saleinfo = saleinfo; + } +} \ No newline at end of file diff --git a/src/main/java/pitapat/party/dongdong/domain/posts/PostsRepository.java b/src/main/java/pitapat/party/dongdong/domain/posts/PostsRepository.java new file mode 100644 index 0000000..4fbd08b --- /dev/null +++ b/src/main/java/pitapat/party/dongdong/domain/posts/PostsRepository.java @@ -0,0 +1,11 @@ +package pitapat.party.dongdong.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 { + @Query("SELECT p FROM Posts p ORDER BY p.id DESC") + List findAllDesc(); +} From ce61fea53db4b3b62d12612cec9eccc04e565d12 Mon Sep 17 00:00:00 2001 From: SongJungwoo Date: Sat, 13 Jun 2020 16:10:45 +0900 Subject: [PATCH 3/3] feat: Make PostsService, Api Controller and Dtos for service I make Posts service and some dtos for saving and reading. Also i make PostsApiController for CR(create, read). --- .../controller/posts/PostsApiController.java | 34 +++++++++++++++ .../party/dongdong/dto/PostsResponseDto.java | 30 +++++++++++++ .../dongdong/dto/PostsSaveRequestDto.java | 38 +++++++++++++++++ .../dongdong/service/posts/PostsService.java | 42 +++++++++++++++++++ src/main/resources/application.properties | 6 ++- 5 files changed, 149 insertions(+), 1 deletion(-) create mode 100644 src/main/java/pitapat/party/dongdong/controller/posts/PostsApiController.java create mode 100644 src/main/java/pitapat/party/dongdong/dto/PostsResponseDto.java create mode 100644 src/main/java/pitapat/party/dongdong/dto/PostsSaveRequestDto.java create mode 100644 src/main/java/pitapat/party/dongdong/service/posts/PostsService.java diff --git a/src/main/java/pitapat/party/dongdong/controller/posts/PostsApiController.java b/src/main/java/pitapat/party/dongdong/controller/posts/PostsApiController.java new file mode 100644 index 0000000..b7df9a8 --- /dev/null +++ b/src/main/java/pitapat/party/dongdong/controller/posts/PostsApiController.java @@ -0,0 +1,34 @@ +package pitapat.party.dongdong.controller.posts; + +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.*; + +import pitapat.party.dongdong.dto.PostsResponseDto; +import pitapat.party.dongdong.dto.PostsSaveRequestDto; +import pitapat.party.dongdong.service.posts.PostsService; + +import java.util.List; + +@RequiredArgsConstructor +@RestController +public class PostsApiController { + private final PostsService postsService; + + // Save Posts + @PostMapping("/api/v1/posts") + public Long save(@RequestBody PostsSaveRequestDto requestDto) { + return postsService.save(requestDto); + } + + // Get one Post by id + @GetMapping("/api/v1/posts/{id}") + public PostsResponseDto findById(@PathVariable Long id) { + return postsService.findById(id); + } + + // Get all posts ordered by desc + @GetMapping("/api/v1/posts") + public List findAllDesc() { + return postsService.findAllDesc(); + } +} diff --git a/src/main/java/pitapat/party/dongdong/dto/PostsResponseDto.java b/src/main/java/pitapat/party/dongdong/dto/PostsResponseDto.java new file mode 100644 index 0000000..7055863 --- /dev/null +++ b/src/main/java/pitapat/party/dongdong/dto/PostsResponseDto.java @@ -0,0 +1,30 @@ +package pitapat.party.dongdong.dto; + +import lombok.Getter; +import pitapat.party.dongdong.domain.posts.Posts; + +import java.time.LocalDateTime; + +@Getter +public class PostsResponseDto { + private Long id; + private String name; + private String description; + private String location; + private String owner; + private String saleinfo; + private LocalDateTime modifiedDate; + + // To-Do + // Section, Image, category 추가 + + public PostsResponseDto(Posts entity) { + this.id = entity.getId(); + this.name = entity.getName(); + this.description = entity.getDescription(); + this.location = entity.getLocation(); + this.owner = entity.getOwner(); + this.saleinfo = entity.getSaleinfo(); + this.modifiedDate = entity.getModifiedDate(); + } +} diff --git a/src/main/java/pitapat/party/dongdong/dto/PostsSaveRequestDto.java b/src/main/java/pitapat/party/dongdong/dto/PostsSaveRequestDto.java new file mode 100644 index 0000000..616d92d --- /dev/null +++ b/src/main/java/pitapat/party/dongdong/dto/PostsSaveRequestDto.java @@ -0,0 +1,38 @@ +package pitapat.party.dongdong.dto; + +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import pitapat.party.dongdong.domain.posts.Posts; + +@Getter +@NoArgsConstructor +public class PostsSaveRequestDto { + private String name; + private String description; + private String location; + private String owner; + private String saleinfo; + + // To-Do + // Section, Image, category 추가 + + @Builder + public PostsSaveRequestDto(String name, String description, String location, String owner, String saleinfo) { + this.name = name; + this.description = description; + this.location = location; + this.owner = owner; + this.saleinfo = saleinfo; + } + + public Posts toEntity() { + return Posts.builder() + .name(name) + .description(description) + .location(location) + .owner(owner) + .saleinfo(saleinfo) + .build(); + } +} diff --git a/src/main/java/pitapat/party/dongdong/service/posts/PostsService.java b/src/main/java/pitapat/party/dongdong/service/posts/PostsService.java new file mode 100644 index 0000000..4415b29 --- /dev/null +++ b/src/main/java/pitapat/party/dongdong/service/posts/PostsService.java @@ -0,0 +1,42 @@ +package pitapat.party.dongdong.service.posts; + +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import pitapat.party.dongdong.domain.posts.Posts; +import pitapat.party.dongdong.domain.posts.PostsRepository; +import pitapat.party.dongdong.dto.PostsResponseDto; +import pitapat.party.dongdong.dto.PostsSaveRequestDto; + +import java.util.List; +import java.util.stream.Collectors; + +@RequiredArgsConstructor +@Service +public class PostsService { + private final PostsRepository postsRepository; + + // Save Posts + @Transactional + public Long save(PostsSaveRequestDto requestDto) { + return postsRepository.save(requestDto.toEntity()).getId(); + } + + // Find Posts by post's id + @Transactional + public PostsResponseDto findById (Long id) { + System.out.println("id is " + id); + Posts entity = postsRepository.findById(id) + .orElseThrow(() -> new IllegalArgumentException("해당 포스트가 없습니다. id=" + id)); + + return new PostsResponseDto(entity); + } + + // Find all Posts order by desc + @Transactional(readOnly=true) + public List findAllDesc() { + return postsRepository.findAllDesc().stream() + .map(PostsResponseDto::new) + .collect(Collectors.toList()); + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 8bd3fa3..56c5290 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1 +1,5 @@ -spring.profiles.include=db-config, mail-config \ No newline at end of file +spring.profiles.include=db-config, mail-config + +spring.jpa.show_sql = true +spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect +spring.h2.console.enabled = true \ No newline at end of file