Skip to content

Commit

Permalink
[fix] change time notation rule
Browse files Browse the repository at this point in the history
  • Loading branch information
minaamim committed Sep 7, 2023
1 parent 4566759 commit 0ed6b2b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
package com.nexters.buyornot.module.post.api.dto.response;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.nexters.buyornot.module.post.domain.model.PollStatus;
import com.nexters.buyornot.module.post.domain.model.PublicStatus;
import com.nexters.buyornot.module.post.domain.post.Post;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import org.checkerframework.checker.formatter.qual.Format;
import lombok.*;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.Period;
import java.util.List;

@Builder(access = AccessLevel.PRIVATE)
@Getter
@Setter
@AllArgsConstructor
public class PostResponse {
private Long id;
Expand All @@ -29,11 +27,13 @@ public class PostResponse {
private List<PollItemResponse> pollItemResponseList;
private boolean participateStatus = false;
private PollResponse pollResponse;

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "Asia/Seoul")
private LocalDateTime updatedAt;
private LocalDateTime now;

int years, months, days;
long hours, minutes, seconds;

public PostResponse(Long id, String userId, String userNickname, String profile, String title, String content, PublicStatus publicStatus, boolean isPublished, PollStatus pollStatus, List<PollItemResponse> pollItems, LocalDateTime updatedAt) {
public PostResponse(Long id, String userId, String userNickname, String profile, String title, String content, PublicStatus publicStatus, boolean isPublished, PollStatus pollStatus, List<PollItemResponse> pollItems, LocalDateTime updatedAt, LocalDateTime now, int years, int months, int days, long hours, long minutes, long seconds) {
this.id = id;
this.userId = userId;
this.userNickname = userNickname;
Expand All @@ -45,10 +45,13 @@ public PostResponse(Long id, String userId, String userNickname, String profile,
this.pollStatus = pollStatus;
this.pollItemResponseList = pollItems;
this.updatedAt = updatedAt;
}

public PostResponse(Post post) {
this.id = post.getId();
this.now = now;
this.years = years;
this.months = months;
this.days = days;
this.hours = hours;
this.minutes = minutes;
this.seconds = seconds;
}

public void addPollResponse(PollResponse pollResponse) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.Where;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.Period;
import java.util.*;


Expand Down Expand Up @@ -103,7 +106,18 @@ public PostResponse newPostResponse() {
PollItemResponse response = pollItem.newPollItemResponse();
pollItemResponseList.add(response);
}
return new PostResponse(id, userId.toString(), nickname, profile, title, content, publicStatus, isPublished, pollStatus, pollItemResponseList, getUpdatedAt());

LocalDateTime now = LocalDateTime.now();
Period period = Period.between(this.getUpdatedAt().toLocalDate(), now.toLocalDate());
Duration duration = Duration.between(this.getUpdatedAt().toLocalTime(), now.toLocalTime());
int years = period.getYears();
int months = period.getMonths();
int days = period.getDays();
long hours = duration.toHours();
long minutes = duration.toMinutes() - hours * 60;
long seconds = duration.getSeconds() - hours * 60 * 60 - minutes * 60;

return new PostResponse(id, userId.toString(), nickname, profile, title, content, publicStatus, isPublished, pollStatus, pollItemResponseList, getUpdatedAt(), now, years, months, days, hours, minutes, seconds);
}

public Long getId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ class PostServiceTest {
//when
PostResponse response = postService.create(user, createPostReq);

log.info("compare");

//then
assertThat(response.getId()).isEqualTo(postRepository.findByTitle(createPostReq.getTitle()).getId());
log.info("response ->{}", response.getPollItemResponseList().get(0).getItemUrl());
Expand Down Expand Up @@ -269,4 +267,29 @@ public void delete() {
assertThat(response.isPublished()).isEqualTo(true);
assertThat(postResponse.getId()).isLessThan(response.getId());
}

@Test
@Transactional
void 시간() {
JwtUser user = JwtUser.fromUser(UUID.randomUUID(), "mina", "mina", PROFILE);

List<String> urls = new ArrayList<>();
urls.add("https://zigzag.kr/catalog/products/113607837");
urls.add("https://www.musinsa.com/app/goods/3404788?loc=goods_rank");

CreatePostReq createPostReq = CreatePostReq.of("test", "test", PublicStatus.PUBLIC, true, urls);

//when
PostResponse response = postService.create(user, createPostReq);
log.info("updatedAT: " + response.getUpdatedAt());
log.info("now: " + response.getNow());
log.info("diff years: " + response.getYears());
log.info("diff months: " + response.getMonths());
log.info("diff days: " + response.getDays());
log.info("diff hours: " + response.getHours());
log.info("diff minutes: " + response.getMinutes());
log.info("diff seconds: " + response.getSeconds());

assertThat(response.getMinutes()).isLessThan(1);
}
}

0 comments on commit 0ed6b2b

Please sign in to comment.