Better logic for comparison

This commit is contained in:
Denis Donici 2025-03-08 20:11:08 +02:00
parent 686fb3b602
commit ad3c09fda0
3 changed files with 33 additions and 9 deletions

View File

@ -32,6 +32,19 @@ export const insertCityTemperatureData = db.query(`
INSERT INTO meteo_data (city_id, min, max, date) INSERT INTO meteo_data (city_id, min, max, date)
VALUES ($id, $min, $max, $date) VALUES ($id, $min, $max, $date)
`); `);
export const getAllCitiesInCountryByTemp = db.query(`
SELECT
c.id as city_id,
c.city as city_name,
md.max as max_temp,
md.min as min_temp,
md.date
FROM cities c
JOIN meteo_data md ON c.id = md.city_id
WHERE c.country_id = $country_id
AND md.date = $date
ORDER BY md.max DESC
`);
export const getHottestAndColdestCityInCountry = db.query(` export const getHottestAndColdestCityInCountry = db.query(`
WITH city_temps AS ( WITH city_temps AS (
@ -40,10 +53,9 @@ export const getHottestAndColdestCityInCountry = db.query(`
c.city as city_name, c.city as city_name,
c.country_id, c.country_id,
md.max as max_temp, md.max as max_temp,
md.min as min_temp,
md.date, md.date,
ROW_NUMBER() OVER (PARTITION BY c.country_id ORDER BY md.max DESC) as hottest_rank, ROW_NUMBER() OVER (PARTITION BY c.country_id ORDER BY md.max DESC) as hottest_rank,
ROW_NUMBER() OVER (PARTITION BY c.country_id ORDER BY md.min ASC) as coldest_rank ROW_NUMBER() OVER (PARTITION BY c.country_id ORDER BY md.max ASC) as coldest_rank
FROM cities c FROM cities c
JOIN meteo_data md ON c.id = md.city_id JOIN meteo_data md ON c.id = md.city_id
WHERE md.date = $date WHERE md.date = $date

View File

@ -27,7 +27,6 @@ export type HotColdCity = {
country_id: number; country_id: number;
country_name: string; country_name: string;
max_temp: number; max_temp: number;
min_temp: number;
date: string; date: string;
hottest_rank: number; hottest_rank: number;
coldest_rank: number; coldest_rank: number;

View File

@ -18,6 +18,9 @@
let { data, form }: PageProps = $props(); let { data, form }: PageProps = $props();
let temperatureData: HotColdCity[] | null = $state(data.temperatureData); let temperatureData: HotColdCity[] | null = $state(data.temperatureData);
let tempDiff: number | null = $derived(
temperatureData ? Number((Math.abs(temperatureData[0].max_temp - temperatureData[1].max_temp)).toFixed(2)) : null
);
let selectedCountryId: number | null = $state(data.country_id); let selectedCountryId: number | null = $state(data.country_id);
let countryForm: HTMLFormElement; let countryForm: HTMLFormElement;
let submitButton: HTMLButtonElement; let submitButton: HTMLButtonElement;
@ -86,19 +89,29 @@
<div class="my-8 flex flex-col justify-evenly gap-12 md:flex-row"> <div class="my-8 flex flex-col justify-evenly gap-12 md:flex-row">
{#each temperatureData as temperature} {#each temperatureData as temperature}
<dl class="flex flex-col gap-2"> <dl class="flex flex-col gap-2">
{#if temperature.coldest_rank === 1}
<dt class="text-center text-lg text-blue-600">❄️ Coldest City</dt>
{:else}
<dt class="text-center text-lg text-red-600">🌡️ Hottest City</dt>
{/if}
<dt class="text-center text-3xl font-medium">{temperature.city_name}</dt> <dt class="text-center text-3xl font-medium">{temperature.city_name}</dt>
<dd> <dd>
Max: {$units == UnitsEnum.IMPERIAL Max: {$units == UnitsEnum.IMPERIAL
? celsiusToFahrenheit(temperature.max_temp) ? celsiusToFahrenheit(temperature.max_temp)
: temperature.min_temp}{$unitsSymbol} : temperature.max_temp}{$unitsSymbol}
</dd>
<dd>
Min: {$units == UnitsEnum.IMPERIAL
? celsiusToFahrenheit(temperature.min_temp)
: temperature.min_temp}{$unitsSymbol}
</dd> </dd>
</dl> </dl>
{/each} {/each}
{#if tempDiff}
<div class="text-center">
<p class="text-lg font-medium">Temperature Difference</p>
<p class="text-2xl">
{$units == UnitsEnum.IMPERIAL
? celsiusToFahrenheit(tempDiff)
: tempDiff}{$unitsSymbol}
</p>
</div>
{/if}
</div> </div>
{/if} {/if}
<!-- Add badges top 7 popular countries --> <!-- Add badges top 7 popular countries -->