refactor(model): persist date, status, and error description
This commit is contained in:
parent
54798b7554
commit
c541119cf0
@ -1,5 +1,6 @@
|
||||
package com.pablotj.restemailbridge.domain.model;
|
||||
|
||||
import java.time.Instant;
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
|
||||
@ -7,9 +8,30 @@ import lombok.Getter;
|
||||
@Builder
|
||||
public class Email {
|
||||
|
||||
private String from;
|
||||
private String to;
|
||||
private String subject;
|
||||
private String body;
|
||||
private final String from;
|
||||
private final String to;
|
||||
private final String subject;
|
||||
private final String body;
|
||||
private EmailStatus status;
|
||||
private final Instant createdAt;
|
||||
private String errorDescription;
|
||||
|
||||
}
|
||||
public void markAsSent() {
|
||||
this.status = EmailStatus.SENT;
|
||||
}
|
||||
|
||||
public void markAsFailed(String errorDescription) {
|
||||
this.status = EmailStatus.FAILED;
|
||||
this.errorDescription = errorDescription;
|
||||
}
|
||||
|
||||
public static Email create(String from, String to, String subject, String body) {
|
||||
return Email.builder()
|
||||
.from(from)
|
||||
.to(to)
|
||||
.subject(subject)
|
||||
.body(body)
|
||||
.status(EmailStatus.PENDING)
|
||||
.build();
|
||||
}
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
package com.pablotj.restemailbridge.domain.model;
|
||||
|
||||
public enum EmailStatus {
|
||||
SENT, FAILED, PENDING
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package com.pablotj.restemailbridge.infrastructure.config;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
|
||||
|
||||
@Configuration
|
||||
@EnableJpaAuditing
|
||||
public class JpaConfig { }
|
@ -1,15 +1,24 @@
|
||||
package com.pablotj.restemailbridge.infrastructure.persistence;
|
||||
|
||||
import com.pablotj.restemailbridge.domain.model.EmailStatus;
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Convert;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.EntityListeners;
|
||||
import jakarta.persistence.EnumType;
|
||||
import jakarta.persistence.Enumerated;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.Table;
|
||||
import java.time.Instant;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.data.annotation.CreatedDate;
|
||||
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
|
||||
|
||||
@Entity
|
||||
@EntityListeners(AuditingEntityListener.class)
|
||||
@Table(name = "MAIL")
|
||||
@Getter
|
||||
@Setter
|
||||
@ -19,15 +28,29 @@ public class MailJpa {
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@Column(length = 100, nullable = false)
|
||||
@Column(length = 200, nullable = false)
|
||||
@Convert(converter = EncryptionConverter.class)
|
||||
private String sender;
|
||||
|
||||
@Column(length = 100, nullable = false)
|
||||
@Column(length = 200, nullable = false)
|
||||
private String recipient;
|
||||
|
||||
@Column(length = 50, nullable = false)
|
||||
@Column(length = 150, nullable = false)
|
||||
@Convert(converter = EncryptionConverter.class)
|
||||
private String subjet;
|
||||
|
||||
@Column(length = 40000, nullable = false)
|
||||
@Column(length = 7000, nullable = false)
|
||||
@Convert(converter = EncryptionConverter.class)
|
||||
private String body;
|
||||
|
||||
@Column(nullable = false)
|
||||
@Enumerated(EnumType.STRING)
|
||||
private EmailStatus status;
|
||||
|
||||
@CreatedDate
|
||||
@Column(nullable = false)
|
||||
private Instant createdAt;
|
||||
|
||||
@Column
|
||||
private String errorDescription;
|
||||
}
|
||||
|
@ -20,7 +20,8 @@ public class MailRepositoryAdapter implements EmailRepository {
|
||||
mailJpa.setRecipient(email.getTo());
|
||||
mailJpa.setSubjet(email.getSubject());
|
||||
mailJpa.setBody(email.getBody());
|
||||
|
||||
mailJpa.setStatus(email.getStatus());
|
||||
mailJpa.setErrorDescription(email.getErrorDescription());
|
||||
springDataMailRepository.save(mailJpa);
|
||||
|
||||
return email;
|
||||
|
@ -1,8 +1,18 @@
|
||||
CREATE TABLE restemailbridge.mail
|
||||
create table mail
|
||||
(
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
body VARCHAR(255) NOT NULL,
|
||||
recipient VARCHAR(255) NOT NULL,
|
||||
sender VARCHAR(255) NOT NULL,
|
||||
subject VARCHAR(255) NOT NULL
|
||||
id bigint generated by default as identity primary key,
|
||||
body varchar(7000) not null,
|
||||
recipient varchar(200) not null,
|
||||
sender varchar(200) not null,
|
||||
subjet varchar(150) not null,
|
||||
created_at timestamp,
|
||||
status varchar
|
||||
constraint check_status
|
||||
check (((status)::text = 'PENDING'::text) OR ((status)::text = 'SENT'::text) OR
|
||||
((status)::text = 'FAILED'::text)),
|
||||
error_description varchar(10000),
|
||||
constraint check_error_description
|
||||
check ((((status)::text = 'FAILED'::text) AND
|
||||
((error_description IS NOT NULL) OR ((error_description)::text <> ''::text))) OR
|
||||
(((status)::text <> 'FAILED'::text) AND (error_description IS NULL)))
|
||||
);
|
Loading…
x
Reference in New Issue
Block a user