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

Create CR(create, read) functions with Feat/posts #5

Merged
merged 3 commits into from
Jul 3, 2020
Merged
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
9 changes: 9 additions & 0 deletions src/main/java/pitapat/party/dongdong/config/JpaConfig.java
Original file line number Diff line number Diff line change
@@ -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 {
}
Copy link
Contributor

Choose a reason for hiding this comment

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

[suggestion]

  • 장기적으로 봤을때 JpaAuditing이 과연 좋은가에 대해서는 한번쯤 생각해볼만 할듯함.

Original file line number Diff line number Diff line change
@@ -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<PostsResponseDto> findAllDesc() {
return postsService.findAllDesc();
}
}
21 changes: 21 additions & 0 deletions src/main/java/pitapat/party/dongdong/domain/BaseTimeEntity.java
Original file line number Diff line number Diff line change
@@ -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;
}
44 changes: 44 additions & 0 deletions src/main/java/pitapat/party/dongdong/domain/posts/Posts.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<Posts, Long> {
@Query("SELECT p FROM Posts p ORDER BY p.id DESC")
List<Posts> findAllDesc();
}
30 changes: 30 additions & 0 deletions src/main/java/pitapat/party/dongdong/dto/PostsResponseDto.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
38 changes: 38 additions & 0 deletions src/main/java/pitapat/party/dongdong/dto/PostsSaveRequestDto.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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<PostsResponseDto> findAllDesc() {
return postsRepository.findAllDesc().stream()
.map(PostsResponseDto::new)
.collect(Collectors.toList());
}
}
6 changes: 5 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
spring.profiles.include=db-config, mail-config
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