2022-11-19 23:16:54 +01:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"blackforestbytes.com/simplecloudnotifier/models"
|
2022-12-07 22:11:44 +01:00
|
|
|
"blackforestbytes.com/simplecloudnotifier/sq"
|
2022-11-19 23:16:54 +01:00
|
|
|
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2022-11-20 22:18:24 +01:00
|
|
|
func (db *Database) CreateClient(ctx TxContext, userid models.UserID, ctype models.ClientType, fcmToken string, agentModel string, agentVersion string) (models.Client, error) {
|
2022-11-19 23:16:54 +01:00
|
|
|
tx, err := ctx.GetOrCreateTransaction(db)
|
|
|
|
if err != nil {
|
|
|
|
return models.Client{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
now := time.Now().UTC()
|
|
|
|
|
2022-12-07 22:11:44 +01:00
|
|
|
res, err := tx.Exec(ctx, "INSERT INTO clients (user_id, type, fcm_token, timestamp_created, agent_model, agent_version) VALUES (:uid, :typ, :fcm, :ts, :am, :av)", sq.PP{
|
|
|
|
"uid": userid,
|
|
|
|
"typ": string(ctype),
|
|
|
|
"fcm": fcmToken,
|
|
|
|
"ts": time2DB(now),
|
|
|
|
"am": agentModel,
|
|
|
|
"av": agentVersion,
|
|
|
|
})
|
2022-11-19 23:16:54 +01:00
|
|
|
if err != nil {
|
|
|
|
return models.Client{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
liid, err := res.LastInsertId()
|
|
|
|
if err != nil {
|
|
|
|
return models.Client{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return models.Client{
|
2022-11-20 22:18:24 +01:00
|
|
|
ClientID: models.ClientID(liid),
|
2022-11-19 23:16:54 +01:00
|
|
|
UserID: userid,
|
|
|
|
Type: ctype,
|
|
|
|
FCMToken: langext.Ptr(fcmToken),
|
|
|
|
TimestampCreated: now,
|
|
|
|
AgentModel: agentModel,
|
|
|
|
AgentVersion: agentVersion,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) ClearFCMTokens(ctx TxContext, fcmtoken string) error {
|
|
|
|
tx, err := ctx.GetOrCreateTransaction(db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-12-07 22:11:44 +01:00
|
|
|
_, err = tx.Exec(ctx, "DELETE FROM clients WHERE fcm_token = :fcm", sq.PP{"fcm": fcmtoken})
|
2022-11-19 23:16:54 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-20 22:18:24 +01:00
|
|
|
func (db *Database) ListClients(ctx TxContext, userid models.UserID) ([]models.Client, error) {
|
2022-11-19 23:16:54 +01:00
|
|
|
tx, err := ctx.GetOrCreateTransaction(db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-12-07 22:11:44 +01:00
|
|
|
rows, err := tx.Query(ctx, "SELECT * FROM clients WHERE user_id = :uid", sq.PP{"uid": userid})
|
2022-11-19 23:16:54 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := models.DecodeClients(rows)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
2022-11-20 22:18:24 +01:00
|
|
|
func (db *Database) GetClient(ctx TxContext, userid models.UserID, clientid models.ClientID) (models.Client, error) {
|
2022-11-19 23:16:54 +01:00
|
|
|
tx, err := ctx.GetOrCreateTransaction(db)
|
|
|
|
if err != nil {
|
|
|
|
return models.Client{}, err
|
|
|
|
}
|
|
|
|
|
2022-12-07 22:11:44 +01:00
|
|
|
rows, err := tx.Query(ctx, "SELECT * FROM clients WHERE user_id = :uid AND client_id = :cid LIMIT 1", sq.PP{
|
|
|
|
"uid": userid,
|
|
|
|
"cid": clientid,
|
|
|
|
})
|
2022-11-19 23:16:54 +01:00
|
|
|
if err != nil {
|
|
|
|
return models.Client{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := models.DecodeClient(rows)
|
|
|
|
if err != nil {
|
|
|
|
return models.Client{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return client, nil
|
|
|
|
}
|
|
|
|
|
2022-11-20 22:18:24 +01:00
|
|
|
func (db *Database) DeleteClient(ctx TxContext, clientid models.ClientID) error {
|
2022-11-19 23:16:54 +01:00
|
|
|
tx, err := ctx.GetOrCreateTransaction(db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-12-07 22:11:44 +01:00
|
|
|
_, err = tx.Exec(ctx, "DELETE FROM clients WHERE client_id = :cid", sq.PP{"cid": clientid})
|
2022-11-19 23:16:54 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-11-30 12:40:03 +01:00
|
|
|
|
|
|
|
func (db *Database) DeleteClientsByFCM(ctx TxContext, fcmtoken string) error {
|
|
|
|
tx, err := ctx.GetOrCreateTransaction(db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-12-07 22:11:44 +01:00
|
|
|
_, err = tx.Exec(ctx, "DELETE FROM clients WHERE fcm_token = :fcm", sq.PP{"fcm": fcmtoken})
|
2022-11-30 12:40:03 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|