diff --git a/website/src/lib/plant_data.ts b/website/src/lib/plant_data.ts index 4fcc41e..bfd3e7d 100644 --- a/website/src/lib/plant_data.ts +++ b/website/src/lib/plant_data.ts @@ -16,7 +16,7 @@ export interface PlantData { location_found: string; small_description: string; long_description: string; - author: string; + author: number[]; last_modified: string; display_image: string; plant_type: string; @@ -416,11 +416,26 @@ export function ConvertApiIntoPlantData(apiData : PlantDataApi){ plantData.location_found = apiData.location_found; plantData.small_description = apiData.small_description; plantData.long_description = apiData.long_description; - plantData.author = apiData.author; plantData.last_modified = apiData.last_modified; plantData.display_image = apiData.display_image; plantData.plant_type = apiData.plant_type; + // Convert the author into array of ints + plantData.author = []; + + if(apiData.author.includes(",")){ + + // Split the string into an array + let authorArray = apiData.author.split(","); + + // Convert the array of strings into an array of ints + for(let i = 0; i < authorArray.length; i++){ + plantData.author.push(parseInt(authorArray[i])); + } + }else{ + plantData.author.push(parseInt(apiData.author)); + } + // Image info for(let i = 0; i < apiData.attachment_paths.length; i++) { let imageInfoOBJ = { @@ -558,11 +573,10 @@ export function ConvertPlantDataIntoApi(plantData : PlantData){ apiData.location_found = plantData.location_found; apiData.small_description = plantData.small_description; apiData.long_description = plantData.long_description; - apiData.author = plantData.author; apiData.last_modified = plantData.last_modified; apiData.display_image = plantData.display_image; apiData.plant_type = plantData.plant_type; - + apiData.author = plantData.author.toString() // Date info for(let i = 0; i < plantData.months_ready_for_use.length; i++) { @@ -643,7 +657,7 @@ export function emptyPlantData(){ location_found: "", small_description: "", long_description: "", - author: "", + author: [], last_modified: "", display_image: "", plant_type: "", diff --git a/website/src/pages/api/users/api_keys.ts b/website/src/pages/api/user/api_keys.ts similarity index 100% rename from website/src/pages/api/users/api_keys.ts rename to website/src/pages/api/user/api_keys.ts diff --git a/website/src/pages/api/users/data.ts b/website/src/pages/api/user/data.ts similarity index 100% rename from website/src/pages/api/users/data.ts rename to website/src/pages/api/user/data.ts diff --git a/website/src/pages/api/user/plants.ts b/website/src/pages/api/user/plants.ts new file mode 100644 index 0000000..14bf4f9 --- /dev/null +++ b/website/src/pages/api/user/plants.ts @@ -0,0 +1,55 @@ +import {NextApiRequest, NextApiResponse} from 'next'; +import {getClient, getTables, makeQuery} from "@/lib/databse"; +import {GetOrigin} from "@/lib/api_tools"; +import {getServerSession} from "next-auth"; +import {authOptions} from "@/pages/api/auth/[...nextauth]"; + +export default async function handler( + request: NextApiRequest, + response: NextApiResponse, +) { + + // Get the origin of the request + const origin = GetOrigin(request); + + // Get the client + const client = await getClient() + + // Get the tables + const tables = getTables(); + + try { + + // Get the session + const session = await getServerSession(request, response, authOptions) + + // If there is no session then return an error + if(!session || !session.user) { + return response.status(401).json({ error: 'User not logged in'}); + } + + // Get the user details + // @ts-ignore + const user_id = session.user.database.id + + // Fetch the plants + let query = `SELECT id, ${tables.preferred_name}, ${tables.english_name}, ${tables.maori_name}, ${tables.latin_name}, ${tables.last_modified}, ${tables.plant_type} FROM plants WHERE ${tables.author} LIKE '%,${user_id},%' OR ${tables.author} LIKE '${user_id},%' OR ${tables.author} LIKE '%,${user_id}' OR ${tables.author} LIKE '${user_id}'`; + console.log(query); + const plants = await makeQuery(query, client) + + if(plants.length == 0) { + return response.status(400).json({ error: 'User doesnt have any plants'}); + } + + // Return the user + return response.status(200).json({ data: plants }); + + + } catch (error) { + console.log("Error"); + console.log(error); + + // If there is an error, return the error + return response.status(500).json({ error: error }); + } +} \ No newline at end of file diff --git a/website/src/pages/index.tsx b/website/src/pages/index.tsx index bcf3323..26cd243 100644 --- a/website/src/pages/index.tsx +++ b/website/src/pages/index.tsx @@ -3,8 +3,8 @@ //TODO: // - Create New User In database when logging on for the first time | DONE // - User session stores data about the user from the database | DONE -// - Use user id to store the author of plants -// - API route to get the plants by author +// - Use user id to store the author of plants | DONE +// - API route to get the plants by author | DONE // - Display the user's info on the profile page // - Edit the user's info // - Permissions for users diff --git a/website/src/pages/plants/create.tsx b/website/src/pages/plants/create.tsx index f5573f9..6b07adf 100644 --- a/website/src/pages/plants/create.tsx +++ b/website/src/pages/plants/create.tsx @@ -1804,37 +1804,33 @@ export default function CreatePlant() { plantOBJ.location_found = cleanInput(location); plantOBJ.small_description = cleanInput(smallDescription) plantOBJ.long_description = cleanInput(largeDescription) - plantOBJ.author = "Unknown"; plantOBJ.last_modified = new Date().toISOString() plantOBJ.display_image = cleanInput(displayImage); plantOBJ.plant_type = cleanInput(plantType); + plantOBJ.author = []; // Get the plant author if(session && session.user){ - // If there is a user then set the author to the user's name - if (session.user.name) - plantOBJ.author = session.user.name; + //TODO: Find away to fix this + // @ts-ignore + let userID = session.user.database.id; - // Get the previous author - let prevAuthors = importedJSON.author; + // If there is a user then set the author to the user's name + if(userID){ - // If there is a previous author then add it to the list - if(prevAuthors){ - // Split at the comma - let prevAuthorsList = prevAuthors.split(","); + let prevAuthors = importedJSON.author; - // Check if the current author is already in the list and remove it if it is - if(prevAuthorsList.includes(plantOBJ.author)) { - prevAuthorsList.splice(prevAuthorsList.indexOf(plantOBJ.author), 1); + if(prevAuthors){ + plantOBJ.author = prevAuthors; } - // Add the prev authors to the author list - plantOBJ.author = plantOBJ.author + "," + prevAuthorsList.join(","); + if(!plantOBJ.author.includes(userID)) { + plantOBJ.author.push(userID) + } } } - // Image info for(let i = 0; i < imageInfoRef.current.length; i++) { const thisImageInfo = imageInfoRef.current[i].state;