Skip to content

Commit

Permalink
feat: migrated handler logic to separate folder
Browse files Browse the repository at this point in the history
  • Loading branch information
bstetzer32 committed Jan 14, 2022
1 parent 96b3879 commit 730ca78
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 71 deletions.
8 changes: 4 additions & 4 deletions server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ const asyncHandler = require('express-async-handler');
const { check, validationResult } = require('express-validator');
const { body } = require('express-validator');
const HttpError = require("./utils/HttpError");
const regionRouter = require("./routes/regionRouter"); // create your router
const {errorHandler} = require("./routes/utils");
const router = require("./routes"); // create your router
const {errorHandler} = require("./handlers/utils");
const log = require("loglevel");
const helper = require("./routes/utils");
const helper = require('./handlers/utils');
const config = require('../config/config')

const app = express();
Expand All @@ -31,7 +31,7 @@ app.use(bodyParser.urlencoded({ extended: false })); // parse application/x-www-
app.use(bodyParser.json()); // parse application/json

//routers
app.use('/region', regionRouter);
app.use('/', router);

//paths
//app.get('/entity', asyncHandler(async (req, res, next) => {
Expand Down
51 changes: 51 additions & 0 deletions server/handlers/regionHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const Session = require('../models/Session');
const RegionService = require('../services/RegionService');

const regionHandlerGet = async function (req, res, next) {
const { ownerId } = req.query;
const session = new Session();
const regionSerivce = new RegionService(session);
const ownerRegions = regionSerivce.regionRepository.getAllByOwnerId(ownerId);
res.status(200).json({
regions: ownerRegions,
});
};

const regionHandlerGetByRegionId = async function (req, res, next) {
const { regionId } = req.params;
const session = new Session();
const regionSerivce = new RegionService(session);
const region = regionSerivce.getById(regionId);
res.status(200).json({
region,
});
};

const regionHandlerPost = async function (req, res, next) {
const region = req.body;
const session = new Session();
const regionSerivce = new RegionService(session);
const newRegion = await regionSerivce.createRegion(region);
res.status(200).json({
region: newRegion,
});
};

const regionHandlerPut = async function (req, res, next) {
const { regionId } = req.params;
const region = req.body;
region.id = regionId;
const session = new Session();
const regionSerivce = new RegionService(session);
const updatedRegion = await regionSerivce.updateRegion(region);
res.status(200).json({
region: updatedRegion,
});
};

module.exports = {
regionHandlerGet,
regionHandlerGetByRegionId,
regionHandlerPost,
regionHandlerPut,
};
File renamed without changes.
31 changes: 31 additions & 0 deletions server/routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const express = require('express');
const { handlerWrapper } = require('./handlers/utils');
const {
regionHandlerGet,
regionHandlerGetByRegionId,
regionHandlerPost,
regionHandlerPut,} = require('./handlers/regionHandler')

const router = express.Router();

router.get(
'/',
handlerWrapper(regionHandlerGet),
);

router.get(
'/:regionId',
handlerWrapper(regionHandlerGetByRegionId),
);

router.post(
'/',
handlerWrapper(regionHandlerPost),
);

router.put(
'/:regionId',
handlerWrapper(regionHandlerPut),
);

module.exports = router;
67 changes: 0 additions & 67 deletions server/routes/regionRouter.js

This file was deleted.

0 comments on commit 730ca78

Please sign in to comment.