Skip to content

Commit 90213d2

Browse files
committed
first commit
0 parents  commit 90213d2

File tree

779 files changed

+170887
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

779 files changed

+170887
-0
lines changed

Diff for: README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Code Hub
2+
3+
This repository it's a collection of all projects that I created to study a technology, development pattern or something else.

Diff for: express/rest-api-playlist/.env.example

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
PORT=4000
2+
LOCAL_MONGO_URI=mongodb://127.0.0.1:27017/ninjago

Diff for: express/rest-api-playlist/.gitignore

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Folders
2+
node_modules
3+
4+
# Archives
5+
.env
6+
package-lock*
7+
yarn*
8+
pnpm-lock*

Diff for: express/rest-api-playlist/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Rest API Playlist
2+
3+
Learning a thing or two about MongoDB and Mongoose through a The Net Ninja course.

Diff for: express/rest-api-playlist/nodemon.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"watch": ["src"],
3+
"ext": "ts"
4+
}

Diff for: express/rest-api-playlist/package.json

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "rest-api-playlist",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "src/app.ts",
6+
"scripts": {
7+
"build": "tsc",
8+
"start": "node dist/app.js",
9+
"dev": "nodemon src/app.ts"
10+
},
11+
"keywords": [],
12+
"author": "H3rmel (https://github.com/H3rmel)",
13+
"license": "ISC",
14+
"dependencies": {
15+
"@types/swagger-ui-express": "^4.1.6",
16+
"body-parser": "^1.20.2",
17+
"dotenv": "^16.4.5",
18+
"express": "^4.18.3",
19+
"express-async-errors": "^3.1.1",
20+
"mongoose": "^8.2.0",
21+
"swagger-autogen": "^2.23.7",
22+
"swagger-ui-express": "^5.0.0"
23+
},
24+
"devDependencies": {
25+
"@types/body-parser": "^1.19.5",
26+
"@types/express": "^4.17.21",
27+
"@types/node": "^20.11.24",
28+
"nodemon": "^3.1.0",
29+
"ts-node": "^10.9.2",
30+
"typescript": "^5.3.3"
31+
}
32+
}

Diff for: express/rest-api-playlist/src/app.ts

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// #region Imports
2+
3+
//* Global Dependencies
4+
import express, { Express } from "express";
5+
import dotenv from "dotenv";
6+
import { json } from "body-parser";
7+
import "express-async-errors";
8+
import swaggerUi from "swagger-ui-express";
9+
import swaggerOutput from "./swagger_output.json";
10+
11+
//* Project Dependencies
12+
import { router as ninjaRoutes } from "./routes/ninja";
13+
import { errorHandler } from "./middlewares/errors";
14+
15+
// #endregion
16+
17+
// #region Config
18+
19+
dotenv.config();
20+
21+
const app: Express = express();
22+
23+
// #endregion
24+
25+
// Middlewares
26+
app.use(json());
27+
28+
// Routes (Ninjas)
29+
app.use("/api", ninjaRoutes);
30+
31+
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerOutput));
32+
33+
// Error Handling
34+
app.use(errorHandler);
35+
36+
app.listen(process.env.PORT || 4000, () => {
37+
console.log(
38+
`[server]: Server is running at https://localhost:${
39+
process.env.PORT || 4000
40+
}`
41+
);
42+
});

Diff for: express/rest-api-playlist/src/database/connect.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { InternalServerError } from "../utils/errors";
2+
import { connect } from "mongoose";
3+
4+
export async function connectToMongo(): Promise<typeof import("mongoose")> {
5+
try {
6+
return await connect(process.env.LOCAL_MONGO_URI as string);
7+
} catch (error) {
8+
console.log(error);
9+
throw new InternalServerError("[server]: Could not connect to MongoDB");
10+
}
11+
}

Diff for: express/rest-api-playlist/src/database/models/geo.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Schema, model } from "mongoose";
2+
3+
export interface IGeo {
4+
type: string;
5+
coordinates: number[];
6+
}
7+
8+
export const GeoSchema = new Schema<IGeo>({
9+
type: {
10+
type: String,
11+
default: "Point",
12+
},
13+
coordinates: {
14+
type: [Number],
15+
index: "2dsphere",
16+
},
17+
});
18+
19+
export const GeoModel = model<IGeo>("geo", GeoSchema);
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { Schema, model } from "mongoose";
2+
import { GeoSchema, IGeo } from "./geo";
3+
4+
interface INinja {
5+
name: string;
6+
rank: string;
7+
available: boolean;
8+
geometry: IGeo;
9+
}
10+
11+
const NinjaSchema = new Schema<INinja>({
12+
name: {
13+
type: String,
14+
required: [true, "Name field is required"],
15+
},
16+
rank: {
17+
type: String,
18+
required: [true, "Rank field is required"],
19+
},
20+
available: {
21+
type: Boolean,
22+
default: false,
23+
},
24+
geometry: GeoSchema
25+
});
26+
27+
export const NinjaModel = model<INinja>("ninja", NinjaSchema);

Diff for: express/rest-api-playlist/src/middlewares/errors.ts

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { CustomError } from "../utils/errors";
2+
import { NextFunction, Request, Response } from "express";
3+
4+
export const errorHandler = (
5+
err: Error,
6+
req: Request,
7+
res: Response,
8+
next: NextFunction
9+
) => {
10+
//* Handled Errors
11+
if (err instanceof CustomError) {
12+
const { status, logging, message } = err;
13+
14+
if (logging) {
15+
console.error(message);
16+
}
17+
18+
return res.status(status).send({ message });
19+
}
20+
21+
// Unhandled errors
22+
console.error(JSON.stringify(err, null, 2));
23+
return res
24+
.status(500)
25+
.send({ errors: [{ message: "Something went wrong" }] });
26+
};

0 commit comments

Comments
 (0)