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"` ItemId int64 `db:"item_id"` PermissionEdit int `db:"permission_edit"` StartDatetime string `db:"start_datetime"` EndDatetime 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_item_shares (token, item_id, permission_edit, start_datetime, end_datetime, password) VALUES (:token, :item_id, :permission_edit, :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_shares WHERE id = $1`, id) if errors.Is(err, sql.ErrNoRows) { return false, nil } return true, err }