forked from mtusi2026/mtusi2026lab4
feat(db): add attachment content type and status enums
This commit is contained in:
@@ -1,14 +1,17 @@
|
||||
-- +goose Up
|
||||
CREATE TYPE attachment_content_type AS ENUM ('image/png', 'image/jpeg');
|
||||
CREATE TYPE attachment_status AS ENUM ('queued', 'processing', 'ready', 'failed');
|
||||
|
||||
CREATE TABLE attachments (
|
||||
id uuid PRIMARY KEY,
|
||||
chat_id uuid NOT NULL REFERENCES chats (id) ON DELETE CASCADE,
|
||||
owner_id uuid NOT NULL REFERENCES users (id) ON DELETE RESTRICT,
|
||||
s3_key text NOT NULL UNIQUE,
|
||||
filename text NOT NULL,
|
||||
content_type text NOT NULL CHECK (content_type IN ('image/png', 'image/jpeg')),
|
||||
content_type attachment_content_type NOT NULL,
|
||||
size_bytes int NOT NULL CHECK (size_bytes > 0 AND size_bytes <= 10485760),
|
||||
sha256 text NOT NULL CHECK (sha256 ~ '^[a-f0-9]{64}$'),
|
||||
status text NOT NULL DEFAULT 'ready' CHECK (status = 'ready'),
|
||||
status attachment_status NOT NULL DEFAULT 'ready',
|
||||
error_code text,
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
updated_at timestamptz NOT NULL DEFAULT now()
|
||||
@@ -30,3 +33,5 @@ CREATE INDEX idx_message_attachments_attachment_id ON message_attachments (attac
|
||||
-- +goose Down
|
||||
DROP TABLE message_attachments;
|
||||
DROP TABLE attachments;
|
||||
DROP TYPE attachment_status;
|
||||
DROP TYPE attachment_content_type;
|
||||
|
||||
@@ -17,10 +17,10 @@ SELECT
|
||||
sqlc.arg(owner_id)::uuid,
|
||||
sqlc.arg(s3_key)::text,
|
||||
sqlc.arg(filename)::text,
|
||||
sqlc.arg(content_type)::text,
|
||||
sqlc.arg(content_type)::attachment_content_type,
|
||||
sqlc.arg(size_bytes)::int,
|
||||
sqlc.arg(sha256)::text,
|
||||
sqlc.arg(status)::text,
|
||||
sqlc.arg(status)::attachment_status,
|
||||
sqlc.arg(error_code)::text
|
||||
WHERE EXISTS (
|
||||
SELECT 1 FROM chat_members
|
||||
|
||||
@@ -30,10 +30,10 @@ SELECT
|
||||
$3::uuid,
|
||||
$4::text,
|
||||
$5::text,
|
||||
$6::text,
|
||||
$6::attachment_content_type,
|
||||
$7::int,
|
||||
$8::text,
|
||||
$9::text,
|
||||
$9::attachment_status,
|
||||
$10::text
|
||||
WHERE EXISTS (
|
||||
SELECT 1 FROM chat_members
|
||||
@@ -49,10 +49,10 @@ type CreateAttachmentParams struct {
|
||||
OwnerID uuid.UUID
|
||||
S3Key string
|
||||
Filename string
|
||||
ContentType string
|
||||
ContentType AttachmentContentType
|
||||
SizeBytes int
|
||||
Sha256 string
|
||||
Status string
|
||||
Status AttachmentStatus
|
||||
ErrorCode string
|
||||
}
|
||||
|
||||
@@ -147,15 +147,15 @@ type ListAttachmentsByIDsParams struct {
|
||||
Ids []uuid.UUID
|
||||
}
|
||||
|
||||
func (q *Queries) ListAttachmentsByIDs(ctx context.Context, arg ListAttachmentsByIDsParams) ([]string, error) {
|
||||
func (q *Queries) ListAttachmentsByIDs(ctx context.Context, arg ListAttachmentsByIDsParams) ([]AttachmentStatus, error) {
|
||||
rows, err := q.db.Query(ctx, listAttachmentsByIDs, arg.ChatID, arg.Ids)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []string{}
|
||||
items := []AttachmentStatus{}
|
||||
for rows.Next() {
|
||||
var status string
|
||||
var status AttachmentStatus
|
||||
if err := rows.Scan(&status); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
+90
-2
@@ -5,21 +5,109 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type AttachmentContentType string
|
||||
|
||||
const (
|
||||
AttachmentContentTypeImagePng AttachmentContentType = "image/png"
|
||||
AttachmentContentTypeImageJpeg AttachmentContentType = "image/jpeg"
|
||||
)
|
||||
|
||||
func (e *AttachmentContentType) Scan(src interface{}) error {
|
||||
switch s := src.(type) {
|
||||
case []byte:
|
||||
*e = AttachmentContentType(s)
|
||||
case string:
|
||||
*e = AttachmentContentType(s)
|
||||
default:
|
||||
return fmt.Errorf("unsupported scan type for AttachmentContentType: %T", src)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type NullAttachmentContentType struct {
|
||||
AttachmentContentType AttachmentContentType
|
||||
Valid bool // Valid is true if AttachmentContentType is not NULL
|
||||
}
|
||||
|
||||
// Scan implements the Scanner interface.
|
||||
func (ns *NullAttachmentContentType) Scan(value interface{}) error {
|
||||
if value == nil {
|
||||
ns.AttachmentContentType, ns.Valid = "", false
|
||||
return nil
|
||||
}
|
||||
ns.Valid = true
|
||||
return ns.AttachmentContentType.Scan(value)
|
||||
}
|
||||
|
||||
// Value implements the driver Valuer interface.
|
||||
func (ns NullAttachmentContentType) Value() (driver.Value, error) {
|
||||
if !ns.Valid {
|
||||
return nil, nil
|
||||
}
|
||||
return string(ns.AttachmentContentType), nil
|
||||
}
|
||||
|
||||
type AttachmentStatus string
|
||||
|
||||
const (
|
||||
AttachmentStatusQueued AttachmentStatus = "queued"
|
||||
AttachmentStatusProcessing AttachmentStatus = "processing"
|
||||
AttachmentStatusReady AttachmentStatus = "ready"
|
||||
AttachmentStatusFailed AttachmentStatus = "failed"
|
||||
)
|
||||
|
||||
func (e *AttachmentStatus) Scan(src interface{}) error {
|
||||
switch s := src.(type) {
|
||||
case []byte:
|
||||
*e = AttachmentStatus(s)
|
||||
case string:
|
||||
*e = AttachmentStatus(s)
|
||||
default:
|
||||
return fmt.Errorf("unsupported scan type for AttachmentStatus: %T", src)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type NullAttachmentStatus struct {
|
||||
AttachmentStatus AttachmentStatus
|
||||
Valid bool // Valid is true if AttachmentStatus is not NULL
|
||||
}
|
||||
|
||||
// Scan implements the Scanner interface.
|
||||
func (ns *NullAttachmentStatus) Scan(value interface{}) error {
|
||||
if value == nil {
|
||||
ns.AttachmentStatus, ns.Valid = "", false
|
||||
return nil
|
||||
}
|
||||
ns.Valid = true
|
||||
return ns.AttachmentStatus.Scan(value)
|
||||
}
|
||||
|
||||
// Value implements the driver Valuer interface.
|
||||
func (ns NullAttachmentStatus) Value() (driver.Value, error) {
|
||||
if !ns.Valid {
|
||||
return nil, nil
|
||||
}
|
||||
return string(ns.AttachmentStatus), nil
|
||||
}
|
||||
|
||||
type Attachment struct {
|
||||
ID uuid.UUID
|
||||
ChatID uuid.UUID
|
||||
OwnerID uuid.UUID
|
||||
S3Key string
|
||||
Filename string
|
||||
ContentType string
|
||||
ContentType AttachmentContentType
|
||||
SizeBytes int
|
||||
Sha256 string
|
||||
Status string
|
||||
Status AttachmentStatus
|
||||
ErrorCode *string
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
|
||||
@@ -31,7 +31,7 @@ type messageQueries interface {
|
||||
CreateMessageAttachment(ctx context.Context, arg db.CreateMessageAttachmentParams) error
|
||||
GetIdempotentMessage(ctx context.Context, arg db.GetIdempotentMessageParams) (db.GetIdempotentMessageRow, error)
|
||||
IsChatMember(ctx context.Context, arg db.IsChatMemberParams) (bool, error)
|
||||
ListAttachmentsByIDs(ctx context.Context, arg db.ListAttachmentsByIDsParams) ([]string, error)
|
||||
ListAttachmentsByIDs(ctx context.Context, arg db.ListAttachmentsByIDsParams) ([]db.AttachmentStatus, error)
|
||||
ListMessageAttachmentIDs(ctx context.Context, messageID uuid.UUID) ([]uuid.UUID, error)
|
||||
ListMessageAttachmentsByMessageIDs(ctx context.Context, messageIDs []uuid.UUID) ([]db.ListMessageAttachmentsByMessageIDsRow, error)
|
||||
ListMessagesAfter(ctx context.Context, arg db.ListMessagesAfterParams) ([]db.Message, error)
|
||||
@@ -79,7 +79,7 @@ func (s *MessageService) Create(
|
||||
return model.Message{}, ErrAttachmentNotFound
|
||||
}
|
||||
for _, status := range statuses {
|
||||
if status != "ready" {
|
||||
if status != db.AttachmentStatusReady {
|
||||
return model.Message{}, ErrAttachmentNotReady
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user