-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Feature/#117
- Loading branch information
Showing
12 changed files
with
327 additions
and
37 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
src/main/java/com/server/bbo_gak/domain/card/controller/CardSearchController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.server.bbo_gak.domain.card.controller; | ||
|
||
import com.server.bbo_gak.domain.card.dto.response.CardSearchByTagListResponse; | ||
import com.server.bbo_gak.domain.card.dto.response.TagGetResponse; | ||
import com.server.bbo_gak.domain.card.service.CardSearchService; | ||
import com.server.bbo_gak.domain.user.entity.User; | ||
import com.server.bbo_gak.global.annotation.AuthUser; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("/api/v1") | ||
@RequiredArgsConstructor | ||
public class CardSearchController { | ||
|
||
private final CardSearchService cardSearchService; | ||
|
||
@GetMapping("/search/cards") | ||
public ResponseEntity<List<CardSearchByTagListResponse>> searchCardByTagList( | ||
@AuthUser User user, | ||
@RequestParam(value = "card-type-value-group", required = false) String cardTypeValueGroup, | ||
@RequestParam(value = "tag-ids") List<Long> tagIdList) { | ||
return ResponseEntity.ok(cardSearchService.searchCardByTagList(user, cardTypeValueGroup, tagIdList)); | ||
} | ||
|
||
@GetMapping("/search/card-tag-history") | ||
public ResponseEntity<List<TagGetResponse>> getCardTagSearchHistory(@AuthUser User user) { | ||
return ResponseEntity.ok(cardSearchService.getCardTagSearchHistoryList(user)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/main/java/com/server/bbo_gak/domain/card/dao/CardTagSearchHistoryRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.server.bbo_gak.domain.card.dao; | ||
|
||
import com.server.bbo_gak.domain.card.entity.CardTagSearchHistory; | ||
import com.server.bbo_gak.domain.user.entity.User; | ||
import java.util.List; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface CardTagSearchHistoryRepository extends JpaRepository<CardTagSearchHistory, Long> { | ||
|
||
List<CardTagSearchHistory> findTop10ByUserOrderByCreatedDate(User user); | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/com/server/bbo_gak/domain/card/dto/request/CardSearchByTagListRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.server.bbo_gak.domain.card.dto.request; | ||
|
||
import com.server.bbo_gak.domain.card.entity.CardTypeValue; | ||
import com.server.bbo_gak.domain.card.entity.CardTypeValueGroup; | ||
import jakarta.annotation.Nullable; | ||
import jakarta.validation.constraints.Size; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public record CardSearchByTagListRequest( | ||
|
||
@Size(min = 1) | ||
List<Long> tagIdList, | ||
|
||
@Nullable | ||
String cardTypeValueGroup | ||
) { | ||
|
||
public CardTypeValue[] getCardTypeValueList() { | ||
return Optional.ofNullable(cardTypeValueGroup) | ||
.map(cardTypeValueGroup -> CardTypeValueGroup.findByValue(cardTypeValueGroup).getCardTypeValueList()) | ||
.orElseGet(() -> new CardTypeValue[0]); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/main/java/com/server/bbo_gak/domain/card/dto/response/CardSearchByTagListResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.server.bbo_gak.domain.card.dto.response; | ||
|
||
import com.server.bbo_gak.domain.card.entity.Card; | ||
import com.server.bbo_gak.domain.card.entity.CardTypeValue; | ||
import com.server.bbo_gak.domain.card.entity.CardTypeValueGroup; | ||
import com.server.bbo_gak.domain.recruit.entity.Recruit; | ||
import com.server.bbo_gak.global.utils.BaseDateTimeFormatter; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
|
||
@Builder(access = AccessLevel.PRIVATE) | ||
public record CardSearchByTagListResponse( | ||
Long id, | ||
String title, | ||
String updatedDate, | ||
List<TagGetResponse> tagList, | ||
String cardTypeValueGroup, | ||
String cardTypeValue, | ||
String recruitTitle, | ||
String content | ||
) { | ||
|
||
public static CardSearchByTagListResponse from(Card card) { | ||
|
||
CardTypeValue cardTypeValue = card.getCardTypeList().getFirst().getCardTypeValue(); | ||
|
||
return CardSearchByTagListResponse.builder() | ||
.id(card.getId()) | ||
.title(card.getTitle()) | ||
.updatedDate(card.getUpdatedDate().format(BaseDateTimeFormatter.getLocalDateTimeFormatter())) | ||
.tagList( | ||
card.getCardTagList().stream() | ||
.map(cardTag -> TagGetResponse.from(cardTag.getTag())) | ||
.toList() | ||
) | ||
.cardTypeValueGroup(CardTypeValueGroup.findByCardTypeValue(cardTypeValue).getValue()) | ||
.recruitTitle(Optional.ofNullable(card.getRecruit()).map(Recruit::getTitle).orElse(null)) | ||
.cardTypeValue(cardTypeValue.getValue()) | ||
.content(card.getContent()) | ||
.build(); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/main/java/com/server/bbo_gak/domain/card/entity/CardTagSearchHistory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.server.bbo_gak.domain.card.entity; | ||
|
||
import com.server.bbo_gak.domain.user.entity.User; | ||
import com.server.bbo_gak.global.common.BaseEntity; | ||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.FetchType; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.JoinColumn; | ||
import jakarta.persistence.ManyToOne; | ||
import lombok.AccessLevel; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import org.hibernate.annotations.SQLDelete; | ||
import org.hibernate.annotations.SQLRestriction; | ||
|
||
@Getter | ||
@Entity | ||
@SQLRestriction("deleted = false") | ||
@SQLDelete(sql = "UPDATE card_tag_search_history SET deleted = true WHERE card_tag_search_history_id = ?") | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class CardTagSearchHistory extends BaseEntity { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
@Column(name = "card_tag_search_history_id") | ||
private Long id; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "user_id") | ||
private User user; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "tag_id") | ||
private Tag tag; | ||
|
||
public CardTagSearchHistory(User user, Tag tag) { | ||
this.user = user; | ||
this.tag = tag; | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
src/main/java/com/server/bbo_gak/domain/card/service/CardSearchService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.server.bbo_gak.domain.card.service; | ||
|
||
import com.server.bbo_gak.domain.card.dao.CardDao; | ||
import com.server.bbo_gak.domain.card.dao.CardTagSearchHistoryRepository; | ||
import com.server.bbo_gak.domain.card.dao.TagRepository; | ||
import com.server.bbo_gak.domain.card.dto.response.CardSearchByTagListResponse; | ||
import com.server.bbo_gak.domain.card.dto.response.TagGetResponse; | ||
import com.server.bbo_gak.domain.card.entity.Card; | ||
import com.server.bbo_gak.domain.card.entity.CardTagSearchHistory; | ||
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 java.util.Comparator; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class CardSearchService { | ||
|
||
private final CardTagSearchHistoryRepository cardTagSearchHistoryRepository; | ||
private final TagRepository tagRepository; | ||
private final CardDao cardDao; | ||
|
||
@Transactional | ||
public List<CardSearchByTagListResponse> searchCardByTagList(User user, String cardTypeValueGroup, | ||
List<Long> tagIdList) { | ||
|
||
List<Tag> tagList = tagRepository.findAllById(tagIdList); | ||
|
||
List<Card> cards = cardDao.findAllByUserIdAndCardTypeValueList(user, getCardTypeValueList(cardTypeValueGroup), | ||
null); | ||
|
||
cardTagSearchHistoryRepository.saveAll(tagList.stream() | ||
.map(tag -> new CardTagSearchHistory(user, tag)) | ||
.toList()); | ||
|
||
// TODO 필터링 로직 디비로 가게 하기 | ||
return cards.stream() | ||
.filter(card -> tagList.isEmpty() || card.isTagListContain(tagList)) | ||
.sorted(Comparator.comparing(Card::getUpdatedDate).reversed()) | ||
.map(CardSearchByTagListResponse::from) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
@Transactional(readOnly = true) | ||
public List<TagGetResponse> getCardTagSearchHistoryList(User user) { | ||
return cardTagSearchHistoryRepository.findTop10ByUserOrderByCreatedDate(user).stream() | ||
.map(cardTagSearchHistory -> TagGetResponse.from(cardTagSearchHistory.getTag())) | ||
.toList(); | ||
} | ||
|
||
private CardTypeValue[] getCardTypeValueList(String cardTypeValueGroupInput) { | ||
return Optional.ofNullable(cardTypeValueGroupInput) | ||
.map(cardTypeValueGroup -> CardTypeValueGroup.findByValue(cardTypeValueGroup).getCardTypeValueList()) | ||
.orElseGet(() -> new CardTypeValue[0]); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.