diff --git a/src/backend/DbConnection.ts b/Backend/DatabaseConnection.ts similarity index 100% rename from src/backend/DbConnection.ts rename to Backend/DatabaseConnection.ts diff --git a/Backend/Schema.ts b/Backend/Schema.ts new file mode 100644 index 0000000..90306a3 --- /dev/null +++ b/Backend/Schema.ts @@ -0,0 +1,94 @@ +export const typeDefs = `#graphql + type Game { + appid: Int! + name: String + price: String + initialprice: String + discount: String + img: String + genre: String + description: String + } + + type User { + id: Int! + email: String! + username: String! + token: String! + } + + type Profile { + username: String! + description: String + setup: String + favouriteGame: Int + } + + type GameOption { + appid: Int! + name: String! + } + + input ProfileInput { + description: String + setup: String + favouriteGame: Int + } + + type Review { + id: Int! + gameId: Int! + userId: Int + reviewer: String! + rating: Int! + text: String! + gameName: String + } + + type Friend { + id: Int! + username: String! + description: String + setup: String + favouriteGame: Game + } + + type Query { + games( + limit: Int = 500 + offset: Int = 0 + search: String + genres: [String!] + sort: String + ): [Game!]! + wishlist(userId: Int!): [Game!]! + game(appid: Int!): Game + genres: [String!]! + profile(userId: Int!): Profile + gameOptions: [GameOption!]! + totalGameCount( + search: String + genres: [String!] + ): Int! + top3Games: [Game!]! + topGamesByGenre(genre: String!): [Game!]! + discountedGames(genre: String!): [Game!]! + reviews(gameId: Int!, userId: Int): [Review!]! + recentReviews(limit: Int = 10): [Review!]! + friends(userId: Int!): [Friend!]! + friendWishlist(userId: Int!, friendId: Int!): [Game!]! + myFriendId(userId: Int!): String + } + + type Mutation { + addToWishlist(userId: Int!, gameId: Int!): Boolean! + removeFromWishlist(userId: Int!, gameId: Int!): Boolean! + signup(username: String!, email: String!, password: String!): User! + signin(email: String!, password: String!): User! + updateProfile(userId: Int!, input: ProfileInput!): Profile! + createReview(userId: Int!, gameId: Int!, rating: Int!, text: String!): Review! + updateReview(reviewId: Int!, userId: Int!, rating: Int!, text: String!): Review! + deleteReview(reviewId: Int!, userId: Int!): Boolean! + addFriend(userId: Int!, friendCode: String!): Friend! + } +` diff --git a/Backend/Server.ts b/Backend/Server.ts new file mode 100644 index 0000000..c83244a --- /dev/null +++ b/Backend/Server.ts @@ -0,0 +1,16 @@ +import { ApolloServer } from '@apollo/server' +import { startStandaloneServer } from '@apollo/server/standalone' +import { typeDefs } from './Schema.ts' +import { resolvers } from './resolvers/index.ts' + +const server = new ApolloServer({ typeDefs, resolvers }) +const { url } = await startStandaloneServer(server, { + listen: { host: process.env.HOST ?? '127.0.0.1', port: 3001 }, + context: async ({ req }) => ({ + token: req.headers.authorization?.replace(/^Bearer /, '') ?? null + }) +}) +console.log( + `Database: ${process.env.DB_USER}@${process.env.DB_HOST}:${process.env.DB_PORT}/${process.env.DB_NAME}` +) +console.log(`GraphQL server ready at ${url}`) diff --git a/src/backend/Steam.ts b/Backend/Steam.ts similarity index 100% rename from src/backend/Steam.ts rename to Backend/Steam.ts diff --git a/deploy/project2-backend.service b/Backend/deploy/project2-backend.service similarity index 100% rename from deploy/project2-backend.service rename to Backend/deploy/project2-backend.service diff --git a/src/backend/FriendLogicBackend.ts b/Backend/logic/FriendLogicBackend.ts similarity index 98% rename from src/backend/FriendLogicBackend.ts rename to Backend/logic/FriendLogicBackend.ts index c9acd4d..2130019 100644 --- a/src/backend/FriendLogicBackend.ts +++ b/Backend/logic/FriendLogicBackend.ts @@ -1,5 +1,5 @@ import { GraphQLError } from 'graphql' -import { pool } from './DbConnection.ts' +import { pool } from '../DatabaseConnection.ts' export interface Friend { id: number diff --git a/src/backend/Genres.ts b/Backend/logic/GenresLogicBackend.ts similarity index 80% rename from src/backend/Genres.ts rename to Backend/logic/GenresLogicBackend.ts index bfc9531..cff35f5 100644 --- a/src/backend/Genres.ts +++ b/Backend/logic/GenresLogicBackend.ts @@ -1,4 +1,4 @@ -import { pool } from './DbConnection.ts' +import { pool } from '../DatabaseConnection.ts' export async function FetchGenres(): Promise { const { rows } = await pool.query(`SELECT genre_name FROM genres ORDER BY genre_name`) diff --git a/src/backend/MainpageLogicBackend.ts b/Backend/logic/MainPageLogicBackend.ts similarity index 86% rename from src/backend/MainpageLogicBackend.ts rename to Backend/logic/MainPageLogicBackend.ts index 107f787..4d0358f 100644 --- a/src/backend/MainpageLogicBackend.ts +++ b/Backend/logic/MainPageLogicBackend.ts @@ -1,7 +1,7 @@ -import { pool } from './DbConnection.ts' -import { type Game } from './Steam.ts' -import { REVIEW_SCORE_JOIN } from './Reviews.ts' -import { GAME_COLUMNS } from './GameColumns.ts' +import { pool } from '../DatabaseConnection.ts' +import { type Game } from '../Steam.ts' +import { REVIEW_SCORE_JOIN } from './ReviewsLogicBackend.ts' +import { GAME_COLUMNS } from '../types/GameColumns.ts' const ORDER_BY_REVIEW_SCORE = ` rs.review_score DESC NULLS LAST, diff --git a/src/backend/ProfileLogicBackend.ts b/Backend/logic/ProfileLogicBackend.ts similarity index 94% rename from src/backend/ProfileLogicBackend.ts rename to Backend/logic/ProfileLogicBackend.ts index 88626b1..b57c94e 100644 --- a/src/backend/ProfileLogicBackend.ts +++ b/Backend/logic/ProfileLogicBackend.ts @@ -1,6 +1,6 @@ import { GraphQLError } from 'graphql' -import { pool } from './DbConnection.ts' -import { SETUP_OPTIONS } from '../types/SetupOptions.ts' +import { pool } from '../DatabaseConnection.ts' +import { SETUP_OPTIONS } from '../../Frontend/src/types/SetupOptions.ts' const MAX_DESCRIPTION_LENGTH = 500 diff --git a/src/backend/Reviews.ts b/Backend/logic/ReviewsLogicBackend.ts similarity index 97% rename from src/backend/Reviews.ts rename to Backend/logic/ReviewsLogicBackend.ts index a5ef7a8..a8c298f 100644 --- a/src/backend/Reviews.ts +++ b/Backend/logic/ReviewsLogicBackend.ts @@ -1,5 +1,5 @@ -import { pool } from './DbConnection.ts' -import type { Review } from '../types/Review.ts' +import { pool } from '../DatabaseConnection.ts' +import type { Review } from '../../Frontend/src/types/Review.ts' const REVIEW_COLUMNS = ` r.review_id AS id, diff --git a/src/backend/UserLogicBackend.ts b/Backend/logic/UserLogicBackend.ts similarity index 98% rename from src/backend/UserLogicBackend.ts rename to Backend/logic/UserLogicBackend.ts index c82382f..6536e44 100644 --- a/src/backend/UserLogicBackend.ts +++ b/Backend/logic/UserLogicBackend.ts @@ -1,5 +1,5 @@ import { GraphQLError } from 'graphql' -import { pool } from './DbConnection.ts' +import { pool } from '../DatabaseConnection.ts' import { randomBytes, randomInt } from 'crypto' import bcrypt from 'bcrypt' diff --git a/src/backend/WishlistLogic.ts b/Backend/logic/WishlistLogicBackend.ts similarity index 96% rename from src/backend/WishlistLogic.ts rename to Backend/logic/WishlistLogicBackend.ts index 73cf5fd..91092c6 100644 --- a/src/backend/WishlistLogic.ts +++ b/Backend/logic/WishlistLogicBackend.ts @@ -1,4 +1,4 @@ -import { pool } from './DbConnection.ts' +import { pool } from '../DatabaseConnection.ts' export async function AddToWishlist( userId: number, diff --git a/Backend/resolvers/FriendResolvers.ts b/Backend/resolvers/FriendResolvers.ts new file mode 100644 index 0000000..9fb9186 --- /dev/null +++ b/Backend/resolvers/FriendResolvers.ts @@ -0,0 +1,71 @@ +import { GraphQLError } from 'graphql' +import { pool } from '../DatabaseConnection.ts' +import { GAME_COLUMNS } from '../GameColumns.ts' +import { AddFriend, AreFriends, GetFriendId, GetFriends } from '../FriendLogicBackend.ts' +import type { Game } from '../Steam.ts' +import { RequireUser } from '../UserLogicBackend.ts' +import type { GraphQLContext } from './types.ts' + +export const friendQueryResolvers = { + friends: async ( + _parent: unknown, + { userId }: { userId: number }, + { token }: GraphQLContext + ) => { + await RequireUser(token, userId) + return GetFriends(userId) + }, + + friendWishlist: async ( + _parent: unknown, + { userId, friendId }: { userId: number; friendId: number }, + { token }: GraphQLContext + ): Promise => { + await RequireUser(token, userId) + if (!(await AreFriends(userId, friendId))) { + throw new GraphQLError('You are not friends with this user.', { + extensions: { code: 'FORBIDDEN' } + }) + } + const { rows } = await pool.query( + `SELECT ${GAME_COLUMNS} + FROM wishlist w + JOIN games g ON g.game_id = w.game_id + WHERE w.user_id = $1 + ORDER BY g.game_name`, + [friendId] + ) + return rows + }, + + myFriendId: async ( + _parent: unknown, + { userId }: { userId: number }, + { token }: GraphQLContext + ) => { + await RequireUser(token, userId) + return GetFriendId(userId) + } +} + +export const friendTypeResolvers = { + favouriteGame: async (friend: { favouriteGameId: number | null }): Promise => { + if (!friend.favouriteGameId) return null + const { rows } = await pool.query( + `SELECT ${GAME_COLUMNS} FROM games g WHERE g.game_id = $1`, + [friend.favouriteGameId] + ) + return rows[0] ?? null + } +} + +export const friendMutationResolvers = { + addFriend: async ( + _parent: unknown, + { userId, friendCode }: { userId: number; friendCode: string }, + { token }: GraphQLContext + ) => { + await RequireUser(token, userId) + return AddFriend(userId, friendCode) + } +} diff --git a/Backend/resolvers/GameResolvers.ts b/Backend/resolvers/GameResolvers.ts new file mode 100644 index 0000000..5929c12 --- /dev/null +++ b/Backend/resolvers/GameResolvers.ts @@ -0,0 +1,131 @@ +import { pool } from '../DatabaseConnection.ts' +import { FetchGenres } from '../Genres.ts' +import { GAME_COLUMNS } from '../GameColumns.ts' +import { + FetchDiscountedGames, + FetchTop10GamesPerGenre, + FetchTop3Games +} from '../MainPageLogicBackend.ts' +import type { Game } from '../Steam.ts' + +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' +} + +function BuildGameFilter(search?: string | null, genres?: string[] | null, withRank = false) { + const conditions: string[] = [] + const params: unknown[] = [] + let rank = '' + + if (search?.trim()) { + const term = search.trim() + + params.push(`%${term}%`) + conditions.push(`g.game_name ILIKE $${params.length}`) + + if (withRank) { + const escaped = term.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') + params.push(`^${escaped}\\M`) + const startIdx = params.length + params.push(`\\m${escaped}\\M`) + const wordIdx = params.length + + rank = `CASE + WHEN g.game_name ~* $${startIdx} THEN 0 + WHEN g.game_name ~* $${wordIdx} THEN 1 + ELSE 2 + END` + } + } + + if (genres?.length) { + params.push(genres) + conditions.push( + `NOT EXISTS ( + SELECT 1 FROM unnest($${params.length}::text[]) AS wanted(name) + WHERE NOT EXISTS ( + SELECT 1 FROM genretogame gg + JOIN genres ge ON ge.genre_id = gg.genre_id + WHERE gg.game_id = g.game_id AND ge.genre_name = wanted.name + ) + )` + ) + } + + return { + where: conditions.length ? `WHERE ${conditions.join(' AND ')}` : '', + params, + rank + } +} + +export const gameQueryResolvers = { + games: async ( + _parent: unknown, + { + limit, + offset, + search, + genres, + sort + }: { + limit: number + offset: number + search?: string | null + genres?: string[] | null + sort?: string | null + } + ): Promise => { + const safeLimit = Math.min(Math.max(limit, 0), 500) + const safeOffset = Math.max(offset, 0) + const { where, params, rank } = BuildGameFilter(search, genres, true) + + const orderBy = [SORT_SQL[sort ?? ''], rank, 'g.game_id'].filter(Boolean).join(', ') + + const { rows } = await pool.query( + `SELECT ${GAME_COLUMNS} + FROM games g + ${where} + ORDER BY ${orderBy} + LIMIT $${params.length + 1} OFFSET $${params.length + 2}`, + [...params, safeLimit, safeOffset] + ) + return rows + }, + + totalGameCount: async ( + _parent: unknown, + { search, genres }: { search?: string | null; genres?: string[] | null } + ): Promise => { + const { where, params } = BuildGameFilter(search, genres) + const { rows } = await pool.query(`SELECT COUNT(*) AS count FROM games g ${where}`, params) + return Number(rows[0].count) + }, + + game: async (_parent: unknown, { appid }: { appid: number }): Promise => { + const { rows } = await pool.query( + `SELECT ${GAME_COLUMNS} + FROM games g + WHERE g.game_id = $1`, + [appid] + ) + return rows[0] ?? null + }, + + genres: () => FetchGenres(), + + gameOptions: async () => { + const { rows } = await pool.query( + `SELECT game_id AS appid, game_name AS name FROM games ORDER BY game_name` + ) + return rows + }, + + top3Games: () => FetchTop3Games(), + topGamesByGenre: (_parent: unknown, { genre }: { genre: string }) => + FetchTop10GamesPerGenre(genre), + discountedGames: (_parent: unknown, { genre }: { genre: string }) => FetchDiscountedGames(genre) +} diff --git a/Backend/resolvers/ReviewResolvers.ts b/Backend/resolvers/ReviewResolvers.ts new file mode 100644 index 0000000..b2bed27 --- /dev/null +++ b/Backend/resolvers/ReviewResolvers.ts @@ -0,0 +1,56 @@ +import { + CreateReview, + DeleteReview, + GetRecentReviews, + GetReviewsForGame, + UpdateReview +} from '../Reviews.ts' +import { RequireUser } from '../UserLogicBackend.ts' +import type { GraphQLContext } from './types.ts' + +export const reviewQueryResolvers = { + reviews: (_parent: unknown, { gameId, userId }: { gameId: number; userId?: number | null }) => + GetReviewsForGame(gameId, userId ?? null), + + recentReviews: (_parent: unknown, { limit }: { limit: number }) => GetRecentReviews(limit) +} + +export const reviewMutationResolvers = { + createReview: async ( + _parent: unknown, + { + userId, + gameId, + rating, + text + }: { userId: number; gameId: number; rating: number; text: string }, + { token }: GraphQLContext + ) => { + await RequireUser(token, userId) + return CreateReview(userId, gameId, rating, text) + }, + + updateReview: async ( + _parent: unknown, + { + reviewId, + userId, + rating, + text + }: { reviewId: number; userId: number; rating: number; text: string }, + { token }: GraphQLContext + ) => { + await RequireUser(token, userId) + return UpdateReview(reviewId, userId, rating, text) + }, + + deleteReview: async ( + _parent: unknown, + { reviewId, userId }: { reviewId: number; userId: number }, + { token }: GraphQLContext + ) => { + await RequireUser(token, userId) + await DeleteReview(reviewId, userId) + return true + } +} diff --git a/Backend/resolvers/UserResolvers.ts b/Backend/resolvers/UserResolvers.ts new file mode 100644 index 0000000..4c316a2 --- /dev/null +++ b/Backend/resolvers/UserResolvers.ts @@ -0,0 +1,33 @@ +import { RequireUser, Signin, Signup } from '../UserLogicBackend.ts' +import { FetchProfile, UpdateProfile, type ProfileInput } from '../ProfileLogicBackend.ts' +import type { GraphQLContext } from './types.ts' + +export const userQueryResolvers = { + profile: async ( + _parent: unknown, + { userId }: { userId: number }, + { token }: GraphQLContext + ) => { + await RequireUser(token, userId) + return FetchProfile(userId) + } +} + +export const userMutationResolvers = { + signup: ( + _parent: unknown, + { username, email, password }: { username: string; email: string; password: string } + ) => Signup(username, email, password), + + signin: (_parent: unknown, { email, password }: { email: string; password: string }) => + Signin(email, password), + + updateProfile: async ( + _parent: unknown, + { userId, input }: { userId: number; input: ProfileInput }, + { token }: GraphQLContext + ) => { + await RequireUser(token, userId) + return UpdateProfile(userId, input) + } +} diff --git a/Backend/resolvers/WishlistResolvers.ts b/Backend/resolvers/WishlistResolvers.ts new file mode 100644 index 0000000..fb3ee07 --- /dev/null +++ b/Backend/resolvers/WishlistResolvers.ts @@ -0,0 +1,48 @@ +import { pool } from '../DatabaseConnection.ts' +import { GAME_COLUMNS } from '../GameColumns.ts' +import { AddToWishlist, RemoveFromWishlist } from '../WishlistLogicBackend.ts' +import type { Game } from '../Steam.ts' +import { RequireUser } from '../UserLogicBackend.ts' +import type { GraphQLContext } from './types.ts' + +export const wishlistQueryResolvers = { + wishlist: async ( + _parent: unknown, + { userId }: { userId: number }, + { token }: GraphQLContext + ): Promise => { + await RequireUser(token, userId) + + const { rows } = await pool.query( + `SELECT ${GAME_COLUMNS} + FROM wishlist w + JOIN games g ON g.game_id = w.game_id + WHERE w.user_id = $1 + ORDER BY g.game_name`, + [userId] + ) + return rows + } +} + +export const wishlistMutationResolvers = { + addToWishlist: async ( + _parent: unknown, + { userId, gameId }: { userId: number; gameId: number }, + { token }: GraphQLContext + ) => { + await RequireUser(token, userId) + await AddToWishlist(userId, gameId) + return true + }, + + removeFromWishlist: async ( + _parent: unknown, + { userId, gameId }: { userId: number; gameId: number }, + { token }: GraphQLContext + ) => { + await RequireUser(token, userId) + await RemoveFromWishlist(userId, gameId) + return true + } +} diff --git a/Backend/resolvers/index.ts b/Backend/resolvers/index.ts new file mode 100644 index 0000000..f5277ba --- /dev/null +++ b/Backend/resolvers/index.ts @@ -0,0 +1,26 @@ +import { + friendMutationResolvers, + friendQueryResolvers, + friendTypeResolvers +} from './FriendResolvers.ts' +import { gameQueryResolvers } from './GameResolvers.ts' +import { reviewMutationResolvers, reviewQueryResolvers } from './ReviewResolvers.ts' +import { userMutationResolvers, userQueryResolvers } from './UserResolvers.ts' +import { wishlistMutationResolvers, wishlistQueryResolvers } from './WishlistResolvers.ts' + +export const resolvers = { + Query: { + ...gameQueryResolvers, + ...wishlistQueryResolvers, + ...userQueryResolvers, + ...reviewQueryResolvers, + ...friendQueryResolvers + }, + Mutation: { + ...wishlistMutationResolvers, + ...userMutationResolvers, + ...reviewMutationResolvers, + ...friendMutationResolvers + }, + Friend: friendTypeResolvers +} diff --git a/Backend/resolvers/types.ts b/Backend/resolvers/types.ts new file mode 100644 index 0000000..2cfad82 --- /dev/null +++ b/Backend/resolvers/types.ts @@ -0,0 +1,3 @@ +export type GraphQLContext = { + token: string | null +} diff --git a/src/tests/Genres.test.ts b/Backend/tests/GenresBackend.test.ts similarity index 93% rename from src/tests/Genres.test.ts rename to Backend/tests/GenresBackend.test.ts index 3edb945..3e539a5 100644 --- a/src/tests/Genres.test.ts +++ b/Backend/tests/GenresBackend.test.ts @@ -3,9 +3,9 @@ import { beforeEach, describe, expect, it, vi } from 'vitest' const { Query } = vi.hoisted(() => ({ Query: vi.fn() })) -vi.mock('../backend/DbConnection.ts', () => ({ pool: { query: Query } })) +vi.mock('../DatabaseConnection.ts', () => ({ pool: { query: Query } })) -import { FetchGenres } from '../backend/Genres.ts' +import { FetchGenres } from '../logic/GenresLogicBackend.ts' describe('fetchGenres (backend)', () => { beforeEach(() => { diff --git a/src/tests/MainpageBackend.test.ts b/Backend/tests/MainpageBackend.test.ts similarity index 95% rename from src/tests/MainpageBackend.test.ts rename to Backend/tests/MainpageBackend.test.ts index 32b9168..5780bac 100644 --- a/src/tests/MainpageBackend.test.ts +++ b/Backend/tests/MainpageBackend.test.ts @@ -4,13 +4,13 @@ import { beforeEach, describe, expect, it, vi } from 'vitest' const { Query } = vi.hoisted(() => ({ Query: vi.fn() })) // Replace the real Postgres pool, so no database (or .env file) is needed -vi.mock('../backend/DbConnection.ts', () => ({ pool: { query: Query } })) +vi.mock('../DatabaseConnection.ts', () => ({ pool: { query: Query } })) import { FetchDiscountedGames, FetchTop10GamesPerGenre, FetchTop3Games -} from '../backend/MainpageLogicBackend.ts' +} from '../logic/MainPageLogicBackend.ts' describe('main page (backend)', () => { beforeEach(() => { diff --git a/src/tests/ProfileLogicBackend.test.ts b/Backend/tests/ProfileLogicBackend.test.ts similarity index 96% rename from src/tests/ProfileLogicBackend.test.ts rename to Backend/tests/ProfileLogicBackend.test.ts index 6a9b03d..2027ffd 100644 --- a/src/tests/ProfileLogicBackend.test.ts +++ b/Backend/tests/ProfileLogicBackend.test.ts @@ -1,12 +1,12 @@ import { describe, it, expect, vi, beforeEach } from 'vitest' -vi.mock('../backend/DbConnection', () => ({ +vi.mock('../DatabaseConnection.ts', () => ({ pool: { query: vi.fn() } })) import { GraphQLError } from 'graphql' -import { pool } from '../backend/DbConnection' -import { FetchProfile, UpdateProfile } from '../backend/ProfileLogicBackend' +import { pool } from '../DatabaseConnection.ts' +import { FetchProfile, UpdateProfile } from '../logic/ProfileLogicBackend.ts' const Query = pool.query as unknown as ReturnType diff --git a/src/tests/ReviewsBackend.test.ts b/Backend/tests/ReviewsBackend.test.ts similarity index 97% rename from src/tests/ReviewsBackend.test.ts rename to Backend/tests/ReviewsBackend.test.ts index da5435a..f67d188 100644 --- a/src/tests/ReviewsBackend.test.ts +++ b/Backend/tests/ReviewsBackend.test.ts @@ -4,7 +4,7 @@ import { beforeEach, describe, expect, it, vi } from 'vitest' const { Query } = vi.hoisted(() => ({ Query: vi.fn() })) // Replace the real Postgres pool, so no database (or .env file) is needed -vi.mock('../backend/DbConnection.ts', () => ({ pool: { query: Query } })) +vi.mock('../DatabaseConnection.ts', () => ({ pool: { query: Query } })) import { CreateReview, @@ -12,8 +12,8 @@ import { GetRecentReviews, GetReviewsForGame, UpdateReview -} from '../backend/Reviews.ts' -import type { Review } from '../types/Review' +} from '../logic/ReviewsLogicBackend.ts' +import type { Review } from '../../Frontend/src/types/Review.ts' const testReview: Review = { id: 1, diff --git a/src/tests/Steam.test.ts b/Backend/tests/SteamBackend.test.ts similarity index 99% rename from src/tests/Steam.test.ts rename to Backend/tests/SteamBackend.test.ts index dfa053d..659fcab 100644 --- a/src/tests/Steam.test.ts +++ b/Backend/tests/SteamBackend.test.ts @@ -1,6 +1,6 @@ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest' -import { FetchBulkGames, FillGame, FillAll, FetchGames, type Game } from '../backend/Steam' -import { ClearPageCache } from '../backend/Steam' +import { FetchBulkGames, FillGame, FillAll, FetchGames, type Game } from '../Steam' +import { ClearPageCache } from '../Steam' beforeEach(() => { vi.restoreAllMocks() diff --git a/src/tests/TokenBackend.test.ts b/Backend/tests/TokenBackend.test.ts similarity index 98% rename from src/tests/TokenBackend.test.ts rename to Backend/tests/TokenBackend.test.ts index 74a994c..ef3b322 100644 --- a/src/tests/TokenBackend.test.ts +++ b/Backend/tests/TokenBackend.test.ts @@ -2,12 +2,12 @@ import { describe, it, expect, vi, beforeAll, beforeEach } from 'vitest' import bcrypt from 'bcrypt' import { GraphQLError } from 'graphql' -vi.mock('../backend/DbConnection', () => ({ +vi.mock('../DatabaseConnection.ts', () => ({ pool: { query: vi.fn() } })) -import { pool } from '../backend/DbConnection' -import { Signup, Signin, RequireUser } from '../backend/UserLogicBackend' +import { pool } from '../DatabaseConnection.ts' +import { Signup, Signin, RequireUser } from '../logic/UserLogicBackend.ts' const Query = pool.query as unknown as ReturnType diff --git a/src/tests/UserLogicBackend.test.ts b/Backend/tests/UserLogicBackend.test.ts similarity index 97% rename from src/tests/UserLogicBackend.test.ts rename to Backend/tests/UserLogicBackend.test.ts index b4f9ad9..83498d1 100644 --- a/src/tests/UserLogicBackend.test.ts +++ b/Backend/tests/UserLogicBackend.test.ts @@ -1,12 +1,12 @@ import { describe, it, expect, vi, beforeAll, beforeEach } from 'vitest' import bcrypt from 'bcrypt' -vi.mock('../backend/DbConnection', () => ({ +vi.mock('../DatabaseConnection.ts', () => ({ pool: { query: vi.fn() } })) -import { pool } from '../backend/DbConnection' -import { Signup, Signin } from '../backend/UserLogicBackend' +import { pool } from '../DatabaseConnection.ts' +import { Signup, Signin } from '../logic/UserLogicBackend.ts' const Query = pool.query as unknown as ReturnType diff --git a/src/tests/WishlistBackend.test.ts b/Backend/tests/WishlistBackend.test.ts similarity index 96% rename from src/tests/WishlistBackend.test.ts rename to Backend/tests/WishlistBackend.test.ts index 4d72c61..dd64a20 100644 --- a/src/tests/WishlistBackend.test.ts +++ b/Backend/tests/WishlistBackend.test.ts @@ -4,9 +4,8 @@ import { beforeEach, describe, expect, it, vi } from 'vitest' const { Query } = vi.hoisted(() => ({ Query: vi.fn() })) // Replace the real Postgres pool, so no database (or .env file) is needed -vi.mock('../backend/DbConnection.ts', () => ({ pool: { query: Query } })) - -import { AddToWishlist, RemoveFromWishlist } from '../backend/WishlistLogic.ts' +vi.mock('../DatabaseConnection.ts', () => ({ pool: { query: Query } })) +import { AddToWishlist, RemoveFromWishlist } from '../logic/WishlistLogicBackend.ts' describe('addToWishlist (backend)', () => { beforeEach(() => { diff --git a/src/backend/GameColumns.ts b/Backend/types/GameColumns.ts similarity index 100% rename from src/backend/GameColumns.ts rename to Backend/types/GameColumns.ts diff --git a/src/database/CreateTables.ts b/Database/CreateTables.ts similarity index 98% rename from src/database/CreateTables.ts rename to Database/CreateTables.ts index 2036585..894aa72 100644 --- a/src/database/CreateTables.ts +++ b/Database/CreateTables.ts @@ -1,5 +1,5 @@ import 'dotenv/config' -import { pool } from '../backend/DbConnection.ts' +import { pool } from '../Backend/DatabaseConnection.ts' const SCHEMA = ` CREATE TABLE IF NOT EXISTS games ( diff --git a/src/database/FetchGamesToJson.ts b/Database/FetchGamesToJson.ts similarity index 97% rename from src/database/FetchGamesToJson.ts rename to Database/FetchGamesToJson.ts index 5695a91..01e5b6e 100644 --- a/src/database/FetchGamesToJson.ts +++ b/Database/FetchGamesToJson.ts @@ -1,6 +1,6 @@ import { mkdir, readFile, rename, writeFile } from 'node:fs/promises' import path from 'node:path' -import { FetchGames, type Game } from '../backend/Steam.ts' +import { FetchGames, type Game } from '../Backend/Steam.ts' //Do not do the math on how long this takes to run... diff --git a/src/database/FillDb.ts b/Database/FillDb.ts similarity index 97% rename from src/database/FillDb.ts rename to Database/FillDb.ts index 8f42106..54d6073 100644 --- a/src/database/FillDb.ts +++ b/Database/FillDb.ts @@ -1,7 +1,7 @@ import 'dotenv/config' import { readFile } from 'node:fs/promises' import path from 'node:path' -import { pool } from '../backend/DbConnection.ts' +import { pool } from '../Backend/DatabaseConnection.ts' const FILE = path.join(process.cwd(), 'src', 'database', 'games.json') const CHUNK = 500 // games per INSERT statement diff --git a/src/database/FillGenres.ts b/Database/FillGenres.ts similarity index 96% rename from src/database/FillGenres.ts rename to Database/FillGenres.ts index b33a922..2c01337 100644 --- a/src/database/FillGenres.ts +++ b/Database/FillGenres.ts @@ -1,5 +1,5 @@ import 'dotenv/config' -import { pool } from '../backend/DbConnection.ts' +import { pool } from '../Backend/DatabaseConnection.ts' const FILL_GENRES = ` INSERT INTO genres (genre_name) diff --git a/src/database/games.json b/Database/games.json similarity index 100% rename from src/database/games.json rename to Database/games.json diff --git a/src/App.tsx b/Frontend/App.tsx similarity index 68% rename from src/App.tsx rename to Frontend/App.tsx index f12fbda..bd686e5 100644 --- a/src/App.tsx +++ b/Frontend/App.tsx @@ -1,12 +1,12 @@ import { HashRouter, Routes, Route } from 'react-router-dom' -import MainPage from './pages/MainPage' -import GamePage from './pages/GamePage' -import { SearchPage } from './pages/SearchPage' -import SignUpPage from './pages/SignUp' -import SignInPage from './pages/SignIn' -import WishlistPage from './pages/WishlistPage' -import ProfilePage from './pages/ProfilePage' -import FriendsPage from './pages/FriendsPage' +import MainPage from './src/pages/MainPage' +import GamePage from './src/pages/GamePage' +import { SearchPage } from './src/pages/SearchPage' +import SignUpPage from './src/pages/SignUp' +import SignInPage from './src/pages/SignIn' +import WishlistPage from './src/pages/WishlistPage' +import ProfilePage from './src/pages/ProfilePage' +import FriendsPage from './src/pages/FriendsPage' function App() { return ( diff --git a/src/Main.tsx b/Frontend/Main.tsx similarity index 74% rename from src/Main.tsx rename to Frontend/Main.tsx index 01eef34..8905f85 100644 --- a/src/Main.tsx +++ b/Frontend/Main.tsx @@ -1,8 +1,8 @@ import { StrictMode } from 'react' import { createRoot } from 'react-dom/client' -import './css/Global.css' +import './src/css/Global.css' import App from './App.tsx' -import { AuthProvider } from './context/AuthContext.tsx' +import { AuthProvider } from './src/context/AuthContext.tsx' createRoot(document.getElementById('root')!).render( diff --git a/index.html b/Frontend/index.html similarity index 85% rename from index.html rename to Frontend/index.html index 6dc9fa4..10595f1 100644 --- a/index.html +++ b/Frontend/index.html @@ -8,6 +8,6 @@
- + diff --git a/src/api/FriendClient.ts b/Frontend/src/FrontendLogic/FriendLogicFrontend.ts similarity index 97% rename from src/api/FriendClient.ts rename to Frontend/src/FrontendLogic/FriendLogicFrontend.ts index c3c67f4..1347f68 100644 --- a/src/api/FriendClient.ts +++ b/Frontend/src/FrontendLogic/FriendLogicFrontend.ts @@ -1,5 +1,5 @@ import { Gql } from './LogicConnection' -import type { Game } from '../backend/Steam' +import type { Game } from '../../../Backend/Steam' export interface Friend { id: number diff --git a/src/api/GameClient.ts b/Frontend/src/FrontendLogic/GameLogicFrontend.ts similarity index 99% rename from src/api/GameClient.ts rename to Frontend/src/FrontendLogic/GameLogicFrontend.ts index 32bdb79..7477352 100644 --- a/src/api/GameClient.ts +++ b/Frontend/src/FrontendLogic/GameLogicFrontend.ts @@ -1,4 +1,4 @@ -import type { Game } from '../backend/Steam' +import type { Game } from '../../../Backend/Steam' import { BACKEND_URL } from './LogicConnection' interface GraphQLResponse { diff --git a/src/api/LogicConnection.ts b/Frontend/src/FrontendLogic/LogicConnection.ts similarity index 100% rename from src/api/LogicConnection.ts rename to Frontend/src/FrontendLogic/LogicConnection.ts diff --git a/src/api/MainPageLogicFrontend.ts b/Frontend/src/FrontendLogic/MainPageLogicFrontend.ts similarity index 98% rename from src/api/MainPageLogicFrontend.ts rename to Frontend/src/FrontendLogic/MainPageLogicFrontend.ts index 3905718..b724b04 100644 --- a/src/api/MainPageLogicFrontend.ts +++ b/Frontend/src/FrontendLogic/MainPageLogicFrontend.ts @@ -1,5 +1,5 @@ import { Gql } from './LogicConnection' -import { type Game } from '../backend/Steam' +import { type Game } from '../../../Backend/Steam' export type MainPageGenreSection = 'top10' | 'discounts' const GAME_FIELDS = `appid name price initialprice discount img genre description` diff --git a/src/api/ProfileLogicFrontend.ts b/Frontend/src/FrontendLogic/ProfileLogicFrontend.ts similarity index 93% rename from src/api/ProfileLogicFrontend.ts rename to Frontend/src/FrontendLogic/ProfileLogicFrontend.ts index ec3bb84..ea12e03 100644 --- a/src/api/ProfileLogicFrontend.ts +++ b/Frontend/src/FrontendLogic/ProfileLogicFrontend.ts @@ -1,5 +1,5 @@ import { Gql } from './LogicConnection' -import type { Profile } from '../backend/ProfileLogicBackend' +import type { Profile } from '../../../Backend/logic/ProfileLogicBackend' export interface GameOption { appid: number diff --git a/src/api/ReviewLogicFrontend.ts b/Frontend/src/FrontendLogic/ReviewLogicFrontend.ts similarity index 100% rename from src/api/ReviewLogicFrontend.ts rename to Frontend/src/FrontendLogic/ReviewLogicFrontend.ts diff --git a/src/api/SearchPageLogicFrontend.ts b/Frontend/src/FrontendLogic/SearchPageLogicFrontend.ts similarity index 100% rename from src/api/SearchPageLogicFrontend.ts rename to Frontend/src/FrontendLogic/SearchPageLogicFrontend.ts diff --git a/src/api/UserLogicFrontend.ts b/Frontend/src/FrontendLogic/UserLogicFrontend.ts similarity index 97% rename from src/api/UserLogicFrontend.ts rename to Frontend/src/FrontendLogic/UserLogicFrontend.ts index 176582f..e5b6f7e 100644 --- a/src/api/UserLogicFrontend.ts +++ b/Frontend/src/FrontendLogic/UserLogicFrontend.ts @@ -1,5 +1,5 @@ import { Gql } from './LogicConnection' -import type { User } from '../backend/UserLogicBackend' +import type { User } from '../../../Backend/logic/UserLogicBackend' const USER_FIELDS = 'id email username token' diff --git a/src/api/WishlistLogicFrontend.ts b/Frontend/src/FrontendLogic/WishlistLogicFrontend.ts similarity index 97% rename from src/api/WishlistLogicFrontend.ts rename to Frontend/src/FrontendLogic/WishlistLogicFrontend.ts index 473b319..c7b92f2 100644 --- a/src/api/WishlistLogicFrontend.ts +++ b/Frontend/src/FrontendLogic/WishlistLogicFrontend.ts @@ -1,4 +1,4 @@ -import { type Game } from '../backend/Steam' +import { type Game } from '../../../Backend/Steam' import { Gql } from './LogicConnection' // Frontend function for calling backend to fetch wishlisted games of a user diff --git a/src/assets/LogoGameStartTransparent.png b/Frontend/src/assets/LogoGameStartTransparent.png similarity index 100% rename from src/assets/LogoGameStartTransparent.png rename to Frontend/src/assets/LogoGameStartTransparent.png diff --git a/src/assets/maskot_gamestart.png b/Frontend/src/assets/maskot_gamestart.png similarity index 100% rename from src/assets/maskot_gamestart.png rename to Frontend/src/assets/maskot_gamestart.png diff --git a/src/assets/maskot_head_gamestart.png b/Frontend/src/assets/maskot_head_gamestart.png similarity index 100% rename from src/assets/maskot_head_gamestart.png rename to Frontend/src/assets/maskot_head_gamestart.png diff --git a/src/components/ActiveGameCard.tsx b/Frontend/src/components/ActiveGameCard.tsx similarity index 97% rename from src/components/ActiveGameCard.tsx rename to Frontend/src/components/ActiveGameCard.tsx index efc8420..a07fc96 100644 --- a/src/components/ActiveGameCard.tsx +++ b/Frontend/src/components/ActiveGameCard.tsx @@ -1,19 +1,19 @@ import { Box, Button, Card, CardContent, CardMedia, Chip, Divider, Typography } from '@mui/material' import '../css/ActiveGameCard.css' import type { GameData } from '../types/Game' -import { GetSavedUser } from '../api/UserLogicFrontend' +import { GetSavedUser } from '../FrontendLogic/UserLogicFrontend' import { useState, useEffect } from 'react' import { RemoveFromWishlistFrontend, AddToWishlistFrontend, FetchWishlistFrontend -} from '../api/WishlistLogicFrontend' +} from '../FrontendLogic/WishlistLogicFrontend' import { CreateReview, DeleteReview, GetReviewsForGame, UpdateReview -} from '../api/ReviewLogicFrontend' +} from '../FrontendLogic/ReviewLogicFrontend' import type { Review } from '../types/Review' import Reviews from './Reviews' import { UseAuth } from '../context/AuthContext' diff --git a/src/components/Footer.tsx b/Frontend/src/components/Footer.tsx similarity index 100% rename from src/components/Footer.tsx rename to Frontend/src/components/Footer.tsx diff --git a/src/components/Header.tsx b/Frontend/src/components/Header.tsx similarity index 100% rename from src/components/Header.tsx rename to Frontend/src/components/Header.tsx diff --git a/src/components/PageHeading.tsx b/Frontend/src/components/PageHeading.tsx similarity index 100% rename from src/components/PageHeading.tsx rename to Frontend/src/components/PageHeading.tsx diff --git a/src/components/PassiveGameCard.tsx b/Frontend/src/components/PassiveGameCard.tsx similarity index 100% rename from src/components/PassiveGameCard.tsx rename to Frontend/src/components/PassiveGameCard.tsx diff --git a/src/components/ProfileCard.tsx b/Frontend/src/components/ProfileCard.tsx similarity index 90% rename from src/components/ProfileCard.tsx rename to Frontend/src/components/ProfileCard.tsx index 43194f9..977c20a 100644 --- a/src/components/ProfileCard.tsx +++ b/Frontend/src/components/ProfileCard.tsx @@ -1,6 +1,6 @@ import { Avatar, Card, CardContent, Divider } from '@mui/material' -import type { Profile } from '../backend/ProfileLogicBackend' -import type { GameOption } from '../api/ProfileLogicFrontend' +import type { Profile } from '../../../Backend/logic/ProfileLogicBackend' +import type { GameOption } from '../FrontendLogic/ProfileLogicFrontend' interface ProfileCardProps { profile: Profile diff --git a/src/components/ProfileEditor.tsx b/Frontend/src/components/ProfileEditor.tsx similarity index 97% rename from src/components/ProfileEditor.tsx rename to Frontend/src/components/ProfileEditor.tsx index 9287ac1..b45fd97 100644 --- a/src/components/ProfileEditor.tsx +++ b/Frontend/src/components/ProfileEditor.tsx @@ -10,8 +10,8 @@ import { MenuItem, TextField } from '@mui/material' -import { type GameOption } from '../api/ProfileLogicFrontend' -import type { Profile } from '../backend/ProfileLogicBackend' +import { type GameOption } from '../FrontendLogic/ProfileLogicFrontend' +import type { Profile } from '../../../Backend/logic/ProfileLogicBackend' import { SETUP_OPTIONS } from '../types/SetupOptions' import '../css/EditProfile.css' diff --git a/src/components/RecentReviews.tsx b/Frontend/src/components/RecentReviews.tsx similarity index 97% rename from src/components/RecentReviews.tsx rename to Frontend/src/components/RecentReviews.tsx index 7c29643..0a84cd1 100644 --- a/src/components/RecentReviews.tsx +++ b/Frontend/src/components/RecentReviews.tsx @@ -3,7 +3,10 @@ import useMediaQuery from '@mui/material/useMediaQuery' import ReviewCard from './ReviewCard' import { Card } from '@mui/material' import '../css/RecentReviews.css' -import { GetSavedRecentReviewCount, SaveRecentReviewCount } from '../api/MainPageLogicFrontend' +import { + GetSavedRecentReviewCount, + SaveRecentReviewCount +} from '../FrontendLogic/MainPageLogicFrontend' import type { Review } from '../types/Review' type RecentReviewsProps = { diff --git a/src/components/ReviewCard.tsx b/Frontend/src/components/ReviewCard.tsx similarity index 100% rename from src/components/ReviewCard.tsx rename to Frontend/src/components/ReviewCard.tsx diff --git a/src/components/Reviews.tsx b/Frontend/src/components/Reviews.tsx similarity index 100% rename from src/components/Reviews.tsx rename to Frontend/src/components/Reviews.tsx diff --git a/src/components/WishlistGameCard.tsx b/Frontend/src/components/WishlistGameCard.tsx similarity index 94% rename from src/components/WishlistGameCard.tsx rename to Frontend/src/components/WishlistGameCard.tsx index e5e7e75..1cc3aa0 100644 --- a/src/components/WishlistGameCard.tsx +++ b/Frontend/src/components/WishlistGameCard.tsx @@ -1,8 +1,8 @@ import { Card, CardMedia, Chip, Button } from '@mui/material' import '../css/WishlistGameCard.css' import type { PassiveGameData } from '../types/Game' -import { RemoveFromWishlistFrontend } from '../api/WishlistLogicFrontend' -import { GetSavedUser } from '../api/UserLogicFrontend' +import { RemoveFromWishlistFrontend } from '../FrontendLogic/WishlistLogicFrontend' +import { GetSavedUser } from '../FrontendLogic/UserLogicFrontend' import { useState } from 'react' import { Link } from 'react-router-dom' diff --git a/src/context/AuthContext.tsx b/Frontend/src/context/AuthContext.tsx similarity index 85% rename from src/context/AuthContext.tsx rename to Frontend/src/context/AuthContext.tsx index a18abe7..91333d7 100644 --- a/src/context/AuthContext.tsx +++ b/Frontend/src/context/AuthContext.tsx @@ -1,6 +1,6 @@ import * as React from 'react' -import type { User } from '../backend/UserLogicBackend' -import { SaveUser, ClearUser, GetSavedUser } from '../api/UserLogicFrontend' +import type { User } from '../../../Backend/logic/UserLogicBackend' +import { SaveUser, ClearUser, GetSavedUser } from '../FrontendLogic/UserLogicFrontend' interface AuthContextValue { user: User | null diff --git a/src/css/ActiveGameCard.css b/Frontend/src/css/ActiveGameCard.css similarity index 100% rename from src/css/ActiveGameCard.css rename to Frontend/src/css/ActiveGameCard.css diff --git a/src/css/CreateAccountPage.css b/Frontend/src/css/CreateAccountPage.css similarity index 100% rename from src/css/CreateAccountPage.css rename to Frontend/src/css/CreateAccountPage.css diff --git a/src/css/EditProfile.css b/Frontend/src/css/EditProfile.css similarity index 100% rename from src/css/EditProfile.css rename to Frontend/src/css/EditProfile.css diff --git a/src/css/Footer.css b/Frontend/src/css/Footer.css similarity index 100% rename from src/css/Footer.css rename to Frontend/src/css/Footer.css diff --git a/src/css/FriendsPage.css b/Frontend/src/css/FriendsPage.css similarity index 100% rename from src/css/FriendsPage.css rename to Frontend/src/css/FriendsPage.css diff --git a/src/css/GamePage.css b/Frontend/src/css/GamePage.css similarity index 100% rename from src/css/GamePage.css rename to Frontend/src/css/GamePage.css diff --git a/src/css/Global.css b/Frontend/src/css/Global.css similarity index 100% rename from src/css/Global.css rename to Frontend/src/css/Global.css diff --git a/src/css/Header.css b/Frontend/src/css/Header.css similarity index 100% rename from src/css/Header.css rename to Frontend/src/css/Header.css diff --git a/src/css/LoginPage.css b/Frontend/src/css/LoginPage.css similarity index 100% rename from src/css/LoginPage.css rename to Frontend/src/css/LoginPage.css diff --git a/src/css/MainPage.css b/Frontend/src/css/MainPage.css similarity index 100% rename from src/css/MainPage.css rename to Frontend/src/css/MainPage.css diff --git a/src/css/PageHeading.css b/Frontend/src/css/PageHeading.css similarity index 100% rename from src/css/PageHeading.css rename to Frontend/src/css/PageHeading.css diff --git a/src/css/PassiveGameCard.css b/Frontend/src/css/PassiveGameCard.css similarity index 100% rename from src/css/PassiveGameCard.css rename to Frontend/src/css/PassiveGameCard.css diff --git a/src/css/ProfilePage.css b/Frontend/src/css/ProfilePage.css similarity index 100% rename from src/css/ProfilePage.css rename to Frontend/src/css/ProfilePage.css diff --git a/src/css/RecentReviews.css b/Frontend/src/css/RecentReviews.css similarity index 100% rename from src/css/RecentReviews.css rename to Frontend/src/css/RecentReviews.css diff --git a/src/css/Reviews.css b/Frontend/src/css/Reviews.css similarity index 100% rename from src/css/Reviews.css rename to Frontend/src/css/Reviews.css diff --git a/src/css/SearchPage.css b/Frontend/src/css/SearchPage.css similarity index 100% rename from src/css/SearchPage.css rename to Frontend/src/css/SearchPage.css diff --git a/src/css/SignIn&UpCard.css b/Frontend/src/css/SignInUpCard.css similarity index 100% rename from src/css/SignIn&UpCard.css rename to Frontend/src/css/SignInUpCard.css diff --git a/src/css/WishlistGameCard.css b/Frontend/src/css/WishlistGameCard.css similarity index 100% rename from src/css/WishlistGameCard.css rename to Frontend/src/css/WishlistGameCard.css diff --git a/src/css/WishlistPage.css b/Frontend/src/css/WishlistPage.css similarity index 100% rename from src/css/WishlistPage.css rename to Frontend/src/css/WishlistPage.css diff --git a/src/pages/FriendsPage.tsx b/Frontend/src/pages/FriendsPage.tsx similarity index 98% rename from src/pages/FriendsPage.tsx rename to Frontend/src/pages/FriendsPage.tsx index c4bc775..1811995 100644 --- a/src/pages/FriendsPage.tsx +++ b/Frontend/src/pages/FriendsPage.tsx @@ -14,18 +14,18 @@ import { import { useEffect, useMemo, useState } from 'react' import '../css/FriendsPage.css' import WishlistGameCard from '../components/WishlistGameCard' -import type { Game } from '../backend/Steam' +import type { Game } from '../../../Backend/Steam' import { AddFriend, GetFriends, GetFriendWishlist, GetMyFriendId, type Friend -} from '../api/FriendClient' +} from '../FrontendLogic/FriendLogicFrontend' import { UseAuth } from '../context/AuthContext' import ProfileCard from '../components/ProfileCard' -import type { Profile } from '../backend/ProfileLogicBackend' -import type { GameOption } from '../api/ProfileLogicFrontend' +import type { Profile } from '../../../Backend/logic/ProfileLogicBackend' +import type { GameOption } from '../FrontendLogic/ProfileLogicFrontend' const GAMES_PER_PAGE = 5 const NO_GAMES: Game[] = [] diff --git a/src/pages/GamePage.tsx b/Frontend/src/pages/GamePage.tsx similarity index 96% rename from src/pages/GamePage.tsx rename to Frontend/src/pages/GamePage.tsx index 074ef19..d1f426a 100644 --- a/src/pages/GamePage.tsx +++ b/Frontend/src/pages/GamePage.tsx @@ -5,8 +5,8 @@ import ActiveGameCard from '../components/ActiveGameCard' import { useParams } from 'react-router-dom' import PageHeading from '../components/PageHeading' import { useEffect, useState } from 'react' -import { GetGameById } from '../api/GameClient' -import type { Game } from '../backend/Steam' +import { GetGameById } from '../FrontendLogic/GameLogicFrontend' +import type { Game } from '../../../Backend/Steam' export default function GamePage() { const { gameId } = useParams() diff --git a/src/pages/MainPage.tsx b/Frontend/src/pages/MainPage.tsx similarity index 98% rename from src/pages/MainPage.tsx rename to Frontend/src/pages/MainPage.tsx index 50e51c5..3eab6a9 100644 --- a/src/pages/MainPage.tsx +++ b/Frontend/src/pages/MainPage.tsx @@ -10,16 +10,16 @@ import { SaveMainPageGenre, PrepareDiscountGenreSearch, type MainPageGenreSection -} from '../api/MainPageLogicFrontend' -import { FetchGenres } from '../api/SearchPageLogicFrontend' -import type { Game } from '../backend/Steam' +} from '../FrontendLogic/MainPageLogicFrontend' +import { FetchGenres } from '../FrontendLogic/SearchPageLogicFrontend' +import type { Game } from '../../../Backend/Steam' import { Link } from 'react-router-dom' import type { CSSProperties } from 'react' import { useEffect, useState } from 'react' import { Autocomplete, Button, TextField, useMediaQuery, Card } from '@mui/material' import maskot_gamestart from '../assets/maskot_gamestart.png' import RecentReviews from '../components/RecentReviews' -import { GetRecentReviews } from '../api/ReviewLogicFrontend' +import { GetRecentReviews } from '../FrontendLogic/ReviewLogicFrontend' import type { Review } from '../types/Review' type PageGame = { diff --git a/src/pages/ProfilePage.tsx b/Frontend/src/pages/ProfilePage.tsx similarity index 94% rename from src/pages/ProfilePage.tsx rename to Frontend/src/pages/ProfilePage.tsx index 8552ecd..a2f55b7 100644 --- a/src/pages/ProfilePage.tsx +++ b/Frontend/src/pages/ProfilePage.tsx @@ -2,16 +2,16 @@ import Footer from '../components/Footer' import Header from '../components/Header' import '../css/ProfilePage.css' import PageHeading from '../components/PageHeading' -import ProfileCard from '../components/ProfileCard' +import ProfileCard from '../components/ProfileCard.tsx' import { useEffect, useState } from 'react' -import { UseAuth } from '../context/AuthContext' +import { UseAuth } from '../context/AuthContext.tsx' import { FetchGameOptions, FetchProfileFrontend, UpdateProfileFrontend, type GameOption -} from '../api/ProfileLogicFrontend.ts' -import type { Profile } from '../backend/ProfileLogicBackend.ts' +} from '../FrontendLogic/ProfileLogicFrontend.ts' +import type { Profile } from '../../../Backend/logic/ProfileLogicBackend.ts' import ProfileEditor from '../components/ProfileEditor.tsx' import { Button, Card, CardContent } from '@mui/material' diff --git a/src/pages/SearchPage.tsx b/Frontend/src/pages/SearchPage.tsx similarity index 98% rename from src/pages/SearchPage.tsx rename to Frontend/src/pages/SearchPage.tsx index 4f6edee..13aac0c 100644 --- a/src/pages/SearchPage.tsx +++ b/Frontend/src/pages/SearchPage.tsx @@ -15,7 +15,7 @@ import { Funnel, SearchIcon } from 'lucide-react' import { useState, useEffect, useMemo } from 'react' import PageHeading from '../components/PageHeading' import PassiveGameCard from '../components/PassiveGameCard' -import FetchGamesFromDB, { GetTotalGameCount } from '../api/GameClient' +import FetchGamesFromDB, { GetTotalGameCount } from '../FrontendLogic/GameLogicFrontend' import { FetchGenres, GetSavedFiltersSearchpage, @@ -26,8 +26,8 @@ import { SaveSortOptionSearchpage, GetPageSearchpage, SavePageSearchpage -} from '../api/SearchPageLogicFrontend' -import type { Game } from '../backend/Steam' +} from '../FrontendLogic/SearchPageLogicFrontend' +import type { Game } from '../../../Backend/Steam' import type { SelectChangeEvent } from '@mui/material' type SortOption = '' | 'discount-desc' | 'price-asc' | 'price-desc' | 'title-asc' diff --git a/src/pages/SignIn.tsx b/Frontend/src/pages/SignIn.tsx similarity index 98% rename from src/pages/SignIn.tsx rename to Frontend/src/pages/SignIn.tsx index 2cca474..b099149 100644 --- a/src/pages/SignIn.tsx +++ b/Frontend/src/pages/SignIn.tsx @@ -18,9 +18,9 @@ import Header from '../components/Header' import Footer from '../components/Footer' import { useNavigate } from 'react-router-dom' import PageHeading from '../components/PageHeading' -import { SigninUser } from '../api/UserLogicFrontend' +import { SigninUser } from '../FrontendLogic/UserLogicFrontend' import { UseAuth } from '../context/AuthContext' -import '../css/SignIn&UpCard.css' +import '../css/SignInUpCard.css' export default function SignInPage() { const [EmailError, SetEmailError] = React.useState(false) diff --git a/src/pages/SignUp.tsx b/Frontend/src/pages/SignUp.tsx similarity index 98% rename from src/pages/SignUp.tsx rename to Frontend/src/pages/SignUp.tsx index fc642d7..37368e1 100644 --- a/src/pages/SignUp.tsx +++ b/Frontend/src/pages/SignUp.tsx @@ -15,9 +15,9 @@ import { useNavigate } from 'react-router-dom' import Header from '../components/Header' import Footer from '../components/Footer' import PageHeading from '../components/PageHeading' -import { SignupUser } from '../api/UserLogicFrontend' +import { SignupUser } from '../FrontendLogic/UserLogicFrontend' import { UseAuth } from '../context/AuthContext' -import '../css/SignIn&UpCard.css' +import '../css/SignInUpCard.css' export default function SignUpPage() { const [UsernameError, SetUsernameError] = React.useState(false) diff --git a/src/pages/WishlistPage.tsx b/Frontend/src/pages/WishlistPage.tsx similarity index 98% rename from src/pages/WishlistPage.tsx rename to Frontend/src/pages/WishlistPage.tsx index 9855f1c..facecec 100644 --- a/src/pages/WishlistPage.tsx +++ b/Frontend/src/pages/WishlistPage.tsx @@ -5,14 +5,14 @@ import WishlistGameCard from '../components/WishlistGameCard' import { FormControl, Select, MenuItem, InputLabel, Pagination, Card } from '@mui/material' import type { SelectChangeEvent } from '@mui/material' import { useState, useEffect, useMemo } from 'react' -import type { Game } from '../backend/Steam' +import type { Game } from '../../../Backend/Steam' import PageHeading from '../components/PageHeading' import { FetchWishlistFrontend, SaveSortOptionWishlist, GetSortOptionWishlist -} from '../api/WishlistLogicFrontend' +} from '../FrontendLogic/WishlistLogicFrontend' import { UseAuth } from '../context/AuthContext' type SortOption = 'discount-desc' | 'discount-asc' | 'price-asc' | 'price-desc' | 'title-asc' diff --git a/src/tests/FriendPage.test.tsx b/Frontend/src/tests/FriendsPage.test.tsx similarity index 99% rename from src/tests/FriendPage.test.tsx rename to Frontend/src/tests/FriendsPage.test.tsx index 76216ef..553771a 100644 --- a/src/tests/FriendPage.test.tsx +++ b/Frontend/src/tests/FriendsPage.test.tsx @@ -1,8 +1,8 @@ // @vitest-environment jsdom import { act, cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' -import type { Friend } from '../api/FriendClient' -import type { Game } from '../backend/Steam' +import type { Friend } from '../FrontendLogic/FriendLogicFrontend' +import type { Game } from '../../../Backend/Steam' type FriendOption = { id: number | string; username: string } @@ -14,7 +14,7 @@ const { AddFriend, GetFriends, GetFriendWishlist, GetMyFriendId, UseAuth } = vi. UseAuth: vi.fn<() => { user: { id: number } | null }>() })) -vi.mock('../api/FriendClient', () => ({ +vi.mock('../FrontendLogic/FriendLogicFrontend', () => ({ AddFriend, GetFriends, GetFriendWishlist, diff --git a/src/tests/GameClient.test.ts b/Frontend/src/tests/GameLogicFrontend.test.ts similarity index 97% rename from src/tests/GameClient.test.ts rename to Frontend/src/tests/GameLogicFrontend.test.ts index 1fefe54..f8b72dc 100644 --- a/src/tests/GameClient.test.ts +++ b/Frontend/src/tests/GameLogicFrontend.test.ts @@ -1,7 +1,10 @@ import { describe, it, expect, vi, beforeEach } from 'vitest' -import type { Game } from '../backend/Steam' -import FetchGamesFromDB, { GetGameById, GetTotalGameCount } from '../api/GameClient' -import { BACKEND_URL } from '../api/LogicConnection' +import type { Game } from '../../../Backend/Steam' +import FetchGamesFromDB, { + GetGameById, + GetTotalGameCount +} from '../FrontendLogic/GameLogicFrontend' +import { BACKEND_URL } from '../FrontendLogic/LogicConnection' function MockResponse(body: unknown, ok = true, status = 200): Response { return { diff --git a/src/tests/GamePage.test.tsx b/Frontend/src/tests/GamePage.test.tsx similarity index 96% rename from src/tests/GamePage.test.tsx rename to Frontend/src/tests/GamePage.test.tsx index a5d62b7..d9c9b15 100644 --- a/src/tests/GamePage.test.tsx +++ b/Frontend/src/tests/GamePage.test.tsx @@ -2,14 +2,14 @@ import { cleanup, render, screen } from '@testing-library/react' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { MemoryRouter, Route, Routes } from 'react-router-dom' -import type { Game } from '../backend/Steam' +import type { Game } from '../../../Backend/Steam' const { GetGameById, FetchGamesFromDB } = vi.hoisted(() => ({ GetGameById: vi.fn<(appid: number) => Promise>(), FetchGamesFromDB: vi.fn() })) -vi.mock('../api/GameClient', () => ({ +vi.mock('../FrontendLogic/GameLogicFrontend', () => ({ GetGameById, default: FetchGamesFromDB })) diff --git a/src/tests/MainPage.test.tsx b/Frontend/src/tests/MainPage.test.tsx similarity index 97% rename from src/tests/MainPage.test.tsx rename to Frontend/src/tests/MainPage.test.tsx index 5ae7357..3cf3cb0 100644 --- a/src/tests/MainPage.test.tsx +++ b/Frontend/src/tests/MainPage.test.tsx @@ -2,7 +2,7 @@ import { act, cleanup, fireEvent, render, screen, within } from '@testing-library/react' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { MemoryRouter } from 'react-router-dom' -import type { Game } from '../backend/Steam' +import type { Game } from '../../../Backend/Steam' import { AuthProvider } from '../context/AuthContext' import type { Review } from '../types/Review' @@ -23,12 +23,12 @@ const { })) // Keep the real sessionStorage helpers, replace only the network loaders -vi.mock('../api/MainPageLogicFrontend', async (importOriginal) => { - const actual = await importOriginal() +vi.mock('../FrontendLogic/MainPageLogicFrontend', async (importOriginal) => { + const actual = await importOriginal() return { ...actual, LoadTop3Games, LoadTopGamesByGenre, LoadDiscountedGames } }) -vi.mock('../api/SearchPageLogicFrontend', () => ({ FetchGenres })) -vi.mock('../api/ReviewLogicFrontend', () => ({ GetRecentReviews })) +vi.mock('../FrontendLogic/SearchPageLogicFrontend', () => ({ FetchGenres })) +vi.mock('../FrontendLogic/ReviewLogicFrontend', () => ({ GetRecentReviews })) vi.mock('@mui/material', async (importOriginal) => { const actual = await importOriginal() return { diff --git a/src/tests/PageHeading.test.tsx b/Frontend/src/tests/PageHeading.test.tsx similarity index 100% rename from src/tests/PageHeading.test.tsx rename to Frontend/src/tests/PageHeading.test.tsx diff --git a/src/tests/PassiveGameCard.test.tsx b/Frontend/src/tests/PassiveGameCard.test.tsx similarity index 100% rename from src/tests/PassiveGameCard.test.tsx rename to Frontend/src/tests/PassiveGameCard.test.tsx diff --git a/src/tests/ProfileLogicFrontend.test.ts b/Frontend/src/tests/ProfileLogicFrontend.test.ts similarity index 95% rename from src/tests/ProfileLogicFrontend.test.ts rename to Frontend/src/tests/ProfileLogicFrontend.test.ts index cd1d470..bc9ef60 100644 --- a/src/tests/ProfileLogicFrontend.test.ts +++ b/Frontend/src/tests/ProfileLogicFrontend.test.ts @@ -1,13 +1,13 @@ import { describe, it, expect, vi, beforeEach } from 'vitest' -vi.mock('../api/LogicConnection', () => ({ Gql: vi.fn() })) +vi.mock('../FrontendLogic/LogicConnection', () => ({ Gql: vi.fn() })) -import { Gql } from '../api/LogicConnection' +import { Gql } from '../FrontendLogic/LogicConnection' import { FetchProfileFrontend, FetchGameOptions, UpdateProfileFrontend -} from '../api/ProfileLogicFrontend' +} from '../FrontendLogic/ProfileLogicFrontend' const GqlMock = Gql as unknown as ReturnType diff --git a/src/tests/RecentReviews.test.tsx b/Frontend/src/tests/RecentReviews.test.tsx similarity index 100% rename from src/tests/RecentReviews.test.tsx rename to Frontend/src/tests/RecentReviews.test.tsx diff --git a/src/tests/ReviewCard.test.tsx b/Frontend/src/tests/ReviewCard.test.tsx similarity index 100% rename from src/tests/ReviewCard.test.tsx rename to Frontend/src/tests/ReviewCard.test.tsx diff --git a/src/tests/ReviewLogicFrontend.test.ts b/Frontend/src/tests/ReviewLogicFrontend.test.ts similarity index 97% rename from src/tests/ReviewLogicFrontend.test.ts rename to Frontend/src/tests/ReviewLogicFrontend.test.ts index 9cf737b..b35d627 100644 --- a/src/tests/ReviewLogicFrontend.test.ts +++ b/Frontend/src/tests/ReviewLogicFrontend.test.ts @@ -5,7 +5,7 @@ import type { Review } from '../types/Review' const { Gql } = vi.hoisted(() => ({ Gql: vi.fn() })) // Replace the backend call, so no server is needed -vi.mock('../api/LogicConnection', () => ({ Gql })) +vi.mock('../FrontendLogic/LogicConnection', () => ({ Gql })) import { CreateReview, @@ -13,7 +13,7 @@ import { GetRecentReviews, GetReviewsForGame, UpdateReview -} from '../api/ReviewLogicFrontend' +} from '../FrontendLogic/ReviewLogicFrontend' const testReview: Review = { id: 1, diff --git a/src/tests/SearchPage.test.tsx b/Frontend/src/tests/SearchPage.test.tsx similarity index 97% rename from src/tests/SearchPage.test.tsx rename to Frontend/src/tests/SearchPage.test.tsx index 9fd6025..fe49923 100644 --- a/src/tests/SearchPage.test.tsx +++ b/Frontend/src/tests/SearchPage.test.tsx @@ -2,7 +2,7 @@ import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { MemoryRouter } from 'react-router-dom' -import type { Game } from '../backend/Steam' +import type { Game } from '../../../Backend/Steam' const { FetchGamesFromDB, GetTotalGameCount, FetchGenres } = vi.hoisted(() => ({ FetchGamesFromDB: @@ -17,11 +17,14 @@ const { FetchGamesFromDB, GetTotalGameCount, FetchGenres } = vi.hoisted(() => ({ FetchGenres: vi.fn<() => Promise>() })) -vi.mock('../api/GameClient', () => ({ default: FetchGamesFromDB, GetTotalGameCount })) +vi.mock('../FrontendLogic/GameLogicFrontend', () => ({ + default: FetchGamesFromDB, + GetTotalGameCount +})) -vi.mock('../api/SearchPageLogicFrontend', async () => { - const actual = await vi.importActual( - '../api/SearchPageLogicFrontend' +vi.mock('../FrontendLogic/SearchPageLogicFrontend', async () => { + const actual = await vi.importActual( + '../FrontendLogic/SearchPageLogicFrontend' ) return { diff --git a/src/tests/TokenFrontend.test.ts b/Frontend/src/tests/TokenFrontend.test.ts similarity index 96% rename from src/tests/TokenFrontend.test.ts rename to Frontend/src/tests/TokenFrontend.test.ts index 17a6f3b..2c0ec99 100644 --- a/src/tests/TokenFrontend.test.ts +++ b/Frontend/src/tests/TokenFrontend.test.ts @@ -1,9 +1,9 @@ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest' // LogicConnection is NOT mocked here: these tests check what Gql really sends -import { Gql, BACKEND_URL } from '../api/LogicConnection' -import { SaveUser, GetSavedUser, ClearUser } from '../api/UserLogicFrontend' -import type { User } from '../backend/UserLogicBackend' +import { Gql, BACKEND_URL } from '../FrontendLogic/LogicConnection' +import { SaveUser, GetSavedUser, ClearUser } from '../FrontendLogic/UserLogicFrontend' +import type { User } from '../../../Backend/logic/UserLogicBackend' const KEY = 'GameStart:user' const user: User = { diff --git a/src/tests/UserLogicFrontend.test.ts b/Frontend/src/tests/UserLogicFrontend.test.ts similarity index 95% rename from src/tests/UserLogicFrontend.test.ts rename to Frontend/src/tests/UserLogicFrontend.test.ts index f2cd928..b0cc3bf 100644 --- a/src/tests/UserLogicFrontend.test.ts +++ b/Frontend/src/tests/UserLogicFrontend.test.ts @@ -1,11 +1,17 @@ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest' -vi.mock('../api/LogicConnection', () => ({ Gql: vi.fn() })) - -import { Gql } from '../api/LogicConnection' -import { SigninUser, SignupUser, SaveUser, GetSavedUser, ClearUser } from '../api/UserLogicFrontend' - -import type { User } from '../backend/UserLogicBackend' +vi.mock('../FrontendLogic/LogicConnection', () => ({ Gql: vi.fn() })) + +import { Gql } from '../FrontendLogic/LogicConnection' +import { + SigninUser, + SignupUser, + SaveUser, + GetSavedUser, + ClearUser +} from '../FrontendLogic/UserLogicFrontend' + +import type { User } from '../../../Backend/logic/UserLogicBackend' const GqlMock = Gql as unknown as ReturnType diff --git a/src/tests/WishlistPage.test.tsx b/Frontend/src/tests/WishlistPage.test.tsx similarity index 99% rename from src/tests/WishlistPage.test.tsx rename to Frontend/src/tests/WishlistPage.test.tsx index e2eadf7..5e8dd41 100644 --- a/src/tests/WishlistPage.test.tsx +++ b/Frontend/src/tests/WishlistPage.test.tsx @@ -6,7 +6,7 @@ import { FetchWishlistFrontend, RemoveFromWishlistFrontend, ToggleGameWishlist -} from '../api/WishlistLogicFrontend' +} from '../FrontendLogic/WishlistLogicFrontend' const FetchMock = vi.fn() diff --git a/src/types/Game.ts b/Frontend/src/types/Game.ts similarity index 100% rename from src/types/Game.ts rename to Frontend/src/types/Game.ts diff --git a/src/types/Review.ts b/Frontend/src/types/Review.ts similarity index 100% rename from src/types/Review.ts rename to Frontend/src/types/Review.ts diff --git a/src/types/SetupOptions.ts b/Frontend/src/types/SetupOptions.ts similarity index 100% rename from src/types/SetupOptions.ts rename to Frontend/src/types/SetupOptions.ts diff --git a/README.md b/README.md index a3ba87b..3ae4792 100644 --- a/README.md +++ b/README.md @@ -72,8 +72,8 @@ You can also access application through the virtual machine by using this link: ```text src ┣ api - ┃ ┣ FriendClient.ts - ┃ ┣ GameClient.ts + ┃ ┣ FriendLogicFrontend.ts + ┃ ┣ GameLogicFrontend.ts ┃ ┣ LogicConnection.ts ┃ ┣ MainPageLogicFrontend.ts ┃ ┣ ProfileLogicFrontend.ts @@ -86,17 +86,17 @@ src ┃ ┣ maskot_gamestart.png ┃ ┗ maskot_head_gamestart.png ┣ backend - ┃ ┣ DbConnection.ts + ┃ ┣ DatabaseConnection.ts ┃ ┣ FriendLogicBackend.ts ┃ ┣ GameColumns.ts ┃ ┣ Genres.ts - ┃ ┣ MainpageLogicBackend.ts + ┃ ┣ MainPageLogicBackend.ts ┃ ┣ ProfileLogicBackend.ts ┃ ┣ Reviews.ts ┃ ┣ Server.ts ┃ ┣ Steam.ts ┃ ┣ UserLogicBackend.ts - ┃ ┗ WishlistLogic.ts + ┃ ┗ WishlistLogicBackend.ts ┣ components ┃ ┣ ActiveGameCard.tsx ┃ ┣ Footer.tsx @@ -128,7 +128,7 @@ src ┃ ┣ RecentReviews.css ┃ ┣ Reviews.css ┃ ┣ SearchPage.css - ┃ ┣ SignIn&UpCard.css + ┃ ┣ SignInUpCard.css ┃ ┣ WishlistGameCard.css ┃ ┗ WishlistPage.css ┣ database @@ -147,12 +147,12 @@ src ┃ ┣ SignUp.tsx ┃ ┗ WishlistPage.tsx ┣ tests - ┃ ┣ FriendPage.test.tsx - ┃ ┣ GameClient.test.ts + ┃ ┣ FriendsPage.test.tsx + ┃ ┣ GameLogicFrontend.test.ts ┃ ┣ GamePage.test.tsx - ┃ ┣ Genres.test.ts + ┃ ┣ GenresBackend.test.ts ┃ ┣ MainPage.test.tsx - ┃ ┣ MainpageBackend.test.ts + ┃ ┣ MainPageBackend.test.ts ┃ ┣ PageHeading.test.tsx ┃ ┣ PassiveGameCard.test.tsx ┃ ┣ ProfileLogicBackend.test.ts @@ -163,7 +163,7 @@ src ┃ ┣ ReviewsBackend.test.ts ┃ ┣ SearchPage.test.tsx ┃ ┣ Setup.ts - ┃ ┣ Steam.test.ts + ┃ ┣ SteamBackend.test.ts ┃ ┣ TokenBackend.test.ts ┃ ┣ TokenFrontend.test.ts ┃ ┣ UserLogicBackend.test.ts diff --git a/package.json b/package.json index be239bd..8ece4c7 100644 --- a/package.json +++ b/package.json @@ -8,8 +8,8 @@ "build": "tsc -b && vite build", "lint": "eslint .", "preview": "vite preview", - "format": "prettier --write \"src/**/*.{ts,tsx,css,md}\"", - "format:check": "prettier --check \"src/**/*.{ts,tsx,css,md}\"", + "format": "prettier --write \"Frontend/**/*.{ts,tsx,css,md}\" \"Backend/**/*.{ts,tsx,css,md}\" \"Database/**/*.{ts,tsx,css,md}\"", + "format:check": "prettier --check \"Frontend/**/*.{ts,tsx,css,md}\" \"Backend/**/*.{ts,tsx,css,md}\" \"Database/**/*.{ts,tsx,css,md}\"", "test": "vitest", "test:ui": "vitest --ui", "test:coverage": "vitest run --coverage", diff --git a/src/backend/Server.ts b/src/backend/Server.ts deleted file mode 100644 index 5f2b56b..0000000 --- a/src/backend/Server.ts +++ /dev/null @@ -1,410 +0,0 @@ -import { ApolloServer } from '@apollo/server' -import { startStandaloneServer } from '@apollo/server/standalone' -import { GraphQLError } from 'graphql' -import { type Game } from './Steam.ts' -import { pool } from './DbConnection.ts' -import { FetchGenres } from './Genres.ts' -import { GAME_COLUMNS } from './GameColumns.ts' -import { RequireUser, Signin, Signup } from './UserLogicBackend.ts' -import { AddToWishlist, RemoveFromWishlist } from './WishlistLogic.ts' -import { FetchProfile, UpdateProfile, type ProfileInput } from './ProfileLogicBackend.ts' -import { - FetchTop3Games, - FetchTop10GamesPerGenre, - FetchDiscountedGames -} from './MainpageLogicBackend.ts' -import { - CreateReview, - DeleteReview, - GetRecentReviews, - GetReviewsForGame, - UpdateReview -} from './Reviews.ts' -import { AddFriend, AreFriends, GetFriendId, GetFriends } from './FriendLogicBackend.ts' - -// Documentation on apollo: https://www.apollographql.com/docs/deploy-preview/5dc6222f824f9e089e2a5735/apollo-server/api/apollo-server - -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' -} - -// Builds the WHERE clause and its parameters for search + genre filters -function BuildGameFilter(search?: string | null, genres?: string[] | null, withRank = false) { - const conditions: string[] = [] - const params: unknown[] = [] - let rank = '' - - if (search?.trim()) { - const term = search.trim() - - params.push(`%${term}%`) - conditions.push(`g.game_name ILIKE $${params.length}`) - - if (withRank) { - const escaped = term.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') - params.push(`^${escaped}\\M`) - const startIdx = params.length - params.push(`\\m${escaped}\\M`) - const wordIdx = params.length - - rank = `CASE - WHEN g.game_name ~* $${startIdx} THEN 0 - WHEN g.game_name ~* $${wordIdx} THEN 1 - ELSE 2 - END` - } - } - - if (genres?.length) { - params.push(genres) - conditions.push( - `NOT EXISTS ( - SELECT 1 FROM unnest($${params.length}::text[]) AS wanted(name) - WHERE NOT EXISTS ( - SELECT 1 FROM genretogame gg - JOIN genres ge ON ge.genre_id = gg.genre_id - WHERE gg.game_id = g.game_id AND ge.genre_name = wanted.name - ) - )` - ) - } - - return { - where: conditions.length ? `WHERE ${conditions.join(' AND ')}` : '', - params, - rank - } -} - -//This is a schema for the server. It tells the shape of the data and which queries exist. -const typeDefs = `#graphql - type Game { - appid: Int! - name: String - price: String - initialprice: String - discount: String - img: String - genre: String - description: String - } - - type User { - id: Int! - email: String! - username: String! - token: String! - } - - type Profile { - username: String! - description: String - setup: String - favouriteGame: Int - } - - type GameOption { - appid: Int! - name: String! - } - - input ProfileInput { - description: String - setup: String - favouriteGame: Int - } - type Review { - id: Int! - gameId: Int! - userId: Int - reviewer: String! - rating: Int! - text: String! - gameName: String - } - - type Friend { - id: Int! - username: String! - description: String - setup: String - favouriteGame: Game - } - - type Query { - games( - limit: Int = 500 - offset: Int = 0 - search: String - genres: [String!] - sort: String - ): [Game!]! - wishlist(userId: Int!): [Game!]! - game(appid: Int!): Game - genres: [String!]! - profile(userId: Int!): Profile - gameOptions: [GameOption!]! - totalGameCount( - search: String - genres: [String!] - ): Int! - top3Games: [Game!]! - topGamesByGenre(genre: String!): [Game!]! - discountedGames(genre: String!): [Game!]! - reviews(gameId: Int!, userId: Int): [Review!]! - recentReviews(limit: Int = 10): [Review!]! - friends(userId: Int!): [Friend!]! - friendWishlist(userId: Int!, friendId: Int!): [Game!]! - myFriendId(userId: Int!): String - } - - type Mutation { - addToWishlist(userId: Int!, gameId: Int!): Boolean! - removeFromWishlist(userId: Int!, gameId: Int!): Boolean! - signup(username: String!, email: String!, password: String!): User! - signin(email: String!, password: String!): User! - updateProfile(userId: Int!, input: ProfileInput!): Profile! - createReview(userId: Int!, gameId: Int!, rating: Int!, text: String!): Review! - updateReview(reviewId: Int!, userId: Int!, rating: Int!, text: String!): Review! - deleteReview(reviewId: Int!, userId: Int!): Boolean! - addFriend(userId: Int!, friendCode: String!): Friend! - } -` - -const resolvers = { - Query: { - games: async ( - _parent: unknown, - { - limit, - offset, - search, - genres, - sort - }: { - limit: number - offset: number - search?: string | null - genres?: string[] | null - sort?: string | null - } - ): Promise => { - const safeLimit = Math.min(Math.max(limit, 0), 500) - const safeOffset = Math.max(offset, 0) - const { where, params, rank } = BuildGameFilter(search, genres, true) - - // game_id as tiebreaker keeps the order stable between pages - const orderBy = [SORT_SQL[sort ?? ''], rank, 'g.game_id'].filter(Boolean).join(', ') - - const { rows } = await pool.query( - `SELECT ${GAME_COLUMNS} - FROM games g - ${where} - ORDER BY ${orderBy} - LIMIT $${params.length + 1} OFFSET $${params.length + 2}`, - [...params, safeLimit, safeOffset] - ) - return rows - }, - - totalGameCount: async ( - _parent: unknown, - { search, genres }: { search?: string | null; genres?: string[] | null } - ): Promise => { - const { where, params } = BuildGameFilter(search, genres) - const { rows } = await pool.query( - `SELECT COUNT(*) AS count FROM games g ${where}`, - params - ) - return Number(rows[0].count) - }, - wishlist: async ( - _: unknown, - { userId }: { userId: number }, - { token }: { token: string | null } - ): Promise => { - await RequireUser(token, userId) - - const { rows } = await pool.query( - `SELECT ${GAME_COLUMNS} - FROM wishlist w - JOIN games g ON g.game_id = w.game_id - WHERE w.user_id = $1 - ORDER BY g.game_name`, - [userId] - ) - return rows - }, - game: async (_parent: unknown, { appid }: { appid: number }): Promise => { - const { rows } = await pool.query( - `SELECT ${GAME_COLUMNS} - FROM games g - WHERE g.game_id = $1`, - [appid] - ) - return rows[0] ?? null - }, - genres: () => FetchGenres(), - profile: async ( - _: unknown, - { userId }: { userId: number }, - { token }: { token: string | null } - ) => { - await RequireUser(token, userId) - return FetchProfile(userId) - }, - gameOptions: async () => { - const { rows } = await pool.query( - `SELECT game_id AS appid, game_name AS name FROM games ORDER BY game_name` - ) - return rows - }, - top3Games: () => FetchTop3Games(), - topGamesByGenre: (_: unknown, { genre }: { genre: string }) => - FetchTop10GamesPerGenre(genre), - discountedGames: (_: unknown, { genre }: { genre: string }) => FetchDiscountedGames(genre), - reviews: (_: unknown, { gameId, userId }: { gameId: number; userId?: number | null }) => - GetReviewsForGame(gameId, userId ?? null), - recentReviews: (_: unknown, { limit }: { limit: number }) => GetRecentReviews(limit), - friends: async ( - _: unknown, - { userId }: { userId: number }, - { token }: { token: string | null } - ) => { - await RequireUser(token, userId) - return GetFriends(userId) - }, - friendWishlist: async ( - _: unknown, - { userId, friendId }: { userId: number; friendId: number }, - { token }: { token: string | null } - ): Promise => { - await RequireUser(token, userId) - if (!(await AreFriends(userId, friendId))) { - throw new GraphQLError('You are not friends with this user.', { - extensions: { code: 'FORBIDDEN' } - }) - } - const { rows } = await pool.query( - `SELECT ${GAME_COLUMNS} - FROM wishlist w - JOIN games g ON g.game_id = w.game_id - WHERE w.user_id = $1 - ORDER BY g.game_name`, - [friendId] - ) - return rows - }, - myFriendId: async ( - _: unknown, - { userId }: { userId: number }, - { token }: { token: string | null } - ) => { - await RequireUser(token, userId) - return GetFriendId(userId) - } - }, - Friend: { - favouriteGame: async (friend: { favouriteGameId: number | null }): Promise => { - if (!friend.favouriteGameId) return null - const { rows } = await pool.query( - `SELECT ${GAME_COLUMNS} FROM games g WHERE g.game_id = $1`, - [friend.favouriteGameId] - ) - return rows[0] ?? null - } - }, - Mutation: { - addToWishlist: async ( - _: unknown, - { userId, gameId }: { userId: number; gameId: number }, - { token }: { token: string | null } - ) => { - await RequireUser(token, userId) - await AddToWishlist(userId, gameId) - return true - }, - removeFromWishlist: async ( - _: unknown, - { userId, gameId }: { userId: number; gameId: number }, - { token }: { token: string | null } - ) => { - await RequireUser(token, userId) - await RemoveFromWishlist(userId, gameId) - return true - }, - addFriend: async ( - _: unknown, - { userId, friendCode }: { userId: number; friendCode: string }, - { token }: { token: string | null } - ) => { - await RequireUser(token, userId) - return AddFriend(userId, friendCode) - }, - signup: ( - _parent: unknown, - { username, email, password }: { username: string; email: string; password: string } - ) => Signup(username, email, password), - signin: (_p: unknown, { email, password }: { email: string; password: string }) => - Signin(email, password), - updateProfile: async ( - _: unknown, - { userId, input }: { userId: number; input: ProfileInput }, - { token }: { token: string | null } - ) => { - await RequireUser(token, userId) - return UpdateProfile(userId, input) - }, - createReview: async ( - _: unknown, - { - userId, - gameId, - rating, - text - }: { userId: number; gameId: number; rating: number; text: string }, - { token }: { token: string | null } - ) => { - await RequireUser(token, userId) - return CreateReview(userId, gameId, rating, text) - }, - updateReview: async ( - _: unknown, - { - reviewId, - userId, - rating, - text - }: { reviewId: number; userId: number; rating: number; text: string }, - { token }: { token: string | null } - ) => { - await RequireUser(token, userId) - return UpdateReview(reviewId, userId, rating, text) - }, - deleteReview: async ( - _: unknown, - { reviewId, userId }: { reviewId: number; userId: number }, - { token }: { token: string | null } - ) => { - await RequireUser(token, userId) - await DeleteReview(reviewId, userId) - return true - } - } -} - -// To run the server you can run the command: "npm run server" -const server = new ApolloServer({ typeDefs, resolvers }) -const { url } = await startStandaloneServer(server, { - listen: { host: process.env.HOST ?? '127.0.0.1', port: 3001 }, - context: async ({ req }) => ({ - token: req.headers.authorization?.replace(/^Bearer /, '') ?? null - }) -}) -console.log( - `Database: ${process.env.DB_USER}@${process.env.DB_HOST}:${process.env.DB_PORT}/${process.env.DB_NAME}` -) -console.log(`GraphQL server ready at ${url}`) diff --git a/src/tests/Setup.ts b/src/tests/Setup.ts deleted file mode 100644 index e69de29..0000000 diff --git a/tsconfig.app.json b/tsconfig.app.json index 13f7fe5..d209b68 100644 --- a/tsconfig.app.json +++ b/tsconfig.app.json @@ -22,5 +22,38 @@ "erasableSyntaxOnly": true, "noFallthroughCasesInSwitch": true }, - "include": ["src"] + "include": [ + "src", + "Frontend/src/types", + "Database/CreateTables.ts", + "Database/FetchGamesToJson.ts", + "Database/FillDb.ts", + "Database/FillGenres.ts", + "Frontend/src/pages", + "Frontend/src/tests/FriendsPage.test.tsx", + "Frontend/src/tests/GamePage.test.tsx", + "Frontend/src/tests/MainPage.test.tsx", + "Frontend/src/tests/WishlistPage.test.tsx", + "Frontend/src/tests/UserLogicFrontend.test.ts", + "Frontend/src/tests/TokenFrontend.test.ts", + "Frontend/src/tests/PageHeading.test.tsx", + "Frontend/src/tests/ProfileLogicFrontend.test.ts", + "Frontend/src/tests/PassiveGameCard.test.tsx", + "Frontend/src/tests/ReviewCard.test.tsx", + "Frontend/src/tests/ReviewLogicFrontend.test.ts", + "Frontend/src/tests/SearchPage.test.tsx", + "Frontend/src/context", + "Backend/tests/UserLogicBackend.test.ts", + "Backend/tests/TokenBackend.test.ts", + "Backend/tests/WishlistBackend.test.ts", + "Backend/tests/ReviewsBackend.test.ts", + "Backend/tests/ProfileLogicBackend.test.ts", + "Backend/tests/MainPageBackend.test.ts", + "Frontend/App.tsx", + "Frontend/Main.tsx", + "Frontend/src/tests/RecentReviews.test.tsx", + "Backend/tests/SteamBackend.test.ts", + "Frontend/src/tests/GameLogicFrontend.test.ts", + "Backend/tests/GenresBackend.test.ts" + ] } diff --git a/vite.config.ts b/vite.config.ts index c135210..fee430f 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -4,6 +4,7 @@ import { defineConfig } from 'vite' // https://vite.dev/config/ export default defineConfig({ + root: 'Frontend', base: '/', //base: '/project2/', diff --git a/vitest.config.ts b/vitest.config.ts index 1f2ff6e..ec79072 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -3,7 +3,10 @@ import { defineConfig } from 'vitest/config' export default defineConfig({ test: { environment: 'node', - include: ['src/tests/**/*.test.{ts,tsx}'], + include: [ + 'Frontend/src/tests/**/*.test.{ts,tsx}', + 'Backend/tests/**/*.test.ts', + ], coverage: { provider: 'v8', reporter: ['text'],