forked from mtusi2026/mtusi2026lab4
feat(db): add attachment content type and status enums
This commit is contained in:
@@ -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