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

lots of stuff #84

Merged
merged 15 commits into from
May 17, 2024
Merged
7 changes: 3 additions & 4 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ on:
auth0Secret:
description: 'Auth0 Secret'
required: true
auth0IssuerBaseUrl:
description: 'Auth0 Issuer Base URL'
auth0Domain:
description: 'Auth0 Domain'
required: true
auth0ClientId:
description: 'Auth0 Client ID'
Expand Down Expand Up @@ -39,7 +39,6 @@ jobs:
eval "${{ github.event.inputs.awsCredentials }}"
terraform init
terraform apply -auto-approve \
-var "auth0_secret=${{ github.event.inputs.auth0Secret }}" \
-var "auth0_issuer_base_url=${{ github.event.inputs.auth0IssuerBaseUrl }}" \
-var "auth0_domain=${{ github.event.inputs.auth0Domain }}" \
-var "auth0_client_id=${{ github.event.inputs.auth0ClientId }}" \
-var "auth0_client_secret=${{ github.event.inputs.auth0ClientSecret }}"
8 changes: 2 additions & 6 deletions .github/workflows/teardown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ on:
awsCredentials:
description: 'AWS Credentials'
required: true
auth0Secret:
description: 'Auth0 Secret'
required: true
auth0IssuerBaseUrl:
auth0Domain:
description: 'Auth0 Issuer Base URL'
required: true
auth0ClientId:
Expand All @@ -34,7 +31,6 @@ jobs:
eval "${{ github.event.inputs.awsCredentials }}"
terraform init
terraform destroy -auto-approve \
-var "auth0_secret=${{ github.event.inputs.auth0Secret }}" \
-var "auth0_issuer_base_url=${{ github.event.inputs.auth0IssuerBaseUrl }}" \
-var "auth0_domain=${{ github.event.inputs.auth0Domain }}" \
-var "auth0_client_id=${{ github.event.inputs.auth0ClientId }}" \
-var "auth0_client_secret=${{ github.event.inputs.auth0ClientSecret }}"
2 changes: 1 addition & 1 deletion backend/__tests__/backend.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { single_nest, nest } = require("../src/routes/helpful_friends");
const { single_nest, nest } = require('../src/routes/helpfulFriends');

// All tests for the nest function
describe("nest function", () => {
Expand Down
2,817 changes: 2,664 additions & 153 deletions backend/package-lock.json

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"@types/cors": "^2.8.17",
"@types/express": "^4.17.21",
"@types/jest": "^29.5.12",
"@types/multer": "^1.4.11",
"@types/pg": "^8.11.5",
"@types/uuid": "^9.0.8",
"cors": "^2.8.5",
Expand All @@ -25,6 +26,9 @@
"uuid": "^9.0.1"
},
"dependencies": {
"@aws-sdk/client-s3": "^3.576.0",
"@aws-sdk/credential-providers": "^3.576.0",
"multer": "^1.4.5-lts.1",
"ts-node": "^10.9.2"
}
}
22 changes: 11 additions & 11 deletions backend/src/routes/comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@
import { Request, Response } from 'express'; // Import Request and Response types
import { CommentBodyParams, CommentRouteParams } from '../types';

import { getConnection } from '../db/index';
import { getConnection } from '../db';
import { User as UserDb } from '../db/User';
import { Question as QuestionDb } from '../db/Questions';
import { Comment as CommentDb } from '../db/Comments';
import { pushImageToS3 } from './helpfulFriends';

export async function editComments(req: Request<CommentRouteParams, any, CommentBodyParams>, res: Response) {
vilnor marked this conversation as resolved.
Show resolved Hide resolved
const { commentId } = req.params;
const { commentText, commentPNG, userId } = req.body;
const { commentText, userId } = req.body;

if (!commentId || !userId) {
res.status(400).json('Invalid commentId');
return;
}

if (!commentText && !commentPNG) {
if (!commentText && !req.file) {
res.status(400).json('No changes made');
return;
}
Expand Down Expand Up @@ -46,8 +47,8 @@ export async function editComments(req: Request<CommentRouteParams, any, Comment
comment.commentText = commentText;
}

if (commentPNG) {
comment.commentPNG = commentPNG;
if (req.file) {
comment.commentPNG = await pushImageToS3(req.file.buffer, `${comment.questionId}_${req.file.originalname}`);
}

comment.updatedAt = new Date();
Expand Down Expand Up @@ -255,7 +256,6 @@ export async function postComment(req: Request<any, any, CommentBodyParams>, res
questionId,
parentCommentId,
commentText,
commentPNG,
} = req.body;

// Check key
Expand All @@ -264,7 +264,7 @@ export async function postComment(req: Request<any, any, CommentBodyParams>, res
return;
}

if (!commentText && !commentPNG) {
if (!commentText && !req.file) {
vilnor marked this conversation as resolved.
Show resolved Hide resolved
res.status(400).json('Missing commentText or commentPNG');
return;
}
Expand Down Expand Up @@ -294,15 +294,15 @@ export async function postComment(req: Request<any, any, CommentBodyParams>, res
res.status(404).json('Parent comment not found');
return;
}
if (parentComment.questionId !== questionId) {
if (parentComment.questionId !== +questionId) {
res.status(400).json('Parent comment is not from the same question');
return;
}
}
// Query the database and get the id of the new comment
const newComment = new CommentDb();
newComment.userId = userId;
newComment.questionId = questionId;
newComment.questionId = +questionId;

if (parentCommentId) {
newComment.parentCommentId = parentCommentId;
Expand All @@ -312,8 +312,8 @@ export async function postComment(req: Request<any, any, CommentBodyParams>, res
newComment.commentText = commentText;
}

if (commentPNG) {
newComment.commentPNG = commentPNG;
if (req.file) {
newComment.commentPNG = await pushImageToS3(req.file.buffer, `${questionId}_${req.file.originalname}`);
}
const savedComment = await commentRepository.save(newComment);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Imports
import { Comment as IComment } from '../types';
import { ObjectCannedACL, PutObjectCommand, S3Client } from '@aws-sdk/client-s3';

// Helper functions

Expand Down Expand Up @@ -40,3 +41,23 @@ export function single_nest(commentRows: IComment[], commentId: number) {
const resultJsonData = commentRows.filter(item => item.commentId === commentId);
return resultJsonData;
}

const bucketName = process.env.S3_BUCKET_NAME;
const region = process.env.AWS_REGION;
// TODO: add creds here
const s3Client = new S3Client({
region,
});

export async function pushImageToS3(file: Buffer, key: string) {
const params = {
Bucket: bucketName,
Key: key,
Body: file,
ACL: ObjectCannedACL.public_read,
};

await s3Client.send(new PutObjectCommand(params));

return `https://${bucketName}.s3.${region}.amazonaws.com/${key}`;
}
18 changes: 9 additions & 9 deletions backend/src/routes/questions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
import { Request, Response } from 'express'; // Import Request and Response types
import { QuestionBodyParams, QuestionQueryParams, QuestionRouteParams } from '../types';

import { getConnection } from '../db/index';
import { getConnection } from '../db';
import { Exam as ExamDb } from '../db/Exam';
import { Question as QuestionDb } from '../db/Questions';
import { Comment as CommentDb } from '../db/Comments';
import { User as UserDb } from '../db/User';

import { nest } from './helpful_friends';
import { nest, pushImageToS3 } from './helpfulFriends';

export async function editQuestion(req: Request<QuestionRouteParams, any, QuestionBodyParams>, res: Response) {
const { questionId } = req.params;
const { questionText, questionType, questionPNG } = req.body;
const { questionText, questionType } = req.body;

if (!questionText && !questionType && !questionPNG) {
if (!questionText && !questionType && !req.file) {
res.status(400).json('No changes made');
return;
}
Expand All @@ -35,8 +35,8 @@ export async function editQuestion(req: Request<QuestionRouteParams, any, Questi
question.questionType = questionType;
}

if (questionPNG) {
question.questionPNG = questionPNG;
if (req.file) {
question.questionPNG = await pushImageToS3(req.file.buffer, `${question.examId}_${req.file.originalname}`);
}

question.updatedAt = new Date();
Expand All @@ -50,7 +50,6 @@ export async function postQuestion(req: Request<any, any, QuestionBodyParams>, r
examId,
questionText,
questionType,
questionPNG,
} = req.body;

// Check key
Expand Down Expand Up @@ -81,9 +80,10 @@ export async function postQuestion(req: Request<any, any, QuestionBodyParams>, r
newQuestion.questionText = questionText;
}

if (questionPNG) {
newQuestion.questionPNG = questionPNG;
if (req.file) {
newQuestion.questionPNG = await pushImageToS3(req.file.buffer, `${examId}_${req.file.originalname}`);
}

newQuestion.questionType = questionType;
const savedQuestion = await questionRepository.save(newQuestion);

Expand Down
9 changes: 5 additions & 4 deletions backend/src/routes/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { EVAN, healthCheck } from './health';

import { setupData } from './sketch';
import { getRecentChanges } from './recentChanges';
import multer from 'multer';

// Export Routers
export const router = Router();
Expand All @@ -42,10 +43,10 @@ export const router = Router();
*/

// Edits a question
router.put('/questions/:questionId/edit', editQuestion);
router.put('/questions/:questionId/edit', <any>multer().single('questionPNG'), editQuestion);

// Edits a comment
router.put('/comments/:commentId/edit', editComments);
router.put('/comments/:commentId/edit', <any>multer().single('commentPNG'), editComments);

router.get('/recent_changes', getRecentChanges);

Expand Down Expand Up @@ -93,10 +94,10 @@ router.patch('/comments/:commentId/upvote', upvoteComments);
router.post('/users', postUser);

// Adds a new comment to the database
router.post('/comments', postComment);
router.post('/comments', multer().single('commentPNG'), postComment);

// Adds a new question to the database
router.post('/questions', postQuestion);
router.post('/questions', multer().single('questionPNG'), postQuestion);

// Adds a new exam to the database
router.post('/exams', postExam);
Expand Down
4 changes: 2 additions & 2 deletions backend/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ export type Comment = {
}


export type CommentBodyParams = Partial<Omit<Comment, 'children' | 'commentId'>> & {
export type CommentBodyParams = Partial<Omit<Comment, 'children' | 'commentId' | 'commentPNG'>> & {
questionId?: number
}

export type CommentRouteParams = {
commentId: number
}

export type QuestionBodyParams = Partial<Omit<Question, 'questionId'>> & {
export type QuestionBodyParams = Partial<Omit<Question, 'questionId' | 'commentPNG'>> & {
examId?: number
}

Expand Down
Loading
Loading