Skip to content

Commit

Permalink
Author IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
maxtyson123 committed Nov 7, 2023
1 parent b763fea commit 34357b2
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 23 deletions.
24 changes: 19 additions & 5 deletions website/src/lib/plant_data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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++) {
Expand Down Expand Up @@ -643,7 +657,7 @@ export function emptyPlantData(){
location_found: "",
small_description: "",
long_description: "",
author: "",
author: [],
last_modified: "",
display_image: "",
plant_type: "",
Expand Down
File renamed without changes.
File renamed without changes.
55 changes: 55 additions & 0 deletions website/src/pages/api/user/plants.ts
Original file line number Diff line number Diff line change
@@ -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 });
}
}
4 changes: 2 additions & 2 deletions website/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 12 additions & 16 deletions website/src/pages/plants/create.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

1 comment on commit 34357b2

@vercel
Copy link

@vercel vercel bot commented on 34357b2 Nov 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.