Skip to content

Commit

Permalink
feat(#53): converted Utils classes java to kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
KimDoubleB committed Jan 16, 2023
1 parent dc4836b commit c1dc055
Show file tree
Hide file tree
Showing 17 changed files with 98 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import site.archive.common.DateTimeUtils;
import site.archive.domain.archive.Archive;
import site.archive.domain.archive.CoverImageType;
import site.archive.domain.archive.Emotion;
Expand All @@ -15,8 +16,6 @@
import java.util.List;
import java.util.Objects;

import static site.archive.common.DateTimeUtil.YY_MM_DD_FORMATTER;

@NoArgsConstructor
@AllArgsConstructor
@Getter
Expand Down Expand Up @@ -50,7 +49,7 @@ public static ArchiveDtoV1 specificFrom(Archive archive) {
return ArchiveDtoV1.builder()
.archiveId(archive.getId())
.name(archive.getName())
.watchedOn(archive.getWatchedOn().format(YY_MM_DD_FORMATTER))
.watchedOn(archive.getWatchedOn().format(DateTimeUtils.getYymmddFormatter()))
.emotion(archive.getEmotion())
.mainImage(archive.getMainImage())
.companions(archive.getCompanions())
Expand All @@ -71,7 +70,7 @@ public static ArchiveDtoV1 simpleFrom(Archive archive) {
return ArchiveDtoV1.builder()
.archiveId(archive.getId())
.name(archive.getName())
.watchedOn(archive.getWatchedOn().format(YY_MM_DD_FORMATTER))
.watchedOn(archive.getWatchedOn().format(DateTimeUtils.getYymmddFormatter()))
.emotion(archive.getEmotion())
.companions(archive.getCompanions())
.mainImage(archive.getMainImage())
Expand All @@ -86,7 +85,7 @@ public Archive toEntity(BaseUser user) {
var archiveCoverImageType = Objects.requireNonNullElse(coverImageType, CoverImageType.EMOTION_COVER);
return Archive.builder()
.name(name)
.watchedOn(LocalDate.parse(watchedOn, YY_MM_DD_FORMATTER))
.watchedOn(LocalDate.parse(watchedOn, DateTimeUtils.getYymmddFormatter()))
.emotion(emotion)
.mainImage(mainImage)
.companions(companions)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import site.archive.common.DateTimeUtil;
import site.archive.common.DateTimeUtils;
import site.archive.domain.user.BaseUser;
import site.archive.domain.user.UserRole;

Expand All @@ -29,7 +29,7 @@ public static BaseUserDtoV1 from(BaseUser baseUser) {
}

public String getCreatedAt() {
return DateTimeUtil.DATE_TIME_FORMATTER.format(this.createdAt);
return DateTimeUtils.getDateTimeFormatter().format(this.createdAt);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import site.archive.common.DateTimeUtil;
import site.archive.common.DateTimeUtils;
import site.archive.domain.user.BaseUser;
import site.archive.domain.user.UserRole;

Expand All @@ -19,7 +19,7 @@ public class SpecificUserDtoV1 {
private final String userType;

public static SpecificUserDtoV1 from(BaseUser baseUser) {
var createdAt = DateTimeUtil.DATE_TIME_FORMATTER.format(baseUser.getCreatedAt());
var createdAt = DateTimeUtils.getDateTimeFormatter().format(baseUser.getCreatedAt());
return new SpecificUserDtoV1(baseUser.getId(),
baseUser.getMailAddress(),
baseUser.getRole(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
import lombok.Builder;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import site.archive.common.DateTimeUtils;
import site.archive.domain.archive.Archive;
import site.archive.domain.archive.CoverImageType;
import site.archive.domain.archive.Emotion;

import static site.archive.common.DateTimeUtil.YY_MM_DD_FORMATTER;

@RequiredArgsConstructor
@Builder
@Getter
Expand Down Expand Up @@ -36,7 +35,7 @@ public static ArchiveCommunityResponseDto from(Archive archive, Long currentUser
return ArchiveCommunityResponseDto.builder()
.archiveId(archive.getId())
.name(archive.getName())
.watchedOn(archive.getWatchedOn().format(YY_MM_DD_FORMATTER))
.watchedOn(archive.getWatchedOn().format(DateTimeUtils.getYymmddFormatter()))
.emotion(archive.getEmotion())
.mainImage(archive.getMainImage())
.coverImageType(archive.getCoverImageType())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import site.archive.common.DateTimeUtils;
import site.archive.domain.archive.Archive;
import site.archive.domain.archive.CoverImageType;
import site.archive.domain.archive.Emotion;
import site.archive.dto.v1.archive.ArchiveImageDtoV1;

import java.util.List;

import static site.archive.common.DateTimeUtil.YY_MM_DD_FORMATTER;

@NoArgsConstructor
@AllArgsConstructor
@Getter
Expand Down Expand Up @@ -52,7 +51,7 @@ public static ArchiveDtoV2 specificFrom(Archive archive) {
return ArchiveDtoV2.builder()
.archiveId(archive.getId())
.name(archive.getName())
.watchedOn(archive.getWatchedOn().format(YY_MM_DD_FORMATTER))
.watchedOn(archive.getWatchedOn().format(DateTimeUtils.getYymmddFormatter()))
.emotion(archive.getEmotion())
.mainImage(archive.getMainImage())
.companions(archive.getCompanions())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
import site.archive.common.DateTimeUtils;
import site.archive.domain.archive.Archive;
import site.archive.domain.archive.CoverImageType;
import site.archive.domain.archive.Emotion;

import static site.archive.common.DateTimeUtil.YY_MM_DD_FORMATTER;

@RequiredArgsConstructor
@Builder
@Getter
Expand Down Expand Up @@ -37,7 +36,7 @@ public static ArchiveLikeResponseDto from(Archive archive, Long currentUserIdx)
return ArchiveLikeResponseDto.builder()
.archiveId(archive.getId())
.name(archive.getName())
.watchedOn(archive.getWatchedOn().format(YY_MM_DD_FORMATTER))
.watchedOn(archive.getWatchedOn().format(DateTimeUtils.getYymmddFormatter()))
.emotion(archive.getEmotion())
.mainImage(archive.getMainImage())
.coverImageType(archive.getCoverImageType())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
import lombok.Builder;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import site.archive.common.DateTimeUtils;
import site.archive.domain.archive.Archive;
import site.archive.domain.archive.CoverImageType;
import site.archive.domain.archive.Emotion;

import static site.archive.common.DateTimeUtil.YY_MM_DD_FORMATTER;

@RequiredArgsConstructor
@Builder
@Getter
Expand All @@ -32,7 +31,7 @@ public static MyArchiveResponseDto from(Archive archive, long dateMilli) {
return MyArchiveResponseDto.builder()
.archiveId(archive.getId())
.name(archive.getName())
.watchedOn(archive.getWatchedOn().format(YY_MM_DD_FORMATTER))
.watchedOn(archive.getWatchedOn().format(DateTimeUtils.getYymmddFormatter()))
.emotion(archive.getEmotion())
.mainImage(archive.getMainImage())
.isPublic(archive.getIsPublic())
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@file:JvmName("ArchiveStringUtils")

package site.archive.common

private const val MAIL_AT = '@'

fun extractIdFromMail(mailAddress: String): String {
return mailAddress.substring(0, mailAddress.indexOf(MAIL_AT))
}
40 changes: 0 additions & 40 deletions archive-common/src/main/java/site/archive/common/DateTimeUtil.java

This file was deleted.

34 changes: 34 additions & 0 deletions archive-common/src/main/java/site/archive/common/DateTimeUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
@file:JvmName("DateTimeUtils")
package site.archive.common

import java.time.Clock
import java.time.Instant
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.YearMonth
import java.time.ZoneId
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter

const val ASIA_SEOUL_ZONE_ID = "Asia/Seoul"
val yymmddFormatter: DateTimeFormatter = DateTimeFormatter.ofPattern("yy/MM/dd")
val dateTimeFormatter: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
val asiaSeoulZone: ZoneId = ZoneId.of(ASIA_SEOUL_ZONE_ID)

var clock: Clock = Clock.system(asiaSeoulZone)

fun changeClock(date: LocalDate) {
val dateTime = date.atStartOfDay()
val zoneOffset = asiaSeoulZone.rules.getOffset(dateTime)
clock = Clock.fixed(dateTime.atOffset(zoneOffset).toInstant(), zoneOffset)
}

fun fromMilli(milli: Long): ZonedDateTime {
return Instant.ofEpochMilli(milli).atZone(asiaSeoulZone)
}

fun firstDateTimeOfMonth(): LocalDateTime {
return YearMonth.now(clock)
.atDay(1)
.atTime(0, 0)
}
30 changes: 0 additions & 30 deletions archive-common/src/main/java/site/archive/common/FileUtils.java

This file was deleted.

22 changes: 22 additions & 0 deletions archive-common/src/main/java/site/archive/common/FileUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
@file:JvmName("FileUtils")

package site.archive.common

import org.springframework.http.MediaType.IMAGE_GIF_VALUE
import org.springframework.http.MediaType.IMAGE_JPEG_VALUE
import org.springframework.http.MediaType.IMAGE_PNG_VALUE
import org.springframework.web.multipart.MultipartFile

private const val HTTP = "http://"
private const val HTTPS = "https://"

fun verifyImageFile(imageFile: MultipartFile) {
if (!listOf(IMAGE_PNG_VALUE, IMAGE_GIF_VALUE, IMAGE_JPEG_VALUE)
.contains(imageFile.contentType)) {
throw IllegalStateException("FIle uploaded is not an image")
}
}

fun isFileUrl(file: String?) : Boolean{
return file != null && (file.startsWith(HTTP) || file.startsWith(HTTPS))
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,25 @@ class DateTimeUtilTest {

@Test
void YY_MM_DD_formatterTest() {
var convertedDate = dummyDate.format(DateTimeUtil.YY_MM_DD_FORMATTER);
var convertedDate = dummyDate.format(DateTimeUtils.getYymmddFormatter());
assertThat(convertedDate).isEqualTo("95/09/25");
}

@Test
void DATE_TIME_formatterTest() {
var convertedDateTime = dummyDateTime.format(DateTimeUtil.DATE_TIME_FORMATTER);
var convertedDateTime = dummyDateTime.format(DateTimeUtils.getDateTimeFormatter());
assertThat(convertedDateTime).isEqualTo("1995-09-25 11:11:11");
}

@Test
void dateOfMonthTest() {
DateTimeUtil.changeClock(LocalDate.of(2022, 7, 2));
var julFirstDate = DateTimeUtil.firstDateTimeOfMonth();
DateTimeUtils.changeClock(LocalDate.of(2022, 7, 2));
var julFirstDate = DateTimeUtils.firstDateTimeOfMonth();
assertThat(julFirstDate).isEqualTo(LocalDateTime.of(2022, 7, 1, 0, 0));

var augFirstDate = julFirstDate.plusMonths(1);
DateTimeUtil.changeClock(LocalDate.of(2022, 8, 22));
assertThat(augFirstDate).isEqualTo(DateTimeUtil.firstDateTimeOfMonth());
DateTimeUtils.changeClock(LocalDate.of(2022, 8, 22));
assertThat(augFirstDate).isEqualTo(DateTimeUtils.firstDateTimeOfMonth());
}

}
Loading

0 comments on commit c1dc055

Please sign in to comment.