From d586eda3108000bec6901ca604f4188814a2c1bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eline=20J=C3=A5sund?= Date: Fri, 6 Nov 2026 09:33:35 +0100 Subject: [PATCH 1/5] fix: handle faulty imports --- Database/FillDb.ts | 8 +++----- vite.config.ts | 1 + 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/Database/FillDb.ts b/Database/FillDb.ts index 54d6073..bd181f9 100644 --- a/Database/FillDb.ts +++ b/Database/FillDb.ts @@ -3,7 +3,7 @@ import { readFile } from 'node:fs/promises' import path from 'node:path' import { pool } from '../Backend/DatabaseConnection.ts' -const FILE = path.join(process.cwd(), 'src', 'database', 'games.json') +const FILE = path.join(process.cwd(), 'Database', 'games.json') const CHUNK = 500 // games per INSERT statement async function FillDb() { @@ -15,25 +15,23 @@ async function FillDb() { await pool.query( `INSERT INTO games (game_id, game_name, current_price, initial_price, - discount_percent, image_url, genre, description) + discount_percent, image_url, description) SELECT appid, name, CASE WHEN price = 'Free' THEN 0 ELSE price::numeric END, initialprice::numeric, discount::int, img, - genre, description FROM jsonb_to_recordset($1::jsonb) AS g(appid int, name text, price text, initialprice text, - discount text, img text, genre text, description text) + discount text, img text, description text) ON CONFLICT (game_id) DO UPDATE SET game_name = EXCLUDED.game_name, current_price = EXCLUDED.current_price, initial_price = EXCLUDED.initial_price, discount_percent = EXCLUDED.discount_percent, image_url = EXCLUDED.image_url, - genre = EXCLUDED.genre, description = EXCLUDED.description`, [JSON.stringify(chunk)] ) diff --git a/vite.config.ts b/vite.config.ts index fee430f..75fd6c7 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -5,6 +5,7 @@ import { defineConfig } from 'vite' // https://vite.dev/config/ export default defineConfig({ root: 'Frontend', + envDir: '..', base: '/', //base: '/project2/', From ecbc3e86267383649a7fade49725e1f4ffab7794 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eline=20J=C3=A5sund?= Date: Fri, 6 Nov 2026 09:42:09 +0100 Subject: [PATCH 2/5] feat: add z-a styling and change game sort to lowercase --- Backend/resolvers/GameResolvers.ts | 3 +- .../src/FrontendLogic/GameLogicFrontend.ts | 5 ++-- Frontend/src/pages/SearchPage.tsx | 19 ++++++++++-- Frontend/src/tests/GameLogicFrontend.test.ts | 30 +++++++++++++++---- Frontend/src/tests/SearchPage.test.tsx | 1 + 5 files changed, 48 insertions(+), 10 deletions(-) diff --git a/Backend/resolvers/GameResolvers.ts b/Backend/resolvers/GameResolvers.ts index a4759c0..e451b80 100644 --- a/Backend/resolvers/GameResolvers.ts +++ b/Backend/resolvers/GameResolvers.ts @@ -12,7 +12,8 @@ const SORT_SQL: Record = { 'discount-desc': 'g.discount_percent DESC NULLS LAST', 'price-asc': 'g.current_price ASC NULLS LAST', 'price-desc': 'g.current_price DESC NULLS LAST', - 'title-asc': 'g.game_name ASC' + 'title-asc': 'LOWER(g.game_name) ASC', + 'title-desc': 'LOWER(g.game_name) DESC' } function BuildGameFilter(search?: string | null, genres?: string[] | null, withRank = false) { diff --git a/Frontend/src/FrontendLogic/GameLogicFrontend.ts b/Frontend/src/FrontendLogic/GameLogicFrontend.ts index 7477352..3a37ad0 100644 --- a/Frontend/src/FrontendLogic/GameLogicFrontend.ts +++ b/Frontend/src/FrontendLogic/GameLogicFrontend.ts @@ -7,6 +7,7 @@ interface GraphQLResponse { } const CACHE_TTL_MS = 12 * 60 * 60 * 1000 +const GAMES_CACHE_PREFIX = `games:v2:${encodeURIComponent(BACKEND_URL)}:` interface CachedGames { fetchedAt: number @@ -49,7 +50,7 @@ export interface GameQueryOptions { // Must include the filters, otherwise different filters would share the same cached page. function GamesCacheKey(limit: number, offset: number, options: GameQueryOptions): string { const { search = '', genres = [], sort = '' } = options - return `games:v1:${limit}:${offset}:${sort}:${search}:${genres.join('|')}` + return `${GAMES_CACHE_PREFIX}${limit}:${offset}:${sort}:${search}:${genres.join('|')}` } function GameByIdCacheKey(appid: number): string { @@ -99,7 +100,7 @@ function FindCachedGameById(appid: number): Game | null { // Collect keys first because readCachedGames may remove expired entries. const cacheKeys = Array.from({ length: storage.length }, (_, index) => storage.key(index) - ).filter((key): key is string => key !== null && key.startsWith('games:v1:')) + ).filter((key): key is string => key !== null && key.startsWith(GAMES_CACHE_PREFIX)) for (const key of cacheKeys) { const game = ReadCachedGames(key)?.find((candidate) => candidate.appid === appid) diff --git a/Frontend/src/pages/SearchPage.tsx b/Frontend/src/pages/SearchPage.tsx index c03ac37..980fd27 100644 --- a/Frontend/src/pages/SearchPage.tsx +++ b/Frontend/src/pages/SearchPage.tsx @@ -34,10 +34,22 @@ import type { Game } from '../../../Backend/Steam' import type { SelectChangeEvent } from '@mui/material' import { X } from 'lucide-react' -type SortOption = '' | 'discount-desc' | 'price-asc' | 'price-desc' | 'title-asc' +type SortOption = + | '' + | 'discount-desc' + | 'price-asc' + | 'price-desc' + | 'title-asc' + | 'title-desc' const DEFAULT_SORT: SortOption = 'title-asc' -const SORT_OPTIONS: SortOption[] = ['title-asc', 'discount-desc', 'price-asc', 'price-desc'] +const SORT_OPTIONS: SortOption[] = [ + 'title-asc', + 'title-desc', + 'discount-desc', + 'price-asc', + 'price-desc' +] const GAMES_PER_PAGE = 12 const NO_GAMES: Game[] = [] @@ -233,6 +245,9 @@ export function SearchPage() { Title: A to Z + + Title: Z to A + Discount: high to low diff --git a/Frontend/src/tests/GameLogicFrontend.test.ts b/Frontend/src/tests/GameLogicFrontend.test.ts index f8b72dc..b707e4d 100644 --- a/Frontend/src/tests/GameLogicFrontend.test.ts +++ b/Frontend/src/tests/GameLogicFrontend.test.ts @@ -6,6 +6,8 @@ import FetchGamesFromDB, { } from '../FrontendLogic/GameLogicFrontend' import { BACKEND_URL } from '../FrontendLogic/LogicConnection' +const GAMES_CACHE_PREFIX = `games:v2:${encodeURIComponent(BACKEND_URL)}:` + function MockResponse(body: unknown, ok = true, status = 200): Response { return { ok, @@ -119,7 +121,7 @@ describe('fetchGamesFromDB', () => { expect(firstResult).toEqual([game]) expect(secondResult).toEqual([game]) expect(global.fetch).toHaveBeenCalledTimes(1) - expect(JSON.parse(values.get('games:v1:1:0:::') ?? '{}')).toEqual({ + expect(JSON.parse(values.get(`${GAMES_CACHE_PREFIX}1:0:::`) ?? '{}')).toEqual({ fetchedAt: expect.any(Number), games: [game] }) @@ -135,14 +137,14 @@ describe('fetchGamesFromDB', () => { await FetchGamesFromDB(1, 0, { genres: ['Action'] }) expect(global.fetch).toHaveBeenCalledTimes(2) - expect(values.has('games:v1:1:0:::Action')).toBe(true) - expect(values.has('games:v1:1:0:::')).toBe(true) + expect(values.has(`${GAMES_CACHE_PREFIX}1:0:::Action`)).toBe(true) + expect(values.has(`${GAMES_CACHE_PREFIX}1:0:::`)).toBe(true) }) it('fetches fresh results when the cached games are at least 12 hours old', async () => { const values = new Map([ [ - 'games:v1:1:0:::', + `${GAMES_CACHE_PREFIX}1:0:::`, JSON.stringify({ fetchedAt: Date.now() - 12 * 60 * 60 * 1000, games: [] @@ -157,6 +159,21 @@ describe('fetchGamesFromDB', () => { expect(global.fetch).toHaveBeenCalledTimes(1) }) + it('does not reuse cached results from the previous cache version', async () => { + const values = new Map([ + [ + 'games:v1:1:0:title-desc::', + JSON.stringify({ fetchedAt: Date.now(), games: [game] }) + ] + ]) + StubLocalStorage(values) + global.fetch = vi.fn().mockResolvedValue(MockResponse({ data: { games: [] } })) + + await expect(FetchGamesFromDB(1, 0, { sort: 'title-desc' })).resolves.toEqual([]) + + expect(global.fetch).toHaveBeenCalledOnce() + }) + describe('getGameById', () => { beforeEach(() => { vi.restoreAllMocks() @@ -165,7 +182,10 @@ describe('fetchGamesFromDB', () => { it('returns a game from an existing page cache without fetching', async () => { const values = new Map([ - ['games:v1:500:0:::', JSON.stringify({ fetchedAt: Date.now(), games: [game] })] + [ + `${GAMES_CACHE_PREFIX}500:0:::`, + JSON.stringify({ fetchedAt: Date.now(), games: [game] }) + ] ]) StubLocalStorage(values) global.fetch = vi.fn() diff --git a/Frontend/src/tests/SearchPage.test.tsx b/Frontend/src/tests/SearchPage.test.tsx index 067935f..e127889 100644 --- a/Frontend/src/tests/SearchPage.test.tsx +++ b/Frontend/src/tests/SearchPage.test.tsx @@ -323,6 +323,7 @@ describe('SearchPage', () => { }) it.each([ + ['Title: Z to A', 'title-desc'], ['Price: low to high', 'price-asc'], ['Price: high to low', 'price-desc'], ['Discount: high to low', 'discount-desc'] From a79fb99574426338746eaf1fab88b50ddcefe70e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eline=20J=C3=A5sund?= Date: Fri, 6 Nov 2026 10:06:06 +0100 Subject: [PATCH 3/5] fix: update imports for running db locally --- .../.env.development.example | 0 .env.example => Frontend/.env.example | 0 README.md | 14 +++++++------- package.json | 8 ++++---- vite.config.ts | 1 - 5 files changed, 11 insertions(+), 12 deletions(-) rename .env.development.example => Frontend/.env.development.example (100%) rename .env.example => Frontend/.env.example (100%) diff --git a/.env.development.example b/Frontend/.env.development.example similarity index 100% rename from .env.development.example rename to Frontend/.env.development.example diff --git a/.env.example b/Frontend/.env.example similarity index 100% rename from .env.example rename to Frontend/.env.example diff --git a/README.md b/README.md index 3ae4792..247ed04 100644 --- a/README.md +++ b/README.md @@ -62,10 +62,10 @@ You can also access application through the virtual machine by using this link: | `npm run db:create` | Creates the database tables | | `npm run db:fill` | Fills the database with games | | `npm run db:fill:genres` | Fills the database with genres | -| `npm run server:local` | Starts the backend server using `.env.development` | -| `npm run db:create:local` | Creates the tables in your local database using `.env.development` | -| `npm run db:fill:local` | Fills your local database with games using `.env.development` | -| `npm run db:fill:genres:local` | Fills your local database with genres using `.env.development` | +| `npm run server:local` | Starts the backend server using `Frontend/.env.development` | +| `npm run db:create:local` | Creates the tables in your local database using `Frontend/.env.development` | +| `npm run db:fill:local` | Fills your local database with games using `Frontend/.env.development` | +| `npm run db:fill:genres:local` | Fills your local database with genres using `Frontend/.env.development` | ## Folder structure @@ -368,7 +368,7 @@ If you don’t see the version after restarting your terminal, something has gon When psql works, you can run: ```psql -h localhost -U USERNAME``` to access PostgreSQL. Username will be the username you created in the first step. When you have logged in you should see this: -Now you can run ```CREATE DATABASE t10db;``` to create a database for our project. Once this is done you have to set up the .env.development. This is the env file used for all the local shenanigans. There is a ```.env.development.example``` file already in the project, so you can just rename it to ```.env.development``` and use it. +Now you can run ```CREATE DATABASE t10db;``` to create a database for our project. Once this is done you have to set up ```Frontend/.env.development```. This is the env file used for all the local shenanigans. There is a ```Frontend/.env.development.example``` file already in the project, so you can copy it to ```Frontend/.env.development``` and use it. ```text DB_HOST=localhost @@ -389,7 +389,7 @@ Lucky for you, we have set up a few custom commands that should make this proces In a separate terminal, run: ```npm run db:create:local```, to create all the empty tables. -Then run: ```npm run db:fill:local```, to fill the database with all the games and ```npm run db:fill:genres:local``` to fill the database with genres. The "local" parameter makes sure that the project uses the ```.env.development``` instead of the ```.env```. +Then run: ```npm run db:fill:local```, to fill the database with all the games and ```npm run db:fill:genres:local``` to fill the database with genres. The "local" parameter makes sure that the project uses ```Frontend/.env.development``` instead of ```.env```. Once this is done running, you should be able to go into your database in the psql terminal, run: ```\c t10db```, to connect to the local database. In there you can run: ```SELECT COUNT(game_id) FROM games;``` and if everything has gone smoothly, you should see ```5334``` as the total amount of games. @@ -399,7 +399,7 @@ Now you should be able to use the database. To use the local database you have to have a local server running. This can be created by running ```npm run server:local```. Now that the database and server is up and running you should be able to run the project by running npm run dev as usual. -If you want to run the project with ```npm run dev``` using the VM backend after creating the ```.env.development```, you may have to uncomment or remove ```VITE_BACKEND_URL=http://localhost:3001/``` as the existence of this variable will cause the project to run on the local backend. +If you want to run the project with ```npm run dev``` using the VM backend after creating ```Frontend/.env.development```, you may have to uncomment or remove ```VITE_BACKEND_URL=http://localhost:3001/``` as the existence of this variable will cause the project to run on the local backend. ## Known issues diff --git a/package.json b/package.json index c36b2ff..066c2a0 100644 --- a/package.json +++ b/package.json @@ -17,10 +17,10 @@ "db:create": "node Database/CreateTables.ts", "db:fill": "node Database/FillDb.ts", "db:fill:genres":"node Database/FillGenres.ts", - "server:local": "node --env-file=.env.development Backend/Server.ts", - "db:create:local": "node --env-file=.env.development Database/CreateTables.ts", - "db:fill:local": "node --env-file=.env.development Database/FillDb.ts", - "db:fill:genres:local":"node --env-file=.env.development Database/FillGenres.ts" + "server:local": "node --env-file=Frontend/.env.development Backend/Server.ts", + "db:create:local": "node --env-file=Frontend/.env.development Database/CreateTables.ts", + "db:fill:local": "node --env-file=Frontend/.env.development Database/FillDb.ts", + "db:fill:genres:local":"node --env-file=Frontend/.env.development Database/FillGenres.ts" }, "dependencies": { "@apollo/server": "^5.5.1", diff --git a/vite.config.ts b/vite.config.ts index 75fd6c7..fee430f 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -5,7 +5,6 @@ import { defineConfig } from 'vite' // https://vite.dev/config/ export default defineConfig({ root: 'Frontend', - envDir: '..', base: '/', //base: '/project2/', From 4149aa95f1d0129e9fc52a11c62bd8a3d7ec592e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eline=20J=C3=A5sund?= Date: Fri, 6 Nov 2026 10:15:18 +0100 Subject: [PATCH 4/5] feat: upgrade A-Z and Z-A filtering --- Backend/resolvers/GameResolvers.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Backend/resolvers/GameResolvers.ts b/Backend/resolvers/GameResolvers.ts index e451b80..143d514 100644 --- a/Backend/resolvers/GameResolvers.ts +++ b/Backend/resolvers/GameResolvers.ts @@ -8,12 +8,18 @@ import { } from '../logic/MainPageLogicBackend.ts' import type { Game } from '../Steam.ts' +const NAME_GROUP = `CASE + WHEN g.game_name ~ '^[[:alpha:]]' THEN 0 + WHEN g.game_name ~ '^[[:digit:]]' THEN 1 + ELSE 2 +END` + const SORT_SQL: Record = { 'discount-desc': 'g.discount_percent DESC NULLS LAST', 'price-asc': 'g.current_price ASC NULLS LAST', 'price-desc': 'g.current_price DESC NULLS LAST', - 'title-asc': 'LOWER(g.game_name) ASC', - 'title-desc': 'LOWER(g.game_name) DESC' + 'title-asc': `${NAME_GROUP} ASC, LOWER(g.game_name) ASC`, + 'title-desc': `${NAME_GROUP} DESC, LOWER(g.game_name) DESC` } function BuildGameFilter(search?: string | null, genres?: string[] | null, withRank = false) { From 5537664084a914b0f9e51cae405ecb94a4c196bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eline=20J=C3=A5sund?= Date: Fri, 6 Nov 2026 10:32:17 +0100 Subject: [PATCH 5/5] feat: add totalGames for search result --- Frontend/src/css/SearchPage.css | 37 +++++++++++++++++++++++++++++++ Frontend/src/pages/SearchPage.tsx | 30 ++++++++++++------------- 2 files changed, 52 insertions(+), 15 deletions(-) diff --git a/Frontend/src/css/SearchPage.css b/Frontend/src/css/SearchPage.css index ba49ce8..1979c65 100644 --- a/Frontend/src/css/SearchPage.css +++ b/Frontend/src/css/SearchPage.css @@ -173,3 +173,40 @@ .searchError { font-weight: 700; } + +.searchResultsHeader { + display: grid; + grid-template-columns: 1fr auto 1fr; + align-items: center; + align-self: center; + width: min(calc(100% - 32px), 1088px); + margin-top: 24px; +} + +.gamesFound { + grid-column: 1; + margin: 0; +} + +.pageinationSearchTop { + grid-column: 2; + justify-self: center; + max-width: 100%; + margin-top: 0; +} + +@media (max-width: 690px) { + .searchResultsHeader { + row-gap: 8px; + } + + .pageinationSearchTop { + grid-column: 2; + grid-row: 1; + } + + .gamesFound { + grid-column: 1 / -1; + grid-row: 2; + } +} diff --git a/Frontend/src/pages/SearchPage.tsx b/Frontend/src/pages/SearchPage.tsx index 980fd27..594d381 100644 --- a/Frontend/src/pages/SearchPage.tsx +++ b/Frontend/src/pages/SearchPage.tsx @@ -34,13 +34,7 @@ import type { Game } from '../../../Backend/Steam' import type { SelectChangeEvent } from '@mui/material' import { X } from 'lucide-react' -type SortOption = - | '' - | 'discount-desc' - | 'price-asc' - | 'price-desc' - | 'title-asc' - | 'title-desc' +type SortOption = '' | 'discount-desc' | 'price-asc' | 'price-desc' | 'title-asc' | 'title-desc' const DEFAULT_SORT: SortOption = 'title-asc' const SORT_OPTIONS: SortOption[] = [ @@ -304,14 +298,20 @@ export function SearchPage() { )} - { - setPage(value) - }} - /> +
+ {totalCount > 0 && ( +

+ {totalCount} {totalCount === 1 ? 'game' : 'games'} found +

+ )} + + setPage(value)} + /> +
{error && (

Could not load games: {error}