Skip to content
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
346 changes: 342 additions & 4 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,30 @@
"dependencies": {
"@types/event-stream": "^3.3.34",
"@types/node": "^16.10.1",
"bunyan": "^1.8.15",
"csvtojson": "^2.0.10",
"dotenv": "^10.0.0",
"event-stream": "^4.0.1",
"express": "^4.17.1",
"express-joi-validation": "^5.0.0",
"joi": "^17.4.2",
"morgan": "^1.10.0",
"pg": "^8.7.1",
"pg-hstore": "^2.3.4",
"sequelize": "^6.9.0",
"tslint": "^6.1.3",
"typescript": "^4.4.3"
"typescript": "^4.4.3",
"winston": "^3.3.3"
},
"devDependencies": {
"@babel/cli": "^7.15.7",
"@babel/core": "^7.15.5",
"@babel/node": "^7.15.4",
"@babel/preset-env": "^7.15.6",
"@babel/preset-typescript": "^7.15.0",
"@types/bunyan": "^1.8.8",
"@types/express": "^4.17.13",
"@types/morgan": "^1.9.3",
"@typescript-eslint/eslint-plugin": "^5.1.0",
"@typescript-eslint/parser": "^5.1.0",
"babel-eslint": "^10.1.0",
Expand Down
66 changes: 41 additions & 25 deletions src/module-3/controllers/groups.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import express from "express";
import { createValidator } from "express-joi-validation";
import { log, logger } from "../loggers";
import * as GroupsService from "../services/groups";
import { Errors, GroupModel } from "../types";
import { isNull } from "./utils";
Expand All @@ -10,53 +11,64 @@ const router = express.Router();

/* Get all groups */

router.get("/", async (req, res) => {
router.get("/", log(GroupsService.findAll), async (req, res) => {
try {
const groups = await GroupsService.findAll();
res.status(200).send(groups);
} catch (e) {
logger.setError(e.message);
res.status(500).send(e.message);
}
});

/* Find group by id */

router.get("/:id", async (req, res) => {
router.get("/:id", log(GroupsService.find), async (req, res) => {
try {
const id = req.params.id;
const group = await GroupsService.find(id);
if (group) {
return res.status(200).send(group);
}
res.status(404).send("Group not found");
const err = "Group not found";
logger.setError(err);
res.status(404).send(err);
} catch (e) {
res.status(500).send(e.message);
}
});

/* Create new group */

router.post("/", validator.body(groupValidationSchema), async (req, res) => {
try {
const body: GroupModel = req.body;
const isSuccess = await GroupsService.create(body);
if (!isSuccess) {
return res.status(400).send("Group with this name is already exists");
router.post(
"/",
[log(GroupsService.create), validator.body(groupValidationSchema)],
async (req, res) => {
try {
const body: GroupModel = req.body;
const isSuccess = await GroupsService.create(body);
if (!isSuccess) {
const err = "Group with this name is already exists";
logger.setError(err);
return res.status(400).send(err);
}
res.redirect("/api/groups");
} catch (e) {
res.status(500).send(e.message);
}
res.redirect("/api/groups");
} catch (e) {
res.status(500).send(e.message);
}
});
);

/* Delete group */

router.delete("/:id", async (req, res) => {
router.delete("/:id", log(GroupsService.remove), async (req, res) => {
try {
const id = req.params.id;
const isSuccess = await GroupsService.remove(id);
if (isNull(isSuccess)) {
return res.status(404).send("Something wrong");
const err = "Something wrong";
logger.setError(err);
return res.status(404).send(err);
}
res.redirect("/api/groups");
} catch (e) {
Expand All @@ -66,17 +78,21 @@ router.delete("/:id", async (req, res) => {

/* Update group */

router.put("/:id", validator.body(groupValidationSchema), async (req, res) => {
try {
const id = req.params.id;
const status = await GroupsService.update(id, req.body);
if ((status as Errors).type === "error") {
return res.status(404).send((status as Errors).message);
router.put(
"/:id",
[log(GroupsService.update), validator.body(groupValidationSchema)],
async ({ params: { id }, body }, res) => {
try {
const status = await GroupsService.update(id, body);
if ((status as Errors).type === "error") {
logger.setError(status);
return res.status(404).send((status as Errors).message);
}
res.redirect("/api/groups");
} catch (e) {
res.status(500).send(e.message);
}
res.redirect("/api/groups");
} catch (e) {
res.status(500).send(e.message);
}
});
);

export default router;
14 changes: 10 additions & 4 deletions src/module-3/controllers/user-groups.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import express from "express";
import { createValidator } from "express-joi-validation";
import { log, logger } from "../loggers";
import { AddUsersToGroupModel } from "../services/types";
import * as UserGroupsService from "../services/user-groups";
import userGroupValidationSchema from "./validation/user-groups";
Expand All @@ -9,11 +10,12 @@ const router = express.Router();

/* Get user-groups list */

router.get("/", async (req, res) => {
router.get("/", log(UserGroupsService.findAll), async (req, res) => {
try {
const userGroups = await UserGroupsService.findAll();
res.status(200).send(userGroups);
} catch (e) {
logger.setError(e);
res.status(500).send(e.message);
}
});
Expand All @@ -22,13 +24,17 @@ router.get("/", async (req, res) => {

router.post(
"/",
validator.body(userGroupValidationSchema),
[
log(UserGroupsService.addUsersToGroup),
validator.body(userGroupValidationSchema)
],
async (req, res) => {
try {
const body: AddUsersToGroupModel = req.body;
await UserGroupsService.addUsersToGroup(body.groupId, body.userIds);
const { groupId, userIds }: AddUsersToGroupModel = req.body;
await UserGroupsService.addUsersToGroup(groupId, userIds);
res.redirect("/api/user-groups");
} catch (e) {
logger.setError(e);
res.status(500).send(e.message);
}
}
Expand Down
90 changes: 56 additions & 34 deletions src/module-3/controllers/users.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import express from "express";
import { createValidator } from "express-joi-validation";
import { log, logger } from "../loggers";
import * as UsersService from "../services/users";
import { UserModel } from "../types";
import { isNull } from "./utils";
Expand All @@ -10,56 +11,71 @@ const router = express.Router();

/* Get users list */

router.get("/", async (req, res) => {
try {
const users = await UsersService.getAutoSuggestUsers(
req.body.loginSubstring,
req.body.limit
);
res.status(200).send(users);
} catch (e) {
res.status(500).send(e.message);
router.get(
"/",
log(UsersService.getAutoSuggestUsers),
async ({ body }, res) => {
try {
const users = await UsersService.getAutoSuggestUsers(
body.loginSubstring,
body.limit
);
res.status(200).send(users);
} catch (e) {
logger.setError(e);
res.status(500).send(e.message);
}
}
});
);

/* Find user by id */

router.get("/:id", async (req, res) => {
router.get("/:id", log(UsersService.find), async (req, res) => {
try {
const id = req.params.id;
const user = await UsersService.find(id);
if (user) {
return res.status(200).send(user);
}
res.status(404).send("User not found");
const err = "User not found";
logger.setError(err);
res.status(404).send(err);
} catch (e) {
res.status(500).send(e.message);
}
});

/* Create user */

router.post("/", validator.body(userValidationSchema), async (req, res) => {
try {
const body: UserModel = req.body;
const isSuccess = await UsersService.create(body);
if (!isSuccess) {
return res.status(400).send("User with this login is already exists");
router.post(
"/",
[log(UsersService.create), validator.body(userValidationSchema)],
async (req, res) => {
try {
const body: UserModel = req.body;
const isSuccess = await UsersService.create(body);
if (!isSuccess) {
const err = "User with this login is already exists";
logger.setError(err);
return res.status(400).send(err);
}
res.redirect("/api/users");
} catch (e) {
res.status(500).send(e.message);
}
res.redirect("/api/users");
} catch (e) {
res.status(500).send(e.message);
}
});
);

/* Delete user */

router.delete("/:id", async (req, res) => {
router.delete("/:id", log(UsersService.remove), async (req, res) => {
try {
const id = req.params.id;
const isSuccess = await UsersService.remove(id);
if (isNull(isSuccess)) {
return res.status(404).send("User with this id is not found");
const err = "User with this id is not found";
logger.setError(err);
return res.status(404).send(err);
}
res.redirect("/api/users");
} catch (e) {
Expand All @@ -69,17 +85,23 @@ router.delete("/:id", async (req, res) => {

/* Update user */

router.put("/:id", validator.body(userValidationSchema), async (req, res) => {
try {
const id = req.params.id;
const isSuccess = await UsersService.update(id, req.body);
if (!isSuccess) {
return res.status(404).send("User with this id is not found");
router.put(
"/:id",
[log(UsersService.update), validator.body(userValidationSchema)],
async (req, res) => {
try {
const id = req.params.id;
const isSuccess = await UsersService.update(id, req.body);
if (!isSuccess) {
const err = "User with this id is not found";
logger.setError(err);
return res.status(404).send(err);
}
res.redirect("/api/users");
} catch (e) {
res.status(500).send(e.message);
}
res.redirect("/api/users");
} catch (e) {
res.status(500).send(e.message);
}
});
);

export default router;
10 changes: 10 additions & 0 deletions src/module-3/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,18 @@ import groupsRouter from "./controllers/groups";
import userGroupsRouter from "./controllers/user-groups";
import usersRouter from "./controllers/users";
import db from "./data-access";
import {
errorLoggerMiddleware,
initHandlers,
mainLoggerMiddleware
} from "./loggers";

const app = express();

initHandlers();

app.use(errorLoggerMiddleware);

app.listen(3000, () =>
db
.authenticate()
Expand All @@ -16,6 +25,7 @@ app.listen(3000, () =>
)
.then(() => app.use(express.json()))
.then(() => {
app.use(mainLoggerMiddleware);
app.use("/api/groups", groupsRouter);
app.use("/api/users", usersRouter);
app.use("/api/user-groups", userGroupsRouter);
Expand Down
Loading