feat(postgres):lab4 add validateAttachments helper
This commit is contained in:
@@ -37,6 +37,7 @@ func (r *MessageRepo) Create(ctx context.Context, m *domain.Message) error {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *MessageRepo) CreateIdempotent(
|
func (r *MessageRepo) CreateIdempotent(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
scope domain.IdempotencyScope,
|
scope domain.IdempotencyScope,
|
||||||
@@ -200,4 +201,43 @@ func (r *MessageRepo) ListByChat(ctx context.Context, chatID string, limit int,
|
|||||||
return page, next, nil
|
return page, next, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *MessageRepo) validateAttachments(ctx context.Context, tx pgx.Tx, chatID string, ids []string) error {
|
||||||
|
if len(ids) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
const q = `SELECT id, chat_id, status FROM attachments WHERE id = ANY($1::uuid[])`
|
||||||
|
rows, err := tx.Query(ctx, q, ids)
|
||||||
|
if err != nil {
|
||||||
|
return mapError(err)
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
type info struct {
|
||||||
|
chatID string
|
||||||
|
status domain.AttachmentStatus
|
||||||
|
}
|
||||||
|
found := make(map[string]info, len(ids))
|
||||||
|
for rows.Next() {
|
||||||
|
var id, chID, st string
|
||||||
|
if err := rows.Scan(&id, &chID, &st); err != nil {
|
||||||
|
return mapError(err)
|
||||||
|
}
|
||||||
|
found[id] = info{chatID: chID, status: domain.AttachmentStatus(st)}
|
||||||
|
}
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
return mapError(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, id := range ids {
|
||||||
|
a, ok := found[id]
|
||||||
|
if !ok || a.chatID != chatID {
|
||||||
|
return &domain.NotFoundError{Entity: "attachment", ID: id}
|
||||||
|
}
|
||||||
|
if a.status != domain.AttachmentStatusReady {
|
||||||
|
return &domain.ConflictError{Reason: "вложение " + id + " не готово"}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
var _ domain.MessageRepository = (*MessageRepo)(nil)
|
var _ domain.MessageRepository = (*MessageRepo)(nil)
|
||||||
|
|||||||
Reference in New Issue
Block a user