diff --git a/db/queries/locations.sql b/db/queries/locations.sql index 5057bab..1a12245 100644 --- a/db/queries/locations.sql +++ b/db/queries/locations.sql @@ -17,34 +17,23 @@ WHERE approved_by IS NOT NULL ORDER BY l.created_at ASC 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 -SELECT * FROM locations -WHERE id = $1; +SELECT + 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 INSERT INTO locations( diff --git a/db/sqlc/locations.go b/db/sqlc/locations.go index f96d1e4..813bab7 100644 --- a/db/sqlc/locations.go +++ b/db/sqlc/locations.go @@ -39,6 +39,7 @@ func (q *Queries) GetTopListLocations(ctx context.Context, arg GetTopListLocatio regionType = fmt.Sprintf("AND reg.id = %d", arg.RegionType.Int16) } + // https://fulmicoton.com/posts/bayesian_rating/ getTopListQ := fmt.Sprintf(`SELECT row_number() over (ORDER BY %s DESC) as row_number, * diff --git a/db/sqlc/locations.sql.go b/db/sqlc/locations.sql.go index 0f29ad6..6699514 100644 --- a/db/sqlc/locations.sql.go +++ b/db/sqlc/locations.sql.go @@ -143,55 +143,49 @@ func (q *Queries) GetListRecentLocationsWithRatings(ctx context.Context, limit i } const getLocation = `-- name: GetLocation :one - - -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 -WHERE id = $1 +SELECT + 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 ` -// 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; -func (q *Queries) GetLocation(ctx context.Context, id int32) (Location, error) { +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"` + RegencyName sql.NullString `json:"regency_name"` + ProvinceName sql.NullString `json:"province_name"` + RegionName sql.NullString `json:"region_name"` + SubmittedByUser string `json:"submitted_by_user"` +} + +func (q *Queries) GetLocation(ctx context.Context, id int32) (GetLocationRow, error) { row := q.db.QueryRowContext(ctx, getLocation, id) - var i Location + var i GetLocationRow err := row.Scan( &i.ID, - &i.Address, &i.Name, - &i.GoogleMapsLink, - &i.SubmittedBy, - &i.TotalVisited, + &i.Address, &i.Thumbnail, - &i.RegencyID, - &i.IsDeleted, - &i.CreatedAt, - &i.UpdatedAt, - &i.ApprovedBy, - &i.ApprovedAt, + &i.SubmittedBy, + &i.RegencyName, + &i.ProvinceName, + &i.RegionName, + &i.SubmittedByUser, ) return i, err }