Skip to content

Commit

Permalink
Add swagger api documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
LogicDX342 committed Mar 30, 2024
1 parent 8e4568f commit e2fb0de
Show file tree
Hide file tree
Showing 10 changed files with 267 additions and 67 deletions.
9 changes: 0 additions & 9 deletions create_database.sql

This file was deleted.

1 change: 1 addition & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"body-parser": "^1.20.2",
"cors": "^2.8.5",
"express": "^4.19.1",
"js-yaml": "^4.1.0",
"mysql2": "^3.9.2",
"sequelize": "^6.37.1",
"swagger-jsdoc": "^6.2.8",
Expand Down
3 changes: 3 additions & 0 deletions server/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 22 additions & 12 deletions server/src/models/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
const Sequelize = require('sequelize');
const UserModel = require('./user');
const Sequelize = require("sequelize");
const UserModel = require("./user");

const sequelize = new Sequelize('mysql://root:123qweasd@localhost:3306/cs304_project');
sequelize.authenticate()
.then(() => console.log('Connection has been established successfully.'))
.catch(err => console.error('Unable to connect to the database:', err));
const sequelize = new Sequelize("cs304_project", "root", "123qweasd", {
host: "localhost",
dialect: "mysql",
logging: (msg) => {
if (msg.startsWith("Executing")) {
console.log(msg);
}
},
});
sequelize
.authenticate()
.then(() =>
console.log("Connection to database has been established successfully.")
)
.catch((err) => console.error("Unable to connect to the database:", err));
const User = UserModel(sequelize, Sequelize);

sequelize.sync({ force: false })
.then(() => {
console.log(`Database & tables created!`)
});
sequelize.sync({ force: false }).then(() => {
console.log(`Database & tables created!`);
});

module.exports = {
User
};
User,
};
25 changes: 25 additions & 0 deletions server/src/models/user.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
/**
* @swagger
* components:
* schemas:
* User:
* type: object
* required:
* - username
* - password
* properties:
* id:
* type: integer
* description: The auto-generated id of the user
* readOnly: true
* username:
* type: string
* description: The username of the user
* password:
* type: string
* description: The password of the user
* example:
* id: 1
* username: "user1"
* password: "pass1"
*/
module.exports = (sequelize, type) => {
return sequelize.define('user', {
id: {
Expand Down
37 changes: 17 additions & 20 deletions server/src/routes/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,26 @@ const router = express.Router();
const { User } = require("../models");

/**
* @swagger
* @openapi
* /users/register:
* post:
* summary: Register a user
* consumes:
* - application/json
* parameters:
* - in: body
* name: body
* description: User object
* required: true
* schema:
* type: object
* required:
* - username
* - password
* properties:
* username:
* type: string
* password:
* type: string
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* required:
* - username
* - password
* properties:
* username:
* type: string
* password:
* type: string
* responses:
* 200:
* '200':
* description: User created successfully
*/
router.post("/register", async (req, res) => {
Expand Down Expand Up @@ -59,7 +56,7 @@ router.post("/register", async (req, res) => {
* content:
* application/json:
* schema:
* $ref: 'models/user'
* $ref: '#/components/schemas/User'
* 401:
* description: Invalid username or password
*/
Expand Down
29 changes: 3 additions & 26 deletions server/src/server.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,16 @@
const express = require("express");
const bodyParser = require("body-parser");
const routes = require("./routes");
const swaggerUi = require("swagger-ui-express");
const swaggerJsdoc = require("swagger-jsdoc");
const setupSwagger = require("./swagger");

const app = express();
app.use(bodyParser.json());
app.use("/api", routes);

const options = {
swaggerDefinition: {
openapi: "3.0.0",
info: {
title: "Campus Events and Entertainment Center API",
version: "1.0.0",
description: "API for managing campus events and entertainment",
},
servers: [
{
url: "http://localhost:5000/api",
},
],
},
apis: ["./src/routes/*.js"],
};
setupSwagger(app);

const specs = swaggerJsdoc(options);

app.use(
"/api-docs",
swaggerUi.serve,
swaggerUi.setup(specs, { explorer: true })
);
app.use(express.json());

app.listen(5000, () => {
console.log("Server is running on port 5000");
});
});
42 changes: 42 additions & 0 deletions server/src/swagger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const fs = require("fs");
const swaggerUi = require("swagger-ui-express");
const swaggerJsdoc = require("swagger-jsdoc");
const yaml = require("js-yaml");

const options = {
swaggerDefinition: {
openapi: "3.0.0",
info: {
title: "Campus Events and Entertainment Center API",
version: "1.0.0",
description: "API for managing campus events and entertainment",
},
servers: [
{
url: "http://localhost:5000/api",
},
],
},
apis: ["./src/routes/*.js", "./src/models/*.js"],
failOnErrors: true,
};

const specs = swaggerJsdoc(options);

const yamlString = yaml.dump(specs);

fs.writeFile("swagger-auto-generated.yaml", yamlString, (err) => {
if (err) {
console.log(err);
} else {
console.log("YAML file has been saved.");
}
});

module.exports = (app) => {
app.use(
"/api-docs",
swaggerUi.serve,
swaggerUi.setup(specs, { explorer: true })
);
};
77 changes: 77 additions & 0 deletions server/swagger-auto-generated.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
openapi: 3.0.0
info:
title: Campus Events and Entertainment Center API
version: 1.0.0
description: API for managing campus events and entertainment
servers:
- url: http://localhost:5000/api
paths:
/users/register:
post:
summary: Register a user
requestBody:
required: true
content:
application/json:
schema:
type: object
required:
- username
- password
properties:
username:
type: string
password:
type: string
responses:
'200':
description: User created successfully
/users/login:
post:
summary: Login a user
requestBody:
required: true
content:
application/json:
schema:
type: object
required:
- username
- password
properties:
username:
type: string
password:
type: string
responses:
'200':
description: User logged in successfully
content:
application/json:
schema:
$ref: '#/components/schemas/User'
'401':
description: Invalid username or password
components:
schemas:
User:
type: object
required:
- username
- password
properties:
id:
type: integer
description: The auto-generated id of the user
readOnly: true
username:
type: string
description: The username of the user
password:
type: string
description: The password of the user
example:
id: 1
username: user1
password: pass1
tags: []
77 changes: 77 additions & 0 deletions server/swagger.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
openapi: 3.0.0
info:
title: Campus Events and Entertainment Center API
version: 1.0.0
description: API for managing campus events and entertainment
servers:
- url: http://localhost:5000/api
paths:
/users/register:
post:
summary: Register a user
requestBody:
required: true
content:
application/json:
schema:
type: object
required:
- username
- password
properties:
username:
type: string
password:
type: string
responses:
'200':
description: User created successfully
/users/login:
post:
summary: Login a user
requestBody:
required: true
content:
application/json:
schema:
type: object
required:
- username
- password
properties:
username:
type: string
password:
type: string
responses:
'200':
description: User logged in successfully
content:
application/json:
schema:
$ref: '#/components/schemas/User'
'401':
description: Invalid username or password
components:
schemas:
User:
type: object
required:
- username
- password
properties:
id:
type: integer
description: The auto-generated id of the user
readOnly: true
username:
type: string
description: The username of the user
password:
type: string
description: The password of the user
example:
id: 1
username: user1
password: pass1
tags: []

0 comments on commit e2fb0de

Please sign in to comment.