Skip to content

Commit

Permalink
코드리뷰 정리
Browse files Browse the repository at this point in the history
  • Loading branch information
5upportPark committed Oct 4, 2024
1 parent 802da19 commit e376897
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import com.pjw.retry_view.request.WriteBoardRequest;
import com.pjw.retry_view.service.BoardService;
import jakarta.validation.Valid;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
Expand All @@ -25,8 +23,7 @@ public List<BoardDTO> getBoardList(){
}

@PostMapping
public ResponseEntity<BoardDTO> writeBoard(@RequestBody @Valid WriteBoardRequest board){
BoardDTO result = boardService.saveBoard(board.toBoardDTO());
return new ResponseEntity<>(result, HttpStatus.OK);
public BoardDTO writeBoard(@RequestBody @Valid WriteBoardRequest board){
return boardService.saveBoard(board);
}
}
4 changes: 0 additions & 4 deletions src/main/java/com/pjw/retry_view/dto/BoardDTO.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
package com.pjw.retry_view.dto;

import com.pjw.retry_view.entity.Board;
import com.pjw.retry_view.entity.BoardImage;
import lombok.*;

import java.time.ZonedDateTime;
import java.util.List;

@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class BoardDTO {
private Long id;
private BoardType type;
Expand Down
39 changes: 36 additions & 3 deletions src/main/java/com/pjw/retry_view/entity/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import com.pjw.retry_view.dto.BoardDTO;
import com.pjw.retry_view.dto.BoardImageDTO;
import com.pjw.retry_view.dto.BoardType;
import com.pjw.retry_view.request.WriteBoardRequest;
import jakarta.persistence.*;
import lombok.*;
import org.hibernate.annotations.ColumnDefault;

import java.time.ZonedDateTime;
import java.util.ArrayList;
Expand All @@ -14,8 +16,6 @@
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Table(name = "board")
@Entity
public class Board {
Expand All @@ -30,11 +30,11 @@ public class Board {
@Column(name = "content")
private String content;
@Column(name = "view_count")
@ColumnDefault("0")
private Long viewCount;
@Column(name = "price")
private Long price;
@OneToMany(mappedBy = "board", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@Builder.Default
private List<BoardImage> boardImage = new ArrayList<>();

@Column(name = "created_by")
Expand Down Expand Up @@ -62,6 +62,39 @@ public BoardDTO toDTO(){
.build();
}

@Builder
public Board(Long id, BoardType type, Long productId, String content, Long viewCount, Long price, List<BoardImage> boardImage, Long createdBy, ZonedDateTime createdAt, Long updatedBy, ZonedDateTime updatedAt) {
this.id = id;
this.type = type;
this.productId = productId;
this.content = content;
this.viewCount = viewCount;
this.price = price;
this.boardImage = boardImage;
this.createdBy = createdBy;
this.createdAt = createdAt;
this.updatedBy = updatedBy;
this.updatedAt = updatedAt;
}

public static Board newBoardFromReq(WriteBoardRequest req, List<BoardImage> images){
return Board.builder()
.id(req.getId())
.type(req.getType())
.productId(req.getProductId())
.content(req.getContent())
.viewCount(0L)
.price(req.getPrice())
.boardImage(images)
.createdBy(req.getCreatedBy())
.createdAt(ZonedDateTime.now())
.build();
}

public void changeBoardImage(List<BoardImage> images){
this.boardImage = images;
}

private List<BoardImageDTO> imagesToDTO(){
if(boardImage == null) return null;
return boardImage.stream().map(BoardImage::toDTO).toList();
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/pjw/retry_view/entity/BoardImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ public class BoardImage {
@Column(name = "updated_at")
private ZonedDateTime updatedAt;

public static BoardImage getBoardImage(Board board, String imageUrl){
public static BoardImage getBoardImage(Board board, String imageUrl, Long createdBy){
return BoardImage.builder()
.board(board)
.imageUrl(imageUrl)
.createdBy(board.getCreatedBy())
.createdBy(createdBy)
.createdAt(ZonedDateTime.now())
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
@Getter
@Setter
public class WriteBoardRequest implements Serializable {
private Long id;
private BoardType type;
private Long productId;
@NotEmpty(message = "내용은 필수 입력값입니다.")
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/pjw/retry_view/service/BoardService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.pjw.retry_view.entity.Board;
import com.pjw.retry_view.entity.BoardImage;
import com.pjw.retry_view.repository.BoardRepository;
import com.pjw.retry_view.request.WriteBoardRequest;
import jakarta.transaction.Transactional;
import org.springframework.stereotype.Service;

Expand Down

0 comments on commit e376897

Please sign in to comment.