From 45bb0dc5e8c785d4889861856c135e322f091329 Mon Sep 17 00:00:00 2001 From: itsscb Date: Tue, 19 Sep 2023 21:00:14 +0200 Subject: [PATCH 1/3] fix/replaces bigserial with bigint as foreign keys in schema --- db/migration/000001_init_schema.down.sql | 2 +- db/migration/000001_init_schema.up.sql | 25 ++++++++++++++---------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/db/migration/000001_init_schema.down.sql b/db/migration/000001_init_schema.down.sql index 992819d..9e62b01 100644 --- a/db/migration/000001_init_schema.down.sql +++ b/db/migration/000001_init_schema.down.sql @@ -1,8 +1,8 @@ DROP TABLE IF EXISTS "returnsLog"; DROP TABLE IF EXISTS "returns"; DROP TABLE IF EXISTS "payments"; -DROP TABLE IF EXISTS "mails"; DROP TABLE IF EXISTS "documents"; +DROP TABLE IF EXISTS "mails"; DROP TABLE IF EXISTS "persons"; DROP TABLE IF EXISTS "providers"; DROP TABLE IF EXISTS "customers"; diff --git a/db/migration/000001_init_schema.up.sql b/db/migration/000001_init_schema.up.sql index ba86b12..87608d2 100644 --- a/db/migration/000001_init_schema.up.sql +++ b/db/migration/000001_init_schema.up.sql @@ -6,7 +6,11 @@ CREATE TABLE "mails" ( "timestamp" timestamptz NOT NULL DEFAULT (now()), "subject" varchar NOT NULL, "body" text NOT NULL, - "attachments" bigserial + "creator" varchar NOT NULL, + "created" timestamptz NOT NULL DEFAULT (now()), + "changer" varchar NOT NULL, + "changed" timestamptz NOT NULL DEFAULT (now()) + ); CREATE TABLE "customers" ( @@ -35,7 +39,7 @@ CREATE TABLE "customers" ( CREATE TABLE "persons" ( "ID" bigserial UNIQUE PRIMARY KEY NOT NULL, - "customerID" bigserial NOT NULL, + "customerID" bigint NOT NULL, "firstname" varchar NOT NULL, "lastname" varchar NOT NULL, "birthday" timestamptz NOT NULL, @@ -51,7 +55,7 @@ CREATE TABLE "persons" ( CREATE TABLE "documents" ( "ID" bigserial UNIQUE PRIMARY KEY NOT NULL, - "personID" bigserial NOT NULL, + "personID" bigint, "name" varchar NOT NULL, "type" varchar NOT NULL, "path" varchar NOT NULL, @@ -59,6 +63,7 @@ CREATE TABLE "documents" ( "valid" boolean NOT NULL DEFAULT false, "validDate" timestamptz, "validatedBy" varchar, + "mailID" bigint, "creator" varchar NOT NULL, "created" timestamptz NOT NULL DEFAULT (now()), "changer" varchar NOT NULL, @@ -67,7 +72,7 @@ CREATE TABLE "documents" ( CREATE TABLE "payments" ( "ID" bigserial UNIQUE PRIMARY KEY NOT NULL, - "customerID" bigserial NOT NULL, + "customerID" bigint NOT NULL, "paymentCategory" varchar NOT NULL, "bankname" varchar, "IBAN" varchar, @@ -96,8 +101,8 @@ CREATE TABLE "providers" ( CREATE TABLE "returns" ( "ID" bigserial UNIQUE PRIMARY KEY NOT NULL, - "personID" bigserial NOT NULL, - "providerID" bigserial NOT NULL, + "personID" bigint NOT NULL, + "providerID" bigint NOT NULL, "name" varchar NOT NULL, "description" text NOT NULL, "category" varchar NOT NULL, @@ -111,8 +116,8 @@ CREATE TABLE "returns" ( CREATE TABLE "returnsLog" ( "ID" bigserial UNIQUE PRIMARY KEY NOT NULL, - "returnsID" bigserial, - "mailID" bigserial, + "returnsID" bigint, + "mailID" bigint, "status" varchar, "creator" varchar NOT NULL, "created" timestamptz NOT NULL DEFAULT (now()), @@ -120,12 +125,12 @@ CREATE TABLE "returnsLog" ( "changed" timestamptz NOT NULL DEFAULT (now()) ); -ALTER TABLE "mails" ADD FOREIGN KEY ("attachments") REFERENCES "documents" ("ID"); - ALTER TABLE "persons" ADD FOREIGN KEY ("customerID") REFERENCES "customers" ("ID"); ALTER TABLE "documents" ADD FOREIGN KEY ("personID") REFERENCES "persons" ("ID"); +ALTER TABLE "documents" ADD FOREIGN KEY ("mailID") REFERENCES "mails" ("ID"); + ALTER TABLE "payments" ADD FOREIGN KEY ("customerID") REFERENCES "customers" ("ID"); ALTER TABLE "returns" ADD FOREIGN KEY ("personID") REFERENCES "persons" ("ID"); From 01f98eea1ecad3def7749552ff45391eb76e4c96 Mon Sep 17 00:00:00 2001 From: itsscb Date: Tue, 19 Sep 2023 21:01:16 +0200 Subject: [PATCH 2/3] ft/differs between person and mail in documents --- db/query/document.sql | 15 ++++++- db/sqlc/document.sql.go | 94 ++++++++++++++++++++++++++++++++-------- db/sqlc/document_test.go | 39 +++++++++-------- 3 files changed, 112 insertions(+), 36 deletions(-) diff --git a/db/query/document.sql b/db/query/document.sql index bf3419c..dcaa9a5 100644 --- a/db/query/document.sql +++ b/db/query/document.sql @@ -2,7 +2,7 @@ SELECT * FROM documents WHERE "ID" = $1 LIMIT 1; --- name: CreateDocument :one +-- name: CreateDocumentUpload :one INSERT INTO documents ( "personID", name, @@ -15,6 +15,19 @@ INSERT INTO documents ( $1, $2, $3, $4, $5, $6, $7 ) RETURNING *; +-- name: CreateDocumentMail :one +INSERT INTO documents ( + "mailID", + name, + type, + path, + url, + creator, + changer +) VALUES ( + $1, $2, $3, $4, $5, $6, $7 +) RETURNING *; + -- name: ListDocuments :many SELECT * FROM documents ORDER BY valid, type, name diff --git a/db/sqlc/document.sql.go b/db/sqlc/document.sql.go index 5bee0ae..610b5ad 100644 --- a/db/sqlc/document.sql.go +++ b/db/sqlc/document.sql.go @@ -10,7 +10,61 @@ import ( "database/sql" ) -const createDocument = `-- name: CreateDocument :one +const createDocumentMail = `-- name: CreateDocumentMail :one +INSERT INTO documents ( + "mailID", + name, + type, + path, + url, + creator, + changer +) VALUES ( + $1, $2, $3, $4, $5, $6, $7 +) RETURNING "ID", "personID", name, type, path, url, valid, "validDate", "validatedBy", "mailID", creator, created, changer, changed +` + +type CreateDocumentMailParams struct { + MailID sql.NullInt64 `json:"mailID"` + Name string `json:"name"` + Type string `json:"type"` + Path string `json:"path"` + Url string `json:"url"` + Creator string `json:"creator"` + Changer string `json:"changer"` +} + +func (q *Queries) CreateDocumentMail(ctx context.Context, arg CreateDocumentMailParams) (Document, error) { + row := q.db.QueryRowContext(ctx, createDocumentMail, + arg.MailID, + arg.Name, + arg.Type, + arg.Path, + arg.Url, + arg.Creator, + arg.Changer, + ) + var i Document + err := row.Scan( + &i.ID, + &i.PersonID, + &i.Name, + &i.Type, + &i.Path, + &i.Url, + &i.Valid, + &i.ValidDate, + &i.ValidatedBy, + &i.MailID, + &i.Creator, + &i.Created, + &i.Changer, + &i.Changed, + ) + return i, err +} + +const createDocumentUpload = `-- name: CreateDocumentUpload :one INSERT INTO documents ( "personID", name, @@ -21,21 +75,21 @@ INSERT INTO documents ( changer ) VALUES ( $1, $2, $3, $4, $5, $6, $7 -) RETURNING "ID", "personID", name, type, path, url, valid, "validDate", "validatedBy", creator, created, changer, changed +) RETURNING "ID", "personID", name, type, path, url, valid, "validDate", "validatedBy", "mailID", creator, created, changer, changed ` -type CreateDocumentParams struct { - PersonID int64 `json:"personID"` - Name string `json:"name"` - Type string `json:"type"` - Path string `json:"path"` - Url string `json:"url"` - Creator string `json:"creator"` - Changer string `json:"changer"` +type CreateDocumentUploadParams struct { + PersonID sql.NullInt64 `json:"personID"` + Name string `json:"name"` + Type string `json:"type"` + Path string `json:"path"` + Url string `json:"url"` + Creator string `json:"creator"` + Changer string `json:"changer"` } -func (q *Queries) CreateDocument(ctx context.Context, arg CreateDocumentParams) (Document, error) { - row := q.db.QueryRowContext(ctx, createDocument, +func (q *Queries) CreateDocumentUpload(ctx context.Context, arg CreateDocumentUploadParams) (Document, error) { + row := q.db.QueryRowContext(ctx, createDocumentUpload, arg.PersonID, arg.Name, arg.Type, @@ -55,6 +109,7 @@ func (q *Queries) CreateDocument(ctx context.Context, arg CreateDocumentParams) &i.Valid, &i.ValidDate, &i.ValidatedBy, + &i.MailID, &i.Creator, &i.Created, &i.Changer, @@ -74,7 +129,7 @@ func (q *Queries) DeleteDocument(ctx context.Context, id int64) error { } const getDocument = `-- name: GetDocument :one -SELECT "ID", "personID", name, type, path, url, valid, "validDate", "validatedBy", creator, created, changer, changed FROM documents +SELECT "ID", "personID", name, type, path, url, valid, "validDate", "validatedBy", "mailID", creator, created, changer, changed FROM documents WHERE "ID" = $1 LIMIT 1 ` @@ -91,6 +146,7 @@ func (q *Queries) GetDocument(ctx context.Context, id int64) (Document, error) { &i.Valid, &i.ValidDate, &i.ValidatedBy, + &i.MailID, &i.Creator, &i.Created, &i.Changer, @@ -108,7 +164,7 @@ SET changer = $2, changed = now() WHERE "ID" = $1 -RETURNING "ID", "personID", name, type, path, url, valid, "validDate", "validatedBy", creator, created, changer, changed +RETURNING "ID", "personID", name, type, path, url, valid, "validDate", "validatedBy", "mailID", creator, created, changer, changed ` type InvalidateDocumentParams struct { @@ -129,6 +185,7 @@ func (q *Queries) InvalidateDocument(ctx context.Context, arg InvalidateDocument &i.Valid, &i.ValidDate, &i.ValidatedBy, + &i.MailID, &i.Creator, &i.Created, &i.Changer, @@ -138,7 +195,7 @@ func (q *Queries) InvalidateDocument(ctx context.Context, arg InvalidateDocument } const listDocuments = `-- name: ListDocuments :many -SELECT "ID", "personID", name, type, path, url, valid, "validDate", "validatedBy", creator, created, changer, changed FROM documents +SELECT "ID", "personID", name, type, path, url, valid, "validDate", "validatedBy", "mailID", creator, created, changer, changed FROM documents ORDER BY valid, type, name LIMIT $1 OFFSET $2 @@ -168,6 +225,7 @@ func (q *Queries) ListDocuments(ctx context.Context, arg ListDocumentsParams) ([ &i.Valid, &i.ValidDate, &i.ValidatedBy, + &i.MailID, &i.Creator, &i.Created, &i.Changer, @@ -197,7 +255,7 @@ SET changer = $2, changed = now() WHERE "ID" = $1 -RETURNING "ID", "personID", name, type, path, url, valid, "validDate", "validatedBy", creator, created, changer, changed +RETURNING "ID", "personID", name, type, path, url, valid, "validDate", "validatedBy", "mailID", creator, created, changer, changed ` type UpdateDocumentParams struct { @@ -231,6 +289,7 @@ func (q *Queries) UpdateDocument(ctx context.Context, arg UpdateDocumentParams) &i.Valid, &i.ValidDate, &i.ValidatedBy, + &i.MailID, &i.Creator, &i.Created, &i.Changer, @@ -248,7 +307,7 @@ SET changer = $2, changed = now() WHERE "ID" = $1 -RETURNING "ID", "personID", name, type, path, url, valid, "validDate", "validatedBy", creator, created, changer, changed +RETURNING "ID", "personID", name, type, path, url, valid, "validDate", "validatedBy", "mailID", creator, created, changer, changed ` type ValidateDocumentParams struct { @@ -269,6 +328,7 @@ func (q *Queries) ValidateDocument(ctx context.Context, arg ValidateDocumentPara &i.Valid, &i.ValidDate, &i.ValidatedBy, + &i.MailID, &i.Creator, &i.Created, &i.Changer, diff --git a/db/sqlc/document_test.go b/db/sqlc/document_test.go index 3b33da5..8a6561b 100644 --- a/db/sqlc/document_test.go +++ b/db/sqlc/document_test.go @@ -10,21 +10,24 @@ import ( "github.com/stretchr/testify/require" ) -func createRandomDocument(t *testing.T) Document { +func createRandomDocumentUpload(t *testing.T) Document { person := createRandomPerson(t) require.NotEmpty(t, person) - arg := CreateDocumentParams{ - PersonID: person.ID, - Name: util.RandomUser(), - Type: util.RandomUser(), - Path: util.RandomString(50), - Url: util.RandomString(60), - Creator: util.RandomUser(), - Changer: util.RandomUser(), + arg := CreateDocumentUploadParams{ + PersonID: sql.NullInt64{ + Valid: true, + Int64: person.ID, + }, + Name: util.RandomUser(), + Type: util.RandomUser(), + Path: util.RandomString(50), + Url: util.RandomString(60), + Creator: util.RandomUser(), + Changer: util.RandomUser(), } - document, err := testQueries.CreateDocument(context.Background(), arg) + document, err := testQueries.CreateDocumentUpload(context.Background(), arg) require.NoError(t, err) require.NotEmpty(t, document) @@ -45,12 +48,12 @@ func createRandomDocument(t *testing.T) Document { return document } -func TestCreateDocument(t *testing.T) { - createRandomDocument(t) +func TestCreateDocumentUpload(t *testing.T) { + createRandomDocumentUpload(t) } func TestGetDocument(t *testing.T) { - newdocument := createRandomDocument(t) + newdocument := createRandomDocumentUpload(t) require.NotEmpty(t, newdocument) document, err := testQueries.GetDocument(context.Background(), newdocument.ID) @@ -72,7 +75,7 @@ func TestGetDocument(t *testing.T) { } func TestDeleteDocument(t *testing.T) { - document1 := createRandomDocument(t) + document1 := createRandomDocumentUpload(t) err := testQueries.DeleteDocument(context.Background(), document1.ID) require.NoError(t, err) @@ -83,7 +86,7 @@ func TestDeleteDocument(t *testing.T) { } func TestUpdateDocument(t *testing.T) { - document1 := createRandomDocument(t) + document1 := createRandomDocumentUpload(t) require.NotEmpty(t, document1) arg := UpdateDocumentParams{ @@ -105,7 +108,7 @@ func TestUpdateDocument(t *testing.T) { func TestListDocuments(t *testing.T) { for i := 0; i < 10; i++ { - createRandomDocument(t) + createRandomDocumentUpload(t) } arg := ListDocumentsParams{ @@ -123,7 +126,7 @@ func TestListDocuments(t *testing.T) { } func TestValidateDocument(t *testing.T) { - document1 := createRandomDocument(t) + document1 := createRandomDocumentUpload(t) validator := util.RandomUser() @@ -148,7 +151,7 @@ func TestValidateDocument(t *testing.T) { } func TestInvalidateDocument(t *testing.T) { - document1 := createRandomDocument(t) + document1 := createRandomDocumentUpload(t) validator := util.RandomUser() From ab307eb027d025743e2afa9fe56c2affbbfe5744 Mon Sep 17 00:00:00 2001 From: itsscb Date: Tue, 19 Sep 2023 21:04:04 +0200 Subject: [PATCH 3/3] ft/add mails Fixes #3 --- db/query/mail.sql | 43 ++++++++++++ db/sqlc/mail.sql.go | 165 ++++++++++++++++++++++++++++++++++++++++++++ db/sqlc/models.go | 22 +++--- 3 files changed, 221 insertions(+), 9 deletions(-) create mode 100644 db/query/mail.sql create mode 100644 db/sqlc/mail.sql.go 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 {