Skip to content

Commit

Permalink
Merge pull request #8 from quentin-aslan/feature/import-destination-i…
Browse files Browse the repository at this point in the history
…mprovement

✨ Train Station Sorted + Select added on the return list.
  • Loading branch information
quentin-aslan authored Nov 10, 2024
2 parents 1c89488 + 60d3d3a commit 427f007
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 29 deletions.
8 changes: 7 additions & 1 deletion components/CityList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
>
{{ destinations.length }} destinations trouvées.
<div
v-for="destination in destinations"
v-for="destination in sortDestinations"
:key="destination.id"
class="w-full cursor-pointer border rounded-xl bg-white border-max-sec pb-4 pt-4 pl-3 pr-3 flex flex-row justify-between"
@click="() => onDestinationClick(destination)"
Expand All @@ -31,6 +31,12 @@ const props = defineProps<{
const destinationSelected = defineModel()
const sortDestinations = computed(() => {
return [...props.destinations]?.sort((a: RoundTripDestination, b: RoundTripDestination) => {
return b.traffic - a.traffic
})
})
const onDestinationClick = (destination: Destination) => {
if (destinationSelected.value === destination) {
destinationSelected.value = null
Expand Down
30 changes: 18 additions & 12 deletions components/TrainList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,14 @@
<TabPanels class="z-10 w-full">
<TabPanel value="departure">
<div class="flex flex-col gap-4">
<div class="flex flex-row gap-4">
<Select
v-model="sortChoice"
:options="sortChoicesOptions"
option-label="label"
option-value="choice"
placeholder="Select a City"
class="w-full md:w-56"
/>
</div>
<Select
v-model="sortChoice"
:options="sortChoicesOptions"
option-label="label"
option-value="choice"
placeholder="Select a City"
class="w-full md:w-56"
/>
<TrainCard
v-for="(journey, index) in departureJourneysSorted"
:key="index"
Expand All @@ -36,6 +34,14 @@
value="return"
>
<div class="flex flex-col gap-4">
<Select
v-model="sortChoice"
:options="sortChoicesOptions"
option-label="label"
option-value="choice"
placeholder="Select a City"
class="w-full md:w-56"
/>
<TrainCard
v-for="(journey, index) in returnJourneysSorted"
:key="index"
Expand Down Expand Up @@ -129,8 +135,8 @@ const sortJourneysByDuration = (journeys: Journey[], order: 'asc' | 'desc' = 'as
const sortJourneysByDepartureTime = (journeys: Journey[], order: 'asc' | 'desc' = 'asc') => {
return [...journeys].sort((a, b) => {
const departureTimeA = new Date(a.departureTime).getTime()
const departureTimeB = new Date(b.departureTime).getTime()
const departureTimeA = new Date(a[0].departureDateTime).getTime()
const departureTimeB = new Date(b[0].departureDateTime).getTime()
return order === 'asc' ? departureTimeA - departureTimeB : departureTimeB - departureTimeA
})
}
Expand Down
Binary file modified prisma/dev.db
Binary file not shown.
32 changes: 19 additions & 13 deletions server/api/import-destination.post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ export default defineEventHandler(async (event) => {
const traffic = trainStationsTraffic.find(t => t.name === trainStationFromAPI.name)
?.traffic ?? 0

if (traffic === 0) {
console.log(`No traffic found for ${trainStationFromAPI.name}`)
}

trainStations.push({ ...trainStationFromAPI, traffic })
}

Expand Down Expand Up @@ -74,15 +78,13 @@ interface TrainStationFromAPI {
}

interface ListeDesGaresCSVRow {
libelle: string
commune: string
c_geo: string
nom: string
position_geographique: string
}

const newHeadersListeDesGares = [
'libelle',
'commune',
'c_geo',
'nom',
'position_geographique',
]

const getTrainStationsFromSNCF = async (): Promise<TrainStationFromAPI[]> => {
Expand Down Expand Up @@ -117,17 +119,22 @@ const getTrainStationsFromSNCF = async (): Promise<TrainStationFromAPI[]> => {

const downloadListeDesGaresCSV = async (): Promise<AxiosResponse<any>> => {
const URL_WITH_OPTIONS
= `https://data.sncf.com/api/explore/v2.1/catalog/datasets/liste-des-gares/exports/csv?lang=fr&timezone=Europe/Paris&limiter=%3B&use_labels=true&select=libelle%2C%20commune%2C%20c_geo`
= `https://ressources.data.sncf.com/api/explore/v2.1/catalog/datasets/gares-de-voyageurs/exports/csv?lang=fr&timezone=Europe%2FBerlin&use_labels=true&delimiter=%3B&select=nom%2C%20position_geographique`
return await axios.get(URL_WITH_OPTIONS, { responseType: 'stream' })
}

const rowToTrainStationAdapter = (row: ListeDesGaresCSVRow): TrainStationFromAPI => {
try {
const latitude = row.c_geo.split(',')[0]?.trim() ?? 0
const longitude = row.c_geo.split(',')[1]?.trim() ?? 0
const latitude = row.position_geographique.split(',')[0]?.trim() ?? 0
const longitude = row.position_geographique.split(',')[1]?.trim() ?? 0

if (latitude === 0 || longitude === 0) {
console.error(`Lat or lng null for: ${row.nom}`)
return
}

return {
name: normalizeName(row.libelle),
name: normalizeName(row.nom),
longitude: parseFloat(longitude),
latitude: parseFloat(latitude),
}
Expand All @@ -146,9 +153,8 @@ interface TrainStationTraffic {
}

interface FrequentationGareCSVRow {
libelle: string
commune: string
c_geo: string
name: string
traffic: string
}

const newHeadersFrequentationGare = [
Expand Down
3 changes: 0 additions & 3 deletions server/api/train-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,6 @@ const findRoundTrips = async (
contains: normName,
},
},
orderBy: {
traffic: 'desc',
},
})

if (trainStation) {
Expand Down

0 comments on commit 427f007

Please sign in to comment.