working the dispense table and relative api route

This commit is contained in:
2022-09-27 00:55:01 +02:00
parent c7e33c8e72
commit 807a4c54ac
11 changed files with 172 additions and 57 deletions
+8 -7
View File
@@ -5,7 +5,7 @@ type DBMigrate interface {
}
type DBQueryAppunti interface {
AllDispenseFile() ([]DispensaFile, error)
AllDispensaFile() ([]DispensaFile, error)
}
type DBDispense interface {
@@ -38,12 +38,13 @@ type DBDownloads interface {
Create(template Download) error
}
// DB main "interface group" for interacting with the database, with this technique we can test each "table" api of the DB in isolation.
type DB struct {
DBMigrate
DBDispense
DBUploads
DBFileApprovals
DBDispensaTags
DBDownloads
DBQueryAppunti
Dispense DBDispense
Uploads DBUploads
FileApprovals DBFileApprovals
DispensaTags DBDispensaTags
Downloads DBDownloads
}
+18 -9
View File
@@ -1,5 +1,9 @@
package database
//
// Tables
//
type Dispensa struct {
Id string `db:"id"`
CreatedAt string `db:"created_at"`
@@ -8,15 +12,6 @@ type Dispensa struct {
Description string `db:"description"`
}
type DispensaFile struct {
DispensaId string `db:"dispensa_id"`
UploadId string `db:"upload_id"`
OwnerId string `db:"owner_id"`
Title string `db:"title"`
Description string `db:"description"`
File string `db:"file"`
}
type Upload struct {
Id string `db:"id"`
CreatedAt string `db:"created_at"`
@@ -42,3 +37,17 @@ type Tag struct {
DispensaId string `db:"dispensa_id"`
Tag string `db:"tag"`
}
//
// Common Joins
//
// DispensaFile is a join of dispensa with the most recent upload for that "dispensa_id"
type DispensaFile struct {
DispensaId string `db:"dispensa_id"`
UploadId string `db:"upload_id"`
OwnerId string `db:"owner_id"`
Title string `db:"title"`
Description string `db:"description"`
File string `db:"file"`
}
+8 -5
View File
@@ -22,10 +22,13 @@ func New(filename string) (*database.DB, error) {
}
return &database.DB{
DBMigrate: &sqliteDBMigrate{db},
DBDispense: &sqliteDBDispense{db},
DBUploads: &sqliteDBUploads{db},
DBFileApprovals: &sqliteDBFileApprovals{db},
DBDownloads: &sqliteDBDownloads{db},
DBMigrate: &sqliteDBMigrate{db},
// DBQueryAppunti: ...,
Dispense: &sqliteDBDispense{db},
Uploads: &sqliteDBUploads{db},
FileApprovals: &sqliteDBFileApprovals{db},
Downloads: &sqliteDBDownloads{db},
DispensaTags: &sqliteDBTags{db},
}, nil
}
+1 -1
View File
@@ -13,7 +13,7 @@ type sqliteDBDispense struct{ *sqlx.DB }
var _ database.DBDispense = sqliteDBDispense{}
func (d sqliteDBDispense) Create(template database.Dispensa) (string, error) {
template.Id = "dispensa/" + util.GenerateRandomString(8)
template.Id = util.GenerateRandomString(8)
template.CreatedAt = time.Now().Format(time.RFC3339)
if _, err := d.DB.NamedExec(`
+1 -1
View File
@@ -11,7 +11,7 @@ type sqliteDBUploads struct{ *sqlx.DB }
var _ database.DBUploads = sqliteDBUploads{}
func (u sqliteDBUploads) Create(template database.Upload) (string, error) {
template.Id = "upload/" + util.GenerateRandomString(8)
template.Id = util.GenerateRandomString(8)
if _, err := u.DB.NamedExec(`
INSERT INTO
+43 -5
View File
@@ -1,26 +1,64 @@
package sqlite
import (
"time"
"git.phc.dm.unipi.it/phc/website/database"
"git.phc.dm.unipi.it/phc/website/util"
"github.com/jmoiron/sqlx"
)
type sqliteDBFileApprovals struct{ *sqlx.DB }
func (f sqliteDBFileApprovals) Create(template database.FileApproval) (string, error) {
panic("not implemented") // TODO: Implement
template.Id = util.GenerateRandomString(8)
template.CreatedAt = time.Now().Format(time.RFC3339)
if _, err := f.DB.NamedExec(`
INSERT INTO
file_approvals(id, created_at, owner_id, upload_id, status)
VALUES
(:id, :created_at, :owner_id, :upload_id, :status)
`, &template); err != nil {
return "", err
}
return template.Id, nil
}
func (f sqliteDBFileApprovals) Get(id string) (database.FileApproval, error) {
panic("not implemented") // TODO: Implement
var fileApproval database.FileApproval
if err := f.DB.Get(&fileApproval, `SELECT * FROM file_approvals WHERE id = ?`, id); err != nil {
return database.FileApproval{}, err
}
return fileApproval, nil
}
func (f sqliteDBFileApprovals) All() ([]database.FileApproval, error) {
panic("not implemented") // TODO: Implement
var fileApprovals []database.FileApproval
if err := f.DB.Select(&fileApprovals, `SELECT * FROM file_approvals`); err != nil {
return nil, err
}
return fileApprovals, nil
}
func (f sqliteDBFileApprovals) Update(d database.FileApproval) error {
panic("not implemented") // TODO: Implement
func (f sqliteDBFileApprovals) Update(fileApproval database.FileApproval) error {
if _, err := f.DB.NamedExec(`
UPDATE
file_approvals
SET
owner_id = :owner_id,
upload_id = :upload_id,
status = :status
WHERE
id = :id
`, &fileApproval); err != nil {
return err
}
return nil
}
func (f sqliteDBFileApprovals) Delete(id string) error {
+52
View File
@@ -0,0 +1,52 @@
package sqlite
import (
"git.phc.dm.unipi.it/phc/website/database"
"github.com/jmoiron/sqlx"
)
type sqliteDBTags struct{ *sqlx.DB }
var _ database.DBDispensaTags = sqliteDBTags{}
func (t sqliteDBTags) Set(dispensaId string, tags []string) error {
tagsRows := []map[string]any{}
for _, t := range tags {
tagsRows = append(tagsRows, map[string]any{
"dispensa_id": dispensaId,
"name": t,
})
}
tx, err := t.DB.Beginx()
if err != nil {
return err
}
if _, err := tx.Exec(`
DELETE FROM tags WHERE dispensa_id = ?
`, dispensaId); err != nil {
return err
}
if _, err := tx.NamedExec(`
INSERT INTO tags(dispensa_id, name) VALUES (:dispensa_id, :name)
`, tagsRows); err != nil {
return err
}
if err := tx.Commit(); err != nil {
return err
}
return nil
}
func (t sqliteDBTags) Get(dispensaId string) ([]string, error) {
var tags []string
if err := t.DB.Select(&tags, `SELECT name FROM tags WHERE dispensa_id = ?`, dispensaId); err != nil {
return nil, err
}
return tags, nil
}