add get location by detail

This commit is contained in:
nochill 2023-09-19 21:50:33 +07:00
parent d05a9671ee
commit de860d9173
3 changed files with 53 additions and 69 deletions

View File

@ -17,34 +17,23 @@ WHERE approved_by IS NOT NULL
ORDER BY l.created_at ASC ORDER BY l.created_at ASC
LIMIT $1; LIMIT $1;
-- https://fulmicoton.com/posts/bayesian_rating/
-- SELECT
-- *,
-- (SELECT 5 * 4 + coalesce(critic_score, 0) * coalesce(critic_count, 0) / 5 + coalesce(critic_count, 0)) as critic_bayes,
-- (SELECT 50 + coalesce(user_score, 0) * coalesce(user_count, 0) / 50 + coalesce(user_count, 0)) as user_bayes,
-- ((SELECT 50 + coalesce(user_score, 0) * coalesce(user_count, 0) / 50 + coalesce(user_count, 0)) + (SELECT 5 * 4 + coalesce(critic_score, 0) * coalesce(critic_count, 0) / 5 + coalesce(critic_count, 0)) ) / 2 as avg_bayes
-- FROM (
-- SELECT
-- l.id,
-- name,
-- thumbnail,
-- re.regency_name,
-- (SELECT SUM(score) 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 SUM(score) 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) iq1
-- ORDER BY avg_bayes DESC
-- LIMIT $1
-- OFFSET $2;
-- name: GetLocation :one -- name: GetLocation :one
SELECT * FROM locations SELECT
WHERE id = $1; l.id,
l.name,
l.address,
l.thumbnail,
l.submitted_by,
r.regency_name,
p.province_name,
r2.region_name,
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;
-- name: CreateLocation :exec -- name: CreateLocation :exec
INSERT INTO locations( INSERT INTO locations(

View File

@ -39,6 +39,7 @@ func (q *Queries) GetTopListLocations(ctx context.Context, arg GetTopListLocatio
regionType = fmt.Sprintf("AND reg.id = %d", arg.RegionType.Int16) regionType = fmt.Sprintf("AND reg.id = %d", arg.RegionType.Int16)
} }
// https://fulmicoton.com/posts/bayesian_rating/
getTopListQ := fmt.Sprintf(`SELECT getTopListQ := fmt.Sprintf(`SELECT
row_number() over (ORDER BY %s DESC) as row_number, row_number() over (ORDER BY %s DESC) as row_number,
* *

View File

@ -143,55 +143,49 @@ func (q *Queries) GetListRecentLocationsWithRatings(ctx context.Context, limit i
} }
const getLocation = `-- name: GetLocation :one const getLocation = `-- name: GetLocation :one
SELECT
l.id,
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 l.name,
WHERE id = $1 l.address,
l.thumbnail,
l.submitted_by,
r.regency_name,
p.province_name,
r2.region_name,
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
` `
// https://fulmicoton.com/posts/bayesian_rating/ type GetLocationRow struct {
// SELECT ID int32 `json:"id"`
// Name string `json:"name"`
// *, Address string `json:"address"`
// (SELECT 5 * 4 + coalesce(critic_score, 0) * coalesce(critic_count, 0) / 5 + coalesce(critic_count, 0)) as critic_bayes, Thumbnail sql.NullString `json:"thumbnail"`
// (SELECT 50 + coalesce(user_score, 0) * coalesce(user_count, 0) / 50 + coalesce(user_count, 0)) as user_bayes, SubmittedBy int32 `json:"submitted_by"`
// ((SELECT 50 + coalesce(user_score, 0) * coalesce(user_count, 0) / 50 + coalesce(user_count, 0)) + (SELECT 5 * 4 + coalesce(critic_score, 0) * coalesce(critic_count, 0) / 5 + coalesce(critic_count, 0)) ) / 2 as avg_bayes RegencyName sql.NullString `json:"regency_name"`
// ProvinceName sql.NullString `json:"province_name"`
// FROM ( RegionName sql.NullString `json:"region_name"`
// SubmittedByUser string `json:"submitted_by_user"`
// SELECT }
// l.id,
// name, func (q *Queries) GetLocation(ctx context.Context, id int32) (GetLocationRow, error) {
// thumbnail,
// re.regency_name,
// (SELECT SUM(score) 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 SUM(score) 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) iq1
//
// ORDER BY avg_bayes DESC
// LIMIT $1
// OFFSET $2;
func (q *Queries) GetLocation(ctx context.Context, id int32) (Location, error) {
row := q.db.QueryRowContext(ctx, getLocation, id) row := q.db.QueryRowContext(ctx, getLocation, id)
var i Location var i GetLocationRow
err := row.Scan( err := row.Scan(
&i.ID, &i.ID,
&i.Address,
&i.Name, &i.Name,
&i.GoogleMapsLink, &i.Address,
&i.SubmittedBy,
&i.TotalVisited,
&i.Thumbnail, &i.Thumbnail,
&i.RegencyID, &i.SubmittedBy,
&i.IsDeleted, &i.RegencyName,
&i.CreatedAt, &i.ProvinceName,
&i.UpdatedAt, &i.RegionName,
&i.ApprovedBy, &i.SubmittedByUser,
&i.ApprovedAt,
) )
return i, err return i, err
} }