-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/#26-사용자-매물-등록-기능-구현' of https://github.com/h-jj…
…ang/bauction into feature/#34-매물-검색-기능-구현
- Loading branch information
Showing
11 changed files
with
230 additions
and
6 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
backend/src/main/java/com/hjjang/backend/domain/post/controller/PostController.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,55 @@ | ||
package com.hjjang.backend.domain.post.controller; | ||
|
||
import com.hjjang.backend.domain.post.dto.PostRequest; | ||
import com.hjjang.backend.domain.post.dto.PostResponse; | ||
import com.hjjang.backend.domain.post.service.PostServiceImpl; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.validation.annotation.Validated; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@RequiredArgsConstructor | ||
@RequestMapping("/api/posts") | ||
@RestController | ||
public class PostController { | ||
|
||
private final PostServiceImpl postService; | ||
|
||
@PostMapping | ||
public ResponseEntity<PostResponse> createItem(@Validated @RequestParam PostRequest postRequest) { | ||
return ResponseEntity.ok( | ||
PostResponse.of( | ||
postService.save( | ||
postRequest.toEntity() | ||
) | ||
) | ||
); | ||
} | ||
|
||
@GetMapping | ||
public ResponseEntity<List<PostResponse>> findAllItem() { | ||
return ResponseEntity.ok(postService | ||
.findAll() | ||
.stream() | ||
.map(PostResponse::of) | ||
.collect(Collectors.toList()) | ||
); | ||
} | ||
|
||
@GetMapping("/{id}") | ||
public ResponseEntity<PostResponse> findOneItem(@PathVariable Long id) { | ||
return ResponseEntity.ok(PostResponse.of(postService.findOneById(id))); | ||
} | ||
|
||
@DeleteMapping("/{id}") | ||
public ResponseEntity<HttpStatus> deleteOneItem(@PathVariable Long id) { | ||
postService.deleteOneById(id); | ||
return ResponseEntity.ok(HttpStatus.ACCEPTED); | ||
} | ||
|
||
|
||
} |
47 changes: 47 additions & 0 deletions
47
backend/src/main/java/com/hjjang/backend/domain/post/domain/entity/Post.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,47 @@ | ||
package com.hjjang.backend.domain.post.domain.entity; | ||
|
||
import com.hjjang.backend.domain.user.entity.User; | ||
import com.hjjang.backend.infra.image.domain.entity.Image; | ||
import lombok.*; | ||
|
||
import javax.persistence.*; | ||
|
||
@Getter | ||
@Builder | ||
@AllArgsConstructor | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@Table(schema = "post") | ||
@Entity | ||
public class Post { | ||
@Id | ||
@Column(name = "id", nullable = false) | ||
private Long id; | ||
|
||
@ManyToOne | ||
@JoinColumn(name = "user_id") | ||
private User user; | ||
|
||
@ManyToOne | ||
@JoinColumn(name = "image_id") | ||
private Image image; | ||
|
||
private String title; | ||
|
||
private String content; | ||
|
||
private Integer item_price; | ||
|
||
private Integer views = 0; | ||
|
||
private Integer interest_number = 0; | ||
|
||
private Integer chat_number = 0; | ||
|
||
private String is_sale_completion; | ||
|
||
private boolean removed = false; | ||
|
||
public void removePost() { | ||
this.removed = true; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
backend/src/main/java/com/hjjang/backend/domain/post/domain/repository/PostRepository.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,8 @@ | ||
package com.hjjang.backend.domain.post.domain.repository; | ||
|
||
import com.hjjang.backend.domain.post.domain.entity.Post; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
|
||
public interface PostRepository extends JpaRepository<Post, Long> { | ||
} |
27 changes: 27 additions & 0 deletions
27
backend/src/main/java/com/hjjang/backend/domain/post/dto/PostRequest.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,27 @@ | ||
package com.hjjang.backend.domain.post.dto; | ||
|
||
import com.hjjang.backend.domain.post.domain.entity.Post; | ||
import lombok.Getter; | ||
|
||
import javax.validation.constraints.NotEmpty; | ||
|
||
@Getter | ||
public class PostRequest { | ||
|
||
@NotEmpty | ||
private String title; | ||
|
||
@NotEmpty | ||
private String content; | ||
|
||
@NotEmpty | ||
private int price; | ||
|
||
public Post toEntity() { | ||
return Post.builder() | ||
.title(this.title) | ||
.content(this.content) | ||
.item_price(this.price) | ||
.build(); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
backend/src/main/java/com/hjjang/backend/domain/post/dto/PostResponse.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,46 @@ | ||
package com.hjjang.backend.domain.post.dto; | ||
|
||
import com.hjjang.backend.domain.post.domain.entity.Post; | ||
import lombok.Builder; | ||
|
||
@Builder | ||
public class PostResponse { | ||
|
||
private Long id; | ||
|
||
private Long user_id; | ||
|
||
private Long image_id; | ||
|
||
private String title; | ||
|
||
private String content; | ||
|
||
private Integer item_price; | ||
|
||
private Integer views; | ||
|
||
private Integer interest_number; | ||
|
||
private Integer chat_number; | ||
|
||
private String is_sale_completion; | ||
|
||
private boolean removed; | ||
|
||
public static PostResponse of(Post post) { | ||
return PostResponse.builder() | ||
.id(post.getId()) | ||
// .user_id(post.getUser().getId()) | ||
.image_id(post.getImage().getId()) | ||
.title(post.getTitle()) | ||
.content(post.getContent()) | ||
.item_price(post.getItem_price()) | ||
.views(post.getViews()) | ||
.interest_number(post.getInterest_number()) | ||
.chat_number(post.getChat_number()) | ||
.is_sale_completion(post.getIs_sale_completion()) | ||
.removed(post.isRemoved()) | ||
.build(); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
backend/src/main/java/com/hjjang/backend/domain/post/exception/PostNotFoundException.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,4 @@ | ||
package com.hjjang.backend.domain.post.exception; | ||
|
||
public class PostNotFoundException extends RuntimeException{ | ||
} |
4 changes: 4 additions & 0 deletions
4
backend/src/main/java/com/hjjang/backend/domain/post/service/PostService.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,4 @@ | ||
package com.hjjang.backend.domain.post.service; | ||
|
||
public interface PostService { | ||
} |
33 changes: 33 additions & 0 deletions
33
backend/src/main/java/com/hjjang/backend/domain/post/service/PostServiceImpl.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,33 @@ | ||
package com.hjjang.backend.domain.post.service; | ||
|
||
import com.hjjang.backend.domain.post.domain.entity.Post; | ||
import com.hjjang.backend.domain.post.domain.repository.PostRepository; | ||
import com.hjjang.backend.domain.post.exception.PostNotFoundException; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class PostServiceImpl implements PostService { | ||
|
||
private final PostRepository postRepository; | ||
|
||
public Post save(Post post){ | ||
return postRepository.save(post); | ||
} | ||
|
||
public List<Post> findAll() { | ||
return postRepository.findAll(); | ||
} | ||
|
||
public Post findOneById(Long id) { | ||
return postRepository.findById(id).orElseThrow(PostNotFoundException::new); | ||
} | ||
|
||
public void deleteOneById(Long id) { | ||
findOneById(id).removePost(); | ||
} | ||
} | ||
|
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
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
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