From c18ba9bb845a1cd192460207165a0145fcf81c5f Mon Sep 17 00:00:00 2001 From: 0BVer Date: Fri, 29 Apr 2022 00:13:55 +0900 Subject: [PATCH 1/2] feat : add post entity --- .../post/controller/PostController.java | 55 +++++++++++++++++++ .../domain/post/domain/entity/Post.java | 34 ++++++++++++ .../domain/repository/PostRepository.java | 8 +++ .../backend/domain/post/dto/PostRequest.java | 24 ++++++++ .../backend/domain/post/dto/PostResponse.java | 27 +++++++++ .../post/exception/PostNotFoundException.java | 4 ++ .../domain/post/service/PostService.java | 4 ++ .../domain/post/service/PostServiceImpl.java | 33 +++++++++++ frontend/src/component/NavBar.js | 4 +- frontend/src/page/LoginPage.js | 2 +- frontend/src/page/RouterPage.js | 6 +- 11 files changed, 195 insertions(+), 6 deletions(-) create mode 100644 backend/src/main/java/com/hjjang/backend/domain/post/controller/PostController.java create mode 100644 backend/src/main/java/com/hjjang/backend/domain/post/domain/entity/Post.java create mode 100644 backend/src/main/java/com/hjjang/backend/domain/post/domain/repository/PostRepository.java create mode 100644 backend/src/main/java/com/hjjang/backend/domain/post/dto/PostRequest.java create mode 100644 backend/src/main/java/com/hjjang/backend/domain/post/dto/PostResponse.java create mode 100644 backend/src/main/java/com/hjjang/backend/domain/post/exception/PostNotFoundException.java create mode 100644 backend/src/main/java/com/hjjang/backend/domain/post/service/PostService.java create mode 100644 backend/src/main/java/com/hjjang/backend/domain/post/service/PostServiceImpl.java diff --git a/backend/src/main/java/com/hjjang/backend/domain/post/controller/PostController.java b/backend/src/main/java/com/hjjang/backend/domain/post/controller/PostController.java new file mode 100644 index 0000000..7beb219 --- /dev/null +++ b/backend/src/main/java/com/hjjang/backend/domain/post/controller/PostController.java @@ -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/items") +@RestController +public class PostController { + + private final PostServiceImpl itemService; + + @PostMapping + public ResponseEntity createItem(@Validated @RequestBody PostRequest postRequest) { + return ResponseEntity.ok(PostResponse + .of(itemService + .save(postRequest + .toEntity() + ) + ) + ); + } + + @GetMapping + public ResponseEntity> findAllItem() { + return ResponseEntity.ok(itemService + .findAll() + .stream() + .map(PostResponse::of) + .collect(Collectors.toList()) + ); + } + + @GetMapping("/{id}") + public ResponseEntity findOneItem(@PathVariable Long id) { + return ResponseEntity.ok(PostResponse.of(itemService.findOneById(id))); + } + + @DeleteMapping("/{id}") + public ResponseEntity deleteOneItem(@PathVariable Long id) { + itemService.deleteOneById(id); + return ResponseEntity.ok(HttpStatus.ACCEPTED); + } + + +} diff --git a/backend/src/main/java/com/hjjang/backend/domain/post/domain/entity/Post.java b/backend/src/main/java/com/hjjang/backend/domain/post/domain/entity/Post.java new file mode 100644 index 0000000..8d83aa0 --- /dev/null +++ b/backend/src/main/java/com/hjjang/backend/domain/post/domain/entity/Post.java @@ -0,0 +1,34 @@ +package com.hjjang.backend.domain.post.domain.entity; + +import com.hjjang.backend.domain.user.entity.User; +import lombok.*; + +import javax.persistence.*; + +@Getter +@Builder +@AllArgsConstructor +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@Table(schema = "item") +@Entity +public class Post { + @Id + @Column(name = "id", nullable = false) + private Long id; + + private String name; + + @ManyToOne + @JoinColumn(name = "seller_id") + private User seller; + +// private List image_list; + + private Integer price; + + private boolean removed = false; + + public void removeItem() { + this.removed = true; + } +} diff --git a/backend/src/main/java/com/hjjang/backend/domain/post/domain/repository/PostRepository.java b/backend/src/main/java/com/hjjang/backend/domain/post/domain/repository/PostRepository.java new file mode 100644 index 0000000..0fcb9a1 --- /dev/null +++ b/backend/src/main/java/com/hjjang/backend/domain/post/domain/repository/PostRepository.java @@ -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 { +} diff --git a/backend/src/main/java/com/hjjang/backend/domain/post/dto/PostRequest.java b/backend/src/main/java/com/hjjang/backend/domain/post/dto/PostRequest.java new file mode 100644 index 0000000..e420d8e --- /dev/null +++ b/backend/src/main/java/com/hjjang/backend/domain/post/dto/PostRequest.java @@ -0,0 +1,24 @@ +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 name; + + @NotEmpty + private int price; + + public Post toEntity() { + return Post.builder() + .name(this.name) + .price(this.price) + //TODO 판매자 추가해야함 + .build(); + } +} diff --git a/backend/src/main/java/com/hjjang/backend/domain/post/dto/PostResponse.java b/backend/src/main/java/com/hjjang/backend/domain/post/dto/PostResponse.java new file mode 100644 index 0000000..7f0da0e --- /dev/null +++ b/backend/src/main/java/com/hjjang/backend/domain/post/dto/PostResponse.java @@ -0,0 +1,27 @@ +package com.hjjang.backend.domain.post.dto; + +import com.hjjang.backend.domain.post.domain.entity.Post; +import com.hjjang.backend.domain.user.entity.User; +import lombok.Builder; + +@Builder +public class PostResponse { + + private Long id; + + private User seller; + + private String name; + + private int price; + + + public static PostResponse of(Post post) { + return PostResponse.builder() + .id(post.getId()) + .seller(post.getSeller()) + .name(post.getName()) + .price(post.getPrice()) + .build(); + } +} diff --git a/backend/src/main/java/com/hjjang/backend/domain/post/exception/PostNotFoundException.java b/backend/src/main/java/com/hjjang/backend/domain/post/exception/PostNotFoundException.java new file mode 100644 index 0000000..094b39c --- /dev/null +++ b/backend/src/main/java/com/hjjang/backend/domain/post/exception/PostNotFoundException.java @@ -0,0 +1,4 @@ +package com.hjjang.backend.domain.post.exception; + +public class PostNotFoundException extends RuntimeException{ +} diff --git a/backend/src/main/java/com/hjjang/backend/domain/post/service/PostService.java b/backend/src/main/java/com/hjjang/backend/domain/post/service/PostService.java new file mode 100644 index 0000000..768030e --- /dev/null +++ b/backend/src/main/java/com/hjjang/backend/domain/post/service/PostService.java @@ -0,0 +1,4 @@ +package com.hjjang.backend.domain.post.service; + +public interface PostService { +} diff --git a/backend/src/main/java/com/hjjang/backend/domain/post/service/PostServiceImpl.java b/backend/src/main/java/com/hjjang/backend/domain/post/service/PostServiceImpl.java new file mode 100644 index 0000000..c8c439c --- /dev/null +++ b/backend/src/main/java/com/hjjang/backend/domain/post/service/PostServiceImpl.java @@ -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 findAll() { + return postRepository.findAll(); + } + + public Post findOneById(Long id) { + return postRepository.findById(id).orElseThrow(PostNotFoundException::new); + } + + public void deleteOneById(Long id) { + findOneById(id).removeItem(); + } +} + diff --git a/frontend/src/component/NavBar.js b/frontend/src/component/NavBar.js index 70cde78..ed7a5ba 100644 --- a/frontend/src/component/NavBar.js +++ b/frontend/src/component/NavBar.js @@ -4,10 +4,10 @@ import {Link} from "react-router-dom"; export const NavBar = () => { return ( <> - + - + diff --git a/frontend/src/page/LoginPage.js b/frontend/src/page/LoginPage.js index 91a35c4..da9e825 100644 --- a/frontend/src/page/LoginPage.js +++ b/frontend/src/page/LoginPage.js @@ -22,7 +22,7 @@ export const LoginPage = () => { id: id, pw: pw, }).then(res => { - console.log(res) + console.log(res); }) } diff --git a/frontend/src/page/RouterPage.js b/frontend/src/page/RouterPage.js index 723be0b..0fabce4 100644 --- a/frontend/src/page/RouterPage.js +++ b/frontend/src/page/RouterPage.js @@ -20,13 +20,13 @@ export const RouterPage = () => { - - + + - + From 43ede051a474e227305d7d6ae6a14842db449f06 Mon Sep 17 00:00:00 2001 From: 0BVer Date: Fri, 29 Apr 2022 23:37:05 +0900 Subject: [PATCH 2/2] feat : add post request and response --- .../post/controller/PostController.java | 20 +++++------ .../domain/post/domain/entity/Post.java | 27 +++++++++++---- .../backend/domain/post/dto/PostRequest.java | 11 ++++--- .../backend/domain/post/dto/PostResponse.java | 33 +++++++++++++++---- .../domain/post/service/PostServiceImpl.java | 2 +- 5 files changed, 64 insertions(+), 29 deletions(-) diff --git a/backend/src/main/java/com/hjjang/backend/domain/post/controller/PostController.java b/backend/src/main/java/com/hjjang/backend/domain/post/controller/PostController.java index 7beb219..b668807 100644 --- a/backend/src/main/java/com/hjjang/backend/domain/post/controller/PostController.java +++ b/backend/src/main/java/com/hjjang/backend/domain/post/controller/PostController.java @@ -13,18 +13,18 @@ import java.util.stream.Collectors; @RequiredArgsConstructor -@RequestMapping("/api/items") +@RequestMapping("/api/posts") @RestController public class PostController { - private final PostServiceImpl itemService; + private final PostServiceImpl postService; @PostMapping - public ResponseEntity createItem(@Validated @RequestBody PostRequest postRequest) { - return ResponseEntity.ok(PostResponse - .of(itemService - .save(postRequest - .toEntity() + public ResponseEntity createItem(@Validated @RequestParam PostRequest postRequest) { + return ResponseEntity.ok( + PostResponse.of( + postService.save( + postRequest.toEntity() ) ) ); @@ -32,7 +32,7 @@ public ResponseEntity createItem(@Validated @RequestBody PostReque @GetMapping public ResponseEntity> findAllItem() { - return ResponseEntity.ok(itemService + return ResponseEntity.ok(postService .findAll() .stream() .map(PostResponse::of) @@ -42,12 +42,12 @@ public ResponseEntity> findAllItem() { @GetMapping("/{id}") public ResponseEntity findOneItem(@PathVariable Long id) { - return ResponseEntity.ok(PostResponse.of(itemService.findOneById(id))); + return ResponseEntity.ok(PostResponse.of(postService.findOneById(id))); } @DeleteMapping("/{id}") public ResponseEntity deleteOneItem(@PathVariable Long id) { - itemService.deleteOneById(id); + postService.deleteOneById(id); return ResponseEntity.ok(HttpStatus.ACCEPTED); } diff --git a/backend/src/main/java/com/hjjang/backend/domain/post/domain/entity/Post.java b/backend/src/main/java/com/hjjang/backend/domain/post/domain/entity/Post.java index 8d83aa0..a931bfe 100644 --- a/backend/src/main/java/com/hjjang/backend/domain/post/domain/entity/Post.java +++ b/backend/src/main/java/com/hjjang/backend/domain/post/domain/entity/Post.java @@ -1,6 +1,7 @@ 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.*; @@ -9,26 +10,38 @@ @Builder @AllArgsConstructor @NoArgsConstructor(access = AccessLevel.PROTECTED) -@Table(schema = "item") +@Table(schema = "post") @Entity public class Post { @Id @Column(name = "id", nullable = false) private Long id; - private String name; + @ManyToOne + @JoinColumn(name = "user_id") + private User user; @ManyToOne - @JoinColumn(name = "seller_id") - private User seller; + @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 List image_list; + private Integer chat_number = 0; - private Integer price; + private String is_sale_completion; private boolean removed = false; - public void removeItem() { + public void removePost() { this.removed = true; } } diff --git a/backend/src/main/java/com/hjjang/backend/domain/post/dto/PostRequest.java b/backend/src/main/java/com/hjjang/backend/domain/post/dto/PostRequest.java index e420d8e..bf1106a 100644 --- a/backend/src/main/java/com/hjjang/backend/domain/post/dto/PostRequest.java +++ b/backend/src/main/java/com/hjjang/backend/domain/post/dto/PostRequest.java @@ -9,16 +9,19 @@ public class PostRequest { @NotEmpty - private String name; + private String title; + + @NotEmpty + private String content; @NotEmpty private int price; public Post toEntity() { return Post.builder() - .name(this.name) - .price(this.price) - //TODO 판매자 추가해야함 + .title(this.title) + .content(this.content) + .item_price(this.price) .build(); } } diff --git a/backend/src/main/java/com/hjjang/backend/domain/post/dto/PostResponse.java b/backend/src/main/java/com/hjjang/backend/domain/post/dto/PostResponse.java index 7f0da0e..7656dee 100644 --- a/backend/src/main/java/com/hjjang/backend/domain/post/dto/PostResponse.java +++ b/backend/src/main/java/com/hjjang/backend/domain/post/dto/PostResponse.java @@ -1,7 +1,6 @@ package com.hjjang.backend.domain.post.dto; import com.hjjang.backend.domain.post.domain.entity.Post; -import com.hjjang.backend.domain.user.entity.User; import lombok.Builder; @Builder @@ -9,19 +8,39 @@ public class PostResponse { private Long id; - private User seller; + private Long user_id; - private String name; + private Long image_id; - private int price; + 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()) - .seller(post.getSeller()) - .name(post.getName()) - .price(post.getPrice()) +// .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(); } } diff --git a/backend/src/main/java/com/hjjang/backend/domain/post/service/PostServiceImpl.java b/backend/src/main/java/com/hjjang/backend/domain/post/service/PostServiceImpl.java index c8c439c..1ffe926 100644 --- a/backend/src/main/java/com/hjjang/backend/domain/post/service/PostServiceImpl.java +++ b/backend/src/main/java/com/hjjang/backend/domain/post/service/PostServiceImpl.java @@ -27,7 +27,7 @@ public Post findOneById(Long id) { } public void deleteOneById(Long id) { - findOneById(id).removeItem(); + findOneById(id).removePost(); } }