Skip to content

Commit

Permalink
Merge pull request #47 from devsoc-unsw/feature/keywordManagement
Browse files Browse the repository at this point in the history
Add keyword create feature
  • Loading branch information
lachlanshoesmith authored Dec 13, 2024
2 parents a704b8c + 6da14f2 commit e87e336
Show file tree
Hide file tree
Showing 7 changed files with 309 additions and 207 deletions.
16 changes: 0 additions & 16 deletions backend/docker-compose.yml

This file was deleted.

2 changes: 2 additions & 0 deletions backend/prisma/migrations/20241213074516_init/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- DropIndex
DROP INDEX "Keyword_text_key";
14 changes: 8 additions & 6 deletions backend/scripts/run-integration.sh
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
#This script only works for integration testing.
DIR="$(cd "$(dirname "$0")" && pwd)"
export $(grep -v '^#' .env.test | xargs)
docker-compose up -d
docker rm -f redis-stack-test 2>/dev/null || true && docker run -d --name redis-stack-test -p 6380:6379 -p 8002:8001 redis/redis-stack:latest
echo '🟡 - Waiting for database to be ready...'
docker rm -f postgres-test-pyramids 2>/dev/null && docker run --name postgres-test-pyramids -e POSTGRES_PASSWORD=postgres -d -p 5432:5432 postgres
docker rm -f redis-stack-test-pyramids 2>/dev/null || true && docker run -d --name redis-stack-test-pyramids -p 6380:6379 -p 8002:8001 redis/redis-stack:latest

echo "node_env is ${NODE_ENV}"
echo "Using database: ${DATABASE_URL}"
echo "And direct URL: ${DIRECT_URL}"
echo "redis port is ${REDIS_PORT}"
$DIR/wait-for-it.sh "${DATABASE_URL}" -- echo '🟢 - Database is ready!'
npx prisma migrate dev --name init

pnpm prisma migrate deploy

if [ "$#" -eq "0" ]
then
vitest -c ./vitest.config.integration.ts
else
vitest -c ./vitest.config.integration.ts --ui
fi
fi
182 changes: 0 additions & 182 deletions backend/scripts/wait-for-it.sh

This file was deleted.

75 changes: 72 additions & 3 deletions backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
eventIdBody,
RegisterBody,
UpdateEventBody,
CreateKeywordBody,
} from "./requestTypes";
import bcrypt from "bcrypt";
import { LoginErrors, SanitisedUser } from "./interfaces";
Expand Down Expand Up @@ -1116,9 +1117,77 @@ app.delete(
return res.status(400).json({ message: "Deletion failed" });
}

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

// gets keywords a user is associated with
app.get(
"/user/keywords",
async (req, res: Response) => {
const sessionFromDB = await validateSession(
req.session ? req.session : null
);
if (!sessionFromDB) {
return res.status(401).json({ message: "Invalid session provided." });
}

const userID = sessionFromDB.userId;

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

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

// creates a keyword
app.post(
"/keyword",
async (req: TypedRequest<CreateKeywordBody>, res: Response) => {
const sessionFromDB = await validateSession(
req.session ? req.session : null
);

if (!sessionFromDB) {
return res.status(401).json({ message: "Invalid session provided." });
}

const { text } = req.body;
if (!text) {
return res.status(400).json({ message: "Invalid input." });
}

const keywordExists = await prisma.keyword.findFirst({
where: {
text: text,
}
});

if (keywordExists) {
return res.status(400).json({ message: "Keyword already exists." });
}

try {
const newKeyword = await prisma.keyword.create({
data: {
text: text,
},
});
return res.status(200).json(newKeyword);
} catch (e) {
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

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 @@ -43,6 +43,10 @@ export interface CreateSocietyBody {
profilePicture: string | null;
}

export interface CreateKeywordBody {
text: string;
}

export interface DiscordLoginBody {
discordID: number;
}
Expand Down
Loading

0 comments on commit e87e336

Please sign in to comment.