-
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/#45-post-관련-rest-api-개선' of https://github.com/…
…h-jjang/bauction into feature/#34-매물-검색-기능-구현
- Loading branch information
Showing
21 changed files
with
985 additions
and
138 deletions.
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
71 changes: 48 additions & 23 deletions
71
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 |
---|---|---|
@@ -1,55 +1,80 @@ | ||
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.dto.PostMapper; | ||
import com.hjjang.backend.domain.post.dto.PostRequestDto; | ||
import com.hjjang.backend.domain.post.dto.PostResponseDto; | ||
import com.hjjang.backend.domain.post.service.PostServiceImpl; | ||
import com.hjjang.backend.global.dto.ApiResponse; | ||
import com.hjjang.backend.global.util.UserUtil; | ||
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; | ||
|
||
import static org.springframework.http.HttpStatus.*; | ||
import static org.springframework.http.ResponseEntity.*; | ||
|
||
@RequiredArgsConstructor | ||
@RequestMapping("/api/posts") | ||
@RestController | ||
public class PostController { | ||
|
||
private final PostServiceImpl postService; | ||
private final PostMapper postMapper; | ||
private final UserUtil userUtil; | ||
|
||
@PostMapping | ||
public ResponseEntity<PostResponse> createItem(@Validated @RequestBody PostRequest postRequest) { | ||
return ResponseEntity.ok( | ||
PostResponse.of( | ||
postService.save( | ||
postRequest.toEntity() | ||
public ResponseEntity<ApiResponse> createItem(@Validated @RequestBody PostRequestDto postRequestDto) { | ||
return status(CREATED) | ||
.body(ApiResponse.success( | ||
"createItem", | ||
postMapper.fromEntity( | ||
postService.save( | ||
postMapper.toEntity( | ||
postRequestDto, userUtil.getLoginUserByToken() | ||
) | ||
) | ||
) | ||
) | ||
) | ||
); | ||
); | ||
} | ||
|
||
@GetMapping | ||
public ResponseEntity<List<PostResponse>> findAllItem() { | ||
return ResponseEntity.ok(postService | ||
.findAll() | ||
.stream() | ||
.map(PostResponse::of) | ||
.collect(Collectors.toList()) | ||
public ResponseEntity<ApiResponse> findAllItem() { | ||
return ok( | ||
ApiResponse.success("findAllItem", postService | ||
.findAll() | ||
.stream() | ||
.map(postMapper::fromEntity) | ||
.collect(Collectors.toList()) | ||
) | ||
); | ||
} | ||
|
||
@GetMapping("/{id}") | ||
public ResponseEntity<PostResponse> findOneItem(@PathVariable Long id) { | ||
return ResponseEntity.ok(PostResponse.of(postService.findOneById(id))); | ||
public ResponseEntity<ApiResponse> findOneItem(@PathVariable Long id) { | ||
return ok(ApiResponse.success("findOneItem", postMapper.fromEntity(postService.findOneById(id)))); | ||
} | ||
|
||
@PutMapping("/{id}") | ||
public ResponseEntity<ApiResponse> putOneItem(@PathVariable Long id, @Validated @RequestBody PostRequestDto postRequestDto) { | ||
return status(CREATED) | ||
.body(ApiResponse.success( | ||
"updatePutOneItem", | ||
postMapper.fromEntity( | ||
postService.updateOneById( | ||
id, postRequestDto, userUtil.getLoginUserByToken() | ||
) | ||
) | ||
) | ||
); | ||
} | ||
|
||
@DeleteMapping("/{id}") | ||
public ResponseEntity<HttpStatus> deleteOneItem(@PathVariable Long id) { | ||
public ResponseEntity<ApiResponse> deleteOneItem(@PathVariable Long id) { | ||
postService.deleteOneById(id); | ||
return ResponseEntity.ok(HttpStatus.ACCEPTED); | ||
return ok(ApiResponse.success("deleteOneItem", ACCEPTED)); | ||
} | ||
|
||
|
||
} |
72 changes: 49 additions & 23 deletions
72
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 |
---|---|---|
@@ -1,53 +1,79 @@ | ||
package com.hjjang.backend.domain.post.domain.entity; | ||
|
||
import com.hjjang.backend.domain.post.dto.PostRequestDto; | ||
import com.hjjang.backend.domain.user.entity.User; | ||
import lombok.*; | ||
import org.hibernate.annotations.ColumnDefault; | ||
import org.hibernate.annotations.DynamicInsert; | ||
import org.hibernate.annotations.CreationTimestamp; | ||
|
||
import javax.persistence.*; | ||
import java.time.LocalDateTime; | ||
|
||
import static com.hjjang.backend.domain.post.domain.entity.PostDefaultValue.*; | ||
|
||
@Getter | ||
@Builder | ||
//@Builder | ||
@AllArgsConstructor | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@DynamicInsert | ||
@Table(schema = "post") | ||
//@DynamicInsert | ||
@Table(name = "post") | ||
@Entity | ||
public class Post { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
// @ManyToOne | ||
// @JoinColumn(name = "user_id") | ||
// private User user; | ||
|
||
// @ManyToOne | ||
// @JoinColumn(name = "image_id") | ||
// private Image image; | ||
@ManyToOne | ||
@JoinColumn(name = "user_id") | ||
private User user; | ||
|
||
@Column(name = "title", nullable = false) | ||
private String title; | ||
|
||
@Column(name = "content", nullable = false) | ||
private String content; | ||
|
||
private Integer item_price; | ||
@Column(name = "item_price", nullable = false) | ||
private int itemPrice; | ||
|
||
@ColumnDefault("0") | ||
private Integer views = 0; | ||
// @ColumnDefault("0") | ||
@Column(name = "views", nullable = false) | ||
private int views = DEFAULT_VIEWS; | ||
|
||
@ColumnDefault("0") | ||
private Integer interest_number = 0; | ||
// @ColumnDefault("0") | ||
@Column(name = "interest_number", nullable = false) | ||
private int interestNumber = DEFAULT_INTEREST_NUMBER; | ||
|
||
@ColumnDefault("0") | ||
private Integer chat_number = 0; | ||
// @ColumnDefault("0") | ||
@Column(name = "chat_number", nullable = false) | ||
private int chatNumber = DEFAULT_CHAT_NUMBER; | ||
|
||
@ColumnDefault("'false'") | ||
private String is_sale_completion = "false"; | ||
@Enumerated(EnumType.STRING) | ||
@Column(name = "is_sale_completion", nullable = false) | ||
private PostState isSaleCompletion = DEFAULT_IS_SALE_COMPLETION; | ||
|
||
@ColumnDefault("false") | ||
private boolean removed = false; | ||
// @ColumnDefault("false") | ||
@Column(name = "removed", nullable = false) | ||
private boolean removed = DEFAULT_REMOVED; | ||
|
||
@CreationTimestamp | ||
private LocalDateTime time; | ||
|
||
@Builder | ||
public Post(User user, String title, String content, int itemPrice) { | ||
this.user = user; | ||
this.title = title; | ||
this.content = content; | ||
this.itemPrice = itemPrice; | ||
} | ||
|
||
public void removePost() { | ||
this.removed = true; | ||
} | ||
|
||
public Post update(PostRequestDto postRequestDto) { | ||
this.title = postRequestDto.getTitle(); | ||
this.content = postRequestDto.getContent(); | ||
this.itemPrice = postRequestDto.getPrice(); | ||
return this; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
backend/src/main/java/com/hjjang/backend/domain/post/domain/entity/PostDefaultValue.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,15 @@ | ||
package com.hjjang.backend.domain.post.domain.entity; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class PostDefaultValue { | ||
public static final int DEFAULT_VIEWS = 0; | ||
public static final int DEFAULT_INTEREST_NUMBER = 0; | ||
public static final int DEFAULT_CHAT_NUMBER = 0; | ||
public static final PostState DEFAULT_IS_SALE_COMPLETION = PostState.SALE; | ||
public static final boolean DEFAULT_REMOVED = false; | ||
} |
15 changes: 15 additions & 0 deletions
15
backend/src/main/java/com/hjjang/backend/domain/post/domain/entity/PostState.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,15 @@ | ||
package com.hjjang.backend.domain.post.domain.entity; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public enum PostState { | ||
SALE("판매중"), | ||
RESERVED("예약중"), | ||
SOLD("판매완료"), | ||
; | ||
|
||
final String state; | ||
} |
35 changes: 35 additions & 0 deletions
35
backend/src/main/java/com/hjjang/backend/domain/post/dto/PostMapper.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,35 @@ | ||
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 org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class PostMapper { | ||
|
||
public Post toEntity(PostRequestDto postRequestDto, User user) { | ||
return Post.builder() | ||
.user(user) | ||
.title(postRequestDto.getTitle()) | ||
.content(postRequestDto.getContent()) | ||
.itemPrice(postRequestDto.getPrice()) | ||
.build(); | ||
} | ||
|
||
public PostResponseDto fromEntity(Post post) { | ||
return PostResponseDto.builder() | ||
.id(post.getId()) | ||
.user_id(post.getUser().getId()) | ||
.title(post.getTitle()) | ||
.content(post.getContent()) | ||
.item_price(post.getItemPrice()) | ||
.views(post.getViews()) | ||
.interest_number(post.getInterestNumber()) | ||
.chat_number(post.getChatNumber()) | ||
.is_sale_completion(post.getIsSaleCompletion().getState()) | ||
.removed(post.isRemoved()) | ||
.time(post.getTime().toString()) | ||
.build(); | ||
} | ||
|
||
} |
32 changes: 0 additions & 32 deletions
32
backend/src/main/java/com/hjjang/backend/domain/post/dto/PostRequest.java
This file was deleted.
Oops, something went wrong.
19 changes: 19 additions & 0 deletions
19
backend/src/main/java/com/hjjang/backend/domain/post/dto/PostRequestDto.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,19 @@ | ||
package com.hjjang.backend.domain.post.dto; | ||
|
||
import lombok.*; | ||
|
||
import javax.validation.constraints.NotNull; | ||
|
||
@Data | ||
@Builder | ||
@AllArgsConstructor | ||
public class PostRequestDto { | ||
@NotNull | ||
private String title; | ||
|
||
@NotNull | ||
private String content; | ||
|
||
@NotNull | ||
private int price; | ||
} |
50 changes: 0 additions & 50 deletions
50
backend/src/main/java/com/hjjang/backend/domain/post/dto/PostResponse.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.