Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
958eca2
refactor: move review form into reusable ReviewComposer component
Nov 6, 2026
16686d6
feat: add paginated userReviews and Count queries
Nov 6, 2026
0e7cd71
feat: add your reviews loading function
Nov 6, 2026
69e3623
feat: add collapsing review cards and change stars placement
Nov 6, 2026
9f01f82
feat: add a "your reviews" section to profile page
Nov 6, 2026
1146d31
test: add tests for your reviews changes
Nov 6, 2026
4236c42
fix: stop passive game card flicker on hover
Nov 6, 2026
e0ef4cf
feat: add original game price when discounted
Nov 6, 2026
f9e48c2
test: add tests for original and discounted prices
Nov 6, 2026
a17e064
refactor: move review form into reusable ReviewComposer component
Nov 6, 2026
7d6d1d7
feat: add paginated userReviews and Count queries
Nov 6, 2026
ce7cf40
feat: add your reviews loading function
Nov 6, 2026
fe39f2a
feat: add collapsing review cards and change stars placement
Nov 6, 2026
433e6d3
feat: add a "your reviews" section to profile page
Nov 6, 2026
b743da6
test: add tests for your reviews changes
Nov 6, 2026
96f0d9c
fix: stop passive game card flicker on hover
Nov 6, 2026
4cb77dd
feat: add original game price when discounted
Nov 6, 2026
ef6ffd4
test: add tests for original and discounted prices
Nov 6, 2026
0e5f5e0
Merge branch 'create-your-reviews' of https://git.ntnu.no/IT2810-H26/…
Nov 10, 2026
aa4e125
fix: remove duplicated review composer styles from ActiveGameCard.css
Nov 10, 2026
d384e60
Merge branch 'create-your-reviews' of https://git.ntnu.no/IT2810-H26/…
Nov 10, 2026
d581bc7
Merge branch 'main' of https://git.ntnu.no/IT2810-H26/T10-Project-2 i…
Nov 10, 2026
46a1cd6
Merge branch 'create-your-reviews' of https://git.ntnu.no/IT2810-H26/…
carmennt Nov 10, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Backend/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ export const typeDefs = `#graphql
discountedGames(genre: String!): [Game!]!
reviews(gameId: Int!, userId: Int): [Review!]!
recentReviews(limit: Int = 10): [Review!]!
userReviews(userId: Int!, limit: Int = 6, offset: Int = 0): [Review!]!
userReviewCount(userId: Int!): Int!
friends(userId: Int!): [Friend!]!
friendWishlist(userId: Int!, friendId: Int!): [Game!]!
myFriendId(userId: Int!): String
Expand Down
44 changes: 36 additions & 8 deletions Backend/logic/ReviewsLogicBackend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ function ValidateId(id: number, name: string) {
if (!Number.isSafeInteger(id) || id <= 0) throw new Error(`The ${name} is invalid.`)
}

function ClampLimit(limit: number): number {
return Math.min(Math.max(Math.trunc(limit) || 1, 1), 50)
}

const REVIEWS_WITH_GAME_NAME = `
SELECT ${REVIEW_COLUMNS},
g.game_name AS "gameName"
FROM reviews r
JOIN users u ON u.user_id = r.user_id
JOIN games g ON g.game_id = r.game_id`

export async function GetReviewsForGame(
gameId: number,
currentUserId: number | null = null
Expand All @@ -47,21 +58,38 @@ export async function GetReviewsForGame(
}

export async function GetRecentReviews(limit = 10): Promise<Review[]> {
const safeLimit = Math.min(Math.max(Math.trunc(limit) || 1, 1), 50)

const { rows } = await pool.query(
`SELECT ${REVIEW_COLUMNS},
g.game_name AS "gameName"
FROM reviews r
JOIN users u ON u.user_id = r.user_id
JOIN games g ON g.game_id = r.game_id
`${REVIEWS_WITH_GAME_NAME}
ORDER BY r.review_id DESC
LIMIT $1`,
[safeLimit]
[ClampLimit(limit)]
)
return rows
}

export async function GetReviewsByUser(userId: number, limit = 6, offset = 0): Promise<Review[]> {
ValidateId(userId, 'user ID')
const safeOffset = Math.max(Math.trunc(offset) || 0, 0)

const { rows } = await pool.query(
`${REVIEWS_WITH_GAME_NAME}
WHERE r.user_id = $1
ORDER BY r.review_id DESC
LIMIT $2 OFFSET $3`,
[userId, ClampLimit(limit), safeOffset]
)
return rows
}

export async function CountReviewsByUser(userId: number): Promise<number> {
ValidateId(userId, 'user ID')

const { rows } = await pool.query('SELECT COUNT(*) AS count FROM reviews WHERE user_id = $1', [
userId
])
return Number(rows[0].count)
}

export async function CreateReview(
userId: number,
gameId: number,
Expand Down
12 changes: 11 additions & 1 deletion Backend/resolvers/ReviewResolvers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import {
CountReviewsByUser,
CreateReview,
DeleteReview,
GetRecentReviews,
GetReviewsByUser,
GetReviewsForGame,
UpdateReview
} from '../logic/ReviewsLogicBackend.ts'
Expand All @@ -12,7 +14,15 @@ 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)
recentReviews: (_parent: unknown, { limit }: { limit: number }) => GetRecentReviews(limit),

userReviews: (
_parent: unknown,
{ userId, limit, offset }: { userId: number; limit: number; offset: number }
) => GetReviewsByUser(userId, limit, offset),

userReviewCount: (_parent: unknown, { userId }: { userId: number }) =>
CountReviewsByUser(userId)
}

export const reviewMutationResolvers = {
Expand Down
55 changes: 55 additions & 0 deletions Backend/tests/ReviewsBackend.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ const { Query } = vi.hoisted(() => ({ Query: vi.fn() }))
vi.mock('../DatabaseConnection.ts', () => ({ pool: { query: Query } }))

import {
CountReviewsByUser,
CreateReview,
DeleteReview,
GetRecentReviews,
GetReviewsByUser,
GetReviewsForGame,
UpdateReview
} from '../logic/ReviewsLogicBackend.ts'
Expand Down Expand Up @@ -86,6 +88,59 @@ describe('reviews (backend)', () => {
})
})

describe('GetReviewsByUser', () => {
it("returns the first page of one user's reviews with the game names, newest first", async () => {
Query.mockResolvedValue({ rows: [testReview] })

await expect(GetReviewsByUser(7)).resolves.toEqual([testReview])

const [sql, params] = Query.mock.calls[0]
expect(String(sql)).toContain('g.game_name AS "gameName"')
expect(String(sql)).toContain('WHERE r.user_id = $1')
expect(String(sql)).toContain('ORDER BY r.review_id DESC')
expect(String(sql)).toContain('LIMIT $2 OFFSET $3')
expect(params).toEqual([7, 6, 0])
})

it('skips the reviews of earlier pages', async () => {
Query.mockResolvedValue({ rows: [] })

await GetReviewsByUser(7, 6, 12)

expect(Query.mock.calls[0][1]).toEqual([7, 6, 12])
})

it('keeps the page size between 1 and 50 and the offset at 0 or more', async () => {
Query.mockResolvedValue({ rows: [] })

await GetReviewsByUser(7, 1000, -5)

expect(Query.mock.calls[0][1]).toEqual([7, 50, 0])
})

it('rejects an invalid user id without touching the database', async () => {
await expect(GetReviewsByUser(0)).rejects.toThrow('The user ID is invalid.')
expect(Query).not.toHaveBeenCalled()
})
})

describe('CountReviewsByUser', () => {
it('counts all reviews written by the user', async () => {
Query.mockResolvedValue({ rows: [{ count: '13' }] })

await expect(CountReviewsByUser(7)).resolves.toBe(13)

const [sql, params] = Query.mock.calls[0]
expect(String(sql)).toContain('WHERE user_id = $1')
expect(params).toEqual([7])
})

it('rejects an invalid user id without touching the database', async () => {
await expect(CountReviewsByUser(-1)).rejects.toThrow('The user ID is invalid.')
expect(Query).not.toHaveBeenCalled()
})
})

describe('CreateReview', () => {
it('saves the review and returns it with the username', async () => {
Query.mockResolvedValue({ rows: [testReview] })
Expand Down
18 changes: 18 additions & 0 deletions Frontend/src/FrontendLogic/ReviewLogicFrontend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,24 @@ export async function GetRecentReviews(limit = 10): Promise<Review[]> {
return data.recentReviews
}

export const USER_REVIEWS_PER_PAGE = 6

export type UserReviewPage = {
reviews: Review[]
totalCount: number
}

export async function GetReviewsByUser(userId: number, page = 1): Promise<UserReviewPage> {
const data = await Gql<{ userReviews: Review[]; userReviewCount: number }>(
`query($userId: Int!, $limit: Int, $offset: Int) {
userReviews(userId: $userId, limit: $limit, offset: $offset) { ${REVIEW_FIELDS} gameName }
userReviewCount(userId: $userId)
}`,
{ userId, limit: USER_REVIEWS_PER_PAGE, offset: (page - 1) * USER_REVIEWS_PER_PAGE }
)
return { reviews: data.userReviews, totalCount: data.userReviewCount }
}

export async function CreateReview(
gameId: number,
userId: number,
Expand Down
15 changes: 14 additions & 1 deletion Frontend/src/components/ActiveGameCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export default function ActiveGameCard({

const isOnSale =
game.salePercent !== undefined && game.salePercent > 0 && game.salePercent <= 100
const showOriginalPrice =
isOnSale && game.originalPrice !== undefined && game.originalPrice > game.price

//Loads the game's reviews, again when the user signs in or out so their own review comes first
useEffect(() => {
Expand Down Expand Up @@ -168,9 +170,20 @@ export default function ActiveGameCard({
size="small"
/>
)}
<Typography className="activeGamePrice" component="p">
<Typography
className={`activeGamePrice${showOriginalPrice ? ' activeGameSalePrice' : ''}`}
component="p"
>
{game.price.toFixed(2)} $
</Typography>
{showOriginalPrice && (
<Typography className="activeGameOriginalPrice" component="s">
<span className="activeGameVisuallyHidden">
Original price:{' '}
</span>
{game.originalPrice!.toFixed(2)} $
</Typography>
)}
</Box>
</Box>
<Divider />
Expand Down
74 changes: 40 additions & 34 deletions Frontend/src/components/PassiveGameCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,40 +40,46 @@ export default function PassiveGameCard({ game, rank }: Props) {
}, [game.name])

return (
<Card className={`passiveGameCard${rank === undefined ? '' : ' rankedPassiveGameCard'}`}>
{rank !== undefined && (
<span className="passiveGameCardRank" aria-label={`Rank ${rank}`}>
#{rank}
</span>
)}
<CardActionArea
className="passiveGameCardAction"
component={Link}
to={`/gamepage/${game.id}`}
<div className="passiveGameCardHoverArea">
<Card
className={`passiveGameCard${rank === undefined ? '' : ' rankedPassiveGameCard'}`}
>
<CardMedia
className="passiveGameCardMedia"
component="img"
image={game.image}
alt={game.name}
/>
<CardContent className="passiveGameCardContent">
<div
ref={titleContainerRef}
className={`GameTitle${isTitleOverflowing ? ' has-overflow' : ''}`}
>
<Typography className="passiveGameCardTitle" component="h2">
{game.name}
</Typography>
</div>
<div className="GamePrice">
<div className="GameSale">{isOnSale ? `-${game.salePercent}%` : ''}</div>
<Typography className="passiveGameCardPrice" component="p">
{game.price.toFixed(2)} $
</Typography>
</div>
</CardContent>
</CardActionArea>
</Card>
{rank !== undefined && (
<span className="passiveGameCardRank" aria-label={`Rank ${rank}`}>
#{rank}
</span>
)}
<CardActionArea
className="passiveGameCardAction"
component={Link}
to={`/gamepage/${game.id}`}
>
<CardMedia
className="passiveGameCardMedia"
component="img"
image={game.image}
alt={game.name}
/>
<CardContent className="passiveGameCardContent">
<div
ref={titleContainerRef}
className={`GameTitle${isTitleOverflowing ? ' has-overflow' : ''}`}
>
<Typography className="passiveGameCardTitle" component="h2">
{game.name}
</Typography>
</div>
<div className="GamePrice">
<div className="GameSale">
{isOnSale ? `-${game.salePercent}%` : ''}
</div>
<Typography className="passiveGameCardPrice" component="p">
{game.price.toFixed(2)} $
</Typography>
</div>
</CardContent>
</CardActionArea>
</Card>
</div>
)
}
Loading