forked from mtusi2026/mtusi2026lab4
feat: implement lab4 persistent attachments and replicated API
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"mtusi2026lab2/internal/model"
|
||||
)
|
||||
|
||||
type AttachmentStore struct{ Pool *pgxpool.Pool }
|
||||
|
||||
func NewAttachmentStore(p *pgxpool.Pool) *AttachmentStore { return &AttachmentStore{p} }
|
||||
func (s *AttachmentStore) Create(ctx context.Context, a model.Attachment) error {
|
||||
_, err := s.Pool.Exec(ctx, `INSERT INTO attachments(id,chat_id,owner_id,filename,content_type,size_bytes,sha256,object_key,created_at,updated_at) VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10)`, a.ID, a.ChatID, a.OwnerID, a.Filename, a.ContentType, a.SizeBytes, a.SHA256, a.ObjectKey, a.CreatedAt, a.UpdatedAt)
|
||||
return err
|
||||
}
|
||||
func (s *AttachmentStore) GetForUser(ctx context.Context, id, user uuid.UUID) (model.Attachment, error) {
|
||||
var a model.Attachment
|
||||
err := s.Pool.QueryRow(ctx, `SELECT a.id,a.chat_id,a.owner_id,a.filename,a.content_type,a.size_bytes,a.sha256,a.object_key,a.created_at,a.updated_at FROM attachments a JOIN chat_members cm ON cm.chat_id=a.chat_id WHERE a.id=$1 AND cm.user_id=$2`, id, user).Scan(&a.ID, &a.ChatID, &a.OwnerID, &a.Filename, &a.ContentType, &a.SizeBytes, &a.SHA256, &a.ObjectKey, &a.CreatedAt, &a.UpdatedAt)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return a, ErrAttachmentNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return a, err
|
||||
}
|
||||
a.Status = "ready"
|
||||
a.Variants = []any{}
|
||||
return a, nil
|
||||
}
|
||||
Reference in New Issue
Block a user