-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMongoBaseEntity.java
49 lines (38 loc) · 1.11 KB
/
MongoBaseEntity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.wypl.mongocommon;
import java.time.LocalDateTime;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.mongodb.core.mapping.Field;
import com.wypl.common.exception.WyplException;
import lombok.Getter;
import lombok.NoArgsConstructor;
@Getter
@NoArgsConstructor
public abstract class MongoBaseEntity {
@CreatedDate
@Field(name = "created_at", write = Field.Write.NON_NULL)
private LocalDateTime createdAt;
@LastModifiedDate
@Field(name = "modified_at", write = Field.Write.NON_NULL)
private LocalDateTime modifiedAt;
@Field(name = "deleted_at")
private LocalDateTime deletedAt;
public void delete() {
if (isDeleted()) {
throw new WyplException(MongoErrorCode.ALREADY_DELETED_ENTITY);
}
this.deletedAt = LocalDateTime.now();
}
public void restore() {
if (isNotDeleted()) {
throw new WyplException(MongoErrorCode.NON_DELETED_ENTITY);
}
this.deletedAt = null;
}
public boolean isNotDeleted() {
return deletedAt == null;
}
public boolean isDeleted() {
return !isNotDeleted();
}
}