forked from mtusi2026/mtusi2026lab6
chore: import lab5 baseline
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
package model
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
ErrNotFound = errors.New("not found")
|
||||
ErrConflict = errors.New("conflict")
|
||||
ErrInvalidMembers = errors.New("invalid members")
|
||||
ErrInvalidCursor = errors.New("invalid cursor")
|
||||
ErrIdempotencyConflict = errors.New("idempotency conflict")
|
||||
ErrInvalidToken = errors.New("invalid token")
|
||||
ErrSessionNotFound = errors.New("session not found")
|
||||
ErrInvalidInput = errors.New("invalid input")
|
||||
ErrUnsupportedMedia = errors.New("unsupported media")
|
||||
ErrPayloadTooLarge = errors.New("payload too large")
|
||||
ErrCapacity = errors.New("capacity exhausted")
|
||||
ErrUnavailable = errors.New("dependency unavailable")
|
||||
ErrStaleLease = errors.New("stale lease")
|
||||
ErrStaleAttempt = errors.New("stale attempt")
|
||||
ErrStalePipeline = errors.New("stale pipeline")
|
||||
ErrJobBusy = errors.New("job is already leased")
|
||||
ErrJobComplete = errors.New("job is already complete")
|
||||
ErrJobNotDue = errors.New("job is not due")
|
||||
)
|
||||
@@ -0,0 +1,223 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
AttachmentPending = "pending"
|
||||
AttachmentQueued = "queued"
|
||||
AttachmentProcessing = "processing"
|
||||
AttachmentReady = "ready"
|
||||
AttachmentFailed = "failed"
|
||||
AttachmentDeleting = "deleting"
|
||||
|
||||
AttachmentJobQueued = "queued"
|
||||
AttachmentJobProcessing = "processing"
|
||||
AttachmentJobReady = "ready"
|
||||
AttachmentJobFailed = "failed"
|
||||
|
||||
JobMutationApplied JobMutationResult = "applied"
|
||||
JobMutationAlreadyComplete JobMutationResult = "already_complete"
|
||||
JobMutationStale JobMutationResult = "stale"
|
||||
)
|
||||
|
||||
const timestampLayout = "2006-01-02T15:04:05.000000Z"
|
||||
|
||||
type User struct {
|
||||
ID string `json:"id"`
|
||||
Username string `json:"username"`
|
||||
DisplayName string `json:"display_name"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
type UserCredentials struct {
|
||||
User
|
||||
PasswordHash string `json:"-"`
|
||||
}
|
||||
|
||||
type Chat struct {
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
MemberIDs []string `json:"member_ids"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
type Message struct {
|
||||
ID string `json:"id"`
|
||||
ChatID string `json:"chat_id"`
|
||||
SenderID string `json:"sender_id"`
|
||||
Text string `json:"text"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
AttachmentIDs []string `json:"attachment_ids"`
|
||||
}
|
||||
|
||||
type Attachment struct {
|
||||
ID string `json:"id"`
|
||||
ChatID string `json:"chat_id"`
|
||||
OwnerID string `json:"owner_id"`
|
||||
Filename string `json:"filename"`
|
||||
ContentType string `json:"content_type"`
|
||||
SizeBytes int64 `json:"size_bytes"`
|
||||
SHA256 string `json:"sha256"`
|
||||
Status string `json:"status"`
|
||||
Variants []Variant `json:"variants"`
|
||||
ErrorCode *string `json:"error_code"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
ObjectKey string `json:"-"`
|
||||
StagingKey string `json:"-"`
|
||||
ReadyAt *time.Time `json:"-"`
|
||||
Referenced bool `json:"-"`
|
||||
PipelineVersion int `json:"-"`
|
||||
}
|
||||
|
||||
type StoredObject struct {
|
||||
Key string
|
||||
Size int64
|
||||
LastModified time.Time
|
||||
}
|
||||
|
||||
type Variant struct {
|
||||
Name string `json:"name"`
|
||||
ContentType string `json:"content_type"`
|
||||
SizeBytes int64 `json:"size_bytes"`
|
||||
SHA256 string `json:"sha256"`
|
||||
Width int `json:"width"`
|
||||
Height int `json:"height"`
|
||||
ObjectKey string `json:"-"`
|
||||
PipelineVersion int `json:"-"`
|
||||
}
|
||||
|
||||
type AttachmentJob struct {
|
||||
ID string
|
||||
AttachmentID string
|
||||
PipelineVersion int
|
||||
Attempt int
|
||||
Status string
|
||||
AvailableAt time.Time
|
||||
LeaseToken string
|
||||
LeaseOwner string
|
||||
LeaseUntil *time.Time
|
||||
ErrorCode *string
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
type AttachmentJobMessage struct {
|
||||
SchemaVersion int `json:"schema_version"`
|
||||
JobID string `json:"job_id"`
|
||||
Type string `json:"type"`
|
||||
AttachmentID string `json:"attachment_id"`
|
||||
PipelineVersion int `json:"pipeline_version"`
|
||||
Attempt int `json:"attempt"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
Traceparent string `json:"traceparent,omitempty"`
|
||||
Tracestate string `json:"tracestate,omitempty"`
|
||||
}
|
||||
|
||||
type AttachmentJobLease struct {
|
||||
Job AttachmentJob
|
||||
Attachment Attachment
|
||||
}
|
||||
|
||||
type OutboxEvent struct {
|
||||
ID string
|
||||
Topic string
|
||||
PartitionKey string
|
||||
Payload json.RawMessage
|
||||
JobID string
|
||||
Attempt int
|
||||
AvailableAt time.Time
|
||||
PublishTries int
|
||||
LeaseToken string
|
||||
LeaseUntil *time.Time
|
||||
LastErrorCode *string
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
type JobMutationResult string
|
||||
|
||||
type RecoveryResult struct {
|
||||
Requeued int
|
||||
Failed int
|
||||
}
|
||||
|
||||
type Position struct {
|
||||
CreatedAt time.Time
|
||||
ID string
|
||||
}
|
||||
|
||||
type userJSON struct {
|
||||
ID string `json:"id"`
|
||||
Username string `json:"username"`
|
||||
DisplayName string `json:"display_name"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
}
|
||||
|
||||
type chatJSON struct {
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
MemberIDs []string `json:"member_ids"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
}
|
||||
|
||||
type messageJSON struct {
|
||||
ID string `json:"id"`
|
||||
ChatID string `json:"chat_id"`
|
||||
SenderID string `json:"sender_id"`
|
||||
Text string `json:"text"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
AttachmentIDs []string `json:"attachment_ids"`
|
||||
}
|
||||
|
||||
type attachmentJSON struct {
|
||||
ID string `json:"id"`
|
||||
ChatID string `json:"chat_id"`
|
||||
OwnerID string `json:"owner_id"`
|
||||
Filename string `json:"filename"`
|
||||
ContentType string `json:"content_type"`
|
||||
SizeBytes int64 `json:"size_bytes"`
|
||||
SHA256 string `json:"sha256"`
|
||||
Status string `json:"status"`
|
||||
Variants []Variant `json:"variants"`
|
||||
ErrorCode *string `json:"error_code"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
UpdatedAt string `json:"updated_at"`
|
||||
}
|
||||
|
||||
func (u User) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(userJSON{u.ID, u.Username, u.DisplayName, formatTime(u.CreatedAt)})
|
||||
}
|
||||
|
||||
func (c Chat) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(chatJSON{c.ID, c.Title, c.MemberIDs, formatTime(c.CreatedAt)})
|
||||
}
|
||||
|
||||
func (m Message) MarshalJSON() ([]byte, error) {
|
||||
attachments := m.AttachmentIDs
|
||||
if attachments == nil {
|
||||
attachments = []string{}
|
||||
}
|
||||
return json.Marshal(messageJSON{m.ID, m.ChatID, m.SenderID, m.Text, formatTime(m.CreatedAt), attachments})
|
||||
}
|
||||
|
||||
func (a Attachment) MarshalJSON() ([]byte, error) {
|
||||
variants := a.Variants
|
||||
if variants == nil {
|
||||
variants = []Variant{}
|
||||
}
|
||||
return json.Marshal(attachmentJSON{
|
||||
a.ID, a.ChatID, a.OwnerID, a.Filename, a.ContentType, a.SizeBytes, a.SHA256,
|
||||
a.Status, variants, a.ErrorCode, formatTime(a.CreatedAt), formatTime(a.UpdatedAt),
|
||||
})
|
||||
}
|
||||
|
||||
func FormatTime(value time.Time) string {
|
||||
return formatTime(value)
|
||||
}
|
||||
|
||||
func formatTime(value time.Time) string {
|
||||
return value.UTC().Format(timestampLayout)
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/suite"
|
||||
)
|
||||
|
||||
type ModelSuite struct {
|
||||
suite.Suite
|
||||
}
|
||||
|
||||
func TestModelSuite(t *testing.T) {
|
||||
suite.Run(t, new(ModelSuite))
|
||||
}
|
||||
|
||||
func (s *ModelSuite) TestUserTimestampHasFixedMicroseconds() {
|
||||
createdAt := time.Date(2026, time.September, 23, 21, 28, 15, 423030000, time.UTC)
|
||||
data, err := json.Marshal(User{ID: "id", Username: "user", DisplayName: "User", CreatedAt: createdAt})
|
||||
s.Require().NoError(err)
|
||||
s.Equal(`{"id":"id","username":"user","display_name":"User","created_at":"2026-09-23T21:28:15.423030Z"}`, string(data))
|
||||
}
|
||||
Reference in New Issue
Block a user