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

@@ -62,33 +62,23 @@ func (db *Database) CreateChannel(ctx TxContext, userid models.UserID, dispName
return models.Channel{}, err
}
now := time.Now().UTC()
channelid := models.NewChannelID()
_, err = tx.Exec(ctx, "INSERT INTO channels (channel_id, owner_user_id, display_name, internal_name, description_name, subscribe_key, timestamp_created) VALUES (:cid, :ouid, :dnam, :inam, :hnam, :subkey, :ts)", sq.PP{
"cid": channelid,
"ouid": userid,
"dnam": dispName,
"inam": intName,
"hnam": nil,
"subkey": subscribeKey,
"ts": time2DB(now),
})
if err != nil {
return models.Channel{}, err
}
return models.Channel{
ChannelID: channelid,
entity := models.ChannelDB{
ChannelID: models.NewChannelID(),
OwnerUserID: userid,
DisplayName: dispName,
InternalName: intName,
SubscribeKey: subscribeKey,
TimestampCreated: now,
TimestampCreated: time2DB(time.Now()),
TimestampLastSent: nil,
MessagesSent: 0,
}, nil
}
_, err = sq.InsertSingle(ctx, tx, "channels", entity)
if err != nil {
return models.Channel{}, err
}
return entity.Model(), nil
}
func (db *Database) ListChannelsByOwner(ctx TxContext, userid models.UserID, subUserID models.UserID) ([]models.ChannelWithSubscription, error) {