Skip to content

Commit

Permalink
Merge branch 'feature/#26-사용자-매물-등록-기능-구현' of https://github.com/h-jj…
Browse files Browse the repository at this point in the history
…ang/bauction into feature/#34-매물-검색-기능-구현
  • Loading branch information
Yunkeun committed Apr 29, 2022
2 parents 993a31f + 43ede05 commit 1006f77
Show file tree
Hide file tree
Showing 11 changed files with 230 additions and 6 deletions.
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);
}


}
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;
}
}
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> {
}
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();
}
}
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();
}
}
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{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.hjjang.backend.domain.post.service;

public interface PostService {
}
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();
}
}

4 changes: 2 additions & 2 deletions frontend/src/component/NavBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import {Link} from "react-router-dom";
export const NavBar = () => {
return (
<>
<Link to="/item-list">
<Link to="/post-list">
<button>HOME</button>
</Link>
<Link to="/item-detail">
<Link to="/post-detail">
<button>Item Detail</button>
</Link>
<Link to="/mypage">
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/page/LoginPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const LoginPage = () => {
id: id,
pw: pw,
}).then(res => {
console.log(res)
console.log(res);
})
}

Expand Down
6 changes: 3 additions & 3 deletions frontend/src/page/RouterPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ export const RouterPage = () => {
<BrowserRouter>
<NavBar/>
<Switch>
<Route path="/item-list" component={ItemListPage}/>
<Route path="/item-detail" component={ItemDetailPage}/>
<Route path="/post-list" component={ItemListPage}/>
<Route path="/post-detail" component={ItemDetailPage}/>
<Route path="/mypage" component={MyPage}/>
<Route path="/login" component={LoginPage}/>
<Route path="/signup" component={SignUpPage}/>
<Route path="/mypage" component={MyPage}/>
<Redirect path="*" to="/item-list"/>
<Redirect path="*" to="/post-list"/>
</Switch>
</BrowserRouter>
</div>
Expand Down

0 comments on commit 1006f77

Please sign in to comment.