Skip to content

Commit

Permalink
feat: 해시태그 엔티티 추가. 일지 응답 필드 반영
Browse files Browse the repository at this point in the history
  • Loading branch information
Beakjiyeon committed Apr 6, 2024
1 parent c4fda50 commit c096520
Show file tree
Hide file tree
Showing 13 changed files with 263 additions and 16 deletions.
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.board.dto.response.AddBoardResponseDto;
import com.unit.daybook.domain.board.dto.response.BoardTmpResponse;
import com.unit.daybook.domain.board.service.BoardService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
Expand Down Expand Up @@ -41,13 +42,14 @@ public List<AddBoardResponseDto> getRandomBoards() {
}

@PostMapping
public AddBoardResponseDto addBoard(@RequestBody AddBoardRequestDto addBoardRequestDto) {
public BoardTmpResponse addBoard(@RequestBody AddBoardRequestDto addBoardRequestDto) {
Long memberId = 1L; // TODO 인증
return boardService.addBoard(addBoardRequestDto, memberId);
AddBoardResponseDto board = boardService.addBoard(addBoardRequestDto, memberId);
return boardService.getBoardWithHashTag(board.boardId());
}

@PostMapping("/{boardId}")
public AddBoardResponseDto modifyBoard(@PathVariable("boardId") Long boardId, @RequestBody AddBoardRequestDto addBoardRequestDto) {
public BoardTmpResponse modifyBoard(@PathVariable("boardId") Long boardId, @RequestBody AddBoardRequestDto addBoardRequestDto) {
return boardService.modifyBoard(boardId, addBoardRequestDto);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.unit.daybook.domain.board.dto.request;


public record AddBoardRequestDto(String content, Long respectBoardId, String category, Long hearts) {
import java.util.List;

public record AddBoardRequestDto(String content, Long respectBoardId, String category, Long hearts, List<String> hashtags) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@

import com.unit.daybook.domain.board.entity.Board;

import java.util.List;

public record AddBoardResponseDto(
Long boardId,
String content,
Long respectBoardId,
Long authorId,
String category,
Long hearts
Long hearts,
List<String> hashtags

) {

public static AddBoardResponseDto from(Board board) {
return new AddBoardResponseDto(board.getBoardId(), board.getContent(), board.getRespectBoardId(), board.getMemeber().getId(), board.getCategory(), board.getHearts());
List<String> hashContents = board.getHashtags().stream().map(hashtag -> hashtag.getContent()).toList();
return new AddBoardResponseDto(board.getBoardId(), board.getContent(), board.getRespectBoardId(), board.getMemeber().getId(), board.getCategory(), board.getHearts(), hashContents);
}

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

import lombok.Builder;
import lombok.Getter;

import java.util.List;

@Getter
public class BoardTmpResponse {
private Long boardId;
private String content;
private Long respectBoardId;
private Long authorId;
private String category;
private Long hearts;
private List<String> hashtags;

@Builder
public BoardTmpResponse(AddBoardResponseDto dto, List<String> hashtags) {
this.boardId = dto.boardId();
this.content = dto.content();
this.respectBoardId = dto.respectBoardId();
this.authorId = dto.authorId();
this.category = dto.category();
this.hearts = dto.hearts();
this.hashtags = hashtags;
}
}
6 changes: 6 additions & 0 deletions src/main/java/com/unit/daybook/domain/board/entity/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import jakarta.persistence.*;
import lombok.*;

import java.util.ArrayList;
import java.util.List;


@Getter
@Entity
Expand Down Expand Up @@ -34,6 +37,9 @@ public class Board extends BaseTimeEntity {
@Column
private Long hearts;

@OneToMany(mappedBy = "board")
private List<Hashtag> hashtags = new ArrayList<>();

@Builder(access = AccessLevel.PRIVATE)
public Board(Long boardId, String content, Long respectBoardId, Member member, String category, Long hearts) {
this.boardId = boardId;
Expand Down
44 changes: 44 additions & 0 deletions src/main/java/com/unit/daybook/domain/board/entity/Hashtag.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.unit.daybook.domain.board.entity;


import com.unit.daybook.domain.board.dto.request.AddBoardRequestDto;
import com.unit.daybook.domain.member.domain.Member;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.List;

@Getter
@Entity
@Table(name = "hashtag")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Hashtag {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "hashtag_id")
private Long hashtagId;

@Column
private String content;

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

@Builder(access = AccessLevel.PRIVATE)
public Hashtag(Long hashtagId, String content, Board board) {
this.hashtagId = hashtagId;
this.content = content;
this.board = board;
}

public static Hashtag createHashtag(String content, Board board) {
return Hashtag.builder()
.content(content)
.board(board)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.List;

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

@Repository
Expand Down Expand Up @@ -51,4 +52,30 @@ public List<Board> findBoardInBoardIds(List<Long> todayBoards) {
.fetch();

}

public List<Board> findBoardInWith(List<Long> todayBoards) {
return queryFactory
.select(board)
.from(board)
.where(
board.boardId.in(todayBoards)
)
.fetch();

}

public List<Board> findBoardWithHashtag(Long boardId) {
// return queryFactory
// .selectFrom(board)
// .join(board.hashtags, hashtag).fetchJoin()
// .where(board.boardId.eq(boardId))
// .fetch();

return queryFactory
.selectFrom(board)
.innerJoin(board.hashtags, hashtag)
.where(board.boardId.eq(boardId))
.fetch();

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

import com.unit.daybook.domain.board.entity.Board;
import com.unit.daybook.domain.board.entity.Hashtag;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface HashtagRepository extends JpaRepository<Hashtag, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.unit.daybook.domain.board.repository;

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

import com.querydsl.jpa.impl.JPAQueryFactory;
import com.unit.daybook.domain.board.entity.Board;
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.board.entity.QHashtag.hashtag;


@Repository
@RequiredArgsConstructor
public class HashtagRepositoryImpl implements HashtagRepositoryCustom {
private final JPAQueryFactory queryFactory;

public List<Board> test(Long boardId) {

// return queryFactory
// .selectFrom()
// .join(hashtag.board, board).fetchJoin()
// .where(
// board.boardId.eq(1L)
// )
// .fetch();
return null;

}

public List<String> findAllByBoardId(Long boardId) {
return queryFactory
.select(hashtag.content)
.from(hashtag)
.where(
board.boardId.eq(boardId)
).fetch();
}

public long deleteByBoardId(Long boardId) {
return queryFactory
.delete(hashtag)
.where(hashtag.board.boardId.eq(boardId))
.execute();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

import com.unit.daybook.domain.board.dto.request.AddBoardRequestDto;
import com.unit.daybook.domain.board.dto.response.AddBoardResponseDto;
import com.unit.daybook.domain.board.dto.response.BoardTmpResponse;
import com.unit.daybook.domain.board.entity.Board;
import com.unit.daybook.domain.board.entity.Hashtag;
import com.unit.daybook.domain.board.entity.ReadBoard;
import com.unit.daybook.domain.board.repository.BoardRepository;
import com.unit.daybook.domain.board.repository.*;

import com.unit.daybook.domain.board.repository.BoardRepositoryImpl;
import com.unit.daybook.domain.board.repository.ReadBoardRepository;
import com.unit.daybook.domain.board.repository.ReadBoardRepositoryImpl;
import com.unit.daybook.domain.member.domain.Member;
import com.unit.daybook.domain.member.repository.MemberRepository;
import lombok.RequiredArgsConstructor;
Expand All @@ -27,17 +26,26 @@ public class BoardService {
private final MemberRepository memberRepository;
private final ReadBoardRepositoryImpl readBoardRepositoryImpl;
private final ReadBoardRepository readBoardRepository;
private final HashtagRepository hashtagRepository;
private final HashtagRepositoryImpl hashtagRepositoryImpl;

public AddBoardResponseDto addBoard(AddBoardRequestDto addBoardRequestDto, Long memberId) {

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)));
Board board = boardRepository.save(Board.createBoard(addBoardRequestDto, member));

List<String> hashtags = addBoardRequestDto.hashtags();
for (int i = 0; i < hashtags.size(); i++) {
hashtagRepository.save(Hashtag.createHashtag(addBoardRequestDto.hashtags().get(i), board));
}

return AddBoardResponseDto.from(board);
}

@Transactional(readOnly = true)
Expand All @@ -48,7 +56,7 @@ public AddBoardResponseDto getBoard(Long boardId) {

@Transactional(readOnly = true)
public List<AddBoardResponseDto> getMyBoards(Long memberId) {
// TODO 페이지네이션 - 스와이프 방식?
// 페이지네이션x - 스와이프 방식
return boardRepositoryImpl.findBoardsByMemberId(memberId)
.stream()
.map(AddBoardResponseDto::from)
Expand Down Expand Up @@ -114,10 +122,20 @@ public static List<Long> selectRandomNumbers(long n) {
return randomNumbers;
}

public AddBoardResponseDto modifyBoard(Long boardId, AddBoardRequestDto addBoardRequestDto) {
public BoardTmpResponse modifyBoard(Long boardId, AddBoardRequestDto addBoardRequestDto) {
Board board = boardRepository.findById(boardId).orElseThrow(() -> new RuntimeException(boardId + ""));
board.modifyBoard(addBoardRequestDto);
return AddBoardResponseDto.from(board);
List<String> newHashContents = addBoardRequestDto.hashtags();
List<String> newHashContents2 = addBoardRequestDto.hashtags();

List<Hashtag> originHashContents = board.getHashtags();


for(int i =0;i<newHashContents.size();i++) {
hashtagRepository.save(Hashtag.createHashtag(newHashContents.get(i), board));
}

return BoardTmpResponse.builder().dto(AddBoardResponseDto.from(board)).hashtags(newHashContents).build();
}

public Map<String, Long> deleteBoard(Long boardId) {
Expand All @@ -126,4 +144,15 @@ public Map<String, Long> deleteBoard(Long boardId) {
result.put("boardId", boardId);
return result;
}

public BoardTmpResponse getBoardWithHashTag(Long boardId) {
//List<Board> x = boardRepositoryImpl.findBoardWithHashtag(boardId).get(0);

List<String> hashContents = hashtagRepositoryImpl.findAllByBoardId(boardId);
AddBoardResponseDto result = getBoard(boardId);
return BoardTmpResponse.builder().dto(result).hashtags(hashContents).build();
// return AddBoardResponseDto.from(boardRepositoryImpl.findBoardWithHashtag(boardId).get(0));


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

import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@Entity
@Table(name = "comment")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Comment {

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

@Column
private String content;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.unit.daybook.domain.reaction.entity;

import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;


@Getter
@Entity
@Table(name = "reaction")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Reaction {

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

@Column
private String content;

}

0 comments on commit c096520

Please sign in to comment.