Skip to content

Commit

Permalink
Merge branch 'feature/#45-post-관련-rest-api-개선' of https://github.com/…
Browse files Browse the repository at this point in the history
…h-jjang/bauction into feature/#34-매물-검색-기능-구현
  • Loading branch information
Yunkeun committed May 11, 2022
2 parents 2c3e1cc + 8d32db4 commit 3f8f297
Show file tree
Hide file tree
Showing 21 changed files with 985 additions and 138 deletions.
4 changes: 4 additions & 0 deletions backend/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,17 @@ dependencies {
developmentOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'io.micrometer:micrometer-registry-prometheus'
runtimeOnly 'mysql:mysql-connector-java'

annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
annotationProcessor 'org.projectlombok:lombok'

testImplementation 'com.h2database:h2'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.batch:spring-batch-test'
testImplementation 'org.springframework.kafka:spring-kafka-test'
testImplementation 'org.springframework.restdocs:spring-restdocs-mockmvc'
testImplementation 'org.springframework.security:spring-security-test'

implementation 'org.springdoc:springdoc-openapi-ui:1.6.6'
//jwt 관련 의존성
implementation 'io.jsonwebtoken:jjwt-api:0.11.2'
Expand Down
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));
}


}
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;
}
}
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;
}
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;
}
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();
}

}

This file was deleted.

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;
}

This file was deleted.

Loading

0 comments on commit 3f8f297

Please sign in to comment.