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

포인트 관련 도메인, 엔티티 추가 #415

Merged
merged 7 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.depromeet.breadmapbackend.domain.event.db;

import com.depromeet.breadmapbackend.domain.breaddiary.BreadDiary;
import com.depromeet.breadmapbackend.domain.event.domain.breaddiaryevent.BreadDiaryEventState;
import lombok.Getter;

import javax.persistence.*;
import javax.validation.constraints.NotNull;

@Entity
@Getter
@Table(name = "bread_diary_event_check")
class BreadDiaryEventEntity extends TimestampEntity {
@Id
private long diaryId;
@OneToOne
@PrimaryKeyJoinColumn(name = "diary_id")
@NotNull
private BreadDiary diary;
@NotNull
private String description = "";
@Enumerated(EnumType.STRING)
@NotNull
private BreadDiaryEventState state = BreadDiaryEventState.PENDING;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.depromeet.breadmapbackend.domain.event.db;

import lombok.Getter;

import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.validation.constraints.NotNull;
import java.time.ZonedDateTime;

@MappedSuperclass
@Getter
public abstract class TimestampEntity {
@Column(updatable = false)
@NotNull
private ZonedDateTime createdAt;
@NotNull
private ZonedDateTime updatedAt;

@PrePersist
public void prePersist() {
createdAt = ZonedDateTime.now();
updatedAt = ZonedDateTime.now();
}

@PreUpdate
public void preUpdate() {
updatedAt = ZonedDateTime.now();
}
}
facewise marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.depromeet.breadmapbackend.domain.event.db;

import com.depromeet.breadmapbackend.domain.user.User;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import javax.validation.constraints.NotNull;

@Entity
@Getter
@Table(name = "user_point")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
class UserPointEntity extends TimestampEntity {
@Id
private long userId;

@OneToOne
@PrimaryKeyJoinColumn(name = "user_id")
@NotNull
private User user;

@NotNull
private int totalPoint = 0;

UserPointEntity(long userId, User user) {
this.userId = userId;
this.user = user;
}

void addPoint(int point) {
totalPoint += point;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.depromeet.breadmapbackend.domain.event.db;

import com.depromeet.breadmapbackend.domain.event.domain.point.PointHistoryType;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import javax.validation.constraints.NotNull;

@Entity
@Table(name = "user_point_history")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
class UserPointHistoryEntity extends TimestampEntity {
@Id @GeneratedValue
private long id;
@NotNull
@Column(updatable = false)
private long userId;
@NotNull
@Column(updatable = false)
private int point;
@NotNull
@Column(updatable = false)
private int grandTotalPoint;
@Enumerated(EnumType.STRING)
@NotNull
@Column(updatable = false)
private PointHistoryType type = PointHistoryType.ETC;
@NotNull
@Column(updatable = false)
private String description = "";
@Column(updatable = false)
private Long targetId;

UserPointHistoryEntity(long id, long userId, int point, int grandTotalPoint, PointHistoryType type, String description, Long targetId) {
this.id = id;
this.userId = userId;
this.point = point;
this.grandTotalPoint = grandTotalPoint;
this.type = type;
this.description = description;
this.targetId = targetId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.depromeet.breadmapbackend.domain.event.domain;

import lombok.Getter;

import java.time.ZonedDateTime;

@Getter
public class Timestamp {
private ZonedDateTime createdAt;
private ZonedDateTime updatedAt;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.depromeet.breadmapbackend.domain.event.domain.breaddiaryevent;

import com.depromeet.breadmapbackend.domain.event.domain.Timestamp;

public record BreadDiaryEvent(BreadDiaryEventPK pk, BreadDiaryEventTarget diary,
BreadDiaryEventContent content, Timestamp timestamps) {
public String getHistoryDescription() {
return "빵일기 "+ content.getState() +" 상태 진입으로 "+ content.getPoint()+"포인트 " + content.getIncreaseKeyword()+"되었습니다";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.depromeet.breadmapbackend.domain.event.domain.breaddiaryevent;

import lombok.Getter;

@Getter
public class BreadDiaryEventContent {
private BreadDiaryEventState state = BreadDiaryEventState.PENDING;
private String description = "";
private int point = 500;

public String getIncreaseKeyword() {
return point < 0 ? "차감" : "지급";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.depromeet.breadmapbackend.domain.event.domain.breaddiaryevent;

public record BreadDiaryEventPK(long id) {

public enum State {
PENDING, ACCEPTED, REJECTED
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.depromeet.breadmapbackend.domain.event.domain.breaddiaryevent;

import lombok.AllArgsConstructor;
import org.codehaus.jackson.annotate.JsonCreator;
import org.codehaus.jackson.annotate.JsonValue;

@AllArgsConstructor
public enum BreadDiaryEventState {
PENDING("대기"), ACCEPTED("수락"), REJECTED("거부");
public final String value;

@JsonCreator
static public BreadDiaryEventState jsonCreator(String state) {
try {
return BreadDiaryEventState.valueOf(state);
} catch (Exception e) {
return null;
}
}

@JsonValue
public String value() {
return value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.depromeet.breadmapbackend.domain.event.domain.breaddiaryevent;

public record BreadDiaryEventTarget(long breadDiaryId) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.depromeet.breadmapbackend.domain.event.domain.point;

public enum PointHistoryType {
BREAD_DIARY, ETC
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.depromeet.breadmapbackend.domain.event.domain.point;

import lombok.Getter;

@Getter
public class UserPoint {
private final UserPointPK pk;
private int totalPoint;

public void addPoint(int point) {
totalPoint += point;
}

public UserPoint(UserPointPK pk, int totalPoint) {
this.pk = pk;
this.totalPoint = totalPoint;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.depromeet.breadmapbackend.domain.event.domain.point;

import com.depromeet.breadmapbackend.domain.event.domain.Timestamp;

public record UserPointHistory(UserPointHistoryPK pk, UserPointHistoryContent content, Timestamp timestamp) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.depromeet.breadmapbackend.domain.event.domain.point;

import lombok.Getter;

@Getter
public class UserPointHistoryContent {
private final int point;
private final int grandTotalPoint;
private final PointHistoryType type;
private final String description;

public UserPointHistoryContent(int point, int grandTotalPoint, PointHistoryType type, String description) {
this.point = point;
this.grandTotalPoint = grandTotalPoint;
this.type = type;
this.description = description;
}


}
facewise marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.depromeet.breadmapbackend.domain.event.domain.point;

public record UserPointHistoryPK(long id, long userId) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.depromeet.breadmapbackend.domain.event.domain.point;

public record UserPointPK(long userId) {
}
33 changes: 33 additions & 0 deletions src/main/resources/db/migration/V3__event_point_related_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
CREATE TABLE `bread_diary_event` (
`diary_id` bigint PRIMARY KEY ,
`state` ENUM ('PENDING', 'ACCEPTED', 'REJECTED') NOT NULL DEFAULT 'PENDING',
`point` int NOT NULL DEFAULT 0,
`description` string NOT NULL DEFAULT '',
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
facewise marked this conversation as resolved.
Show resolved Hide resolved
);

CREATE TABLE `point_history` (
`id` bigint auto_increment PRIMARY KEY,
`user_id` bigint NOT NULL,
`target_id` bigint,
`point` int NOT NULL DEFAULT 0,
`grand_total_point` int NOT NULL DEFAULT 0,
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`type` ENUM ('BREAD_DIARY', 'ETC') NOT NULL DEFAULT 'ETC',
`description` string NOT NULL DEFAULT ''
);

CREATE TABLE `user_point` (
`user_id` bigint PRIMARY KEY,
`total_point` int NOT NULL DEFAULT 0,
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
);

ALTER TABLE `bread_diary_event` ADD FOREIGN KEY (`diary_id`) REFERENCES `bread_diary` (`id`);

ALTER TABLE `point_history` ADD FOREIGN KEY (`user_id`) REFERENCES `user` (`id`);

ALTER TABLE `user_point` ADD FOREIGN KEY (`user_id`) REFERENCES `user` (`id`);
Loading