First table is working
This commit is contained in:
+15
-59
@@ -1,15 +1,11 @@
|
||||
package database
|
||||
|
||||
import "time"
|
||||
|
||||
// Tools
|
||||
|
||||
type DBMigrate interface {
|
||||
Migrate(migrationDir string) error
|
||||
}
|
||||
|
||||
// Entities
|
||||
|
||||
type DBDispense interface {
|
||||
CreateDispensa(template Dispensa) (string, error)
|
||||
GetDispensa(id string) (Dispensa, error)
|
||||
@@ -18,74 +14,34 @@ type DBDispense interface {
|
||||
DeleteDispensa(id string) error
|
||||
}
|
||||
|
||||
type DBUpload interface {
|
||||
CreateUpload(template UploadedContent) (string, error)
|
||||
DeleteUpload(id UploadedContent) (UploadedContent, error)
|
||||
type DBUploads interface {
|
||||
CreateUpload(template Upload) (string, error)
|
||||
GetUpload(id string) (Upload, error)
|
||||
}
|
||||
|
||||
type DBHashApprovals interface {
|
||||
CreateApprovedHash(a HashApproval) error
|
||||
GetApprovedHash(hash string) (HashApproval, error)
|
||||
AllApprovedHash(a HashApproval) error
|
||||
type DBFileApprovals interface {
|
||||
CreateFileApproval(template FileApproval) (string, error)
|
||||
GetFileApproval(id string) (FileApproval, error)
|
||||
AllFileApprovals() ([]FileApproval, error)
|
||||
UpdateFileApproval(d FileApproval) error
|
||||
DeleteFileApproval(id string) error
|
||||
}
|
||||
|
||||
type DBHashRejections interface {
|
||||
CreateRejectedHash(a HashRejection) error
|
||||
GetRejectedHash(hash string) (HashRejection, error)
|
||||
AllRejectedHash(a HashRejection) error
|
||||
type DBDownloads interface {
|
||||
CreateDownload(template Download) (string, error)
|
||||
}
|
||||
|
||||
// Relations
|
||||
|
||||
type DBTags interface {
|
||||
SetTags(dispensaId string, tag []string) error
|
||||
SetTags(dispensaId string, tags []string) error
|
||||
GetTags(dispensaId string) ([]string, error)
|
||||
}
|
||||
|
||||
type DBOwners interface {
|
||||
CreateOwner(owner, owned string) error
|
||||
GetOwner(ownedId string) (string, error)
|
||||
}
|
||||
|
||||
type DBAuthors interface {
|
||||
CreateAuthor(userId, dispensaId string) error
|
||||
GetAuthorId(dispensaId string) (string, error)
|
||||
GetUserDispenseIds(user string) ([]string, error)
|
||||
}
|
||||
|
||||
type DBCreationTimes interface {
|
||||
CreateCreationTime(entityId string, createdAt time.Time) error
|
||||
GetCreationTime(entityId string) (time.Time, error)
|
||||
}
|
||||
|
||||
type DBOther interface {
|
||||
// AllDispenseOfUser ritorna tutte le dispense di un certo utente
|
||||
AllDispenseOfUser(username string) ([]Dispensa, error)
|
||||
// AllLatestsApprovedDispenseUploads ritorna una lista contenente tutte le dispense con almeno un upload approvato e ritorna la coppia della (dispensa, upload più recente)
|
||||
AllLatestsApprovedDispenseUploads() ([]struct {
|
||||
Dispensa
|
||||
UploadedContent
|
||||
}, error)
|
||||
// AllDispenseWithState ...
|
||||
AllDispenseWithState(username string) ([]struct {
|
||||
Dispensa
|
||||
State string
|
||||
}, 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
|
||||
DBUpload
|
||||
DBHashApprovals
|
||||
DBHashRejections
|
||||
|
||||
DBUploads
|
||||
DBFileApprovals
|
||||
DBDownloads
|
||||
DBTags
|
||||
DBOwners
|
||||
DBAuthors
|
||||
DBCreationTimes
|
||||
|
||||
DBOther
|
||||
}
|
||||
|
||||
@@ -1,8 +1,4 @@
|
||||
|
||||
--
|
||||
-- Entities
|
||||
--
|
||||
|
||||
-- Dispense
|
||||
CREATE TABLE IF NOT EXISTS "dispense"(
|
||||
"id" TEXT NOT NULL PRIMARY KEY,
|
||||
@@ -22,7 +18,7 @@ CREATE TABLE IF NOT EXISTS "uploads"(
|
||||
FOREIGN KEY (dispensa_id) REFERENCES dispense(id)
|
||||
);
|
||||
|
||||
-- Contenuti con hash approvati
|
||||
-- Approvazioni contenuti caricati
|
||||
CREATE TABLE IF NOT EXISTS "file_approvals"(
|
||||
"id" TEXT NOT NULL PRIMARY KEY,
|
||||
"created_at" TEXT NOT NULL,
|
||||
@@ -37,11 +33,7 @@ CREATE TABLE IF NOT EXISTS "downloads"(
|
||||
"dispensa_id" TEXT NOT NULL,
|
||||
"timestamp" TEXT NOT NULL,
|
||||
FOREIGN KEY (dispensa_id) REFERENCES dispense(id)
|
||||
)
|
||||
|
||||
--
|
||||
-- Relations
|
||||
--
|
||||
);
|
||||
|
||||
-- Tags per le dispense
|
||||
CREATE TABLE IF NOT EXISTS "tags"(
|
||||
|
||||
+23
-32
@@ -2,43 +2,34 @@ package database
|
||||
|
||||
type Dispensa struct {
|
||||
Id string `db:"id"`
|
||||
CreatedAt string `db:"created_at"`
|
||||
OwnerId string `db:"owner_id"`
|
||||
Title string `db:"title"`
|
||||
Description string `db:"description"`
|
||||
}
|
||||
|
||||
type Upload struct {
|
||||
Id string `db:"id"`
|
||||
CreatedAt string `db:"created_at"`
|
||||
OwnerId string `db:"owner_id"`
|
||||
DispensaId string `db:"dispensa_id"`
|
||||
File string `db:"file"`
|
||||
}
|
||||
|
||||
type FileApproval struct {
|
||||
Id string `db:"id"`
|
||||
CreatedAt string `db:"created_at"`
|
||||
OwnerId string `db:"owner_id"`
|
||||
UploadId string `db:"upload_id"`
|
||||
Status string `db:"status"`
|
||||
}
|
||||
|
||||
type Download struct {
|
||||
DispensaId string `db:"dispensa_id"`
|
||||
Timestamp string `db:"timestamp"`
|
||||
}
|
||||
|
||||
type Tag struct {
|
||||
DispensaId string `db:"dispensa_id"`
|
||||
Tag string `db:"tag"`
|
||||
}
|
||||
|
||||
type UploadedContent struct {
|
||||
Id string `db:"id"`
|
||||
Hash string `db:"hash"`
|
||||
}
|
||||
|
||||
type HashApproval struct {
|
||||
Id string `db:"id"`
|
||||
Hash string `db:"hash"`
|
||||
}
|
||||
|
||||
type HashRejection struct {
|
||||
Id string `db:"id"`
|
||||
Hash string `db:"hash"`
|
||||
}
|
||||
|
||||
// Relations
|
||||
|
||||
type Owner struct {
|
||||
OwnerId string `db:"owner_id"`
|
||||
OwnedId string `db:"owned_id"`
|
||||
}
|
||||
|
||||
type Author struct {
|
||||
UserId string `db:"user_id"`
|
||||
DispensaId string `db:"dispensa_id"`
|
||||
}
|
||||
|
||||
type CreationTime struct {
|
||||
EntityId string `db:"entity_id"`
|
||||
CreatedAt string `db:"created_at"`
|
||||
}
|
||||
|
||||
+40
-14
@@ -22,10 +22,12 @@ type sqliteDB struct {
|
||||
}
|
||||
|
||||
func (db *sqliteDB) Migrate(migrationFolder string) error {
|
||||
log.Printf(`Creating migrations table`)
|
||||
if _, err := db.Exec(`CREATE TABLE IF NOT EXISTS migrations(timestamp TEXT, filename TEXT)`); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Printf(`Loading applied migrations`)
|
||||
appliedMigrations := []migration{}
|
||||
if err := db.Select(&appliedMigrations, `SELECT * FROM migrations`); err != nil {
|
||||
return err
|
||||
@@ -86,13 +88,14 @@ func (db *sqliteDB) Migrate(migrationFolder string) error {
|
||||
}
|
||||
|
||||
func (db *sqliteDB) CreateDispensa(template Dispensa) (string, error) {
|
||||
template.Id = "dispensa/" + util.GenerateRandomString(15)
|
||||
template.Id = "dispensa/" + util.GenerateRandomString(8)
|
||||
template.CreatedAt = time.Now().Format(time.RFC3339)
|
||||
|
||||
if _, err := db.NamedExec(`
|
||||
INSERT INTO
|
||||
dispense(id, title, description)
|
||||
dispense(id, created_at, owner_id, title, description)
|
||||
VALUES
|
||||
(:id, :title, :description)
|
||||
(:id, :created_at, :owner_id, :title, :description)
|
||||
`, &template); err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -102,7 +105,6 @@ func (db *sqliteDB) CreateDispensa(template Dispensa) (string, error) {
|
||||
|
||||
func (db *sqliteDB) GetDispensa(id string) (Dispensa, error) {
|
||||
var dispensa Dispensa
|
||||
|
||||
if err := db.Get(&dispensa, `SELECT * FROM dispense WHERE id = ?`, id); err != nil {
|
||||
return Dispensa{}, err
|
||||
}
|
||||
@@ -112,7 +114,6 @@ func (db *sqliteDB) GetDispensa(id string) (Dispensa, error) {
|
||||
|
||||
func (db *sqliteDB) AllDispense() ([]Dispensa, error) {
|
||||
var dispense []Dispensa
|
||||
|
||||
if err := db.Select(&dispense, `SELECT * FROM dispense`); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -122,10 +123,14 @@ func (db *sqliteDB) AllDispense() ([]Dispensa, error) {
|
||||
|
||||
func (db *sqliteDB) UpdateDispensa(d Dispensa) error {
|
||||
if _, err := db.NamedExec(`
|
||||
UPDATE dispense
|
||||
SET title = :title,
|
||||
description = :description
|
||||
WHERE id = :id
|
||||
UPDATE
|
||||
dispense
|
||||
SET
|
||||
owner_id = :owner_id
|
||||
title = :title,
|
||||
description = :description
|
||||
WHERE
|
||||
id = :id
|
||||
`, &d); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -137,8 +142,32 @@ func (db *sqliteDB) DeleteDispensa(id string) error {
|
||||
panic("TODO: Not implemented")
|
||||
}
|
||||
|
||||
func (db *sqliteDB) CreateUpload(template Upload) (string, error) {
|
||||
template.Id = "upload/" + util.GenerateRandomString(8)
|
||||
|
||||
if _, err := db.NamedExec(`
|
||||
INSERT INTO
|
||||
uploads(id, created_at, owner_id, dispensa_id, file)
|
||||
VALUES
|
||||
(:id, :created_at, :owner_id, :dispensa_id, :file)
|
||||
`, &template); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return template.Id, nil
|
||||
}
|
||||
|
||||
func (db *sqliteDB) GetUpload(id string) (Upload, error) {
|
||||
var upload Upload
|
||||
if err := db.Select(`SELECT * FROM uploads WHERE id = ?`, id); err != nil {
|
||||
return Upload{}, err
|
||||
}
|
||||
|
||||
return upload, nil
|
||||
}
|
||||
|
||||
func NewSqlite3Database(filename string) (*DB, error) {
|
||||
sqlDB, err := sqlx.Open("sqlite3", filename)
|
||||
sqlDB, err := sqlx.Open("sqlite3", filename+"?_fk=1")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@@ -153,9 +182,6 @@ func NewSqlite3Database(filename string) (*DB, error) {
|
||||
return &DB{
|
||||
DBMigrate: db,
|
||||
DBDispense: db,
|
||||
// DBUpload: db,
|
||||
// DBApprovedHashes: db,
|
||||
// DBRejectedHashes: db,
|
||||
// DBOther: db,
|
||||
DBUploads: db,
|
||||
}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user