2021-09-01 18:40:13 +03:00
|
|
|
package repositories
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
Db "micrach/db"
|
2021-09-01 23:00:37 +03:00
|
|
|
|
|
|
|
"github.com/jackc/pgtype"
|
2021-09-03 02:34:45 +03:00
|
|
|
"github.com/jackc/pgx/v4"
|
2021-09-01 18:40:13 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type FilesRepository struct{}
|
|
|
|
|
|
|
|
var Files FilesRepository
|
|
|
|
|
|
|
|
func (r *FilesRepository) Create(f File) error {
|
|
|
|
conn, err := Db.Pool.Acquire(context.TODO())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer conn.Release()
|
|
|
|
|
|
|
|
sql := `
|
|
|
|
INSERT INTO files (post_id, name, ext, size)
|
|
|
|
VALUES ($1, $2, $3, $4)
|
|
|
|
`
|
|
|
|
|
2021-09-03 02:34:45 +03:00
|
|
|
row := conn.QueryRow(
|
2021-09-01 18:40:13 +03:00
|
|
|
context.TODO(), sql, f.PostID, f.Name, f.Ext, f.Size,
|
|
|
|
)
|
|
|
|
|
2021-09-03 02:34:45 +03:00
|
|
|
err = row.Scan()
|
|
|
|
if err != nil && err != pgx.ErrNoRows {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-01 18:40:13 +03:00
|
|
|
return nil
|
|
|
|
}
|
2021-09-01 23:00:37 +03:00
|
|
|
|
|
|
|
func (r *FilesRepository) GetByPostIDs(postIDs []int) (map[int][]File, error) {
|
|
|
|
conn, err := Db.Pool.Acquire(context.TODO())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer conn.Release()
|
|
|
|
|
|
|
|
sql := `
|
|
|
|
SELECT post_id, name, size
|
|
|
|
FROM files
|
|
|
|
WHERE post_id = ANY ($1)
|
|
|
|
ORDER BY id ASC
|
|
|
|
`
|
|
|
|
|
|
|
|
ids := &pgtype.Int4Array{}
|
|
|
|
ids.Set(postIDs)
|
|
|
|
|
|
|
|
rows, err := conn.Query(context.TODO(), sql, ids)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if rows.Err() != nil {
|
|
|
|
return nil, rows.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
filesMapByPostId := make(map[int][]File)
|
|
|
|
for rows.Next() {
|
|
|
|
var file File
|
|
|
|
err = rows.Scan(&file.PostID, &file.Name, &file.Size)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if filesMapByPostId[file.PostID] == nil {
|
|
|
|
filesMapByPostId[file.PostID] = []File{}
|
|
|
|
}
|
|
|
|
|
|
|
|
filesMapByPostId[file.PostID] = append(
|
|
|
|
filesMapByPostId[file.PostID],
|
|
|
|
file,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return filesMapByPostId, nil
|
|
|
|
}
|