hiling_go/db/sqlc/images.go
2023-09-19 21:49:48 +07:00

62 lines
1.2 KiB
Go

package db
import (
"context"
"time"
)
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) {
rows, err := q.db.QueryContext(ctx, getImagesByLocationQ, arg.LocationId, arg.Limit, arg.Offset)
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)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}