Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/keyword management #52

Merged
merged 5 commits into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
138 changes: 125 additions & 13 deletions backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
RegisterBody,
UpdateEventBody,
CreateKeywordBody,
keywordIdBody,
} from './requestTypes';
import bcrypt from 'bcrypt';
import { LoginErrors, SanitisedUser } from './interfaces';
Expand Down Expand Up @@ -1162,18 +1163,18 @@ app.get('/user/keywords', async (req, res: Response) => {

const userID = sessionFromDB.userId;

const userKeywords = await prisma.user.findFirst({
where: {
id: userID,
},
select: {
keywords: {
select: { text: true },
let userKeywords = await prisma.user.findFirst({
where: {
id: userID,
},
},
});
select: {
keywords: true,
}
});

if (userKeywords === null) return res.status(200).json([]);
lachlanshoesmith marked this conversation as resolved.
Show resolved Hide resolved

return res.status(200).json(userKeywords);
return res.status(200).json(userKeywords.keywords);
});

// creates a keyword
Expand Down Expand Up @@ -1211,13 +1212,124 @@ app.post(
});
return res.status(200).json(newKeyword);
} catch (e) {
return res.status(400).json({ message: 'invalid keyword input' });
return res.status(400).json({ message: "Invalid keyword input." });
}
}
);

// - app.post("/user/keyword") - Associates a user with a keyword
// - app.delete("/user/keyword") - Disassociates a user with a keyword
// associates a user with a keyword
app.post(
"/user/keyword",
async (req: TypedRequest<keywordIdBody>, res: Response) => {
//get userid from session
const sessionFromDB = await validateSession(
req.session ? req.session : null
);
if (!sessionFromDB) {
return res.status(401).json({ message: "Invalid session provided." });
}
const userID = sessionFromDB.userId;

//Make sure keyword exists
const keywordExists = await prisma.keyword.findFirst({
where: {
id: req.body.keywordId,
},
select: {
id: true,
},
});

if (!keywordExists) {
return res.status(400).json({ message: "Invalid keyword." });
lachlanshoesmith marked this conversation as resolved.
Show resolved Hide resolved
}

//Connect keyword and user
await prisma.keyword.update({
where: {
id: req.body.keywordId,
},
data: {
subscribers: {
connect: {
id: userID,
},
},
},
});

await prisma.user.update({
where: {
id: userID,
},
data: {
keywords: {
connect: {
id: req.body.keywordId,
},
},
},
});

return res.status(200).json({ message: "ok" });
});

// disassociates a user with a keyword
app.delete(
"/user/keyword",
async (req: TypedRequest<keywordIdBody>, res: Response) => {
//get userid from session
const sessionFromDB = await validateSession(
req.session ? req.session : null
);
if (!sessionFromDB) {
return res.status(401).json({ message: "Invalid session provided." });
}
const userID = sessionFromDB.userId;

//Make sure keyword exists
const societyId = await prisma.keyword.findFirst({
where: {
id: req.body.keywordId,
},
select: {
id: true,
},
});

if (!societyId) {
return res.status(400).json({ message: "Invalid keyword." });
}

//Disconnect keyword and user
await prisma.keyword.update({
where: {
id: req.body.keywordId,
},
data: {
subscribers: {
disconnect: {
id: userID,
},
},
},
});

await prisma.user.update({
where: {
id: userID,
},
data: {
keywords: {
disconnect: {
id: req.body.keywordId,
},
},
},
});

return res.status(200).json({ message: "ok" });
});

app.get('/hello', () => {
console.log('Hello World!');
Expand Down
4 changes: 4 additions & 0 deletions backend/src/requestTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,8 @@ export interface eventIdBody {
eventId: number;
}

export interface keywordIdBody {
keywordId: number;
}

export interface eventDetailsBody {}
Loading
Loading