2023-09-19 21:49:48 +07:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-10-03 14:56:57 +07:00
|
|
|
"strings"
|
2023-09-19 21:49:48 +07:00
|
|
|
"time"
|
2023-10-03 14:56:57 +07:00
|
|
|
|
|
|
|
"git.nochill.in/nochill/hiling_go/util"
|
2023-09-19 21:49:48 +07:00
|
|
|
)
|
|
|
|
|
|
|
|
type GetImagesByLocationParams struct {
|
|
|
|
Limit int32
|
|
|
|
Offset int32
|
|
|
|
LocationId int32
|
|
|
|
}
|
|
|
|
|
|
|
|
type GetImagesByLocationRow struct {
|
|
|
|
ID int32 `json:"id"`
|
|
|
|
Src string `json:"src"`
|
|
|
|
CreatedAt time.Time `json:"created_at"`
|
|
|
|
UploadedBy string `json:"uploaded_by"`
|
|
|
|
}
|
|
|
|
|
|
|
|
const getImagesByLocationQ = `
|
|
|
|
SELECT
|
|
|
|
i.id,
|
|
|
|
i.image_url as src,
|
|
|
|
i.created_at,
|
|
|
|
u.username as uploaded_by
|
|
|
|
FROM images i
|
|
|
|
JOIN users u on i.uploaded_by = u.id
|
|
|
|
WHERE i.image_type = 'locations' AND image_of = $1
|
|
|
|
LIMIT $2
|
|
|
|
OFFSET $3
|
|
|
|
`
|
|
|
|
|
|
|
|
func (q *Queries) GetImagesByLocation(ctx context.Context, arg GetImagesByLocationParams) ([]GetImagesByLocationRow, error) {
|
2024-02-06 11:55:25 +07:00
|
|
|
rows, err := q.db.Query(ctx, getImagesByLocationQ, arg.LocationId, arg.Limit, arg.Offset)
|
2023-09-19 21:49:48 +07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
items := []GetImagesByLocationRow{}
|
|
|
|
for rows.Next() {
|
|
|
|
var i GetImagesByLocationRow
|
|
|
|
if err := rows.Scan(
|
|
|
|
&i.ID,
|
|
|
|
&i.Src,
|
|
|
|
&i.CreatedAt,
|
|
|
|
&i.UploadedBy,
|
|
|
|
); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
items = append(items, i)
|
|
|
|
}
|
2024-02-06 11:55:25 +07:00
|
|
|
rows.Close()
|
2023-09-19 21:49:48 +07:00
|
|
|
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return items, nil
|
|
|
|
}
|
2023-10-03 14:56:57 +07:00
|
|
|
|
|
|
|
type CreateImageParams struct {
|
|
|
|
ImageUrl string `json:"url"`
|
|
|
|
UploadedBy int32 `json:"uploaded_by"`
|
|
|
|
ImageType string `json:"image_type"`
|
|
|
|
ImageOf int32 `json:"image_of"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) CreateImage(ctx context.Context, arg []CreateImageParams) error {
|
|
|
|
|
|
|
|
values := []interface{}{}
|
|
|
|
|
|
|
|
queryStr := ` INSERT INTO
|
|
|
|
images(image_url, uploaded_by, image_type, image_of)
|
|
|
|
VALUES
|
|
|
|
`
|
|
|
|
|
|
|
|
for _, row := range arg {
|
|
|
|
queryStr += "(?, ?, ?, ?),"
|
|
|
|
values = append(values, row.ImageUrl, row.UploadedBy, row.ImageType, row.ImageOf)
|
|
|
|
}
|
|
|
|
|
|
|
|
queryStr = strings.TrimSuffix(queryStr, ",")
|
|
|
|
|
|
|
|
// Replacing ? with $n for postgres
|
|
|
|
queryStr = util.ReplaceSQL(queryStr, "?")
|
|
|
|
|
|
|
|
// prepare the statement
|
2024-02-06 11:55:25 +07:00
|
|
|
_, err := q.db.Exec(ctx, queryStr)
|
2023-10-03 14:56:57 +07:00
|
|
|
|
|
|
|
// format all vals at once
|
2024-02-06 11:55:25 +07:00
|
|
|
// _, err := stmt.Ex(ctx, values...)
|
2023-10-03 14:56:57 +07:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|