Skip to content

Commit c12efbd

Browse files
refactor: repo queries and add endpoint query
1 parent 76bd9c9 commit c12efbd

File tree

13 files changed

+281
-192
lines changed

13 files changed

+281
-192
lines changed

config/config.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ func (p Parser) Parse(raw Raw) (Config, error) {
272272
if value.BodyTemplate != nil {
273273
bodyTemplate = *value.BodyTemplate
274274
}
275-
e, err := endpoint.NewInternal(endpointFactory, endpoint.CreateEndpoint{
275+
e, err := endpoint.NewInternal(endpointFactory, models.DTOEndpointCreate{
276276
Name: value.Name,
277277
AttachmentDisable: value.AttachmentDisable,
278278
TextDisable: value.TextDisable,

internal/endpoint/endpoint.go

+3-13
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,10 @@ func validate(f Factory, end models.Endpoint) error {
2525
return nil
2626
}
2727

28-
type CreateEndpoint struct {
29-
Name string
30-
AttachmentDisable bool
31-
TextDisable bool
32-
TitleTemplate string
33-
BodyTemplate string
34-
Kind string
35-
Config models.EndpointConfig
36-
}
37-
3828
const DefaultTitleTemplate = "{{ .Message.Subject }}"
3929
const DefaultBodyTemplate = "{{ .Message.Text }}"
4030

41-
func new(f Factory, r CreateEndpoint) models.Endpoint {
31+
func new(f Factory, r models.DTOEndpointCreate) models.Endpoint {
4232
return models.Endpoint{
4333
Internal: false,
4434
InternalID: sql.NullString{},
@@ -52,12 +42,12 @@ func new(f Factory, r CreateEndpoint) models.Endpoint {
5242
}
5343
}
5444

55-
func New(f Factory, r CreateEndpoint) (models.Endpoint, error) {
45+
func New(f Factory, r models.DTOEndpointCreate) (models.Endpoint, error) {
5646
end := new(f, r)
5747
return end, validate(f, end)
5848
}
5949

60-
func NewInternal(f Factory, r CreateEndpoint, internalID string) (models.Endpoint, error) {
50+
func NewInternal(f Factory, r models.DTOEndpointCreate, internalID string) (models.Endpoint, error) {
6151
if r.Name == "" {
6252
r.Name = internalID
6353
}

internal/models/dto.go

+21
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,24 @@ type DTORuleUpdate struct {
8484
Enable *bool
8585
Endpoints *[]int64
8686
}
87+
88+
type DTOEndpointCreate struct {
89+
Name string
90+
AttachmentDisable bool
91+
TextDisable bool
92+
TitleTemplate string
93+
BodyTemplate string
94+
Kind string
95+
Config EndpointConfig
96+
}
97+
98+
type DTOEndpointUpdate struct {
99+
ID int64
100+
Name *string
101+
AttachmentDisable *bool
102+
TextDisable *bool
103+
TitleTemplate *string
104+
BodyTemplate *string
105+
Kind *string
106+
Config *EndpointConfig
107+
}

internal/repo/endpoint.go

+61
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package repo
22

33
import (
44
"context"
5+
"time"
56

67
"github.com/ItsNotGoodName/smtpbridge/internal/database"
78
. "github.com/ItsNotGoodName/smtpbridge/internal/jet/table"
@@ -22,6 +23,39 @@ var endpointPJ ProjectionList = ProjectionList{
2223
Endpoints.Config.AS("endpoint.config"),
2324
}
2425

26+
func EndpointCreate(ctx context.Context, db database.Querier, end models.Endpoint) (int64, error) {
27+
now := models.NewTime(time.Now())
28+
m := struct {
29+
models.Endpoint
30+
CreatedAt models.Time
31+
UpdatedAt models.Time
32+
}{
33+
Endpoint: end,
34+
CreatedAt: now,
35+
UpdatedAt: now,
36+
}
37+
res, err := Endpoints.
38+
INSERT(
39+
Endpoints.Internal,
40+
Endpoints.InternalID,
41+
Endpoints.Name,
42+
Endpoints.AttachmentDisable,
43+
Endpoints.TextDisable,
44+
Endpoints.TitleTemplate,
45+
Endpoints.BodyTemplate,
46+
Endpoints.Kind,
47+
Endpoints.Config,
48+
Endpoints.UpdatedAt,
49+
Endpoints.CreatedAt,
50+
).
51+
MODEL(m).
52+
ExecContext(ctx, db)
53+
if err != nil {
54+
return 0, err
55+
}
56+
return res.LastInsertId()
57+
}
58+
2559
func EndpointGet(ctx context.Context, db database.Querier, id int64) (models.Endpoint, error) {
2660
var endpoint models.Endpoint
2761
err := Endpoints.
@@ -40,6 +74,33 @@ func EndpointList(ctx context.Context, db database.Querier) ([]models.Endpoint,
4074
return endpoints, err
4175
}
4276

77+
func EndpointUpdate(ctx context.Context, db database.Querier, end models.Endpoint) error {
78+
m := struct {
79+
models.Endpoint
80+
UpdatedAt models.Time
81+
}{
82+
Endpoint: end,
83+
UpdatedAt: models.NewTime(time.Now()),
84+
}
85+
_, err := Endpoints.
86+
UPDATE(
87+
Endpoints.Internal,
88+
Endpoints.InternalID,
89+
Endpoints.Name,
90+
Endpoints.AttachmentDisable,
91+
Endpoints.TextDisable,
92+
Endpoints.TitleTemplate,
93+
Endpoints.BodyTemplate,
94+
Endpoints.Kind,
95+
Endpoints.Config,
96+
Endpoints.UpdatedAt,
97+
).
98+
MODEL(m).
99+
WHERE(Endpoints.ID.EQ(Int64(end.ID))).
100+
ExecContext(ctx, db)
101+
return err
102+
}
103+
43104
func EndpointDelete(ctx context.Context, db database.Querier, id int64) error {
44105
res, err := Endpoints.
45106
DELETE().

0 commit comments

Comments
 (0)