2022-11-18 21:25:40 +01:00
|
|
|
package models
|
|
|
|
|
2022-11-19 12:47:23 +01:00
|
|
|
import (
|
2022-12-07 22:11:44 +01:00
|
|
|
"github.com/jmoiron/sqlx"
|
2022-11-19 12:47:23 +01:00
|
|
|
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
|
|
|
"time"
|
|
|
|
)
|
2022-11-18 21:25:40 +01:00
|
|
|
|
|
|
|
type ClientType string
|
|
|
|
|
|
|
|
const (
|
|
|
|
ClientTypeAndroid ClientType = "ANDROID"
|
|
|
|
ClientTypeIOS ClientType = "IOS"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Client struct {
|
2022-11-20 22:18:24 +01:00
|
|
|
ClientID ClientID
|
|
|
|
UserID UserID
|
2022-11-18 21:25:40 +01:00
|
|
|
Type ClientType
|
|
|
|
FCMToken *string
|
|
|
|
TimestampCreated time.Time
|
|
|
|
AgentModel string
|
|
|
|
AgentVersion string
|
|
|
|
}
|
|
|
|
|
2022-11-19 12:47:23 +01:00
|
|
|
func (c Client) JSON() ClientJSON {
|
|
|
|
return ClientJSON{
|
|
|
|
ClientID: c.ClientID,
|
|
|
|
UserID: c.UserID,
|
|
|
|
Type: c.Type,
|
|
|
|
FCMToken: c.FCMToken,
|
|
|
|
TimestampCreated: c.TimestampCreated.Format(time.RFC3339Nano),
|
|
|
|
AgentModel: c.AgentModel,
|
|
|
|
AgentVersion: c.AgentVersion,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-18 21:25:40 +01:00
|
|
|
type ClientJSON struct {
|
2022-11-20 22:18:24 +01:00
|
|
|
ClientID ClientID `json:"client_id"`
|
|
|
|
UserID UserID `json:"user_id"`
|
2022-11-19 12:47:23 +01:00
|
|
|
Type ClientType `json:"type"`
|
|
|
|
FCMToken *string `json:"fcm_token"`
|
|
|
|
TimestampCreated string `json:"timestamp_created"`
|
|
|
|
AgentModel string `json:"agent_model"`
|
|
|
|
AgentVersion string `json:"agent_version"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type ClientDB struct {
|
2022-11-20 22:18:24 +01:00
|
|
|
ClientID ClientID `db:"client_id"`
|
|
|
|
UserID UserID `db:"user_id"`
|
2022-11-19 12:47:23 +01:00
|
|
|
Type ClientType `db:"type"`
|
|
|
|
FCMToken *string `db:"fcm_token"`
|
|
|
|
TimestampCreated int64 `db:"timestamp_created"`
|
|
|
|
AgentModel string `db:"agent_model"`
|
|
|
|
AgentVersion string `db:"agent_version"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c ClientDB) Model() Client {
|
|
|
|
return Client{
|
|
|
|
ClientID: c.ClientID,
|
|
|
|
UserID: c.UserID,
|
|
|
|
Type: c.Type,
|
|
|
|
FCMToken: c.FCMToken,
|
|
|
|
TimestampCreated: time.UnixMilli(c.TimestampCreated),
|
|
|
|
AgentModel: c.AgentModel,
|
|
|
|
AgentVersion: c.AgentVersion,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-07 22:11:44 +01:00
|
|
|
func DecodeClient(r *sqlx.Rows) (Client, error) {
|
|
|
|
data, err := scanSingle[ClientDB](r)
|
2022-11-19 12:47:23 +01:00
|
|
|
if err != nil {
|
|
|
|
return Client{}, err
|
|
|
|
}
|
|
|
|
return data.Model(), nil
|
|
|
|
}
|
|
|
|
|
2022-12-07 22:11:44 +01:00
|
|
|
func DecodeClients(r *sqlx.Rows) ([]Client, error) {
|
|
|
|
data, err := scanAll[ClientDB](r)
|
2022-11-19 12:47:23 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return langext.ArrMap(data, func(v ClientDB) Client { return v.Model() }), nil
|
2022-11-18 21:25:40 +01:00
|
|
|
}
|