Skip to content

Commit

Permalink
Base Entity(createdAt, updatedAt) 추가
Browse files Browse the repository at this point in the history
Base Entity(createdAt, updatedAt) 추가
  • Loading branch information
xGreenNarae authored Oct 10, 2023
2 parents 12a40ab + 1b30e4d commit b7e877e
Show file tree
Hide file tree
Showing 14 changed files with 53 additions and 22 deletions.
2 changes: 2 additions & 0 deletions animory/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ dependencies {
// lombok
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testCompileOnly 'org.projectlombok:lombok'
testAnnotationProcessor 'org.projectlombok:lombok'

// test
testImplementation 'org.springframework.boot:spring-boot-starter-test'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.daggle.animory.common.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

@Configuration
@EnableJpaAuditing
public class JpaAuditConfiguration {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.daggle.animory.common.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Getter;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.Column;
import javax.persistence.EntityListeners;
import javax.persistence.MappedSuperclass;
import java.time.LocalDateTime;

@Getter
@EntityListeners(AuditingEntityListener.class)
@MappedSuperclass
public class BaseEntity {
@CreatedDate
@Column(updatable = false)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd-HH-mm-ss")
private LocalDateTime createdAt;

@LastModifiedDate
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd-HH-mm-ss")
private LocalDateTime updatedAt;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.daggle.animory.domain.account.entity;

import com.daggle.animory.common.entity.BaseEntity;
import lombok.*;

import javax.persistence.*;
Expand All @@ -11,7 +12,7 @@
@AllArgsConstructor
@Table(name = "account")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Account {
public class Account extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import javax.validation.constraints.NotNull;
import java.time.LocalDate;
import java.time.LocalDateTime;

@Builder
public record PetRegisterRequestDto(
Expand Down Expand Up @@ -41,7 +40,6 @@ public Pet toEntity(final Shelter shelter, final String imageUrl, final String v
.profileImageUrl(imageUrl)
.profileShortFormUrl(videoUrl)
.size(size)
.createdAt(LocalDateTime.now())
.shelter(shelter)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.daggle.animory.domain.pet.entity;

import com.daggle.animory.common.entity.BaseEntity;
import com.daggle.animory.domain.pet.dto.request.PetUpdateRequestDto;
import com.daggle.animory.domain.pet.util.PetAgeToBirthDateConverter;
import com.daggle.animory.domain.shelter.entity.Shelter;
Expand All @@ -9,15 +10,14 @@
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.time.LocalDate;
import java.time.LocalDateTime;

@Getter
@Entity
@Builder
@AllArgsConstructor
@Table(name = "pet")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Pet {
public class Pet extends BaseEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand All @@ -43,7 +43,7 @@ public class Pet {
@Column(length = 1000)
private String description;

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd", timezone = "Asia/Seoul")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
private LocalDate protectionExpirationDate;

private String vaccinationStatus;
Expand All @@ -63,9 +63,6 @@ public class Pet {

private String size;

@Column(nullable = false, columnDefinition = "datetime")
private LocalDateTime createdAt;

@ManyToOne
@JoinColumn(name = "shelter_id")
private Shelter shelter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class PetPolygonProfile {

private int activeness;

public void update(PetPolygonProfileDto petPolygonProfileDto) {
public void update(final PetPolygonProfileDto petPolygonProfileDto) {
this.intelligence = petPolygonProfileDto.intelligence();
this.affinity = petPolygonProfileDto.affinity();
this.athletic = petPolygonProfileDto.athletic();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class PetWriteService {
private final ShelterRepository shelterRepository;
private final PetRepository petRepository;
private final PetPolygonRepository petPolygonRepository;

private final PetValidator petValidator;

public RegisterPetSuccessDto registerPet(final Account account,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.daggle.animory.domain.shelter.entity;

import com.daggle.animory.common.entity.BaseEntity;
import com.daggle.animory.domain.account.entity.Account;
import lombok.*;

Expand All @@ -12,7 +13,7 @@
@AllArgsConstructor
@Table(name = "shelter")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Shelter {
public class Shelter extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
import com.daggle.animory.domain.account.dto.request.ShelterSignUpDto;
import com.daggle.animory.domain.shelter.entity.Province;
import com.daggle.animory.testutil.webmvctest.BaseWebMvcTest;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
import org.springframework.http.MediaType;
Expand All @@ -21,9 +19,6 @@
@Import(AccountController.class)
public class AccountControllerTest extends BaseWebMvcTest {

@Autowired
ObjectMapper om;

@MockBean
AccountService accountService;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void findByShelterId() {
final Integer shelterId = 1;
final Page<Pet> page = petRepository.findByShelterId(shelterId, PageRequest.of(0, 10));

List<Pet> pets = page.getContent();
final List<Pet> pets = page.getContent();
print(pets);

pets.forEach(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.daggle.animory.domain.shelter.entity.Shelter;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -25,7 +24,6 @@ public static Pet getOne(final Shelter shelter) {
.profileImageUrl("http://amazon.server/api/petImage/20231001104521_test1.jpg")
.profileShortFormUrl("http://amazon.server/api/petVideo/20231001104521_test1.mp4")
.size("작아요 소형견입니다.")
.createdAt(LocalDateTime.now())
.shelter(shelter)
.build();
}
Expand All @@ -50,7 +48,6 @@ public static List<Pet> get(final int n,
.profileImageUrl("http://amazon.server/api/petImage/20231001104521_test" + i + ".jpg")
.profileShortFormUrl("http://amazon.server/api/petVideo/20231001104521_test" + i + ".mp4")
.size("작아요 소형견입니다." + i)
.createdAt(LocalDateTime.now())
.shelter(shelter)
.build()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
import org.junit.jupiter.api.BeforeEach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

import java.util.List;

@DataJpaTest
@EnableJpaAuditing
public abstract class DataJpaTestWithDummyData extends WithTimeSupportObjectMapper {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import lombok.extern.slf4j.Slf4j;

/**
* time type serialization 설정이 추가된 object mapper 가 지원됩니다.
* DataJpaTest 등에서 결과를 콘솔에 찍어보고자 할때 상속해서 사용하면 좋을 듯 합니다.
*/
@Slf4j
public abstract class WithTimeSupportObjectMapper {

protected ObjectMapper om = new ObjectMapper()
.registerModule(new JavaTimeModule());

protected void print(final Object o) {
try{
System.out.println(om.writerWithDefaultPrettyPrinter().writeValueAsString(o));
log.debug(om.writerWithDefaultPrettyPrinter().writeValueAsString(o));
} catch (final JsonProcessingException e){
System.out.println("json parsing error: " + e.getMessage());
log.debug("json parsing error: " + e.getMessage());
}
}

Expand Down

0 comments on commit b7e877e

Please sign in to comment.