SimpleCloudNotifier/scnserver/models/client.go

95 lines
2.7 KiB
Go
Raw Normal View History

2022-11-18 21:25:40 +01:00
package models
2022-11-19 12:47:23 +01:00
import (
2024-05-31 23:56:16 +02:00
"context"
"github.com/jmoiron/sqlx"
2022-11-19 12:47:23 +01:00
"gogs.mikescher.com/BlackForestBytes/goext/langext"
2022-12-11 03:14:42 +01:00
"gogs.mikescher.com/BlackForestBytes/goext/sq"
2022-11-19 12:47:23 +01:00
"time"
)
2022-11-18 21:25:40 +01:00
2023-04-21 21:45:16 +02:00
type ClientType string //@enum:type
2022-11-18 21:25:40 +01:00
const (
ClientTypeAndroid ClientType = "ANDROID"
ClientTypeIOS ClientType = "IOS"
2024-05-31 15:22:27 +02:00
ClientTypeLinux ClientType = "LINUX"
ClientTypeMacOS ClientType = "MACOS"
ClientTypeWindows ClientType = "WINDOWS"
2022-11-18 21:25:40 +01:00
)
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
2023-05-28 23:25:18 +02:00
FCMToken string
2022-11-18 21:25:40 +01:00
TimestampCreated time.Time
AgentModel string
AgentVersion string
2024-06-01 01:01:58 +02:00
DescriptionName *string
2022-11-18 21:25:40 +01:00
}
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,
2024-06-01 01:01:58 +02:00
DescriptionName: c.DescriptionName,
2022-11-19 12:47:23 +01:00
}
}
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"`
2023-05-28 23:25:18 +02:00
FCMToken string `json:"fcm_token"`
2022-11-19 12:47:23 +01:00
TimestampCreated string `json:"timestamp_created"`
AgentModel string `json:"agent_model"`
AgentVersion string `json:"agent_version"`
2024-06-01 01:01:58 +02:00
DescriptionName *string `json:"description_name"`
2022-11-19 12:47:23 +01:00
}
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"`
2023-05-28 23:25:18 +02:00
FCMToken string `db:"fcm_token"`
2022-11-19 12:47:23 +01:00
TimestampCreated int64 `db:"timestamp_created"`
AgentModel string `db:"agent_model"`
AgentVersion string `db:"agent_version"`
2024-06-01 01:01:58 +02:00
DescriptionName *string `db:"description_name"`
2022-11-19 12:47:23 +01:00
}
func (c ClientDB) Model() Client {
return Client{
ClientID: c.ClientID,
UserID: c.UserID,
Type: c.Type,
FCMToken: c.FCMToken,
2023-04-21 21:45:16 +02:00
TimestampCreated: timeFromMilli(c.TimestampCreated),
2022-11-19 12:47:23 +01:00
AgentModel: c.AgentModel,
AgentVersion: c.AgentVersion,
2024-06-01 01:01:58 +02:00
DescriptionName: c.DescriptionName,
2022-11-19 12:47:23 +01:00
}
}
2024-05-31 23:56:16 +02:00
func DecodeClient(ctx context.Context, q sq.Queryable, r *sqlx.Rows) (Client, error) {
data, err := sq.ScanSingle[ClientDB](ctx, q, r, sq.SModeFast, sq.Safe, true)
2022-11-19 12:47:23 +01:00
if err != nil {
return Client{}, err
}
return data.Model(), nil
}
2024-05-31 23:56:16 +02:00
func DecodeClients(ctx context.Context, q sq.Queryable, r *sqlx.Rows) ([]Client, error) {
data, err := sq.ScanAll[ClientDB](ctx, q, r, sq.SModeFast, sq.Safe, true)
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
}