ЛР 2: PostgreSQL, сессии в Valkey, кэш истории (трек 1)

This commit is contained in:
Нурлыгаянова Екатерина
2026-10-29 11:29:38 +03:00
parent 65003b6119
commit 1503b5db81
132 changed files with 15114 additions and 20 deletions
+10
View File
@@ -0,0 +1,10 @@
package domain
import "time"
type Chat struct {
ID string
Title string
MemberIDs []string
CreatedAt time.Time
}
+19
View File
@@ -0,0 +1,19 @@
package domain
import "errors"
var (
ErrNotFound = errors.New("not found")
ErrUnauthorized = errors.New("unauthorized")
ErrConflict = errors.New("conflict")
ErrInvalidArgument = errors.New("invalid argument")
ErrUnavailable = errors.New("unavailable")
ErrPayloadTooLarge = errors.New("payload too large")
ErrRateLimited = errors.New("rate limited")
)
+11
View File
@@ -0,0 +1,11 @@
package domain
import "time"
type Message struct {
ID string
ChatID string
AuthorID string
Text string
CreatedAt time.Time
}
+55
View File
@@ -0,0 +1,55 @@
package domain
import (
"encoding/base64"
"encoding/json"
"time"
)
type Page[T any] struct {
Items []T
NextCursor string
}
type Cursor struct {
CreatedAt time.Time `json:"t"`
ID string `json:"i"`
Scope string `json:"s,omitempty"`
}
func (c Cursor) IsZero() bool {
return c.ID == "" && c.CreatedAt.IsZero()
}
func (c Cursor) Less(createdAt time.Time, id string) bool {
if c.CreatedAt.Before(createdAt) {
return true
}
if c.CreatedAt.After(createdAt) {
return false
}
return c.ID < id
}
func EncodeCursor(c Cursor) string {
if c.IsZero() {
return ""
}
b, _ := json.Marshal(c)
return base64.RawURLEncoding.EncodeToString(b)
}
func DecodeCursor(s string) (Cursor, error) {
if s == "" {
return Cursor{}, nil
}
raw, err := base64.RawURLEncoding.DecodeString(s)
if err != nil {
return Cursor{}, ErrInvalidArgument
}
var c Cursor
if err := json.Unmarshal(raw, &c); err != nil {
return Cursor{}, ErrInvalidArgument
}
return c, nil
}
+9
View File
@@ -0,0 +1,9 @@
package domain
import "time"
type Session struct {
Token string
ExpiresAt time.Time
User User
}
+10
View File
@@ -0,0 +1,10 @@
package domain
import "time"
type User struct {
ID string
Username string
DisplayName string
CreatedAt time.Time
}