diff --git a/db/queries/locations.sql b/db/queries/locations.sql index 1a12245..1007dd0 100644 --- a/db/queries/locations.sql +++ b/db/queries/locations.sql @@ -24,9 +24,9 @@ SELECT l.address, l.thumbnail, l.submitted_by, - r.regency_name, - p.province_name, - r2.region_name, + COALESCE(r.regency_name, '') as regency_name, + COALESCE(p.province_name, '') as province_name, + COALESCE(r2.region_name, '') as region_name, u.username as submitted_by_user FROM locations l JOIN regencies r on r.id = l.regency_id diff --git a/db/sqlc/locations.go b/db/sqlc/locations.go index 813bab7..2bad765 100644 --- a/db/sqlc/locations.go +++ b/db/sqlc/locations.go @@ -22,9 +22,9 @@ type GetTopListLocationsRow struct { Address string `json:"address"` GoogleMapsLink string `json:"google_maps_link"` RegencyName string `json:"regency_name"` - CriticScore sql.NullInt32 `json:"critic_score"` + CriticScore int16 `json:"critic_score"` CriticCount int16 `json:"critic_count"` - UserScore sql.NullInt32 `json:"user_score"` + UserScore int16 `json:"user_score"` UserCount int16 `json:"user_count"` TotalCount int16 `json:"total_count"` CriticBayes int16 `json:"critic_bayes"` @@ -46,9 +46,9 @@ func (q *Queries) GetTopListLocations(ctx context.Context, arg GetTopListLocatio FROM( 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 + (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 @@ -59,9 +59,9 @@ func (q *Queries) GetTopListLocations(ctx context.Context, arg GetTopListLocatio l.google_maps_link, 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 COALESCE(SUM(score), 0) 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 COALESCE(SUM(score), 0) 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 diff --git a/db/sqlc/locations.sql.go b/db/sqlc/locations.sql.go index 6699514..53df913 100644 --- a/db/sqlc/locations.sql.go +++ b/db/sqlc/locations.sql.go @@ -149,9 +149,9 @@ SELECT l.address, l.thumbnail, l.submitted_by, - r.regency_name, - p.province_name, - r2.region_name, + COALESCE(r.regency_name, '') as regency_name, + COALESCE(p.province_name, '') as province_name, + COALESCE(r2.region_name, '') as region_name, u.username as submitted_by_user FROM locations l JOIN regencies r on r.id = l.regency_id @@ -167,9 +167,9 @@ type GetLocationRow struct { 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"` + RegencyName string `json:"regency_name"` + ProvinceName string `json:"province_name"` + RegionName string `json:"region_name"` SubmittedByUser string `json:"submitted_by_user"` }