diff --git a/db/query/mail.sql b/db/query/mail.sql new file mode 100644 index 0000000..160d45f --- /dev/null +++ b/db/query/mail.sql @@ -0,0 +1,43 @@ +-- name: GetMail :one +SELECT * FROM mails +WHERE "ID" = $1 LIMIT 1; + +-- name: CreateMail :one +INSERT INTO mails ( + "from", + "to", + cc, + "subject", + body, + "timestamp", + creator, + changer +) +VALUES ( + $1, $2, $3, $4, $5, $6, $7, $8 +) +RETURNING *; + +-- name: ListMails :many +SELECT * FROM mails +ORDER BY "timestamp", "from" +LIMIT $1 +OFFSET $2; + +-- -- name: UpdateMail :one +-- UPDATE mails +-- SET +-- "from" = COALESCE(sqlc.narg(from), "from"), +-- "to" = COALESCE(sqlc.narg(to), "to"), +-- cc = COALESCE(sqlc.narg(cc), cc), +-- "subject" = COALESCE(sqlc.narg(subject), "subject"), +-- body = COALESCE(sqlc.narg(body), body), +-- "timestamp" = COALESCE(sqlc.narg(timestamp), "timestamp"), +-- changer = $2, +-- changed = now() +-- WHERE "ID" = $1 +-- RETURNING *; + +-- name: DeleteMail :exec +DELETE FROM mails +WHERE "ID" = $1; \ No newline at end of file diff --git a/db/sqlc/mail.sql.go b/db/sqlc/mail.sql.go new file mode 100644 index 0000000..8cad725 --- /dev/null +++ b/db/sqlc/mail.sql.go @@ -0,0 +1,165 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.21.0 +// source: mail.sql + +package db + +import ( + "context" + "database/sql" + "time" +) + +const createMail = `-- name: CreateMail :one +INSERT INTO mails ( + "from", + "to", + cc, + "subject", + body, + "timestamp", + creator, + changer +) +VALUES ( + $1, $2, $3, $4, $5, $6, $7, $8 +) +RETURNING "ID", "from", "to", cc, timestamp, subject, body, creator, created, changer, changed +` + +type CreateMailParams struct { + From string `json:"from"` + To string `json:"to"` + Cc sql.NullString `json:"cc"` + Subject string `json:"subject"` + Body string `json:"body"` + Timestamp time.Time `json:"timestamp"` + Creator string `json:"creator"` + Changer string `json:"changer"` +} + +func (q *Queries) CreateMail(ctx context.Context, arg CreateMailParams) (Mail, error) { + row := q.db.QueryRowContext(ctx, createMail, + arg.From, + arg.To, + arg.Cc, + arg.Subject, + arg.Body, + arg.Timestamp, + arg.Creator, + arg.Changer, + ) + var i Mail + err := row.Scan( + &i.ID, + &i.From, + &i.To, + &i.Cc, + &i.Timestamp, + &i.Subject, + &i.Body, + &i.Creator, + &i.Created, + &i.Changer, + &i.Changed, + ) + return i, err +} + +const deleteMail = `-- name: DeleteMail :exec + +DELETE FROM mails +WHERE "ID" = $1 +` + +// -- name: UpdateMail :one +// UPDATE mails +// SET +// +// "from" = COALESCE(sqlc.narg(from), "from"), +// "to" = COALESCE(sqlc.narg(to), "to"), +// cc = COALESCE(sqlc.narg(cc), cc), +// "subject" = COALESCE(sqlc.narg(subject), "subject"), +// body = COALESCE(sqlc.narg(body), body), +// "timestamp" = COALESCE(sqlc.narg(timestamp), "timestamp"), +// changer = $2, +// changed = now() +// +// WHERE "ID" = $1 +// RETURNING *; +func (q *Queries) DeleteMail(ctx context.Context, id int64) error { + _, err := q.db.ExecContext(ctx, deleteMail, id) + return err +} + +const getMail = `-- name: GetMail :one +SELECT "ID", "from", "to", cc, timestamp, subject, body, creator, created, changer, changed FROM mails +WHERE "ID" = $1 LIMIT 1 +` + +func (q *Queries) GetMail(ctx context.Context, id int64) (Mail, error) { + row := q.db.QueryRowContext(ctx, getMail, id) + var i Mail + err := row.Scan( + &i.ID, + &i.From, + &i.To, + &i.Cc, + &i.Timestamp, + &i.Subject, + &i.Body, + &i.Creator, + &i.Created, + &i.Changer, + &i.Changed, + ) + return i, err +} + +const listMails = `-- name: ListMails :many +SELECT "ID", "from", "to", cc, timestamp, subject, body, creator, created, changer, changed FROM mails +ORDER BY "timestamp", "from" +LIMIT $1 +OFFSET $2 +` + +type ListMailsParams struct { + Limit int32 `json:"limit"` + Offset int32 `json:"offset"` +} + +func (q *Queries) ListMails(ctx context.Context, arg ListMailsParams) ([]Mail, error) { + rows, err := q.db.QueryContext(ctx, listMails, arg.Limit, arg.Offset) + if err != nil { + return nil, err + } + defer rows.Close() + items := []Mail{} + for rows.Next() { + var i Mail + if err := rows.Scan( + &i.ID, + &i.From, + &i.To, + &i.Cc, + &i.Timestamp, + &i.Subject, + &i.Body, + &i.Creator, + &i.Created, + &i.Changer, + &i.Changed, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} diff --git a/db/sqlc/models.go b/db/sqlc/models.go index ad6b080..2dc8960 100644 --- a/db/sqlc/models.go +++ b/db/sqlc/models.go @@ -35,7 +35,7 @@ type Customer struct { type Document struct { ID int64 `json:"ID"` - PersonID int64 `json:"personID"` + PersonID sql.NullInt64 `json:"personID"` Name string `json:"name"` Type string `json:"type"` Path string `json:"path"` @@ -43,6 +43,7 @@ type Document struct { Valid bool `json:"valid"` ValidDate sql.NullTime `json:"validDate"` ValidatedBy sql.NullString `json:"validatedBy"` + MailID sql.NullInt64 `json:"mailID"` Creator string `json:"creator"` Created time.Time `json:"created"` Changer string `json:"changer"` @@ -50,14 +51,17 @@ type Document struct { } type Mail struct { - ID int64 `json:"ID"` - From string `json:"from"` - To string `json:"to"` - Cc sql.NullString `json:"cc"` - Timestamp time.Time `json:"timestamp"` - Subject string `json:"subject"` - Body string `json:"body"` - Attachments sql.NullInt32 `json:"attachments"` + ID int64 `json:"ID"` + From string `json:"from"` + To string `json:"to"` + Cc sql.NullString `json:"cc"` + Timestamp time.Time `json:"timestamp"` + Subject string `json:"subject"` + Body string `json:"body"` + Creator string `json:"creator"` + Created time.Time `json:"created"` + Changer string `json:"changer"` + Changed time.Time `json:"changed"` } type Payment struct {