feat(users + general utils): users basic routes added and used to make and test basic global utils
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import mysql, { QueryError } from "mysql2/promise";
|
||||
import { ResponseError } from "./utils/error/response-error.model";
|
||||
import { ResponseError } from "./utils/response/response-error.model";
|
||||
|
||||
const db_pool = mysql.createPool({
|
||||
host: "localhost",
|
||||
|
||||
8
back-express/src/routes/default.route.ts
Normal file
8
back-express/src/routes/default.route.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { Router } from "express";
|
||||
|
||||
export const defaultRoute = Router();
|
||||
|
||||
defaultRoute.get("/", (_, res) => {
|
||||
res.send("Nothing to see here");
|
||||
res.status(200);
|
||||
});
|
||||
@@ -1,17 +0,0 @@
|
||||
import { Router } from "express";
|
||||
|
||||
export const defaultRoute = Router();
|
||||
|
||||
defaultRoute.get("/", (req, res) => {
|
||||
res.status(400);
|
||||
res.send("Something");
|
||||
});
|
||||
|
||||
defaultRoute.get("/", (req, res) => {
|
||||
res.status(400);
|
||||
res.send("Something");
|
||||
});
|
||||
|
||||
defaultRoute.post("/test", (req, res) => {
|
||||
res.send("Something with " + JSON.stringify(req.body));
|
||||
});
|
||||
@@ -1,6 +1,6 @@
|
||||
import express from "express";
|
||||
|
||||
import { defaultRoute } from "./defaultRoute";
|
||||
import { defaultRoute } from "./default.route";
|
||||
import { exampleRoutes } from "./example/example.routes";
|
||||
import { userRoutes } from "./users/users.routes";
|
||||
|
||||
|
||||
@@ -1,16 +1,19 @@
|
||||
import { Router } from "express";
|
||||
import UserService from "./users.service";
|
||||
import { sanitize_user } from "./users.utils";
|
||||
import { ResponseSuccess } from "../../utils/response/response-success.model";
|
||||
import { ResponseError } from "../../utils/response/response-error.model";
|
||||
|
||||
export const userRoutes = Router();
|
||||
|
||||
const userService = new UserService();
|
||||
|
||||
//TODO: remove info from these Users and block access to NON-admins
|
||||
//TODO: block access to NON-admins or simply comment
|
||||
userRoutes.get("/", async (_, res) => {
|
||||
try {
|
||||
const response = await userService.getAllUsers();
|
||||
res.status(200);
|
||||
res.send(response);
|
||||
res.send(new ResponseSuccess(response.map((user) => sanitize_user(user))));
|
||||
} catch (e) {
|
||||
res.status(500);
|
||||
res.send(e);
|
||||
@@ -22,10 +25,10 @@ userRoutes.get("/:username", async (req, res) => {
|
||||
const response = await userService.getUserByUsername(req.params.username);
|
||||
if (response) {
|
||||
res.status(200);
|
||||
res.send(response);
|
||||
res.send(new ResponseSuccess(sanitize_user(response)));
|
||||
} else {
|
||||
res.status(400);
|
||||
res.send("error");
|
||||
res.status(404);
|
||||
res.send(new ResponseError({ code: "NOT_FOUND", number: 404 }));
|
||||
}
|
||||
} catch (e) {
|
||||
res.status(500);
|
||||
|
||||
@@ -5,7 +5,7 @@ class UserService {
|
||||
constructor() {}
|
||||
|
||||
async getAllUsers(): Promise<User[]> {
|
||||
const data = await db_query("select * from user where id == 1");
|
||||
const data = await db_query("select * from user");
|
||||
return data as User[];
|
||||
}
|
||||
|
||||
@@ -16,20 +16,6 @@ class UserService {
|
||||
console.log("Data:", data);
|
||||
return data.length ? (data[0] as User) : null;
|
||||
}
|
||||
|
||||
// getUserById(id: number) {
|
||||
// return new Promise((resolve, reject) =>
|
||||
// db_connection.query("select * from user", (err, rows, fields) => {
|
||||
// console.log(rows);
|
||||
// if (err) {
|
||||
// console.error("Mysql Error on User SELECT: ", err);
|
||||
// reject(err);
|
||||
// } else {
|
||||
// resolve(rows);
|
||||
// }
|
||||
// }),
|
||||
// );
|
||||
// }
|
||||
}
|
||||
|
||||
export default UserService;
|
||||
|
||||
@@ -11,4 +11,7 @@ export type User = {
|
||||
password: string;
|
||||
roles: Role[];
|
||||
picture: string;
|
||||
created_at: string;
|
||||
};
|
||||
|
||||
export type UserDetailResult = Omit<User, "password">;
|
||||
|
||||
7
back-express/src/routes/users/users.utils.ts
Normal file
7
back-express/src/routes/users/users.utils.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { User, UserDetailResult } from "./users.types";
|
||||
|
||||
// TODO: prettify this
|
||||
export function sanitize_user(user: User): UserDetailResult {
|
||||
delete (user as any).password;
|
||||
return user;
|
||||
}
|
||||
16
back-express/src/utils/response/response-success.model.ts
Normal file
16
back-express/src/utils/response/response-success.model.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
export class ResponseSuccess {
|
||||
public data?: unknown;
|
||||
public results?: unknown[];
|
||||
public count?: number;
|
||||
|
||||
constructor(data: unknown) {
|
||||
if (data instanceof Array) {
|
||||
this.results = data;
|
||||
this.count = data.length;
|
||||
} else if (data instanceof Object) {
|
||||
Object.assign(this, data);
|
||||
} else {
|
||||
this.data = data;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user