Skip to content

Commit

Permalink
feat: ArtistSynonym 엔티티 및 테이블 생성
Browse files Browse the repository at this point in the history
  • Loading branch information
somsom13 committed Oct 10, 2023
1 parent df3248e commit 73036d1
Show file tree
Hide file tree
Showing 10 changed files with 245 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public enum ErrorCode {
WRONG_GENRE_TYPE(3013, "잘못된 장르 타입입니다."),
EMPTY_ARTIST_PROFILE_URL(3014, "가수 프로필 이미지는 비어있을 수 없습니다."),
TOO_LONG_ARTIST_PROFILE_URL(3015, "가수 프로필 이미지URL은 65,356자를 넘길 수 없습니다."),
EMPTY_ARTIST_SYNONYM(3016, "가수 동의어는 비어있을 수 없습니다."),
TOO_LONG_ARTIST_SYNONYM(3017, "가수 동의어는 255자를 넘길 수 없습니다.."),


// 4000: 투표
Expand Down
62 changes: 62 additions & 0 deletions backend/src/main/java/shook/shook/song/domain/ArtistSynonym.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package shook.shook.song.domain;

import jakarta.persistence.Embedded;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.ForeignKey;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import java.util.Objects;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

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

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "artist_id", foreignKey = @ForeignKey(name = "none"), updatable = false, nullable = false)
private Artist artist;

@Embedded
private Synonym synonym;

public String getArtistName() {
return artist.getArtistName();
}

public String getSynonym() {
return synonym.getValue();
}

@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final ArtistSynonym artistSynonym = (ArtistSynonym) o;
if (Objects.isNull(artistSynonym.id) || Objects.isNull(this.id)) {
return false;
}
return Objects.equals(id, artistSynonym.id);
}

@Override
public int hashCode() {
return Objects.hash(id);
}
}
39 changes: 39 additions & 0 deletions backend/src/main/java/shook/shook/song/domain/Synonym.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package shook.shook.song.domain;

import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;
import java.util.Map;
import lombok.AccessLevel;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import shook.shook.song.exception.ArtistException;
import shook.shook.util.StringChecker;

@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
@EqualsAndHashCode
@Embeddable
public class Synonym {

private static final int MAXIMUM_LENGTH = 255;

@Column(name = "synonym", nullable = false)
private String value;

public Synonym(final String value) {
validate(value);
this.value = value;
}

private void validate(final String value) {
if (StringChecker.isNullOrBlank(value)) {
throw new ArtistException.NullOrEmptySynonymException();
}
if (value.length() > MAXIMUM_LENGTH) {
throw new ArtistException.TooLongSynonymException(
Map.of("ArtistSynonym", value)
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,26 @@ public TooLongNameException(final Map<String, String> inputValuesByProperty) {
super(ErrorCode.TOO_LONG_SINGER_NAME, inputValuesByProperty);
}
}

public static class NullOrEmptySynonymException extends ArtistException {

public NullOrEmptySynonymException() {
super(ErrorCode.EMPTY_ARTIST_SYNONYM);
}

public NullOrEmptySynonymException(final Map<String, String> inputValuesByProperty) {
super(ErrorCode.EMPTY_ARTIST_SYNONYM, inputValuesByProperty);
}
}

public static class TooLongSynonymException extends ArtistException {

public TooLongSynonymException() {
super(ErrorCode.TOO_LONG_ARTIST_SYNONYM);
}

public TooLongSynonymException(final Map<String, String> inputValuesByProperty) {
super(ErrorCode.TOO_LONG_ARTIST_SYNONYM, inputValuesByProperty);
}
}
}
9 changes: 9 additions & 0 deletions backend/src/main/resources/dev/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ drop table if exists voting_song;
drop table if exists vote;
drop table if exists member;
drop table if exists artist;
drop table if exists artist_synonym;

create table if not exists song
(
Expand Down Expand Up @@ -102,3 +103,11 @@ create table if not exists artist
created_at timestamp(6) not null,
primary key (id)
);

create table if not exists artist_synonym
(
id bigint auto_increment,
artist_id bigint not null,
synonym varchar(255) not null,
primary key (id)
);
8 changes: 8 additions & 0 deletions backend/src/main/resources/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,11 @@ ALTER TABLE song ADD COLUMN artist_id BIGINT NOT NULL;
ALTER TABLE song DROP COLUMN singer;
ALTER TABLE voting_song ADD COLUMN artist_id BIGINT NOT NULL;
ALTER TABLE voting_song DROP COLUMN singer;

create table if not exists artist_synonym
(
id bigint auto_increment,
artist_id bigint not null,
synonym varchar(255) not null,
primary key (id)
);
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

class ArtistNameTest {

@DisplayName("가수을 뜻하는 객체를 생성한다.")
@DisplayName("가수 이름을 뜻하는 객체를 생성한다.")
@Test
void create_success() {
//given
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package shook.shook.song.domain;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.NullSource;
import org.junit.jupiter.params.provider.ValueSource;
import shook.shook.song.exception.ArtistException;

class ProfileImageUrlTest {

@DisplayName("ProfileImageUrl 을 생성한다.")
@Test
void create_success() {
// given
// when, then
assertDoesNotThrow(() -> new ProfileImageUrl("image"));
}

@DisplayName("이미지 URL이 비어있으면 예외를 던진다.")
@NullSource
@ParameterizedTest(name = "이미지 URL이 \"{0}\" 일 때")
@ValueSource(strings = {"", " "})
void create_fail_lessThanOne(final String value) {
//given
//when
//then
assertThatThrownBy(() -> new ProfileImageUrl(value))
.isInstanceOf(ArtistException.NullOrEmptyProfileUrlException.class);
}

@DisplayName("이미지 URL의 길이가 65_536을 넘을 경우 예외를 던진다.")
@Test
void create_fail_lengthOver65_536() {
//given
final String name = ".".repeat(65_537);

//when
//then
assertThatThrownBy(() -> new ProfileImageUrl(name))
.isInstanceOf(ArtistException.TooLongProfileUrlException.class);
}
}
47 changes: 47 additions & 0 deletions backend/src/test/java/shook/shook/song/domain/SynonymTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package shook.shook.song.domain;

import static org.assertj.core.api.Assertions.assertThatThrownBy;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.NullSource;
import org.junit.jupiter.params.provider.ValueSource;
import shook.shook.song.exception.ArtistException;

class SynonymTest {

@DisplayName("가수 이름 동의어를 뜻하는 객체를 생성한다.")
@Test
void create_success() {
//given
//when
//then
Assertions.assertDoesNotThrow(() -> new Synonym("동의어"));
}

@DisplayName("가수 이름 동의어가 유효하지 않으면 예외를 던진다.")
@NullSource
@ParameterizedTest(name = "동의어가 \"{0}\" 일 때")
@ValueSource(strings = {"", " "})
void create_fail_lessThanOne(final String synonym) {
//given
//when
//then
assertThatThrownBy(() -> new Synonym(synonym))
.isInstanceOf(ArtistException.NullOrEmptySynonymException.class);
}

@DisplayName("가수 이름 동의어의 길이가 255를 넘을 경우 예외를 던진다.")
@Test
void create_fail_lengthOver255() {
//given
final String synonym = ".".repeat(256);

//when
//then
assertThatThrownBy(() -> new Synonym(synonym))
.isInstanceOf(ArtistException.TooLongSynonymException.class);
}
}
9 changes: 9 additions & 0 deletions backend/src/test/resources/schema-test.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ drop table if exists voting_song;
drop table if exists vote;
drop table if exists member;
drop table if exists artist;
drop table if exists artist_synonym;

create table if not exists song
(
Expand Down Expand Up @@ -102,3 +103,11 @@ create table if not exists artist
created_at timestamp(6) not null,
primary key (id)
);

create table if not exists artist_synonym
(
id bigint auto_increment,
artist_id bigint not null,
synonym varchar(255) not null,
primary key (id)
);

0 comments on commit 73036d1

Please sign in to comment.