Skip to content

Commit

Permalink
➡️ merge pull request #52 from devsoc-unsw/feature/keywordManagement
Browse files Browse the repository at this point in the history
Feature/keyword management
  • Loading branch information
lachlanshoesmith authored Dec 14, 2024
2 parents 0cad749 + 8b7abdb commit 95d3d8c
Show file tree
Hide file tree
Showing 5 changed files with 487 additions and 187 deletions.
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 @@ -1174,18 +1175,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(404).json([]);

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

// creates a keyword
Expand Down Expand Up @@ -1223,13 +1224,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(404).json({ message: "Invalid keyword." });
}

//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

0 comments on commit 95d3d8c

Please sign in to comment.