Use sq.InsertSingle to insert entities

This commit is contained in:
2023-05-28 22:27:38 +02:00
parent e9b4db0f1c
commit 3a9b15c2be
11 changed files with 114 additions and 242 deletions

View File

@@ -13,32 +13,22 @@ func (db *Database) CreateClient(ctx TxContext, userid models.UserID, ctype mode
return models.Client{}, err
}
now := time.Now().UTC()
entity := models.ClientDB{
ClientID: models.NewClientID(),
UserID: userid,
Type: ctype,
FCMToken: langext.Ptr(fcmToken),
TimestampCreated: time2DB(time.Now()),
AgentModel: agentModel,
AgentVersion: agentVersion,
}
clientid := models.NewClientID()
_, err = tx.Exec(ctx, "INSERT INTO clients (client_id, user_id, type, fcm_token, timestamp_created, agent_model, agent_version) VALUES (:cid, :uid, :typ, :fcm, :ts, :am, :av)", sq.PP{
"cid": clientid,
"uid": userid,
"typ": string(ctype),
"fcm": fcmToken,
"ts": time2DB(now),
"am": agentModel,
"av": agentVersion,
})
_, err = sq.InsertSingle(ctx, tx, "clients", entity)
if err != nil {
return models.Client{}, err
}
return models.Client{
ClientID: clientid,
UserID: userid,
Type: ctype,
FCMToken: langext.Ptr(fcmToken),
TimestampCreated: now,
AgentModel: agentModel,
AgentVersion: agentVersion,
}, nil
return entity.Model(), nil
}
func (db *Database) ClearFCMTokens(ctx TxContext, fcmtoken string) error {