import React, { Component } from "react";
import "./App.css";
class App extends Component {
BASE_URL = "http://localhost:8001/courses";
state = {
data: [],
rating: 1, // default is 1 -> matches test's expected body '{"rating":1}'
};
componentDidMount() {
this.handleGetData();
}
handleGetData = async () => {
const res = await fetch(this.BASE_URL + "/get");
const json = await res.json();
this.setState({ data: json });
};
handleApply = async (id) => {
const res = await fetch(this.BASE_URL + "/enroll/" + id, {
method: "post",
headers: { "Content-Type": "application/json" },
});
const data = await res.json();
alert(data.message);
this.handleGetData();
};
handleRating = (e) => {
// keep as string or number — tests only check state value for simulated change
this.setState({ rating: e.target.value });
};
handleAddRating = async (id) => {
const res = await fetch(this.BASE_URL + "/rating/" + id, {
method: "PATCH",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ rating: this.state.rating }),
});
const body = await res.json();
if (body.error) alert(body.error);
this.handleGetData();
};
handleDrop = async (id) => {
const res = await fetch(this.BASE_URL + "/drop/" + id, {
method: "DELETE",
headers: { "Content-Type": "application/json" },
});
const data = await res.json();
alert(data.message);
this.handleGetData();
};
render() {
return (
<div className="home">
<header>
<h2>ABC Learning</h2>
</header>
<div className="cardContainer">
{this.state.data.map((course) => {
const {
_id,
courseName,
courseDept,
description,
isApplied,
isRated,
duration,
noOfRatings,
rating,
} = course;
return (
<div className="card" key={_id}>
<ul>
<div className="header">
<li>{courseName}</li>
<li>{courseDept}</li>
<li>{description}</li>
{isApplied ? (
<li>
{!isRated && (
<li>
Rate:
<select
className="rating"
name="rating"
onChange={this.handleRating}
>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
<button
className="rate"
onClick={() => this.handleAddRating(_id)}
>
Add
</button>
</li>
)}
<button
className="drop"
onClick={() => this.handleDrop(_id)}
>
Drop Course
</button>
</li>
) : (
<li>
<button
className="btn"
onClick={() => this.handleApply(_id)}
>
Apply
</button>
</li>
)}
</div>
<div className="footer">
<li>
{duration} hrs . {noOfRatings} Ratings . {rating}/5
</li>
</div>
</ul>
</div>
);
})}
</div>
</div>
);
}
}
export default App;
const express = require("express");
const Course = require("../mongoose/models/courses");
const usersRouter = new express.Router();
// Get all courses
usersRouter.get("/courses/get", async (req, res) => {
try {
const courses = await Course.find();
return res.send(courses);
} catch (err) {
return res.sendStatus(400);
}
});
// Enroll
usersRouter.post("/courses/enroll/:id", async (req, res) => {
try {
const id = req.params.id;
const course = await Course.findById(id);
if (!course) return res.sendStatus(400);
if (course.isApplied) {
return res.status(403).send({ message: "You have already applied for this course" });
}
course.isApplied = true;
await course.save();
return res.send({ message: "You have successfully enrolled for the course" });
} catch (err) {
return res.sendStatus(400);
}
});
// Drop
usersRouter.delete("/courses/drop/:id", async (req, res) => {
try {
const id = req.params.id;
const course = await Course.findById(id);
if (!course) return res.sendStatus(400);
if (!course.isApplied) {
return res.status(403).send({ error: "You have not enrolled for this course" });
}
course.isApplied = false;
await course.save();
return res.send({ message: "You have dropped the course" });
} catch (err) {
return res.sendStatus(400);
}
});
// Rating
usersRouter.patch("/courses/rating/:id", async (req, res) => {
try {
const id = req.params.id;
const course = await Course.findById(id);
if (!course) return res.sendStatus(400);
if (!course.isApplied) {
return res.status(403).send({ error: "You have not enrolled for this course" });
}
if (course.isRated) {
return res.status(403).send({ error: "You have already rated this course" });
}
const newRating = Number(req.body.rating);
if (!newRating && newRating !== 0) return res.sendStatus(400);
const resNoOfRatings = course.noOfRatings + 1;
const resRating = (((course.rating || 0) * course.noOfRatings + newRating) / resNoOfRatings).toFixed(1);
course.rating = parseFloat(resRating);
course.noOfRatings = resNoOfRatings;
course.isRated = true;
await course.save();
return res.send({ message: "You have rated this course" });
} catch (err) {
return res.sendStatus(400);
}
});
module.exports = usersRouter;
0 Comments