Compare commits
2 Commits
d75ea3f2aa
...
b87d40ade8
Author | SHA1 | Date | |
---|---|---|---|
|
b87d40ade8 | ||
|
c7dc5314a1 |
@@ -1,7 +1,7 @@
|
|||||||
import * as express from "express";
|
import * as express from "express";
|
||||||
import * as cors from "cors";
|
import * as cors from "cors";
|
||||||
|
|
||||||
import { CatType, Cat } from "./app.model";
|
import catsRouter from "./cats/cats.route";
|
||||||
|
|
||||||
const app: express.Express = express();
|
const app: express.Express = express();
|
||||||
const port: number = 8000;
|
const port: number = 8000;
|
||||||
@@ -17,52 +17,7 @@ app.use((req, res, next) => {
|
|||||||
// * json middleware
|
// * json middleware
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
|
|
||||||
// * C
|
app.use(catsRouter);
|
||||||
app.post("/cats", (req, res) => {
|
|
||||||
try {
|
|
||||||
const data = req.body;
|
|
||||||
Cat.push(data);
|
|
||||||
res.status(200).send({
|
|
||||||
success: true,
|
|
||||||
data: { data },
|
|
||||||
});
|
|
||||||
} catch (error) {}
|
|
||||||
});
|
|
||||||
|
|
||||||
// * R
|
|
||||||
app.get("/cats", (req, res) => {
|
|
||||||
try {
|
|
||||||
const cats = Cat;
|
|
||||||
res.status(200).send({
|
|
||||||
success: true,
|
|
||||||
data: { cats },
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
res.status(400).send({
|
|
||||||
success: false,
|
|
||||||
error: error.message,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
app.get("/cats/:id", (req, res) => {
|
|
||||||
try {
|
|
||||||
const params = req.params;
|
|
||||||
const cats = Cat.find((cat) => {
|
|
||||||
return cat.id === params.id;
|
|
||||||
});
|
|
||||||
if (!cats) throw new Error("no matched data");
|
|
||||||
res.status(200).send({
|
|
||||||
success: true,
|
|
||||||
data: { cats },
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
res.status(400).send({
|
|
||||||
success: false,
|
|
||||||
error: error.message,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// * 404 middleware
|
// * 404 middleware
|
||||||
app.use((req, res, next) => {
|
app.use((req, res, next) => {
|
||||||
|
124
catDataMocking/src/cats/cats.route.ts
Normal file
124
catDataMocking/src/cats/cats.route.ts
Normal file
@@ -0,0 +1,124 @@
|
|||||||
|
import { Router } from "express";
|
||||||
|
import { CatType, Cat } from "./cats.model";
|
||||||
|
|
||||||
|
const router = Router();
|
||||||
|
|
||||||
|
// * C
|
||||||
|
router.post("/cats", (req, res) => {
|
||||||
|
try {
|
||||||
|
const data = req.body;
|
||||||
|
Cat.push(data);
|
||||||
|
res.status(200).send({
|
||||||
|
success: true,
|
||||||
|
data: { data },
|
||||||
|
});
|
||||||
|
} catch (error) {}
|
||||||
|
});
|
||||||
|
|
||||||
|
// * R
|
||||||
|
router.get("/cats", (req, res) => {
|
||||||
|
try {
|
||||||
|
const cats = Cat;
|
||||||
|
res.status(200).send({
|
||||||
|
success: true,
|
||||||
|
data: { cats },
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
res.status(400).send({
|
||||||
|
success: false,
|
||||||
|
error: error.message,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
router.get("/cats/:id", (req, res) => {
|
||||||
|
try {
|
||||||
|
const params = req.params;
|
||||||
|
const cats = Cat.find((cat) => {
|
||||||
|
return cat.id === params.id;
|
||||||
|
});
|
||||||
|
if (!cats) throw new Error("no matched data");
|
||||||
|
res.status(200).send({
|
||||||
|
success: true,
|
||||||
|
data: { cats },
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
res.status(400).send({
|
||||||
|
success: false,
|
||||||
|
error: error.message,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// * U
|
||||||
|
router.put("/cats/:id", (req, res) => {
|
||||||
|
try {
|
||||||
|
const params = req.params;
|
||||||
|
const body = req.body;
|
||||||
|
let result;
|
||||||
|
const index = Cat.findIndex((cat) => cat.id === params.id);
|
||||||
|
if (index !== -1) {
|
||||||
|
const tempId = params.id;
|
||||||
|
const newCat = { ...body, id: tempId };
|
||||||
|
Cat[index] = newCat;
|
||||||
|
result = newCat;
|
||||||
|
}
|
||||||
|
res.status(200).send({
|
||||||
|
success: true,
|
||||||
|
data: { cat: result },
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
res.status(400).send({
|
||||||
|
success: false,
|
||||||
|
error: error.message,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
router.patch("/cats/:id", (req, res) => {
|
||||||
|
try {
|
||||||
|
const params = req.params;
|
||||||
|
const body = req.body;
|
||||||
|
let result;
|
||||||
|
const index = Cat.findIndex((cat) => cat.id === params.id);
|
||||||
|
if (index !== -1) {
|
||||||
|
const tempId = params.id;
|
||||||
|
const newCat = { ...Cat[index], ...body, id: tempId };
|
||||||
|
Cat[index] = newCat;
|
||||||
|
result = newCat;
|
||||||
|
}
|
||||||
|
res.status(200).send({
|
||||||
|
success: true,
|
||||||
|
data: { cat: result },
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
res.status(400).send({
|
||||||
|
success: false,
|
||||||
|
error: error.message,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// * D
|
||||||
|
router.delete("/cats/:id", (req, res) => {
|
||||||
|
try {
|
||||||
|
const params = req.params;
|
||||||
|
const body = req.body;
|
||||||
|
let result;
|
||||||
|
const index = Cat.findIndex((cat) => cat.id === params.id);
|
||||||
|
if (index !== -1) {
|
||||||
|
Cat.splice(index, 1);
|
||||||
|
}
|
||||||
|
res.status(200).send({
|
||||||
|
success: true,
|
||||||
|
data: { cat: result },
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
res.status(400).send({
|
||||||
|
success: false,
|
||||||
|
error: error.message,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
export default router;
|
Reference in New Issue
Block a user