Skip to content

Commit 26bfec5

Browse files
Merge pull request #100 from prgrms-web-devcourse-final-project/feat/#92
[Artist] 아티스트 검색
2 parents c8d4cd2 + 8e7c68d commit 26bfec5

7 files changed

Lines changed: 59 additions & 8 deletions

File tree

src/main/java/com/back/web7_9_codecrete_be/domain/artists/controller/ArtistsController.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
package com.back.web7_9_codecrete_be.domain.artists.controller;
22

33
import com.back.web7_9_codecrete_be.domain.artists.dto.request.CreateRequest;
4+
import com.back.web7_9_codecrete_be.domain.artists.dto.request.SearchRequest;
45
import com.back.web7_9_codecrete_be.domain.artists.dto.request.UpdateRequest;
56
import com.back.web7_9_codecrete_be.domain.artists.dto.response.ArtistListResponse;
67
import com.back.web7_9_codecrete_be.domain.artists.dto.response.ArtistDetailResponse;
8+
import com.back.web7_9_codecrete_be.domain.artists.dto.response.SearchResponse;
79
import com.back.web7_9_codecrete_be.domain.artists.service.ArtistService;
810
import com.back.web7_9_codecrete_be.domain.artists.service.ArtistEnrichService;
911
import com.back.web7_9_codecrete_be.global.rsData.RsData;
1012
import io.swagger.v3.oas.annotations.Operation;
1113
import io.swagger.v3.oas.annotations.tags.Tag;
14+
import jakarta.validation.Valid;
1215
import lombok.RequiredArgsConstructor;
1316
import org.springframework.web.bind.annotation.*;
1417

@@ -80,12 +83,13 @@ public RsData<Void> delete(
8083
return RsData.success("아티스트 정보를 삭제했습니다.", null);
8184
}
8285

83-
@Operation(summary = "아티스트 검색", description = "아티스트 이름을 입력받아 검색합니다.")
84-
@PostMapping("/{id}")
85-
public RsData<Void> search(
86-
@PathVariable Long id
86+
@Operation(summary = "아티스트 검색",
87+
description = "아티스트 이름 또는 키워드를 입력하면, 해당 키워드가 포함된 아티스트 목록 또는 이름에 해당하는 아티스트를 조회합니다.")
88+
@PostMapping("/search")
89+
public RsData<List<SearchResponse>> search(
90+
@Valid @RequestBody SearchRequest reqBody
8791
) {
88-
return RsData.success("아티스트 검색에 성공했습니다.", null);
92+
return RsData.success("아티스트 검색에 성공했습니다.", artistService.search(reqBody.artistName()));
8993
}
9094

9195
@Operation(summary = "아티스트 찜하기", description = "id 에 해당하는 특정 아티스트를 찜합니다.")
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.back.web7_9_codecrete_be.domain.artists.dto.request;
2+
3+
import jakarta.validation.constraints.NotBlank;
4+
5+
public record SearchRequest(
6+
@NotBlank(message = "검색어를 입력해주세요")
7+
String artistName
8+
) {
9+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.back.web7_9_codecrete_be.domain.artists.dto.response;
2+
3+
import com.back.web7_9_codecrete_be.domain.artists.entity.Artist;
4+
5+
public record SearchResponse(
6+
String artistName,
7+
String artistGroup,
8+
int likeCount
9+
) {
10+
public static SearchResponse from(Artist artist) {
11+
return new SearchResponse(
12+
artist.getArtistName(),
13+
artist.getArtistGroup(),
14+
artist.getLikeCount()
15+
);
16+
}
17+
}

src/main/java/com/back/web7_9_codecrete_be/domain/artists/entity/Artist.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ public class Artist {
3737
@Column(name = "name_ko", length = 200)
3838
private String nameKo;
3939

40+
@Column(name = "like_count", nullable = false)
41+
private int likeCount = 0;
42+
4043
public Artist(String spotifyArtistId, String artistName, String artistGroup, ArtistType artistType, Genre genre) {
4144
this.spotifyArtistId = spotifyArtistId;
4245
this.artistName = artistName;

src/main/java/com/back/web7_9_codecrete_be/domain/artists/repository/ArtistRepository.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import org.springframework.stereotype.Repository;
88

99
import java.util.List;
10-
import java.util.Optional;
1110

1211
@Repository
1312
public interface ArtistRepository extends JpaRepository<Artist, Long> {
@@ -21,4 +20,6 @@ public interface ArtistRepository extends JpaRepository<Artist, Long> {
2120

2221
List<Artist> findTop5ByArtistGroupAndIdNot(String artistGroup, long excludeId);
2322
List<Artist> findTop5ByGenreIdAndIdNot(Long genreId, long excludeId);
23+
24+
List<Artist> findAllByArtistNameContainingIgnoreCaseOrNameKoContainingIgnoreCase(String artistName1, String artistName2);
2425
}

src/main/java/com/back/web7_9_codecrete_be/domain/artists/service/ArtistService.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.back.web7_9_codecrete_be.domain.artists.dto.request.UpdateRequest;
44
import com.back.web7_9_codecrete_be.domain.artists.dto.response.ArtistListResponse;
55
import com.back.web7_9_codecrete_be.domain.artists.dto.response.ArtistDetailResponse;
6+
import com.back.web7_9_codecrete_be.domain.artists.dto.response.SearchResponse;
67
import com.back.web7_9_codecrete_be.domain.artists.entity.Artist;
78
import com.back.web7_9_codecrete_be.domain.artists.entity.ArtistType;
89
import com.back.web7_9_codecrete_be.domain.artists.entity.Genre;
@@ -98,7 +99,7 @@ public void updateArtist(Long id, UpdateRequest req) {
9899
}
99100

100101
if (!changed) {
101-
throw new BusinessException(ArtistErrorCode.INVALID_UPDATE_REQUEST); // "수정할 값이 없습니다"
102+
throw new BusinessException(ArtistErrorCode.INVALID_UPDATE_REQUEST);
102103
}
103104
}
104105

@@ -109,4 +110,19 @@ public void delete(Long id) {
109110
artistRepository.delete(artist);
110111
}
111112

113+
@Transactional
114+
public List<SearchResponse> search(String artistName) {
115+
116+
List<Artist> artists =
117+
artistRepository.findAllByArtistNameContainingIgnoreCaseOrNameKoContainingIgnoreCase(artistName, artistName);
118+
119+
if (artists.isEmpty()) {
120+
throw new BusinessException(ArtistErrorCode.ARTIST_NOT_FOUND);
121+
}
122+
123+
return artists.stream()
124+
.map(SearchResponse::from)
125+
.toList();
126+
}
127+
112128
}

src/main/java/com/back/web7_9_codecrete_be/global/error/code/ArtistErrorCode.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ public enum ArtistErrorCode implements ErrorCode {
1414
ARTIST_NOT_FOUND(HttpStatus.NOT_FOUND, "AT-102", "아티스트를 찾을 수 없습니다."),
1515
ARTIST_ALREADY_EXISTS(HttpStatus.CONFLICT, "AT-103", "이미 존재하는 아티스트입니다."),
1616
SPOTIFY_NOT_FOUND(HttpStatus.NOT_FOUND, "AT-104", "존재하지 않는 Spotify Artist Key 입니다."),
17-
INVALID_UPDATE_REQUEST(HttpStatus.BAD_REQUEST, "AT-105", "수정할 내용이 없습니다.");
17+
INVALID_UPDATE_REQUEST(HttpStatus.BAD_REQUEST, "AT-105", "수정할 내용이 없습니다."),
18+
INVALID_SEARCH_KEYWORD(HttpStatus.BAD_REQUEST, "AT-106", "입력 값이 없습니다.");
1819

1920
private final HttpStatus status;
2021
private final String code;

0 commit comments

Comments
 (0)