Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: 해시태그에 카테고리 적용 완료 #647

Merged
merged 8 commits into from
Jan 1, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -12,8 +13,6 @@
import page.clab.api.domain.community.board.application.port.in.RetrieveHotBoardsUseCase;
import page.clab.api.global.common.dto.ApiResponse;

import java.util.List;

@RestController
@RequestMapping("/api/v1/boards")
@RequiredArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package page.clab.api.domain.community.board.adapter.out.persistence;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
Expand All @@ -10,10 +13,6 @@
import page.clab.api.domain.community.board.domain.BoardCategory;
import page.clab.api.global.exception.NotFoundException;

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

@Component
@RequiredArgsConstructor
public class BoardPersistenceAdapter implements
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package page.clab.api.domain.community.board.adapter.out.persistence;

import java.util.List;
import java.util.Objects;
import java.util.Set;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import page.clab.api.domain.community.board.application.port.out.RegisterHotBoardPort;
import page.clab.api.domain.community.board.application.port.out.RemoveHotBoardPort;
import page.clab.api.domain.community.board.application.port.out.RetrieveHotBoardPort;

import java.util.List;
import java.util.Objects;
import java.util.Set;

@Component
@RequiredArgsConstructor
public class RedisHotBoardPersistenceAdapter implements
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import page.clab.api.domain.community.board.application.dto.response.BoardDetailsResponseDto;
import page.clab.api.domain.community.board.application.dto.response.BoardEmojiCountResponseDto;
import page.clab.api.domain.community.board.application.dto.response.BoardHashtagResponseDto;
import page.clab.api.domain.community.board.application.dto.response.BoardEmojiToggleResponseDto;
import page.clab.api.domain.community.board.application.dto.response.BoardListResponseDto;
import page.clab.api.domain.community.board.application.dto.response.BoardMyResponseDto;
import page.clab.api.domain.community.board.application.dto.response.BoardOverviewResponseDto;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ public BoardHashtag fromDto(Long boardId, Long hashtagId) {
.build();
}

public BoardHashtagRequestDto toDto(Long boardId, List<Long> hashtagIdList) {
public BoardHashtagRequestDto toDto(Long boardId, List<String> hashtagNameList) {
return BoardHashtagRequestDto.builder()
.boardId(boardId)
.hashtagIds(hashtagIdList)
.hashtagNameList(hashtagNameList)
limehee marked this conversation as resolved.
Show resolved Hide resolved
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
public class BoardHashtagRequestDto {

private Long boardId;
private List<Long> hashtagIds;
private List<String> hashtagNameList;
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ public class BoardRequestDto {
@Schema(description = "익명 사용 여부", example = "false", required = true)
private boolean wantAnonymous;

@Schema(description = "해시태그 id 리스트", example = "[1, 2]")
private List<Long> hashtagIdList;
@Schema(description = "해시태그 이름 리스트", example = "[\"Java\", \"C++\"]")
private List<String> hashtagNameList;
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ public class BoardUpdateRequestDto {
@Schema(description = "익명 사용 여부", example = "false")
private boolean wantAnonymous;

@Schema(description = "해시태그 id 리스트", example = "[1, 2]")
private List<Long> hashtagIdList;
@Schema(description = "해시태그 이름 리스트", example = "[\"Java\", \"Devops\"]")
private List<String> hashtagNameList;
limehee marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package page.clab.api.domain.community.board.application.port.in;

import page.clab.api.domain.community.board.application.dto.response.BoardListResponseDto;

import java.util.List;
import page.clab.api.domain.community.board.application.dto.response.BoardListResponseDto;

public interface RetrieveHotBoardsUseCase {
List<BoardListResponseDto> retrieveHotBoards(String strategyName);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package page.clab.api.domain.community.board.application.port.out;

import java.time.LocalDateTime;
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import page.clab.api.domain.community.board.domain.Board;
import page.clab.api.domain.community.board.domain.BoardCategory;

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

public interface RetrieveBoardPort {

Board getById(Long boardId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public class BoardHashtagRegisterService implements RegisterBoardHashtagUseCase
@Override
public Long registerBoardHashtag(BoardHashtagRequestDto requestDto) {
Long boardId = requestDto.getBoardId();
for (Long hashtagId : requestDto.getHashtagIds()) {
Hashtag hashtag = externalRetrieveHashtagUseCase.getById(hashtagId);
BoardHashtag boardHashtag = mapper.fromDto(boardId, hashtagId);
for (String hashtagName : requestDto.getHashtagNameList()) {
Hashtag hashtag = externalRetrieveHashtagUseCase.getByName(hashtagName);
BoardHashtag boardHashtag = mapper.fromDto(boardId, hashtag.getId());
registerBoardHashtagPort.save(boardHashtag);
hashtag.incrementBoardUsage();
externalRegisterHashtagUseCase.save(hashtag);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public String registerBoard(BoardRequestDto requestDto) throws PermissionDeniedE
List<UploadedFile> uploadedFiles = uploadedFileService.getUploadedFilesByUrls(requestDto.getFileUrlList());
Board board = boardDtoMapper.fromDto(requestDto, currentMemberInfo.getMemberId(), uploadedFiles);
board.validateAccessPermissionForCreation(currentMemberInfo);
board.validateBoardHashtagRegistration(requestDto.getHashtagIdList());
board.validateBoardHashtagRegistration(requestDto.getHashtagNameList());

if (board.shouldNotifyForNewBoard(currentMemberInfo)) {
externalSendNotificationUseCase.sendNotificationToMember(currentMemberInfo.getMemberId(),
Expand All @@ -65,9 +65,9 @@ public String registerBoard(BoardRequestDto requestDto) throws PermissionDeniedE

Board savedBoard = registerBoardPort.save(board);

if (requestDto.getHashtagIdList() != null) {
if (requestDto.getHashtagNameList() != null) {
limehee marked this conversation as resolved.
Show resolved Hide resolved
registerBoardHashtagUseCase.registerBoardHashtag(
boardHashtagDtoMapper.toDto(savedBoard.getId(), requestDto.getHashtagIdList()));
boardHashtagDtoMapper.toDto(savedBoard.getId(), requestDto.getHashtagNameList()));
}
return savedBoard.getCategory().getKey();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ public String updateBoard(Long boardId, BoardUpdateRequestDto requestDto) throws

board.update(requestDto);

List<Long> hastagIdList = requestDto.getHashtagIdList();
List<String> hashtagNameList = requestDto.getHashtagNameList();

List<Long> hastagIdList = hashtagNameList.stream()
.map(externalRetrieveHashtagUseCase::getIdByName)
.toList();
handleBoardHashtagUpdate(boardId, board, hastagIdList);

eventPublisher.publishEvent(new BoardUpdatedEvent(this, board.getId()));
Expand All @@ -70,6 +74,9 @@ private void deleteAllHashtagsForBoard(Long boardId) {
currentBoardHashtags.forEach(boardHashtag -> {
if (!boardHashtag.getIsDeleted()) {
boardHashtag.toggleIsDeletedStatus();
Hashtag hashtag = externalRetrieveHashtagUseCase.getById(boardHashtag.getHashtagId());
hashtag.decreaseBoardUsage();
externalRegisterHashtagUseCase.save(hashtag);
registerBoardHashtagPort.save(boardHashtag);
}
});
Expand Down Expand Up @@ -151,7 +158,8 @@ private void addOrUpdateHashtag(Long boardId, Long hashtagId, List<BoardHashtag>
},
() -> {
registerBoardHashtagUseCase.registerBoardHashtag(
boardHashtagDtoMapper.toDto(boardId, List.of(hashtagId))
boardHashtagDtoMapper.toDto(boardId,
List.of(externalRetrieveHashtagUseCase.getNameById(hashtagId)))
);
}
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package page.clab.api.domain.community.board.application.service;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -8,13 +14,6 @@
import page.clab.api.domain.community.board.domain.Board;
import page.clab.api.external.community.comment.application.port.ExternalRetrieveCommentUseCase;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Service(HotBoardSelectionStrategies.DEFAULT)
@RequiredArgsConstructor
public class DefaultHotBoardSelectionStrategy implements HotBoardSelectionStrategy {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package page.clab.api.domain.community.board.application.service;

import java.util.List;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
Expand All @@ -8,9 +10,6 @@
import page.clab.api.domain.community.board.application.port.out.RemoveHotBoardPort;
import page.clab.api.domain.community.board.domain.Board;

import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class HotBoardRegisterService {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.drew.lang.annotations.NotNull;
import jakarta.transaction.Transactional;
import java.util.List;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import page.clab.api.domain.community.board.application.dto.mapper.BoardDtoMapper;
Expand All @@ -15,9 +17,6 @@
import page.clab.api.external.community.comment.application.port.ExternalRetrieveCommentUseCase;
import page.clab.api.external.memberManagement.member.application.port.ExternalRetrieveMemberUseCase;

import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class HotBoardRetrievalService implements RetrieveHotBoardsUseCase {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package page.clab.api.domain.community.board.application.service;

import page.clab.api.domain.community.board.domain.Board;

import java.util.List;
import page.clab.api.domain.community.board.domain.Board;

public interface HotBoardSelectionStrategy {
List<Board> getHotBoards();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ public void validateAccessPermissionForCreation(MemberDetailedInfoDto currentMem
}
}

public void validateBoardHashtagRegistration(List<Long> hashtagIds) {
if (!isDevelopmentQna() && (hashtagIds != null && !hashtagIds.isEmpty())) {
public void validateBoardHashtagRegistration(List<String> hashtagNameList) {
if (!isDevelopmentQna() && (hashtagNameList != null && !hashtagNameList.isEmpty())) {
throw new InvalidBoardCategoryHashtagException("개발질문 게시판에만 해시태그를 등록할 수 있습니다.");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class HashtagRegisterController {
@PreAuthorize("hasRole('ADMIN')")
@PostMapping("")
public ApiResponse<List<HashtagResponseDto>> registerHashtag(
@Valid @RequestBody HashtagRequestDto requestDto
@Valid @RequestBody List<HashtagRequestDto> requestDto
) {
List<HashtagResponseDto> hashTags = registerHashtagUseCase.registerHashtag(requestDto);
return ApiResponse.success(hashTags);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
Expand All @@ -14,6 +16,7 @@
import lombok.Setter;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.SQLRestriction;
import page.clab.api.domain.community.hashtag.domain.HashtagCategory;
import page.clab.api.global.common.domain.BaseEntity;

@Entity
Expand All @@ -34,6 +37,10 @@ public class HashtagJpaEntity extends BaseEntity {
@Column(name = "name", unique = true, nullable = false)
private String name;

@Column(name = "hashtag_category", nullable = false)
@Enumerated(EnumType.STRING)
private HashtagCategory hashtagCategory;

@Column(name = "board_usage", nullable = false)
private Long boardUsage;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
import org.springframework.stereotype.Component;
import page.clab.api.domain.community.hashtag.application.dto.response.HashtagResponseDto;
import page.clab.api.domain.community.hashtag.domain.Hashtag;
import page.clab.api.domain.community.hashtag.domain.HashtagCategory;

@Component
@RequiredArgsConstructor
public class HashtagDtoMapper {

public Hashtag of(String name) {
public Hashtag of(String name, HashtagCategory hashtagCategory) {
return Hashtag.builder()
.name(name)
.hashtagCategory(hashtagCategory)
.boardUsage(0L)
.isDeleted(false)
.build();
Expand All @@ -21,6 +23,7 @@ public HashtagResponseDto toDto(Hashtag hashtag) {
return HashtagResponseDto.builder()
.id(hashtag.getId())
.name(hashtag.getName())
.hashtagCategory(hashtag.getHashtagCategory().getKey())
.boardUsageCount(hashtag.getBoardUsage())
.build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package page.clab.api.domain.community.hashtag.application.dto.request;

import java.util.ArrayList;
import java.util.List;
import jakarta.validation.constraints.NotNull;
import lombok.Getter;
import lombok.Setter;
import page.clab.api.domain.community.hashtag.domain.HashtagCategory;

@Getter
@Setter
public class HashtagRequestDto {

List<String> hashtagNames = new ArrayList<>();
private String name;
limehee marked this conversation as resolved.
Show resolved Hide resolved

@NotNull
private HashtagCategory hashtagCategory;
limehee marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ public class HashtagResponseDto {

private Long id;
private String name;
private String hashtagCategory;
private Long boardUsageCount;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

public interface RegisterHashtagUseCase {

List<HashtagResponseDto> registerHashtag(HashtagRequestDto requestDto);
List<HashtagResponseDto> registerHashtag(List<HashtagRequestDto> requestDto);
}
Loading
Loading