use gorm model

This commit is contained in:
Quaternions 2024-12-09 22:27:52 -08:00
parent 69047eacb0
commit 37e4e29f04
9 changed files with 81 additions and 87 deletions

View File

@ -515,7 +515,8 @@ components:
- DisplayName - DisplayName
- Creator - Creator
- GameID - GameID
- Date - CreatedAt
- UpdatedAt
- Submitter - Submitter
- AssetID - AssetID
- AssetVersion - AssetVersion
@ -537,7 +538,10 @@ components:
GameID: GameID:
type: integer type: integer
format: int32 format: int32
Date: CreatedAt:
type: integer
format: int64
UpdatedAt:
type: integer type: integer
format: int64 format: int64
Submitter: Submitter:
@ -577,9 +581,6 @@ components:
GameID: GameID:
type: integer type: integer
format: int32 format: int32
Date:
type: integer
format: int64
SubmissionCreate: SubmissionCreate:
required: required:
- DisplayName - DisplayName

View File

@ -1166,8 +1166,12 @@ func (s *Submission) encodeFields(e *jx.Encoder) {
e.Int32(s.GameID) e.Int32(s.GameID)
} }
{ {
e.FieldStart("Date") e.FieldStart("CreatedAt")
e.Int64(s.Date) e.Int64(s.CreatedAt)
}
{
e.FieldStart("UpdatedAt")
e.Int64(s.UpdatedAt)
} }
{ {
e.FieldStart("Submitter") e.FieldStart("Submitter")
@ -1201,19 +1205,20 @@ func (s *Submission) encodeFields(e *jx.Encoder) {
} }
} }
var jsonFieldsNameOfSubmission = [12]string{ var jsonFieldsNameOfSubmission = [13]string{
0: "ID", 0: "ID",
1: "DisplayName", 1: "DisplayName",
2: "Creator", 2: "Creator",
3: "GameID", 3: "GameID",
4: "Date", 4: "CreatedAt",
5: "Submitter", 5: "UpdatedAt",
6: "AssetID", 6: "Submitter",
7: "AssetVersion", 7: "AssetID",
8: "Completed", 8: "AssetVersion",
9: "SubmissionType", 9: "Completed",
10: "TargetAssetID", 10: "SubmissionType",
11: "StatusID", 11: "TargetAssetID",
12: "StatusID",
} }
// Decode decodes Submission from json. // Decode decodes Submission from json.
@ -1273,20 +1278,32 @@ func (s *Submission) Decode(d *jx.Decoder) error {
}(); err != nil { }(); err != nil {
return errors.Wrap(err, "decode field \"GameID\"") return errors.Wrap(err, "decode field \"GameID\"")
} }
case "Date": case "CreatedAt":
requiredBitSet[0] |= 1 << 4 requiredBitSet[0] |= 1 << 4
if err := func() error { if err := func() error {
v, err := d.Int64() v, err := d.Int64()
s.Date = int64(v) s.CreatedAt = int64(v)
if err != nil { if err != nil {
return err return err
} }
return nil return nil
}(); err != nil { }(); err != nil {
return errors.Wrap(err, "decode field \"Date\"") return errors.Wrap(err, "decode field \"CreatedAt\"")
}
case "UpdatedAt":
requiredBitSet[0] |= 1 << 5
if err := func() error {
v, err := d.Int64()
s.UpdatedAt = int64(v)
if err != nil {
return err
}
return nil
}(); err != nil {
return errors.Wrap(err, "decode field \"UpdatedAt\"")
} }
case "Submitter": case "Submitter":
requiredBitSet[0] |= 1 << 5 requiredBitSet[0] |= 1 << 6
if err := func() error { if err := func() error {
v, err := d.Int64() v, err := d.Int64()
s.Submitter = int64(v) s.Submitter = int64(v)
@ -1298,7 +1315,7 @@ func (s *Submission) Decode(d *jx.Decoder) error {
return errors.Wrap(err, "decode field \"Submitter\"") return errors.Wrap(err, "decode field \"Submitter\"")
} }
case "AssetID": case "AssetID":
requiredBitSet[0] |= 1 << 6 requiredBitSet[0] |= 1 << 7
if err := func() error { if err := func() error {
v, err := d.Int64() v, err := d.Int64()
s.AssetID = int64(v) s.AssetID = int64(v)
@ -1310,7 +1327,7 @@ func (s *Submission) Decode(d *jx.Decoder) error {
return errors.Wrap(err, "decode field \"AssetID\"") return errors.Wrap(err, "decode field \"AssetID\"")
} }
case "AssetVersion": case "AssetVersion":
requiredBitSet[0] |= 1 << 7 requiredBitSet[1] |= 1 << 0
if err := func() error { if err := func() error {
v, err := d.Int64() v, err := d.Int64()
s.AssetVersion = int64(v) s.AssetVersion = int64(v)
@ -1322,7 +1339,7 @@ func (s *Submission) Decode(d *jx.Decoder) error {
return errors.Wrap(err, "decode field \"AssetVersion\"") return errors.Wrap(err, "decode field \"AssetVersion\"")
} }
case "Completed": case "Completed":
requiredBitSet[1] |= 1 << 0 requiredBitSet[1] |= 1 << 1
if err := func() error { if err := func() error {
v, err := d.Bool() v, err := d.Bool()
s.Completed = bool(v) s.Completed = bool(v)
@ -1334,7 +1351,7 @@ func (s *Submission) Decode(d *jx.Decoder) error {
return errors.Wrap(err, "decode field \"Completed\"") return errors.Wrap(err, "decode field \"Completed\"")
} }
case "SubmissionType": case "SubmissionType":
requiredBitSet[1] |= 1 << 1 requiredBitSet[1] |= 1 << 2
if err := func() error { if err := func() error {
v, err := d.Int32() v, err := d.Int32()
s.SubmissionType = int32(v) s.SubmissionType = int32(v)
@ -1356,7 +1373,7 @@ func (s *Submission) Decode(d *jx.Decoder) error {
return errors.Wrap(err, "decode field \"TargetAssetID\"") return errors.Wrap(err, "decode field \"TargetAssetID\"")
} }
case "StatusID": case "StatusID":
requiredBitSet[1] |= 1 << 3 requiredBitSet[1] |= 1 << 4
if err := func() error { if err := func() error {
v, err := d.Int32() v, err := d.Int32()
s.StatusID = int32(v) s.StatusID = int32(v)
@ -1378,7 +1395,7 @@ func (s *Submission) Decode(d *jx.Decoder) error {
var failures []validate.FieldError var failures []validate.FieldError
for i, mask := range [2]uint8{ for i, mask := range [2]uint8{
0b11111111, 0b11111111,
0b00001011, 0b00010111,
} { } {
if result := (requiredBitSet[i] & mask) ^ mask; result != 0 { if result := (requiredBitSet[i] & mask) ^ mask; result != 0 {
// Mask only required fields and check equality to mask using XOR. // Mask only required fields and check equality to mask using XOR.

View File

@ -1022,7 +1022,7 @@ func decodeListSubmissionsParams(args [0]string, argsEscaped bool, r *http.Reque
Name: "filter", Name: "filter",
Style: uri.QueryStyleForm, Style: uri.QueryStyleForm,
Explode: true, Explode: true,
Fields: []uri.QueryParameterObjectField{{Name: "ID", Required: true}, {Name: "DisplayName", Required: false}, {Name: "Creator", Required: false}, {Name: "GameID", Required: false}, {Name: "Date", Required: false}}, Fields: []uri.QueryParameterObjectField{{Name: "ID", Required: true}, {Name: "DisplayName", Required: false}, {Name: "Creator", Required: false}, {Name: "GameID", Required: false}},
} }
if err := q.HasParam(cfg); err == nil { if err := q.HasParam(cfg); err == nil {

View File

@ -585,7 +585,8 @@ type Submission struct {
DisplayName string `json:"DisplayName"` DisplayName string `json:"DisplayName"`
Creator string `json:"Creator"` Creator string `json:"Creator"`
GameID int32 `json:"GameID"` GameID int32 `json:"GameID"`
Date int64 `json:"Date"` CreatedAt int64 `json:"CreatedAt"`
UpdatedAt int64 `json:"UpdatedAt"`
Submitter int64 `json:"Submitter"` Submitter int64 `json:"Submitter"`
AssetID int64 `json:"AssetID"` AssetID int64 `json:"AssetID"`
AssetVersion int64 `json:"AssetVersion"` AssetVersion int64 `json:"AssetVersion"`
@ -615,9 +616,14 @@ func (s *Submission) GetGameID() int32 {
return s.GameID return s.GameID
} }
// GetDate returns the value of Date. // GetCreatedAt returns the value of CreatedAt.
func (s *Submission) GetDate() int64 { func (s *Submission) GetCreatedAt() int64 {
return s.Date return s.CreatedAt
}
// GetUpdatedAt returns the value of UpdatedAt.
func (s *Submission) GetUpdatedAt() int64 {
return s.UpdatedAt
} }
// GetSubmitter returns the value of Submitter. // GetSubmitter returns the value of Submitter.
@ -675,9 +681,14 @@ func (s *Submission) SetGameID(val int32) {
s.GameID = val s.GameID = val
} }
// SetDate sets the value of Date. // SetCreatedAt sets the value of CreatedAt.
func (s *Submission) SetDate(val int64) { func (s *Submission) SetCreatedAt(val int64) {
s.Date = val s.CreatedAt = val
}
// SetUpdatedAt sets the value of UpdatedAt.
func (s *Submission) SetUpdatedAt(val int64) {
s.UpdatedAt = val
} }
// SetSubmitter sets the value of Submitter. // SetSubmitter sets the value of Submitter.
@ -791,7 +802,6 @@ type SubmissionFilter struct {
DisplayName OptString `json:"DisplayName"` DisplayName OptString `json:"DisplayName"`
Creator OptString `json:"Creator"` Creator OptString `json:"Creator"`
GameID OptInt32 `json:"GameID"` GameID OptInt32 `json:"GameID"`
Date OptInt64 `json:"Date"`
} }
// GetID returns the value of ID. // GetID returns the value of ID.
@ -814,11 +824,6 @@ func (s *SubmissionFilter) GetGameID() OptInt32 {
return s.GameID return s.GameID
} }
// GetDate returns the value of Date.
func (s *SubmissionFilter) GetDate() OptInt64 {
return s.Date
}
// SetID sets the value of ID. // SetID sets the value of ID.
func (s *SubmissionFilter) SetID(val int64) { func (s *SubmissionFilter) SetID(val int64) {
s.ID = val s.ID = val
@ -839,11 +844,6 @@ func (s *SubmissionFilter) SetGameID(val OptInt32) {
s.GameID = val s.GameID = val
} }
// SetDate sets the value of Date.
func (s *SubmissionFilter) SetDate(val OptInt64) {
s.Date = val
}
// UpdateScriptOK is response for UpdateScript operation. // UpdateScriptOK is response for UpdateScript operation.
type UpdateScriptOK struct{} type UpdateScriptOK struct{}

View File

@ -152,23 +152,14 @@ func (s *SubmissionFilter) EncodeURI(e uri.Encoder) error {
}); err != nil { }); err != nil {
return errors.Wrap(err, "encode field \"GameID\"") return errors.Wrap(err, "encode field \"GameID\"")
} }
if err := e.EncodeField("Date", func(e uri.Encoder) error {
if val, ok := s.Date.Get(); ok {
return e.EncodeValue(conv.Int64ToString(val))
}
return nil
}); err != nil {
return errors.Wrap(err, "encode field \"Date\"")
}
return nil return nil
} }
var uriFieldsNameOfSubmissionFilter = [5]string{ var uriFieldsNameOfSubmissionFilter = [4]string{
0: "ID", 0: "ID",
1: "DisplayName", 1: "DisplayName",
2: "Creator", 2: "Creator",
3: "GameID", 3: "GameID",
4: "Date",
} }
// DecodeURI decodes SubmissionFilter from URI form. // DecodeURI decodes SubmissionFilter from URI form.
@ -270,30 +261,6 @@ func (s *SubmissionFilter) DecodeURI(d uri.Decoder) error {
}(); err != nil { }(); err != nil {
return errors.Wrap(err, "decode field \"GameID\"") return errors.Wrap(err, "decode field \"GameID\"")
} }
case "Date":
if err := func() error {
var sDotDateVal int64
if err := func() error {
val, err := d.DecodeValue()
if err != nil {
return err
}
c, err := conv.ToInt64(val)
if err != nil {
return err
}
sDotDateVal = c
return nil
}(); err != nil {
return err
}
s.Date.SetTo(sDotDateVal)
return nil
}(); err != nil {
return errors.Wrap(err, "decode field \"Date\"")
}
default: default:
return nil return nil
} }

View File

@ -1,5 +1,7 @@
package model package model
import "time"
type Policy int32 type Policy int32
const( const(
@ -10,7 +12,7 @@ const(
) )
type ScriptPolicy struct { type ScriptPolicy struct {
ID int64 ID int64 `gorm:"primaryKey"`
// Hash of the source code that leads to this policy. // Hash of the source code that leads to this policy.
// If this is a replacement mapping, the original source may not be pointed to by any policy. // If this is a replacement mapping, the original source may not be pointed to by any policy.
// The original source should still exist in the scripts table, which can be located by the same hash. // The original source should still exist in the scripts table, which can be located by the same hash.
@ -20,4 +22,6 @@ type ScriptPolicy struct {
// or 0 (other) // or 0 (other)
ToScriptID int64 ToScriptID int64
Policy Policy Policy Policy
CreatedAt time.Time
UpdatedAt time.Time
} }

View File

@ -1,8 +1,12 @@
package model package model
import "time"
type Script struct { type Script struct {
ID int64 ID int64 `gorm:"primaryKey"`
Hash uint64 Hash uint64
Source string Source string
SubmissionID int64 // which submission did this script first appear in SubmissionID int64 // which submission did this script first appear in
CreatedAt time.Time
UpdatedAt time.Time
} }

View File

@ -19,11 +19,12 @@ const(
) )
type Submission struct { type Submission struct {
ID int64 ID int64 `gorm:"primaryKey"`
DisplayName string DisplayName string
Creator string Creator string
GameID int32 GameID int32
Date time.Time CreatedAt time.Time
UpdatedAt time.Time
Submitter int64 // UserID Submitter int64 // UserID
AssetID int64 AssetID int64
AssetVersion int64 AssetVersion int64

View File

@ -1,7 +1,6 @@
package service package service
import ( import (
"time"
"errors" "errors"
"context" "context"
"git.itzana.me/strafesnet/maps-service/pkg/api" "git.itzana.me/strafesnet/maps-service/pkg/api"
@ -26,7 +25,6 @@ func (svc *Service) CreateSubmission(ctx context.Context, request *api.Submissio
DisplayName: request.DisplayName, DisplayName: request.DisplayName,
Creator: request.Creator, Creator: request.Creator,
GameID: request.GameID, GameID: request.GameID,
Date: time.Now(),
Submitter: int64(userInfo.UserID), Submitter: int64(userInfo.UserID),
AssetID: request.AssetID, AssetID: request.AssetID,
AssetVersion: request.AssetVersion, AssetVersion: request.AssetVersion,
@ -57,7 +55,8 @@ func (svc *Service) GetSubmission(ctx context.Context, params api.GetSubmissionP
DisplayName: submission.DisplayName, DisplayName: submission.DisplayName,
Creator: submission.Creator, Creator: submission.Creator,
GameID: submission.GameID, GameID: submission.GameID,
Date: submission.Date.Unix(), CreatedAt: submission.CreatedAt.Unix(),
UpdatedAt: submission.UpdatedAt.Unix(),
Submitter: submission.Submitter, Submitter: submission.Submitter,
AssetID: submission.AssetID, AssetID: submission.AssetID,
AssetVersion: submission.AssetVersion, AssetVersion: submission.AssetVersion,
@ -96,7 +95,8 @@ func (svc *Service) ListSubmissions(ctx context.Context, request api.ListSubmiss
DisplayName: items[i].DisplayName, DisplayName: items[i].DisplayName,
Creator: items[i].Creator, Creator: items[i].Creator,
GameID: items[i].GameID, GameID: items[i].GameID,
Date: items[i].Date.Unix(), CreatedAt: items[i].CreatedAt.Unix(),
UpdatedAt: items[i].UpdatedAt.Unix(),
Submitter: items[i].Submitter, Submitter: items[i].Submitter,
AssetID: items[i].AssetID, AssetID: items[i].AssetID,
AssetVersion: items[i].AssetVersion, AssetVersion: items[i].AssetVersion,