Skip to content
Merged
Changes from all commits
Commits
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
81 changes: 81 additions & 0 deletions backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,25 @@ const typeDefs = `#graphql
ratingCount: Int!
}
type WineDetails {
id: ID!
name: String!
type: String!
elaborate: String
abv: Float
body: String
acidity: String
winery: String!
wineryWebsite: String
region: String!
country: String!
vintages: [Int!]!
grapes: [String!]!
foodPairings: [String!]!
avgRating: Float
ratingCount: Int!
}
type WinePage {
items: [Wine!]!
totalCount: Int!
Expand All @@ -36,6 +55,8 @@ const typeDefs = `#graphql
offset: Int = 0
): WinePage!
wine(id: ID!): WineDetails
foodCategories: [FoodCategory!]!
winesByFoodCategory(
Expand Down Expand Up @@ -92,6 +113,66 @@ const resolvers = {
};
},

wine: async (
_: unknown,
args: { id: string }
) => {
const wineId = Number(args.id);

if (!Number.isInteger(wineId)) {
return null;
}

const result = await pool.query(
`
SELECT
w.id,
w.name,
w.type,
w.elaborate,
w.abv::float AS abv,
w.body,
w.acidity,
wi.name AS winery,
wi.website AS "wineryWebsite",
r.name AS region,
c.name AS country,
w.vintages,
w.avg_rating::float AS "avgRating",
w.rating_count AS "ratingCount",
COALESCE(
(
SELECT array_agg(g.name ORDER BY g.name)
FROM wine_grapes wg
JOIN grapes g ON g.id = wg.grape_id
WHERE wg.wine_id = w.id
),
ARRAY[]::text[]
) AS grapes,
COALESCE(
(
SELECT array_agg(f.name ORDER BY f.name)
FROM wine_foods wf
JOIN foods f ON f.id = wf.food_id
WHERE wf.wine_id = w.id
),
ARRAY[]::text[]
) AS "foodPairings"
FROM wines w
JOIN wineries wi ON w.winery_id = wi.id
JOIN regions r ON w.region_id = r.id
JOIN countries c ON w.country_code = c.code
WHERE w.id = $1
`,
[wineId]
);

return result.rows[0] ?? null;
},

foodCategories: async () => {
const result = await pool.query(
`
Expand Down