Skip to content

Commit

Permalink
Merge pull request #13 from T1F5/feature/#12
Browse files Browse the repository at this point in the history
Feature/#12
  • Loading branch information
Beakjiyeon authored Apr 6, 2024
2 parents 904212e + df1858e commit 2f8ca8c
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequiredArgsConstructor
@RequestMapping("/board")
@RestController
Expand All @@ -14,15 +16,26 @@ public class BoardController {
private final BoardService boardService;

@GetMapping("/{boardId}")
public String getBoard(@PathVariable("boardId") Long boardId) {
return "단일 조회 정보 " + boardId;
public AddBoardResponseDto getBoard(@PathVariable("boardId") Long boardId) {
return boardService.getBoard(boardId);
}

@GetMapping("/boards")
public String getBoards() {
return "목록 조회 정보";
public List<AddBoardResponseDto> getMyBoards() {
// 사용자가 작성한 일지 목록 조회
Long memberId = 1L; // TODO 인증
return boardService.getMyBoards(memberId);
}

// TODO 사용자가 보지 않은 글 중에서 랜덤 3개 골라 주기
@GetMapping("/random")
public List<AddBoardResponseDto> getRandomBoards() {
// 사용자가 작성한 일지 목록 조회
// TODO api 호출 시점마다 랜덤x. 12시에 사용자가 읽지 않은 글을 today_user_table에 저장한다. 전날 건 삭제
// todo api는 today_user_table 에 있는 정보를 read. 계산 x
Long memberId = 1L; // TODO 인증
return boardService.getRandomBoards(memberId);
}

@PostMapping
public AddBoardResponseDto addBoard(@RequestBody AddBoardRequestDto addBoardRequestDto) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.unit.daybook.domain.board.dto.request;


public record AddBoardRequestDto(String content, Long respectBoardId) {
public record AddBoardRequestDto(String content, Long respectBoardId, String category, Long hearts) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
public record AddBoardResponseDto(
Long boardId,
String content,
Long respectBoardId
Long respectBoardId,
Long authorId,
String category,
Long hearts

) {

public static AddBoardResponseDto from(Board board) {
return new AddBoardResponseDto(board.getBoardId(), board.getContent(), board.getRespectBoardId());
return new AddBoardResponseDto(board.getBoardId(), board.getContent(), board.getRespectBoardId(), board.getMemeber().getId(), board.getCategory(), board.getHearts());
}

}
24 changes: 22 additions & 2 deletions src/main/java/com/unit/daybook/domain/board/entity/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.unit.daybook.domain.board.dto.request.AddBoardRequestDto;
import com.unit.daybook.domain.common.model.BaseTimeEntity;
import com.unit.daybook.domain.member.domain.Member;
import jakarta.persistence.*;
import lombok.*;

Expand All @@ -20,22 +21,41 @@ public class Board extends BaseTimeEntity {
@Column
private String content;

@Column
private String category;

@Column(name = "respect_board_id")
private Long respectBoardId;

@ManyToOne
@JoinColumn(name = "member_id")
private Member memeber;

@Column
private Long hearts;

@Builder(access = AccessLevel.PRIVATE)
public Board(Long boardId, String content, Long respectBoardId) {
public Board(Long boardId, String content, Long respectBoardId, Member member, String category, Long hearts) {
this.boardId = boardId;
this.content = content;
this.respectBoardId = respectBoardId;
this.memeber = member;
this.category = category;
this.hearts = hearts;
}

public static Board createBoard(AddBoardRequestDto addBoardRequestDto) {
public static Board createBoard(AddBoardRequestDto addBoardRequestDto, Member member) {
return Board.builder()
.content(addBoardRequestDto.content())
.respectBoardId(addBoardRequestDto.respectBoardId())
.member(member)
.category(addBoardRequestDto.category())
.hearts(0L) // todo
.build();
}

public void plusRespect() {
this.hearts += 1;
}

}
29 changes: 29 additions & 0 deletions src/main/java/com/unit/daybook/domain/board/entity/ReadBoard.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.unit.daybook.domain.board.entity;

import com.unit.daybook.domain.common.model.BaseTimeEntity;
import com.unit.daybook.domain.member.domain.Member;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@Entity
@Table(name = "read_board")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
class ReadBoard extends BaseTimeEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "read_board_id")
private Long readBoardId;

@ManyToOne
@JoinColumn(name = "member_id")
private Member member;

@ManyToOne
@JoinColumn(name = "board_id")
private Board board;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.unit.daybook.domain.board.repository;


public interface BoardRepositoryCustom {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.unit.daybook.domain.board.repository;

import com.querydsl.jpa.impl.JPAQueryFactory;
import com.unit.daybook.domain.board.entity.Board;
import com.unit.daybook.domain.board.entity.QBoard;
import com.unit.daybook.domain.member.domain.QMember;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.List;

import static com.unit.daybook.domain.board.entity.QBoard.board;
import static com.unit.daybook.domain.member.domain.QMember.member;

@Repository
@RequiredArgsConstructor
public class BoardRepositoryImpl implements BoardRepositoryCustom {
private final JPAQueryFactory queryFactory;

public List<Board> findBoardsByMemberId(Long memberId) {

List<Board> boards = queryFactory
.select(board)
.from(board)
.join(board.memeber, member).fetchJoin()
.where(
member.id.eq(memberId)
)
.fetch();

return boards;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,52 @@
import com.unit.daybook.domain.board.entity.Board;
import com.unit.daybook.domain.board.repository.BoardRepository;

import com.unit.daybook.domain.board.repository.BoardRepositoryImpl;
import com.unit.daybook.domain.member.domain.Member;
import com.unit.daybook.domain.member.repository.MemberRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Transactional
@Service
@RequiredArgsConstructor
public class BoardService {

private final BoardRepository boardRepository;
private final BoardRepositoryImpl boardRepositoryImpl;
private final MemberRepository memberRepository;

public AddBoardResponseDto addBoard(AddBoardRequestDto addBoardRequestDto, Long memberId) {
return AddBoardResponseDto.from(boardRepository.save(Board.createBoard(addBoardRequestDto)));
Member member = memberRepository.findById(memberId).orElseThrow(() -> new RuntimeException(memberId + "not found"));
if (addBoardRequestDto.respectBoardId() != null) {
Board respectBoard = boardRepository.findById(addBoardRequestDto.respectBoardId()).orElseThrow(() -> new RuntimeException(addBoardRequestDto.respectBoardId() + "not found"));
respectBoard.plusRespect();
boardRepository.save(respectBoard);
}
// 글의 카운트 올리기.. redis..?

return AddBoardResponseDto.from(boardRepository.save(Board.createBoard(addBoardRequestDto, member)));
}


public AddBoardResponseDto getBoard(Long boardId) {
return AddBoardResponseDto.from(
boardRepository.findById(boardId).orElseThrow(() -> new RuntimeException(boardId + "not found")));
}

public List<AddBoardResponseDto> getMyBoards(Long memberId) {
// TODO 페이지네이션 - 스와이프 방식?
return boardRepositoryImpl.findBoardsByMemberId(memberId)
.stream()
.map(AddBoardResponseDto::from)
.toList();
}

public List<AddBoardResponseDto> getRandomBoards(Long memberId) {
return null;
}
}
15 changes: 7 additions & 8 deletions src/main/java/com/unit/daybook/domain/member/domain/Member.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
package com.unit.daybook.domain.member.domain;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import com.unit.daybook.domain.board.entity.Board;
import com.unit.daybook.domain.common.model.BaseTimeEntity;

import jakarta.persistence.Column;
import jakarta.persistence.Embedded;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
Expand Down Expand Up @@ -40,6 +36,8 @@ public class Member extends BaseTimeEntity {

private LocalDateTime lastLoginAt;

@OneToMany(fetch = FetchType.LAZY)
private List<Board> boards = new ArrayList<>();

@Builder(access = AccessLevel.PRIVATE)
private Member(
Expand All @@ -64,6 +62,7 @@ public static Member createNormalMember(OauthInfo oauthInfo, String nickname) {
.build();
}


public void updateLastLoginAt() {
this.lastLoginAt = LocalDateTime.now();
}
Expand Down

0 comments on commit 2f8ca8c

Please sign in to comment.