Use sq.InsertSingle to insert entities
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -14,38 +14,28 @@ func (db *Database) CreateRetryDelivery(ctx TxContext, client models.Client, msg
|
||||
return models.Delivery{}, err
|
||||
}
|
||||
|
||||
now := time.Now().UTC()
|
||||
now := time.Now()
|
||||
next := scn.NextDeliveryTimestamp(now)
|
||||
|
||||
deliveryid := models.NewDeliveryID()
|
||||
entity := models.DeliveryDB{
|
||||
DeliveryID: models.NewDeliveryID(),
|
||||
MessageID: msg.MessageID,
|
||||
ReceiverUserID: client.UserID,
|
||||
ReceiverClientID: client.ClientID,
|
||||
TimestampCreated: time2DB(now),
|
||||
TimestampFinalized: nil,
|
||||
Status: models.DeliveryStatusRetry,
|
||||
RetryCount: 0,
|
||||
NextDelivery: langext.Ptr(time2DB(next)),
|
||||
FCMMessageID: nil,
|
||||
}
|
||||
|
||||
_, err = tx.Exec(ctx, "INSERT INTO deliveries (delivery_id, message_id, receiver_user_id, receiver_client_id, timestamp_created, timestamp_finalized, status, fcm_message_id, next_delivery) VALUES (:did, :mid, :ruid, :rcid, :tsc, :tsf, :stat, :fcm, :next)", sq.PP{
|
||||
"did": deliveryid,
|
||||
"mid": msg.MessageID,
|
||||
"ruid": client.UserID,
|
||||
"rcid": client.ClientID,
|
||||
"tsc": time2DB(now),
|
||||
"tsf": nil,
|
||||
"stat": models.DeliveryStatusRetry,
|
||||
"fcm": nil,
|
||||
"next": time2DB(next),
|
||||
})
|
||||
_, err = sq.InsertSingle(ctx, tx, "deliveries", entity)
|
||||
if err != nil {
|
||||
return models.Delivery{}, err
|
||||
}
|
||||
|
||||
return models.Delivery{
|
||||
DeliveryID: deliveryid,
|
||||
MessageID: msg.MessageID,
|
||||
ReceiverUserID: client.UserID,
|
||||
ReceiverClientID: client.ClientID,
|
||||
TimestampCreated: now,
|
||||
TimestampFinalized: nil,
|
||||
Status: models.DeliveryStatusRetry,
|
||||
RetryCount: 0,
|
||||
NextDelivery: langext.Ptr(next),
|
||||
FCMMessageID: nil,
|
||||
}, nil
|
||||
return entity.Model(), nil
|
||||
}
|
||||
|
||||
func (db *Database) CreateSuccessDelivery(ctx TxContext, client models.Client, msg models.Message, fcmDelivID string) (models.Delivery, error) {
|
||||
@@ -54,37 +44,27 @@ func (db *Database) CreateSuccessDelivery(ctx TxContext, client models.Client, m
|
||||
return models.Delivery{}, err
|
||||
}
|
||||
|
||||
now := time.Now().UTC()
|
||||
now := time.Now()
|
||||
|
||||
deliveryid := models.NewDeliveryID()
|
||||
|
||||
_, err = tx.Exec(ctx, "INSERT INTO deliveries (delivery_id, message_id, receiver_user_id, receiver_client_id, timestamp_created, timestamp_finalized, status, fcm_message_id, next_delivery) VALUES (:did, :mid, :ruid, :rcid, :tsc, :tsf, :stat, :fcm, :next)", sq.PP{
|
||||
"did": deliveryid,
|
||||
"mid": msg.MessageID,
|
||||
"ruid": client.UserID,
|
||||
"rcid": client.ClientID,
|
||||
"tsc": time2DB(now),
|
||||
"tsf": time2DB(now),
|
||||
"stat": models.DeliveryStatusSuccess,
|
||||
"fcm": fcmDelivID,
|
||||
"next": nil,
|
||||
})
|
||||
if err != nil {
|
||||
return models.Delivery{}, err
|
||||
}
|
||||
|
||||
return models.Delivery{
|
||||
DeliveryID: deliveryid,
|
||||
entity := models.DeliveryDB{
|
||||
DeliveryID: models.NewDeliveryID(),
|
||||
MessageID: msg.MessageID,
|
||||
ReceiverUserID: client.UserID,
|
||||
ReceiverClientID: client.ClientID,
|
||||
TimestampCreated: now,
|
||||
TimestampFinalized: langext.Ptr(now),
|
||||
TimestampCreated: time2DB(now),
|
||||
TimestampFinalized: langext.Ptr(time2DB(now)),
|
||||
Status: models.DeliveryStatusSuccess,
|
||||
RetryCount: 0,
|
||||
NextDelivery: nil,
|
||||
FCMMessageID: langext.Ptr(fcmDelivID),
|
||||
}, nil
|
||||
}
|
||||
|
||||
_, err = sq.InsertSingle(ctx, tx, "deliveries", entity)
|
||||
if err != nil {
|
||||
return models.Delivery{}, err
|
||||
}
|
||||
|
||||
return entity.Model(), nil
|
||||
}
|
||||
|
||||
func (db *Database) ListRetrieableDeliveries(ctx TxContext, pageSize int) ([]models.Delivery, error) {
|
||||
|
||||
@@ -15,36 +15,25 @@ func (db *Database) CreateKeyToken(ctx TxContext, name string, owner models.User
|
||||
return models.KeyToken{}, err
|
||||
}
|
||||
|
||||
now := time.Now().UTC()
|
||||
entity := models.KeyTokenDB{
|
||||
KeyTokenID: models.NewKeyTokenID(),
|
||||
Name: name,
|
||||
TimestampCreated: time2DB(time.Now()),
|
||||
TimestampLastUsed: nil,
|
||||
OwnerUserID: owner,
|
||||
AllChannels: allChannels,
|
||||
Channels: strings.Join(langext.ArrMap(channels, func(v models.ChannelID) string { return v.String() }), ";"),
|
||||
Token: token,
|
||||
Permissions: permissions.String(),
|
||||
MessagesSent: 0,
|
||||
}
|
||||
|
||||
keyTokenid := models.NewKeyTokenID()
|
||||
|
||||
_, err = tx.Exec(ctx, "INSERT INTO keytokens (keytoken_id, name, timestamp_created, owner_user_id, all_channels, channels, token, permissions) VALUES (:tid, :nam, :tsc, :owr, :all, :cha, :tok, :prm)", sq.PP{
|
||||
"tid": keyTokenid,
|
||||
"nam": name,
|
||||
"tsc": time2DB(now),
|
||||
"owr": owner.String(),
|
||||
"all": bool2DB(allChannels),
|
||||
"cha": strings.Join(langext.ArrMap(channels, func(v models.ChannelID) string { return v.String() }), ";"),
|
||||
"tok": token,
|
||||
"prm": permissions.String(),
|
||||
})
|
||||
_, err = sq.InsertSingle(ctx, tx, "keytokens", entity)
|
||||
if err != nil {
|
||||
return models.KeyToken{}, err
|
||||
}
|
||||
|
||||
return models.KeyToken{
|
||||
KeyTokenID: keyTokenid,
|
||||
Name: name,
|
||||
TimestampCreated: now,
|
||||
TimestampLastUsed: nil,
|
||||
OwnerUserID: owner,
|
||||
AllChannels: allChannels,
|
||||
Channels: channels,
|
||||
Token: token,
|
||||
Permissions: permissions,
|
||||
MessagesSent: 0,
|
||||
}, nil
|
||||
return entity.Model(), nil
|
||||
}
|
||||
|
||||
func (db *Database) ListKeyTokens(ctx TxContext, ownerID models.UserID) ([]models.KeyToken, error) {
|
||||
|
||||
@@ -62,46 +62,30 @@ func (db *Database) CreateMessage(ctx TxContext, senderUserID models.UserID, cha
|
||||
return models.Message{}, err
|
||||
}
|
||||
|
||||
now := time.Now().UTC()
|
||||
|
||||
messageid := models.NewMessageID()
|
||||
|
||||
_, err = tx.Exec(ctx, "INSERT INTO messages (message_id, sender_user_id, owner_user_id, channel_internal_name, channel_id, timestamp_real, timestamp_client, title, content, priority, usr_message_id, sender_ip, sender_name, used_key_id) VALUES (:mid, :suid, :ouid, :cnam, :cid, :tsr, :tsc, :tit, :cnt, :prio, :umid, :ip, :snam, :uk)", sq.PP{
|
||||
"mid": messageid,
|
||||
"suid": senderUserID,
|
||||
"ouid": channel.OwnerUserID,
|
||||
"cnam": channel.InternalName,
|
||||
"cid": channel.ChannelID,
|
||||
"tsr": time2DB(now),
|
||||
"tsc": time2DBOpt(timestampSend),
|
||||
"tit": title,
|
||||
"cnt": content,
|
||||
"prio": priority,
|
||||
"umid": userMsgId,
|
||||
"ip": senderIP,
|
||||
"snam": senderName,
|
||||
"uk": usedKeyID,
|
||||
})
|
||||
if err != nil {
|
||||
return models.Message{}, err
|
||||
}
|
||||
|
||||
return models.Message{
|
||||
MessageID: messageid,
|
||||
entity := models.MessageDB{
|
||||
MessageID: models.NewMessageID(),
|
||||
SenderUserID: senderUserID,
|
||||
OwnerUserID: channel.OwnerUserID,
|
||||
ChannelInternalName: channel.InternalName,
|
||||
ChannelID: channel.ChannelID,
|
||||
SenderIP: senderIP,
|
||||
SenderName: senderName,
|
||||
TimestampReal: now,
|
||||
TimestampClient: timestampSend,
|
||||
TimestampReal: time2DB(time.Now()),
|
||||
TimestampClient: time2DBOpt(timestampSend),
|
||||
Title: title,
|
||||
Content: content,
|
||||
Priority: priority,
|
||||
UserMessageID: userMsgId,
|
||||
UsedKeyID: usedKeyID,
|
||||
}, nil
|
||||
Deleted: bool2DB(false),
|
||||
}
|
||||
|
||||
_, err = sq.InsertSingle(ctx, tx, "messages", entity)
|
||||
if err != nil {
|
||||
return models.Message{}, err
|
||||
}
|
||||
|
||||
return entity.Model(), nil
|
||||
}
|
||||
|
||||
func (db *Database) DeleteMessage(ctx TxContext, messageID models.MessageID) error {
|
||||
|
||||
@@ -13,32 +13,22 @@ func (db *Database) CreateSubscription(ctx TxContext, subscriberUID models.UserI
|
||||
return models.Subscription{}, err
|
||||
}
|
||||
|
||||
now := time.Now().UTC()
|
||||
|
||||
subscriptionid := models.NewSubscriptionID()
|
||||
|
||||
_, err = tx.Exec(ctx, "INSERT INTO subscriptions (subscription_id, subscriber_user_id, channel_owner_user_id, channel_internal_name, channel_id, timestamp_created, confirmed) VALUES (:sid, :suid, :ouid, :cnam, :cid, :ts, :conf)", sq.PP{
|
||||
"sid": subscriptionid,
|
||||
"suid": subscriberUID,
|
||||
"ouid": channel.OwnerUserID,
|
||||
"cnam": channel.InternalName,
|
||||
"cid": channel.ChannelID,
|
||||
"ts": time2DB(now),
|
||||
"conf": confirmed,
|
||||
})
|
||||
if err != nil {
|
||||
return models.Subscription{}, err
|
||||
}
|
||||
|
||||
return models.Subscription{
|
||||
SubscriptionID: subscriptionid,
|
||||
entity := models.SubscriptionDB{
|
||||
SubscriptionID: models.NewSubscriptionID(),
|
||||
SubscriberUserID: subscriberUID,
|
||||
ChannelOwnerUserID: channel.OwnerUserID,
|
||||
ChannelID: channel.ChannelID,
|
||||
ChannelInternalName: channel.InternalName,
|
||||
TimestampCreated: now,
|
||||
Confirmed: confirmed,
|
||||
}, nil
|
||||
TimestampCreated: time2DB(time.Now()),
|
||||
Confirmed: bool2DB(confirmed),
|
||||
}
|
||||
|
||||
_, err = sq.InsertSingle(ctx, tx, "subscriptions", entity)
|
||||
if err != nil {
|
||||
return models.Subscription{}, err
|
||||
}
|
||||
|
||||
return entity.Model(), nil
|
||||
}
|
||||
|
||||
func (db *Database) ListSubscriptionsByChannel(ctx TxContext, channelID models.ChannelID) ([]models.Subscription, error) {
|
||||
|
||||
@@ -13,25 +13,10 @@ func (db *Database) CreateUser(ctx TxContext, protoken *string, username *string
|
||||
return models.User{}, err
|
||||
}
|
||||
|
||||
now := time.Now().UTC()
|
||||
|
||||
userid := models.NewUserID()
|
||||
|
||||
_, err = tx.Exec(ctx, "INSERT INTO users (user_id, username, is_pro, pro_token, timestamp_created) VALUES (:uid, :un, :pro, :tok, :ts)", sq.PP{
|
||||
"uid": userid,
|
||||
"un": username,
|
||||
"pro": bool2DB(protoken != nil),
|
||||
"tok": protoken,
|
||||
"ts": time2DB(now),
|
||||
})
|
||||
if err != nil {
|
||||
return models.User{}, err
|
||||
}
|
||||
|
||||
return models.User{
|
||||
UserID: userid,
|
||||
entity := models.UserDB{
|
||||
UserID: models.NewUserID(),
|
||||
Username: username,
|
||||
TimestampCreated: now,
|
||||
TimestampCreated: time2DB(time.Now()),
|
||||
TimestampLastRead: nil,
|
||||
TimestampLastSent: nil,
|
||||
MessagesSent: 0,
|
||||
@@ -39,7 +24,14 @@ func (db *Database) CreateUser(ctx TxContext, protoken *string, username *string
|
||||
QuotaUsedDay: nil,
|
||||
IsPro: protoken != nil,
|
||||
ProToken: protoken,
|
||||
}, nil
|
||||
}
|
||||
|
||||
_, err = sq.InsertSingle(ctx, tx, "users", entity)
|
||||
if err != nil {
|
||||
return models.User{}, err
|
||||
}
|
||||
|
||||
return entity.Model(), nil
|
||||
}
|
||||
|
||||
func (db *Database) ClearProTokens(ctx TxContext, protoken string) error {
|
||||
|
||||
Reference in New Issue
Block a user