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..b668807 --- /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/posts") +@RestController +public class PostController { + + private final PostServiceImpl postService; + + @PostMapping + public ResponseEntity createItem(@Validated @RequestParam PostRequest postRequest) { + return ResponseEntity.ok( + PostResponse.of( + postService.save( + postRequest.toEntity() + ) + ) + ); + } + + @GetMapping + public ResponseEntity> findAllItem() { + return ResponseEntity.ok(postService + .findAll() + .stream() + .map(PostResponse::of) + .collect(Collectors.toList()) + ); + } + + @GetMapping("/{id}") + public ResponseEntity findOneItem(@PathVariable Long id) { + return ResponseEntity.ok(PostResponse.of(postService.findOneById(id))); + } + + @DeleteMapping("/{id}") + public ResponseEntity deleteOneItem(@PathVariable Long 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 new file mode 100644 index 0000000..a931bfe --- /dev/null +++ b/backend/src/main/java/com/hjjang/backend/domain/post/domain/entity/Post.java @@ -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; + } +} 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..bf1106a --- /dev/null +++ b/backend/src/main/java/com/hjjang/backend/domain/post/dto/PostRequest.java @@ -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(); + } +} 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..7656dee --- /dev/null +++ b/backend/src/main/java/com/hjjang/backend/domain/post/dto/PostResponse.java @@ -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(); + } +} 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..1ffe926 --- /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).removePost(); + } +} + 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 = () => { - - + + - +