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

POST /reviews endpoint #23

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions app/challenges-platform/services/reviews-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ export const create = async (
export const convert = async (result: any): Promise<Result<Review, Error>> => {
let status: Status;
switch (result.status) {
case "approved":
case "APPROVED":
status = Status.APPROVED;
break;
case "rejected":
case "REJECTED":
status = Status.REJECTED;
break;
default:
Expand Down
31 changes: 31 additions & 0 deletions config/express/router.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import express, { Request, Response } from "express";
import { ReviewsService } from "../../app/challenges-platform";
import { Status } from "../../app/challenges-platform/models";
import { db } from "../../db";
import { challenges } from "../../db/schema";

Expand All @@ -16,7 +18,36 @@ const getChallenges = async (_: Request, response: Response) => {
response.json(result);
};

const submitReview = async (request: Request, response: Response) => {
if (!request.body.review) {
response.status(400).json({ message: "Invalid request" });
return;
}

const review = request.body.review;
const { submissionId, comment } = review;
let sentStatus = review.status;

if (
(sentStatus !== "APPROVED" && sentStatus !== "REJECTED") ||
!submissionId ||
!comment
) {
response.status(400).json({ message: "Invalid request" });
return;
}
const status = sentStatus === "APPROVED" ? Status.APPROVED : Status.REJECTED;

const result = await ReviewsService.create(status, submissionId, comment);
if (result.err) {
response.status(500).json({ message: "Failed to create review" });
return;
}
response.json(result);
};

router.get("/", getAPIRoot);
router.get("/challenges", getChallenges);
router.post("/reviews", submitReview);

export default router;
2 changes: 1 addition & 1 deletion test/challenges-platform/factories/review-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const reviewFactory = async ({
const s = submission || (await submissionFactory());
const b = body || "test review";

const statusString = st === Status.APPROVED ? "approved" : "rejected";
const statusString = st === Status.APPROVED ? "APPROVED" : "REJECTED";
const insertResult = await db
.insert(reviews)
.values({
Expand Down
Loading