Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] 선물 생성 시 날짜 저장 추가 #173

Merged
merged 2 commits into from
Feb 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
170 changes: 87 additions & 83 deletions src/main/java/com/sopterm/makeawish/domain/Present.java
Original file line number Diff line number Diff line change
@@ -1,95 +1,99 @@
package com.sopterm.makeawish.domain;

import static jakarta.persistence.FetchType.*;
import static jakarta.persistence.GenerationType.*;
import static java.util.Objects.*;

import com.sopterm.makeawish.domain.wish.Wish;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.LocalDateTime;

import static jakarta.persistence.FetchType.LAZY;
import static jakarta.persistence.GenerationType.IDENTITY;
import static java.util.Objects.nonNull;

@Getter
@Entity
@NoArgsConstructor
@EntityListeners(AuditingEntityListener.class)
public class Present {

@Id @GeneratedValue(strategy = IDENTITY)
@Column(name = "present_id")
private Long id;

private String name;

@Column(columnDefinition = "TEXT")
private String message;

@ManyToOne(fetch = LAZY)
@JoinColumn(name = "wish_id")
private Wish wish;

@ManyToOne(fetch = LAZY)
@JoinColumn(name = "cake_id")
private Cake cake;

public static class PresentBuilder {
private String name;
private String message;
private Wish wish;
private Cake cake;

private PresentBuilder() {}

public static PresentBuilder builder() {
return new PresentBuilder();
}

public PresentBuilder name(String name) {
this.name = name;
return this;
}

public PresentBuilder message(String message) {
this.message = message;
return this;
}

public PresentBuilder wish(Wish wish) {
this.wish = wish;
return this;
}

public PresentBuilder cake(Cake cake) {
this.cake = cake;
return this;
}

private void setWish(Present present, Wish wish) {
if (nonNull(present.wish)) {
present.wish.getPresents().remove(present);
}
present.wish = wish;
if (nonNull(wish)) {
wish.getPresents().add(present);
}
}

public Present build() {
Present present = new Present();
present.name = this.name;
present.message = this.message;
setWish(present, this.wish);
present.cake = this.cake;
return present;
}
}

public static PresentBuilder builder() {
return new PresentBuilder();
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "present_id")
private Long id;

private String name;

@Column(columnDefinition = "TEXT")
private String message;

@ManyToOne(fetch = LAZY)
@JoinColumn(name = "wish_id")
private Wish wish;

@ManyToOne(fetch = LAZY)
@JoinColumn(name = "cake_id")
private Cake cake;

@CreatedDate
protected LocalDateTime createdAt;

public static class PresentBuilder {
private String name;
private String message;
private Wish wish;
private Cake cake;

private PresentBuilder() {
}

public static PresentBuilder builder() {
return new PresentBuilder();
}

public PresentBuilder name(String name) {
this.name = name;
return this;
}

public PresentBuilder message(String message) {
this.message = message;
return this;
}

public PresentBuilder wish(Wish wish) {
this.wish = wish;
return this;
}

public PresentBuilder cake(Cake cake) {
this.cake = cake;
return this;
}

private void setWish(Present present, Wish wish) {
if (nonNull(present.wish)) {
present.wish.getPresents().remove(present);
}
present.wish = wish;
if (nonNull(wish)) {
wish.getPresents().add(present);
}
}

public Present build() {
Present present = new Present();
present.name = this.name;
present.message = this.message;
setWish(present, this.wish);
present.cake = this.cake;
return present;
}
}

public static PresentBuilder builder() {
return new PresentBuilder();
}
}
Loading