feat(postgres):lab4 add linkAttachments helper
This commit is contained in:
@@ -240,4 +240,26 @@ func (r *MessageRepo) validateAttachments(ctx context.Context, tx pgx.Tx, chatID
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *MessageRepo) linkAttachments(ctx context.Context, tx pgx.Tx, messageID string, ids []string) error {
|
||||||
|
if len(ids) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
const q = `INSERT INTO message_attachments (message_id, attachment_id, position) VALUES ($1::uuid, $2::uuid, $3)`
|
||||||
|
for i, id := range ids {
|
||||||
|
if _, err := tx.Exec(ctx, q, messageID, id, int16(i)); err != nil {
|
||||||
|
if foreignKeyViolation(err) {
|
||||||
|
return &domain.NotFoundError{Entity: "attachment", ID: id}
|
||||||
|
}
|
||||||
|
if uniqueViolation(err) {
|
||||||
|
return &domain.ValidationError{Field: "attachment_ids", Message: "дубликаты запрещены"}
|
||||||
|
}
|
||||||
|
if checkViolation(err) {
|
||||||
|
return &domain.ValidationError{Field: "attachment_ids", Message: "нарушено ограничение"}
|
||||||
|
}
|
||||||
|
return mapError(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
var _ domain.MessageRepository = (*MessageRepo)(nil)
|
var _ domain.MessageRepository = (*MessageRepo)(nil)
|
||||||
|
|||||||
Reference in New Issue
Block a user