diff --git a/catDataMocking/src/app.ts b/catDataMocking/src/app.ts index 700a538..3f06e0a 100644 --- a/catDataMocking/src/app.ts +++ b/catDataMocking/src/app.ts @@ -3,29 +3,53 @@ import * as cors from "cors"; import catsRouter from "./cats/cats.route"; -const app: express.Express = express(); const port: number = 8000; - -app.use(cors()); - -// * Logging Middleware -app.use((req, res, next) => { - console.log(req.rawHeaders[1]); - next(); -}); - -// * json middleware -app.use(express.json()); - -app.use(catsRouter); - -// * 404 middleware -app.use((req, res, next) => { - console.log(req.rawHeaders[1]); - console.log("This is 404 middleware"); - res.send({ error: "404 not found error" }); -}); - -app.listen(port, () => { - console.log(`server is on ${port}`); -}); +class Server { + public app: express.Application; + + constructor() { + const app: express.Application = express(); + this.app = app; + } + + private setRoute() { + // * router + this.app.use(catsRouter); + } + + private setMiddleware() { + this.app.use(cors()); + + // * Logging Middleware + this.app.use((req, res, next) => { + console.log(req.rawHeaders[1]); + next(); + }); + + // * json middleware + this.app.use(express.json()); + + this.setRoute(); + + // * 404 middleware + this.app.use((req, res, next) => { + console.log(req.rawHeaders[1]); + console.log("This is 404 middleware"); + res.send({ error: "404 not found error" }); + }); + } + + public listen() { + this.setMiddleware(); + this.app.listen(port, () => { + console.log(`server is on ${port}`); + }); + } +} + +function init() { + const server = new Server(); + server.listen(); +} + +init(); diff --git a/catDataMocking/src/cats/cats.route.ts b/catDataMocking/src/cats/cats.route.ts index 054569f..19e4d4b 100644 --- a/catDataMocking/src/cats/cats.route.ts +++ b/catDataMocking/src/cats/cats.route.ts @@ -1,124 +1,21 @@ import { Router } from "express"; import { CatType, Cat } from "./cats.model"; +import { + createCat, + deleteCat, + readAllCat, + readCat, + updateCat, + updatePartialCat, +} from "./cats.service"; 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, - }); - } -}); +router.post("/cats", createCat); +router.get("/cats", readAllCat); +router.get("/cats/:id", readCat); +router.put("/cats/:id", updateCat); +router.patch("/cats/:id", updatePartialCat); +router.delete("/cats/:id", deleteCat); export default router; diff --git a/catDataMocking/src/cats/cats.service.ts b/catDataMocking/src/cats/cats.service.ts new file mode 100644 index 0000000..ff0b84b --- /dev/null +++ b/catDataMocking/src/cats/cats.service.ts @@ -0,0 +1,120 @@ +import { Request, Response } from "express"; +import { CatType, Cat } from "./cats.model"; + +// * C +export const createCat = (req: Request, res: Response) => { + try { + const data = req.body; + Cat.push(data); + res.status(200).send({ + success: true, + data: { data }, + }); + } catch (error) {} +}; + +// * R +export const readAllCat = (req: Request, res: Response) => { + try { + const cats = Cat; + res.status(200).send({ + success: true, + data: { cats }, + }); + } catch (error) { + res.status(400).send({ + success: false, + error: error.message, + }); + } +}; + +export const readCat = (req: Request, res: Response) => { + 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 +export const updateCat = (req: Request, res: Response) => { + 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, + }); + } +}; + +export const updatePartialCat = (req: Request, res: Response) => { + 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 +export const deleteCat = (req: Request, res: Response) => { + 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, + }); + } +};