-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} | ||
34 changes: 34 additions & 0 deletions
34
src/main/java/pitapat/party/dongdong/controller/posts/PostsApiController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
21
src/main/java/pitapat/party/dongdong/domain/BaseTimeEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
44
src/main/java/pitapat/party/dongdong/domain/posts/Posts.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/pitapat/party/dongdong/domain/posts/PostsRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
30
src/main/java/pitapat/party/dongdong/dto/PostsResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
38
src/main/java/pitapat/party/dongdong/dto/PostsSaveRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/pitapat/party/dongdong/service/posts/PostsService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[suggestion]