BrainMinder/models/itemshare.go
2025-01-06 12:03:37 +01:00

53 lines
1.2 KiB
Go

package models
import (
"brainminder.speedtech.it/internal/database"
"database/sql"
"errors"
)
type ItemShareModel struct {
DB *database.DB
}
type ItemShare struct {
Id int64 `db:"id"`
Token string `db:"token"`
Item_id int64 `db:"item_id"`
Update int `db:"hidden"`
Start_datetime string `db:"start_datetime"`
End_datetime string `db:"end_datetime"`
Password string `db:"password"`
}
func (model *ItemShareModel) Create(ItemShare *ItemShare) (int64, error) {
ctx, cancel := database.GetContext()
defer cancel()
query := `INSERT INTO bm_items_share (token, item_id, update, start_datetime, end_datetime, password) VALUES (:token, :item_id, :update, :start_datetime, :end_datetime, :password)`
result, err := model.DB.NamedExecContext(ctx, query, ItemShare)
if err != nil {
return 0, err
}
id, err := result.LastInsertId()
if err != nil {
return 0, err
}
return id, err
}
func (model *ItemShareModel) Delete(id int) (bool, error) {
ctx, cancel := database.GetContext()
defer cancel()
_, err := model.DB.ExecContext(ctx, `DELETE FROM bm_item_share WHERE id = $1`, id)
if errors.Is(err, sql.ErrNoRows) {
return false, nil
}
return true, err
}