hiling_go/db/sqlc/locations.sql.go

152 lines
3.8 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 getListLocations = `-- name: GetListLocations :many
2023-10-02 08:49:41 +07:00
SELECT id, address, name, google_maps_link, location_type, submitted_by, total_visited, thumbnail, regency_id, is_deleted, created_at, updated_at, approved_by, approved_at FROM locations
2023-09-13 21:42:04 +07:00
`
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,
2023-10-02 08:49:41 +07:00
&i.LocationType,
2023-09-13 21:42:04 +07:00
&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,
2023-09-20 13:37:14 +07:00
COALESCE(re.regency_name, '') as regency_name,
COALESCE(pr.province_name, '') as province_name,
(SELECT COALESCE(SUM(score), 0) from reviews re where re.is_from_critic = true and re.location_id = l.id) as critic_score,
2023-09-14 22:51:01 +07:00
(SELECT COUNT(id) from reviews re where re.is_from_critic = true and re.location_id = l.id) as critic_count,
2023-09-20 13:37:14 +07:00
(SELECT COALESCE(SUM(score), 0) from reviews re where re.is_from_critic = false and re.location_id = l.id) as user_score,
2023-09-14 22:51:01 +07:00
(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
2023-09-20 13:37:14 +07:00
JOIN provinces pr on re.province_id = pr.id
2023-09-14 22:51:01 +07:00
WHERE approved_by IS NOT NULL
ORDER BY l.created_at ASC
LIMIT $1
`
type GetListRecentLocationsWithRatingsRow struct {
2023-09-20 13:37:14 +07:00
ID int32 `json:"id"`
Name string `json:"name"`
Thumbnail sql.NullString `json:"thumbnail"`
RegencyName string `json:"regency_name"`
ProvinceName string `json:"province_name"`
CriticScore interface{} `json:"critic_score"`
CriticCount int64 `json:"critic_count"`
UserScore interface{} `json:"user_score"`
UserCount int64 `json:"user_count"`
2023-09-14 22:51:01 +07:00
}
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,
2023-09-20 13:37:14 +07:00
&i.ProvinceName,
2023-09-14 22:51:01 +07:00
&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-20 13:37:14 +07:00
const getLocationTag = `-- name: GetLocationTag :many
SELECT
name
FROM tags
WHERE tags_type = 'location'
AND
target_id = $1
AND
approved_by IS NOT NULL
`
func (q *Queries) GetLocationTag(ctx context.Context, targetID int32) ([]string, error) {
rows, err := q.db.QueryContext(ctx, getLocationTag, targetID)
if err != nil {
return nil, err
}
defer rows.Close()
items := []string{}
for rows.Next() {
var name string
if err := rows.Scan(&name); err != nil {
return nil, err
}
items = append(items, name)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}