Skip to content

Commit 88c529d

Browse files
committed
LLM Prompt generate repo methods
1 parent 0d5189c commit 88c529d

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package database
2+
3+
import (
4+
"context"
5+
"gateway-api/internal/pkg/database/schema"
6+
sq "github.com/Masterminds/squirrel"
7+
"github.com/georgysavva/scany/v2/pgxscan"
8+
"github.com/pkg/errors"
9+
"time"
10+
)
11+
12+
// GetRecordsByIDs получает записи из указанной таблицы по списку ID.
13+
// Возвращает слайс ссылок на model.Specification и ошибку, если она возникла.
14+
func (r *SpecRepository) GetRecordsByIDs(ctx context.Context, tableName string, ids []int64) ([]*schema.Specification, error) {
15+
// 1. Построение SQL-запроса с использованием Squirrel
16+
query := psql.
17+
Select("id", "status").
18+
From(tableName).
19+
Where(sq.Eq{"id": ids})
20+
21+
// 2. Преобразование запроса в SQL и получение аргументов
22+
rawSQL, args, err := query.ToSql()
23+
if err != nil {
24+
return nil, errors.Wrap(err, "GetRecordsByIDs.ToSql")
25+
}
26+
27+
// 3. Выполнение запроса и сканирование результатов
28+
var specifications []*schema.Specification
29+
err = pgxscan.Select(ctx, r.pool, &specifications, rawSQL, args...)
30+
if err != nil {
31+
return nil, errors.Wrap(err, "GetRecordsByIDs.Select")
32+
}
33+
34+
return specifications, nil
35+
}
36+
37+
// UpdateRecordStatus обновляет статус записей в указанной таблице по списку ID.
38+
// Возвращает ошибку, если операция не удалась.
39+
func (r *SpecRepository) UpdateRecordStatus(ctx context.Context, tableName string, ids []int64, newStatus string) error {
40+
// 1. Построение UPDATE-запроса с использованием Squirrel
41+
query := psql.
42+
Update(tableName).
43+
Set("status", newStatus).
44+
Set("updated_at", time.Now()).
45+
Where(sq.Eq{"id": ids})
46+
47+
// 2. Преобразование запроса в SQL и получение аргументов
48+
rawSQL, args, err := query.ToSql()
49+
if err != nil {
50+
return errors.Wrap(err, "UpdateRecordStatus.ToSql")
51+
}
52+
53+
// 3. Выполнение запроса
54+
cmdTag, err := r.pool.Exec(ctx, rawSQL, args...)
55+
if err != nil {
56+
return errors.Wrap(err, "UpdateRecordStatus.Exec")
57+
}
58+
59+
// 4. Проверка, что хотя бы одна запись была обновлена
60+
if cmdTag.RowsAffected() == 0 {
61+
return errors.New("записи не найдены или статус не изменён")
62+
}
63+
64+
return nil
65+
}

0 commit comments

Comments
 (0)