Skip to content

Commit

Permalink
[BE] 기본 이미지 URL 정책 빈 문자열로 변경 (#952) (#953)
Browse files Browse the repository at this point in the history
* feat: Festival posterImageUrl null 혹은 공백 문자열 시 빈 문자열 설정

* feat: ArtistV1CreateRequest, ArtistV1UpdateRequest 이미지 URL nullable 하도록 변경

* feat: Artist profileImage, backgroundImageUrl null 혹은 공백 문자열 시 빈 문자열 설정

* refactor: maxLength 검증 null 검사 삭제

- maxLength 검증 시 null 무시

* feat: Member profileImageUrl null 혹은 공백 문자열 시 빈 문자열 설정

* feat: School logoUrl, backgroundImageUrl null 혹은 공백 문자열 시 빈 문자열 설정

* feat: SocialMedia logoUrl null 혹은 공백 문자열 시 빈 문자열 설정 및 검증 추가
  • Loading branch information
seokjin8678 authored May 13, 2024
1 parent 6faa3a9 commit 7362efa
Show file tree
Hide file tree
Showing 14 changed files with 83 additions and 64 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.festago.admin.dto.artist;

import com.festago.artist.dto.command.ArtistCreateCommand;
import jakarta.annotation.Nullable;
import jakarta.validation.constraints.NotBlank;

public record ArtistV1CreateRequest(
@NotBlank
String name,
@NotBlank
@Nullable
String profileImageUrl,
@NotBlank
@Nullable
String backgroundImageUrl
) {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.festago.admin.dto.artist;

import com.festago.artist.dto.command.ArtistUpdateCommand;
import jakarta.annotation.Nullable;
import jakarta.validation.constraints.NotBlank;

public record ArtistV1UpdateRequest(
@NotBlank
String name,
@NotBlank
@Nullable
String profileImageUrl,
@NotBlank
@Nullable
String backgroundImageUrl
) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.festago.socialmedia.domain.OwnerType;
import com.festago.socialmedia.domain.SocialMediaType;
import com.festago.socialmedia.dto.command.SocialMediaCreateCommand;
import jakarta.annotation.Nullable;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Builder;
Expand All @@ -17,7 +18,7 @@ public record SocialMediaCreateV1Request(
SocialMediaType socialMediaType,
@NotBlank
String name,
@NotBlank
@Nullable
String logoUrl,
@NotBlank
String url
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.festago.admin.dto.socialmedia;

import com.festago.socialmedia.dto.command.SocialMediaUpdateCommand;
import jakarta.annotation.Nullable;
import jakarta.validation.constraints.NotBlank;
import lombok.Builder;

@Builder
public record SocialMediaUpdateV1Request(
@NotBlank
String name,
@NotBlank
@Nullable
String logoUrl,
@NotBlank
String url
Expand Down
18 changes: 10 additions & 8 deletions backend/src/main/java/com/festago/artist/domain/Artist.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.festago.artist.domain;

import com.festago.common.domain.BaseTimeEntity;
import com.festago.common.util.ImageUrlHelper;
import com.festago.common.util.Validator;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
Expand All @@ -13,8 +15,6 @@
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Artist extends BaseTimeEntity {

private static final String DEFAULT_URL = "https://picsum.photos/536/354";

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
Expand All @@ -27,24 +27,26 @@ public class Artist extends BaseTimeEntity {
private String backgroundImageUrl;

public Artist(Long id, String name, String profileImage, String backgroundImageUrl) {
validateName(name);
this.id = id;
this.name = name;
this.profileImage = profileImage;
this.backgroundImageUrl = backgroundImageUrl;
this.profileImage = ImageUrlHelper.getBlankStringIfBlank(profileImage);
this.backgroundImageUrl = ImageUrlHelper.getBlankStringIfBlank(backgroundImageUrl);
}

public Artist(String name, String profileImage) {
this(null, name, profileImage, DEFAULT_URL);
private void validateName(String name) {
Validator.notBlank(name, "name");
}

public Artist(String name, String profileImage, String backgroundImageUrl) {
this(null, name, profileImage, backgroundImageUrl);
}

public void update(String name, String profileImage, String backgroundImageUrl) {
validateName(name);
this.name = name;
this.profileImage = profileImage;
this.backgroundImageUrl = backgroundImageUrl;
this.profileImage = ImageUrlHelper.getBlankStringIfBlank(profileImage);
this.backgroundImageUrl = ImageUrlHelper.getBlankStringIfBlank(backgroundImageUrl);
}

public Long getId() {
Expand Down
17 changes: 17 additions & 0 deletions backend/src/main/java/com/festago/common/util/ImageUrlHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.festago.common.util;

import org.springframework.util.StringUtils;

public class ImageUrlHelper {

private ImageUrlHelper() {

}

public static String getBlankStringIfBlank(String input) {
if (StringUtils.hasText(input)) {
return input;
}
return "";
}
}
14 changes: 4 additions & 10 deletions backend/src/main/java/com/festago/festival/domain/Festival.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.festago.festival.domain;

import com.festago.common.domain.BaseTimeEntity;
import com.festago.common.util.ImageUrlHelper;
import com.festago.common.util.Validator;
import com.festago.school.domain.School;
import jakarta.persistence.Embedded;
Expand All @@ -21,7 +22,6 @@
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Festival extends BaseTimeEntity {

private static final String DEFAULT_POSTER_IMAGE_URL = "https://picsum.photos/536/354";
private static final int MAX_NAME_LENGTH = 50;
private static final int MAX_POSTER_IMAGE_URL_LENGTH = 255;

Expand All @@ -44,10 +44,6 @@ public class Festival extends BaseTimeEntity {
@ManyToOne(fetch = FetchType.LAZY)
private School school;

public Festival(String name, FestivalDuration festivalDuration, School school) {
this(null, name, festivalDuration, DEFAULT_POSTER_IMAGE_URL, school);
}

public Festival(String name, FestivalDuration festivalDuration, String posterImageUrl, School school) {
this(null, name, festivalDuration, posterImageUrl, school);
}
Expand All @@ -60,7 +56,7 @@ public Festival(Long id, String name, FestivalDuration festivalDuration, String
this.id = id;
this.name = name;
this.festivalDuration = festivalDuration;
this.posterImageUrl = posterImageUrl;
this.posterImageUrl = ImageUrlHelper.getBlankStringIfBlank(posterImageUrl);
this.school = school;
}

Expand All @@ -71,9 +67,7 @@ private void validateName(String name) {
}

private void validatePosterImageUrl(String posterImageUrl) {
String fieldName = "posterImageUrl";
Validator.notBlank(posterImageUrl, fieldName);
Validator.maxLength(posterImageUrl, MAX_POSTER_IMAGE_URL_LENGTH, fieldName);
Validator.maxLength(posterImageUrl, MAX_POSTER_IMAGE_URL_LENGTH, "posterImageUrl");
}

private void validateFestivalDuration(FestivalDuration festivalDuration) {
Expand All @@ -99,7 +93,7 @@ public void changeName(String name) {

public void changePosterImageUrl(String posterImageUrl) {
validatePosterImageUrl(posterImageUrl);
this.posterImageUrl = posterImageUrl;
this.posterImageUrl = ImageUrlHelper.getBlankStringIfBlank(posterImageUrl);
}

public void changeFestivalDuration(FestivalDuration festivalDuration) {
Expand Down
4 changes: 2 additions & 2 deletions backend/src/main/java/com/festago/member/domain/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.festago.auth.domain.SocialType;
import com.festago.common.domain.BaseTimeEntity;
import com.festago.common.util.ImageUrlHelper;
import com.festago.common.util.Validator;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
Expand Down Expand Up @@ -37,7 +38,6 @@
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Member extends BaseTimeEntity {

private static final String DEFAULT_IMAGE_URL = "https://festa-go.site/images/default-profile.png";
private static final String DEFAULT_NICKNAME = "FestivalLover";
private static final int MAX_SOCIAL_ID_LENGTH = 255;
private static final int MAX_NICKNAME_LENGTH = 30;
Expand Down Expand Up @@ -77,7 +77,7 @@ public Member(Long id, String socialId, SocialType socialType, String nickname,
this.socialId = socialId;
this.socialType = socialType;
this.nickname = (StringUtils.hasText(nickname)) ? nickname : DEFAULT_NICKNAME;
this.profileImage = (StringUtils.hasText(profileImage)) ? profileImage : DEFAULT_IMAGE_URL;
this.profileImage = ImageUrlHelper.getBlankStringIfBlank(profileImage);
}

private void validate(String socialId, SocialType socialType, String nickname, String profileImage) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public List<Festival> generate(List<School> schools) {
return new Festival(
school.getName() + " " + festivalDuration.getStartDate().format(DATE_TIME_FORMATTER) + " 축제",
festivalDuration,
"",
school
);
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ private School crateSchool(SchoolRegion schoolRegion, String schoolName, String
return new School(
schoolEmail,
schoolName,
"",
"",
schoolRegion
);
}
Expand Down
26 changes: 9 additions & 17 deletions backend/src/main/java/com/festago/school/domain/School.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.festago.school.domain;

import com.festago.common.domain.BaseTimeEntity;
import com.festago.common.util.ImageUrlHelper;
import com.festago.common.util.Validator;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
Expand All @@ -13,13 +14,11 @@
import jakarta.validation.constraints.Size;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.springframework.util.StringUtils;

@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class School extends BaseTimeEntity {

private static final String DEFAULT_URL = "https://picsum.photos/536/354";
private static final int MAX_DOMAIN_LENGTH = 50;
private static final int MAX_NAME_LENGTH = 255;
private static final int MAX_IMAGE_URL_LENGTH = 255;
Expand All @@ -45,27 +44,20 @@ public class School extends BaseTimeEntity {
@Enumerated(EnumType.STRING)
private SchoolRegion region;

public School(String domain, String name, String logoUrl, String backgroundUrl, SchoolRegion region) {
this(null, domain, name, logoUrl, backgroundUrl, region);
}

public School(Long id, String domain, String name, String logoUrl, String backgroundImageUrl, SchoolRegion region) {
validate(domain, name, region, logoUrl, backgroundImageUrl);
this.id = id;
this.domain = domain;
this.name = name;
this.logoUrl = getDefaultUrlIfBlank(logoUrl);
this.backgroundUrl = getDefaultUrlIfBlank(backgroundImageUrl);
this.logoUrl = ImageUrlHelper.getBlankStringIfBlank(logoUrl);
this.backgroundUrl = ImageUrlHelper.getBlankStringIfBlank(backgroundImageUrl);
this.region = region;
}

private String getDefaultUrlIfBlank(String imageUrl) {
if (StringUtils.hasText(imageUrl)) {
return imageUrl;
}
return DEFAULT_URL;
}

public School(String domain, String name, SchoolRegion region) {
this(null, domain, name, DEFAULT_URL, DEFAULT_URL, region);
}

private void validate(String domain, String name, SchoolRegion region, String logoUrl, String backgroundImageUrl) {
validateDomain(domain);
validateName(name);
Expand Down Expand Up @@ -111,12 +103,12 @@ public void changeRegion(SchoolRegion region) {

public void changeLogoUrl(String logoUrl) {
validateImageUrl(logoUrl, "logoUrl");
this.logoUrl = getDefaultUrlIfBlank(logoUrl);
this.logoUrl = ImageUrlHelper.getBlankStringIfBlank(logoUrl);
}

public void changeBackgroundImageUrl(String backgroundImageUrl) {
validateImageUrl(backgroundImageUrl, "backgroundImageUrl");
this.backgroundUrl = getDefaultUrlIfBlank(backgroundImageUrl);
this.backgroundUrl = ImageUrlHelper.getBlankStringIfBlank(backgroundImageUrl);
}

public Long getId() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.festago.socialmedia.domain;

import com.festago.common.domain.BaseTimeEntity;
import com.festago.common.util.ImageUrlHelper;
import com.festago.common.util.Validator;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
Expand All @@ -19,7 +21,7 @@
name = "social_media",
uniqueConstraints = {
@UniqueConstraint(
columnNames= {"owner_id", "owner_type", "media_type"}
columnNames = {"owner_id", "owner_type", "media_type"}
)
}
)
Expand Down Expand Up @@ -47,14 +49,18 @@ public class SocialMedia extends BaseTimeEntity {
private String url;

public SocialMedia(Long id, Long ownerId, OwnerType ownerType, SocialMediaType mediaType, String name,
String logoUrl,
String url) {
String logoUrl, String url) {
Validator.notNull(ownerId, "ownerId");
Validator.notNull(ownerType, "ownerType");
Validator.notNull(mediaType, "mediaType");
Validator.notBlank(name, "name");
Validator.notBlank(url, "url");
this.id = id;
this.ownerId = ownerId;
this.ownerType = ownerType;
this.mediaType = mediaType;
this.name = name;
this.logoUrl = logoUrl;
this.logoUrl = ImageUrlHelper.getBlankStringIfBlank(logoUrl);
this.url = url;
}

Expand All @@ -64,10 +70,12 @@ public SocialMedia(Long ownerId, OwnerType ownerType, SocialMediaType mediaType,
}

public void changeName(String name) {
Validator.notBlank(name, "name");
this.name = name;
}

public void changeUrl(String url) {
Validator.notBlank(url, "url");
this.url = url;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ class MemberTest {
Member actual = new Member("12345", SocialType.FESTAGO, "nickname", profileImage);

// when & then
assertThat(actual.getProfileImage()).isNotNull();
assertThat(actual.getProfileImage()).isNotBlank();
assertThat(actual.getProfileImage()).isEmpty();
}
}
Loading

0 comments on commit 7362efa

Please sign in to comment.