Skip to content

Commit

Permalink
Merge pull request #84 from depromeet/feature/#69
Browse files Browse the repository at this point in the history
[feature][#69] 카드 태그의 추가 작업 #69
  • Loading branch information
JeongSangByuk authored Aug 26, 2024
2 parents 9e725fa + 62345c9 commit 1cd97ca
Show file tree
Hide file tree
Showing 27 changed files with 352 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.server.bbo_gak.domain.card.dto.request.CardContentUpdateRequest;
import com.server.bbo_gak.domain.card.dto.request.CardCreateRequest;
import com.server.bbo_gak.domain.card.dto.request.CardTitleUpdateRequest;
import com.server.bbo_gak.domain.card.dto.request.CardTypeUpdateRequest;
import com.server.bbo_gak.domain.card.dto.response.CardCreateResponse;
import com.server.bbo_gak.domain.card.dto.response.CardGetResponse;
import com.server.bbo_gak.domain.card.dto.response.CardListGetResponse;
Expand Down Expand Up @@ -46,9 +47,11 @@ public ResponseEntity<CardGetResponse> getCardDetail(
@GetMapping("/cards")
public ResponseEntity<List<CardListGetResponse>> getCardList(
@AuthUser User user,
@RequestParam("type") String type) {
@RequestParam("type") String type,
@RequestParam(value = "tag-ids", required = false) List<Long> tagIdList
) {

return ResponseEntity.ok(cardService.getCardList(user, type));
return ResponseEntity.ok(cardService.getCardList(user, type, tagIdList));
}

@PostMapping("/card")
Expand All @@ -59,6 +62,16 @@ public ResponseEntity<CardCreateResponse> createCard(
return ResponseEntity.ok(cardService.createCard(user, cardCreateRequest));
}

@PutMapping("/cards/{card-id}/card-type")
public ResponseEntity<Void> updateCardType(
@AuthUser User user,
@PathVariable("card-id") Long cardId,
@RequestBody CardTypeUpdateRequest request
) {
cardService.updateCardType(user, cardId, request);
return ResponseEntity.ok().body(null);
}

@PutMapping("/cards/{card-id}/title")
public ResponseEntity<Void> updateCardTitle(
@AuthUser User user,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ public ResponseEntity<CardTypeCountInRecruitGetResponse> getCardTypeCounts(@Auth
}

@GetMapping("/recruits/{recruit-id}/cards")
public ResponseEntity<List<CardListGetResponse>> getCardDetail(
public ResponseEntity<List<CardListGetResponse>> getCardList(
@AuthUser User user,
@PathVariable("recruit-id") Long recruitId,
@RequestParam("type") String type) {
@RequestParam("type") String type,
@RequestParam(value = "tag-ids", required = false) List<Long> tagIdList) {

return ResponseEntity.ok(cardInRecruitService.getCardListInRecruit(user, recruitId, type));
return ResponseEntity.ok(cardInRecruitService.getCardListInRecruit(user, recruitId, type, tagIdList));
}

@PostMapping("/recruits/{recruit-id}/cards/{card-id}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ public class TagController {
private final TagService tagService;

@GetMapping("/tags")
public ResponseEntity<List<TagGetResponse>> getAllTagList() {
return ResponseEntity.ok(tagService.getAllTagList());
public ResponseEntity<List<TagGetResponse>> getAllTagList(
@AuthUser User user
) {
return ResponseEntity.ok(tagService.getAllTagList(user));
}

@GetMapping("/cards/{card-id}/tags")
Expand Down
8 changes: 2 additions & 6 deletions src/main/java/com/server/bbo_gak/domain/card/dao/CardDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ public List<Card> findAllByUserIdAndCardTypeValueList(User user, CardTypeValue[]
QCard qCard = QCard.card;
QCardType qCardType = QCardType.cardType;

createRecruitBooleanBuilder(qCard, isRecruit);

return query.selectFrom(qCard)
.leftJoin(qCard.cardTypeList, qCardType).fetchJoin()
.where(qCard.user.id.eq(user.getId())
Expand All @@ -41,13 +39,12 @@ public List<Card> findAllByUserIdAndCardTypeValue(User user, CardTypeValue cardT
QCard qCard = QCard.card;
QCardType qCardType = QCardType.cardType;

createRecruitBooleanBuilder(qCard, recruitId);

return query.selectFrom(qCard)
.leftJoin(qCard.cardTypeList, qCardType).fetchJoin()
.where(qCard.user.id.eq(user.getId())
.and(qCardType.cardTypeValue.eq(cardTypeValue))
.and(createRecruitBooleanBuilder(qCard, recruitId)))
.and(createRecruitBooleanBuilder(qCard, recruitId))
)
.distinct()
.fetch();
}
Expand All @@ -72,6 +69,5 @@ private BooleanBuilder createRecruitBooleanBuilder(QCard qCard, Long recruitId)
}

return builder.and(qCard.recruit.id.eq(recruitId)).and(qCard.copyFlag.isTrue());

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ public interface CardRepository extends JpaRepository<Card, Long> {
Optional<Card> findByIdAndUser(Long id, User user);

List<Card> findAllByUser(User user);

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.server.bbo_gak.domain.card.dao;

import com.server.bbo_gak.domain.card.entity.Tag;
import com.server.bbo_gak.domain.user.entity.Job;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;

public interface TagRepository extends JpaRepository<Tag, Long> {

List<Tag> findAllByJob(Job job);

List<Tag> findAllByIdIsNotIn(List<Long> idList);

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ public record CardCreateRequest(
List<String> cardTypeValueList,

@Size(max = 3)
List<Long> tagIdList
List<Long> tagIdList,

String cardTypeValueGroup
) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.server.bbo_gak.domain.card.dto.request;

import java.util.List;

public record CardTypeUpdateRequest(
List<String> cardTypeValueList,

String cardTypeValueGroup
) {

}
10 changes: 10 additions & 0 deletions src/main/java/com/server/bbo_gak/domain/card/entity/Card.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,15 @@ public void updateContent(String content) {
this.content = content;
}

public void updateCardTypeList(List<CardType> cardTypeList) {
this.cardTypeList = cardTypeList;
}

public boolean isTagListContain(List<Tag> tagList) {
return cardTagList.stream()
.map(CardTag::getTag)
.anyMatch(tagList::contains);
}


}
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
package com.server.bbo_gak.domain.card.entity;

import com.server.bbo_gak.global.error.exception.ErrorCode;
import com.server.bbo_gak.global.error.exception.NotFoundException;
import java.util.Arrays;
import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public enum CardTypeValueGroup {

MY_INFO(new CardTypeValue[]{CardTypeValue.EXPERIENCE, CardTypeValue.INTERVIEW_QUESTION,
MY_INFO("내_정보", new CardTypeValue[]{CardTypeValue.EXPERIENCE, CardTypeValue.INTERVIEW_QUESTION,
CardTypeValue.PERSONAL_STATEMENT}),

RECRUIT(new CardTypeValue[]{CardTypeValue.ASSIGNMENT_PREPARING, CardTypeValue.DOCUMENT_PREPARING,
RECRUIT("공고", new CardTypeValue[]{CardTypeValue.ASSIGNMENT_PREPARING, CardTypeValue.DOCUMENT_PREPARING,
CardTypeValue.INTERVIEW_PREPARING});


private String value;
private CardTypeValue[] cardTypeValueList;

public static CardTypeValueGroup findByValue(String value) {
return Arrays.stream(CardTypeValueGroup.values())
.filter(cardType -> cardType.getValue().equals(value))
.findFirst()
.orElseThrow((() -> new NotFoundException(ErrorCode.CARD_TYPE_VALUE_GROUP_NOT_FOUND)));
}

public boolean contains(CardTypeValue cardTypeValue) {
return Arrays.asList(cardTypeValueList).contains(cardTypeValue);
}
}
5 changes: 5 additions & 0 deletions src/main/java/com/server/bbo_gak/domain/card/entity/Tag.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.server.bbo_gak.domain.card.entity;

import com.server.bbo_gak.domain.user.entity.Job;
import com.server.bbo_gak.global.common.BaseEntity;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
Expand Down Expand Up @@ -29,5 +31,8 @@ public class Tag extends BaseEntity {
@Enumerated(EnumType.STRING)
private TagType tagType;

@Enumerated(EnumType.STRING)
private Job job;

private String name;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.server.bbo_gak.domain.card.dao.CardRepository;
import com.server.bbo_gak.domain.card.dao.CardTagRepository;
import com.server.bbo_gak.domain.card.dao.CardTypeRepository;
import com.server.bbo_gak.domain.card.dao.TagRepository;
import com.server.bbo_gak.domain.card.dto.response.CardCreateResponse;
import com.server.bbo_gak.domain.card.dto.response.CardListGetResponse;
import com.server.bbo_gak.domain.card.dto.response.CardTypeCountInRecruitGetResponse;
Expand All @@ -18,13 +19,16 @@
import com.server.bbo_gak.domain.card.entity.CardType;
import com.server.bbo_gak.domain.card.entity.CardTypeValue;
import com.server.bbo_gak.domain.card.entity.CardTypeValueGroup;
import com.server.bbo_gak.domain.card.entity.Tag;
import com.server.bbo_gak.domain.recruit.dao.RecruitRepository;
import com.server.bbo_gak.domain.recruit.entity.Recruit;
import com.server.bbo_gak.domain.user.entity.User;
import com.server.bbo_gak.global.error.exception.ErrorCode;
import com.server.bbo_gak.global.error.exception.NotFoundException;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
Expand All @@ -44,6 +48,7 @@ public class CardInRecruitService {
private final CardTagRepository cardTagRepository;
private final CardImageRepository cardImageRepository;
private final CardCopyInfoRepository cardCopyInfoRepository;
private final TagRepository tagRepository;


@Transactional(readOnly = true)
Expand All @@ -57,12 +62,16 @@ public CardTypeCountInRecruitGetResponse getCardTypeCountsInRecruit(User user) {
}

@Transactional(readOnly = true)
public List<CardListGetResponse> getCardListInRecruit(User user, Long recruitId, String cardTypeValue) {
public List<CardListGetResponse> getCardListInRecruit(User user, Long recruitId, String cardTypeValue
, List<Long> tagIdList) {

List<Tag> tagList = tagRepository.findAllById(Optional.ofNullable(tagIdList).orElse(Collections.emptyList()));

List<Card> cards = cardDao.findAllByUserIdAndCardTypeValue(user, CardTypeValue.findByValue(cardTypeValue),
recruitId);

return cards.stream()
.filter(card -> tagList.isEmpty() || card.isTagListContain(tagList))
.sorted(Comparator.comparing(Card::getUpdatedDate).reversed())
.map(card -> CardListGetResponse.of(card, card.getCardTagList()))
.collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.server.bbo_gak.domain.card.dto.request.CardContentUpdateRequest;
import com.server.bbo_gak.domain.card.dto.request.CardCreateRequest;
import com.server.bbo_gak.domain.card.dto.request.CardTitleUpdateRequest;
import com.server.bbo_gak.domain.card.dto.request.CardTypeUpdateRequest;
import com.server.bbo_gak.domain.card.dto.response.CardCreateResponse;
import com.server.bbo_gak.domain.card.dto.response.CardGetResponse;
import com.server.bbo_gak.domain.card.dto.response.CardListGetResponse;
Expand All @@ -17,11 +18,15 @@
import com.server.bbo_gak.domain.card.entity.CardType;
import com.server.bbo_gak.domain.card.entity.CardTypeValue;
import com.server.bbo_gak.domain.card.entity.CardTypeValueGroup;
import com.server.bbo_gak.domain.card.entity.Tag;
import com.server.bbo_gak.domain.user.entity.User;
import com.server.bbo_gak.global.error.exception.ErrorCode;
import com.server.bbo_gak.global.error.exception.InvalidValueException;
import com.server.bbo_gak.global.error.exception.NotFoundException;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -60,12 +65,22 @@ public CardGetResponse getCardDetail(User user, Long cardId) {
}

@Transactional(readOnly = true)
public List<CardListGetResponse> getCardList(User user, String cardTypeValue) {
public List<CardListGetResponse> getCardList(User user, String cardTypeValue, List<Long> tagIdList) {

List<Tag> tagList = tagRepository.findAllById(Optional.ofNullable(tagIdList).orElse(Collections.emptyList()));

List<Card> cards = cardDao.findAllByUserIdAndCardTypeValue(user, CardTypeValue.findByValue(cardTypeValue),
null);

return cards.stream()
.filter(card -> {

if (tagList.isEmpty()) {
return true;
}

return card.isTagListContain(tagList);
})
.sorted(Comparator.comparing(Card::getUpdatedDate).reversed())
.map(card -> CardListGetResponse.of(card, card.getCardTagList()))
.collect(Collectors.toList());
Expand All @@ -77,9 +92,8 @@ public CardCreateResponse createCard(User user, CardCreateRequest cardCreateRequ

Card card = cardRepository.save(Card.creatEmptyCard(user));

List<CardType> cardTypeList = cardCreateRequest.cardTypeValueList().stream()
.map(cardTypeValue -> new CardType(card, CardTypeValue.findByValue(cardTypeValue)))
.toList();
List<CardType> cardTypeList = getValidCardTypeList(cardCreateRequest.cardTypeValueGroup(), card,
cardCreateRequest.cardTypeValueList());

cardTypeRepository.saveAll(cardTypeList);

Expand All @@ -94,6 +108,24 @@ public CardCreateResponse createCard(User user, CardCreateRequest cardCreateRequ
return new CardCreateResponse(card.getId());
}

@Transactional
public void updateCardType(User user, Long cardId, CardTypeUpdateRequest request) {

Card card = cardRepository.findByIdAndUser(cardId, user)
.orElseThrow(() -> new NotFoundException(ErrorCode.CARD_NOT_FOUND));

cardTypeRepository.deleteAll(card.getCardTypeList());

List<CardType> cardTypeList = getValidCardTypeList(request.cardTypeValueGroup(), card,
request.cardTypeValueList());

cardTypeRepository.saveAll(cardTypeList);

// 양방향 연관 관계 고려 메소드
card.updateCardTypeList(cardTypeList);
}


@Transactional
public void updateCardTitle(User user, Long cardId, CardTitleUpdateRequest request) {

Expand Down Expand Up @@ -121,4 +153,42 @@ public void deleteCard(User user, Long cardId) {
cardTagRepository.deleteAll(card.getCardTagList());
cardRepository.delete(card);
}

private List<CardType> getValidCardTypeList(String cardTypeValueGroupValue, Card card,
List<String> cardTypeValueList) {

CardTypeValueGroup cardTypeValueGroup = CardTypeValueGroup.findByValue(cardTypeValueGroupValue);

List<CardType> cardTypeList = cardTypeValueList.stream()
.map(cardTypeValue -> new CardType(card, CardTypeValue.findByValue(cardTypeValue)))
.toList();

// 내정보에서 카드 생성인 경우
if (cardTypeValueGroup.equals(CardTypeValueGroup.MY_INFO)) {

if (cardTypeList.size() > 1) {
throw new InvalidValueException(ErrorCode.MY_INFO_CARD_TYPE_OVERSIZE);
}

if (!CardTypeValueGroup.MY_INFO.contains(cardTypeList.getFirst().getCardTypeValue())) {
throw new InvalidValueException(ErrorCode.CARD_TYPE_NOT_MATCHED);
}

return cardTypeList;
}

// 공고에서 카드 생성인 경우
if (cardTypeValueGroup.equals(CardTypeValueGroup.RECRUIT)) {

for (CardType cardType : cardTypeList) {
if (!CardTypeValueGroup.RECRUIT.contains(cardType.getCardTypeValue())) {
throw new InvalidValueException(ErrorCode.CARD_TYPE_NOT_MATCHED);
}
}

return cardTypeList;
}

throw new InvalidValueException(ErrorCode.CARD_TYPE_NOT_FOUND);
}
}
Loading

0 comments on commit 1cd97ca

Please sign in to comment.