Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ http://it2810-03.idi.ntnu.no/project2/
- **Plant Match:** Shows plants one at a time, best matches for your preferences first. Click ✿ to like a plant or ✖ to skip it.
- **Preferences:** Choose what you want from a plant: how much water it needs (minimum, average, frequent), how much sun it needs (full sun, part shade, filtered shade) and how much care it needs (easy, medium, hard). The preferences are saved in the database. Plant cards highlight the traits that match your preferences, and Plant Match shows the plants that match the most of your preferences first. Without preferences, the plants come in random order.
- **My Matches:** A list of all the plants you have liked in Plant Match or added from Explore Plants. You can also remove plants from the list.
- **Explore Plants:** Search the plant collection by common or scientific name and filter by watering, sunlight and care level. Sort the plants alphabetically (A–Z or Z–A), by watering or by care level. Search, filtering and sorting run in the backend on the whole collection, and the results are split into pages. The search, filters, sorting, and current page are stored in the URL, so an Explore view can be refreshed or shared as a link. You can also add plants to My Matches from here.
- **Explore Plants:** Search the plant collection by common or scientific name and filter by watering, sunlight and care level. Sort the plants alphabetically (A–Z or Z–A), by watering or by care level. Search, filtering and sorting run in the backend on the whole collection, and the number of results is shown above the plants. The results are split into pages, and you can jump straight to any page. The search, filters, sorting and current page are stored in the URL, so an Explore view can be refreshed or shared as a link. Add a plant to My Matches with the ✿ button, and click it again to remove it.
- **Plant details:** Each plant card shows an image, the scientific name, a description and care information (watering, sunlight and care level). Flip the card to read more about the plant.
- **Profile:** Your name, description, avatar and preferences are saved in the database, and your matched plants are shown here.
- **Plant Owner Test:** A short quiz that tells you what kind of plant owner you are. Each result has its own picture, which you can only get through the test, and you can save it as your profile picture.
Expand Down
3 changes: 2 additions & 1 deletion backend/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ export const rankOf = (order: string[], value: unknown): Document => ({
export const buildSortStages = (sort?: string): Document[] => {
switch (sort) {
case "name_desc":
return [{ $sort: { commonName: -1, _id: 1 } }];
// _id is also reversed, so the sort can read the commonName index backwards
return [{ $sort: { commonName: -1, _id: -1 } }];
case "watering_asc":
return [
{
Expand Down
10 changes: 10 additions & 0 deletions backend/src/indexes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { Db } from "mongodb";

// The indexes the app needs. createIndex does nothing if the index already
// exists, so this is safe to run every time the server starts.
export async function ensureIndexes(database: Db) {
// Sorting by name (A–Z, the default, and Z–A) reads the plants in order from
// this index instead of sorting the whole collection in memory. _id is part
// of the sort as a tie-breaker, so it is part of the index too.
await database.collection("plants").createIndex({ commonName: 1, _id: 1 });
}
5 changes: 5 additions & 0 deletions backend/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import { ApolloServer } from "@apollo/server";
import { startStandaloneServer } from "@apollo/server/standalone";
import { resolvers } from "./resolvers.js";
import { typeDefs } from "./schema.js";
import { db } from "./db.js";
import { ensureIndexes } from "./indexes.js";

// Create the database indexes before the server starts taking requests
await ensureIndexes(db);

const server = new ApolloServer({ typeDefs, resolvers });

Expand Down
23 changes: 23 additions & 0 deletions backend/tests/indexes.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { MongoClient } from "mongodb";
import { afterAll, expect, inject, it } from "vitest";
import { ensureIndexes } from "../src/indexes.js";

// Uses the in-memory MongoDB from globalSetup.ts, with its own database for this file
const client = await MongoClient.connect(inject("mongoUri"));
const db = client.db("indexes-test");

afterAll(async () => {
await client.close();
});

it("creates the index for sorting by name, and can run again on the next start", async () => {
await ensureIndexes(db);
// The server runs this on every start, so running it twice must not fail
await ensureIndexes(db);

const indexes = await db.collection("plants").indexes();
expect(indexes.map((index) => index.key)).toContainEqual({
commonName: 1,
_id: 1,
});
});
8 changes: 8 additions & 0 deletions backend/tests/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"rootDir": "..",
"noEmit": true
},
"include": ["../src", "."]
}
2 changes: 1 addition & 1 deletion backend/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
"resolveJsonModule": true,
"sourceMap": true
},
"include": ["src", "test", "globalSetup.ts"]
"include": ["src"]
}
6 changes: 5 additions & 1 deletion docs/ai-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ AI was used as an assistant throughout the development of the web application. T
## What AI was used for

- **Setup and configuration:** setting up the project in VS Code, Vitest, and the project structure with backend and database.
- **Explaining technical concepts:** how the database, backend and frontend are related, and how to implement debounce.
- **Explaining technical concepts:** how the database, backend and frontend are related, how to implement debounce, the difference between offset- and cursor-based pagination, and when a database index helps.
- **Writing code:** suggestions for code, which were reviewed by the team before they were used.
- **Writing and reviewing tests:** drafting test files with mocked dependencies, and explaining the reasoning behind the test structure and snapshot testing. AI also suggested what should be tested to get better test coverage.
- **Writing documentation:** drafting and refining documentation and comments in the code.
Expand All @@ -31,6 +31,10 @@ The avatar pictures that the user can choose between on the profile page were ge

AI drafted many of the Vitest tests and the Playwright e2e and API tests, and suggested atypical cases to test, such as special characters in the search, too long names and invalid ids. The team reviewed the tests, adjusted them, and ran them to check that they pass and fail for the right reasons. The accessibility tests found a real problem with the language button (see [tests.md](tests.md)), which we then fixed.

### Improvements to Explore Plants

After the peer reviews, we used AI to work through the feedback on Explore Plants. We decided that the pagination, the filter dropdowns and the sort menu should be their own reusable components, `Pagination`, `FilterSelect` and `SortMenu`, and that the URL state should be moved into its own hook, `useExploreParams`. AI helped us move the right parts out of `ExplorePlants.tsx`, which had a lot of logic in one file. AI also made us aware of the trade-offs between offset- and cursor-based pagination, and between a text index and the regular expression search. Based on that, we chose to keep offset-based pagination so users can jump to any page, to keep the search on parts of names instead of using a text index, and to only add a database index where it is useful. The design was decided by the team. We reviewed and ran all tests before committing.

## How we kept control

All AI-generated suggestions were reviewed, tested and adapted by the team before they were committed. Every change went through a pull request that had to be approved by another group member (see [quality.md](quality.md#work-practices)), so no AI-generated code was merged without a review. Design and architectural decisions were made by the team.
Loading
Loading