hiling_go/db/sqlc/locations.sql.go

192 lines
4.9 KiB
Go
Raw Normal View History

2023-09-13 21:42:04 +07:00
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.20.0
// source: locations.sql
package db
import (
"context"
"database/sql"
)
const createLocation = `-- name: CreateLocation :exec
INSERT INTO locations(
address,
name,
submitted_by,
regency_id,
google_maps_link
) values (
$1, $2, $3, $4, $5
)
`
type CreateLocationParams struct {
Address string `json:"address"`
Name string `json:"name"`
SubmittedBy int32 `json:"submitted_by"`
RegencyID int16 `json:"regency_id"`
GoogleMapsLink sql.NullString `json:"google_maps_link"`
}
func (q *Queries) CreateLocation(ctx context.Context, arg CreateLocationParams) error {
_, err := q.db.ExecContext(ctx, createLocation,
arg.Address,
arg.Name,
arg.SubmittedBy,
arg.RegencyID,
arg.GoogleMapsLink,
)
return err
}
const getListLocations = `-- name: GetListLocations :many
SELECT id, address, name, google_maps_link, submitted_by, total_visited, thumbnail, regency_id, is_deleted, created_at, updated_at, approved_by, approved_at FROM locations
`
2023-09-14 13:49:03 +07:00
func (q *Queries) GetListLocations(ctx context.Context) ([]Location, error) {
rows, err := q.db.QueryContext(ctx, getListLocations)
2023-09-13 21:42:04 +07:00
if err != nil {
return nil, err
}
defer rows.Close()
items := []Location{}
for rows.Next() {
var i Location
if err := rows.Scan(
&i.ID,
&i.Address,
&i.Name,
&i.GoogleMapsLink,
&i.SubmittedBy,
&i.TotalVisited,
&i.Thumbnail,
&i.RegencyID,
&i.IsDeleted,
&i.CreatedAt,
&i.UpdatedAt,
&i.ApprovedBy,
&i.ApprovedAt,
); 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
}
2023-09-14 22:51:01 +07:00
const getListRecentLocationsWithRatings = `-- name: GetListRecentLocationsWithRatings :many
SELECT
l.id,
name,
thumbnail,
re.regency_name,
(SELECT COALESCE(SUM(score), -1) from reviews re where re.is_from_critic = true and re.location_id = l.id) as critic_score,
(SELECT COUNT(id) from reviews re where re.is_from_critic = true and re.location_id = l.id) as critic_count,
(SELECT COALESCE(SUM(score), -1) from reviews re where re.is_from_critic = false and re.location_id = l.id) as user_score,
(SELECT COUNT(id) from reviews re where re.is_from_critic = false and re.location_id = l.id) as user_count
FROM locations l
JOIN regencies re on re.id = l.regency_id
WHERE approved_by IS NOT NULL
ORDER BY l.created_at ASC
LIMIT $1
`
type GetListRecentLocationsWithRatingsRow struct {
ID int32 `json:"id"`
Name string `json:"name"`
Thumbnail sql.NullString `json:"thumbnail"`
RegencyName sql.NullString `json:"regency_name"`
CriticScore interface{} `json:"critic_score"`
CriticCount int64 `json:"critic_count"`
UserScore interface{} `json:"user_score"`
UserCount int64 `json:"user_count"`
}
func (q *Queries) GetListRecentLocationsWithRatings(ctx context.Context, limit int32) ([]GetListRecentLocationsWithRatingsRow, error) {
rows, err := q.db.QueryContext(ctx, getListRecentLocationsWithRatings, limit)
if err != nil {
return nil, err
}
defer rows.Close()
items := []GetListRecentLocationsWithRatingsRow{}
for rows.Next() {
var i GetListRecentLocationsWithRatingsRow
if err := rows.Scan(
&i.ID,
&i.Name,
&i.Thumbnail,
&i.RegencyName,
&i.CriticScore,
&i.CriticCount,
&i.UserScore,
&i.UserCount,
); 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
}
2023-09-13 21:42:04 +07:00
const getLocation = `-- name: GetLocation :one
2023-09-19 21:50:33 +07:00
SELECT
l.id,
l.name,
l.address,
l.thumbnail,
l.submitted_by,
2023-09-20 11:20:06 +07:00
COALESCE(r.regency_name, '') as regency_name,
COALESCE(p.province_name, '') as province_name,
COALESCE(r2.region_name, '') as region_name,
2023-09-19 21:50:33 +07:00
u.username as submitted_by_user
FROM locations l
JOIN regencies r on r.id = l.regency_id
JOIN provinces p on p.id = r.province_id
JOIN regions r2 on r2.id = p.region_id
JOIN users u on l.approved_by = u.id
WHERE l.id = $1
2023-09-13 21:42:04 +07:00
`
2023-09-19 21:50:33 +07:00
type GetLocationRow struct {
ID int32 `json:"id"`
Name string `json:"name"`
Address string `json:"address"`
Thumbnail sql.NullString `json:"thumbnail"`
SubmittedBy int32 `json:"submitted_by"`
2023-09-20 11:20:06 +07:00
RegencyName string `json:"regency_name"`
ProvinceName string `json:"province_name"`
RegionName string `json:"region_name"`
2023-09-19 21:50:33 +07:00
SubmittedByUser string `json:"submitted_by_user"`
}
func (q *Queries) GetLocation(ctx context.Context, id int32) (GetLocationRow, error) {
2023-09-13 21:42:04 +07:00
row := q.db.QueryRowContext(ctx, getLocation, id)
2023-09-19 21:50:33 +07:00
var i GetLocationRow
2023-09-13 21:42:04 +07:00
err := row.Scan(
&i.ID,
&i.Name,
2023-09-19 21:50:33 +07:00
&i.Address,
2023-09-13 21:42:04 +07:00
&i.Thumbnail,
2023-09-19 21:50:33 +07:00
&i.SubmittedBy,
&i.RegencyName,
&i.ProvinceName,
&i.RegionName,
&i.SubmittedByUser,
2023-09-13 21:42:04 +07:00
)
return i, err
}