Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

71 adding swagger documentation for users #86

Merged
merged 5 commits into from
Aug 8, 2023
Merged
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
40 changes: 39 additions & 1 deletion src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@ const connectToMongo = require('./db/connection');
const cookieParser = require('cookie-parser');
require('./middleware/passport-setup');
const apiRoutes = require('./routes');
const swaggerJsdoc = require("swagger-jsdoc");
const swaggerUi = require("swagger-ui-express");
const cart = require('./models/cart');

const app = express();
const port = 3000;




app.use(express.json());
app.set('view engine', 'ejs');
app.set('views', './views');
Expand All @@ -27,8 +33,40 @@ app.get('/', (req, res) => {
});
app.use(passport.initialize());

const options = {
definition: {
openapi: "3.0.0",
info: {
title: "Local eat API with Swagger",
version: "0.1.0",
description:
"This is a simple API application made with Express and documented with Swagger",
license: {
name: "MIT",
url: "https://spdx.org/licenses/MIT.html",
},
contact: {
name: "Your Name",
url: "https://re-coded.com",
email: "[email protected]",
},
},
servers: [
{
url: "http://localhost:3000",
},
],
},

apis: [`src/routes/cart.js`],
apis: [`src/routes/users.js`],
};

const specs = swaggerJsdoc(options);
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(specs, { explorer: true }));

app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});

module.exports = app;
module.exports = app;
1 change: 0 additions & 1 deletion src/controllers/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const User = require('../models/users');
const { generateToken } = require('../middleware/checkAuth');
const sendEmail = require('../utils/email');

const sendEmail = require('../utils/email');

require('dotenv').config();

Expand Down
188 changes: 186 additions & 2 deletions src/routes/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,196 @@ const router = express.Router();
const userController = require('../controllers/users');
const checkAuth = require('../middleware/checkAuth');

/**
* @swagger
* components:
* schemas:
* User:
* type: object
* required:
* - user_id
* - userName
* - email
* - password
*
* properties:
* user_id:
* type: integer
* description: The auto-generated id number of the user.
* userName:
* type: string
* description: The name of user.
* email:
* type: string
* description: user's email.
* password:
* type: string (hashed)
* description: user's password.
* address:
* type: subdocument
* description: user's address.
* phoneNumber:
* type: integer
* description: user's phone number.
* role:
* type: string
* description: user type (regular user or cook).
*/

/**
* @swagger
*info:
* title: User Management API
* description: API for managing user data
* version: 1.0.0
*paths:
* /users:
* post:
* summary: Create a new user
* requestBody:
* required: true
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/UserInput'
* responses:
* '201':
* description: User created successfully
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/User'
* '400':
* description: Bad request
* get:
* summary: Get all users
* responses:
* '200':
* description: List of users
* content:
* application/json:
* schema:
* type: array
* items:
* $ref: '#/components/schemas/User'
* '500':
* description: Internal server error
* /users/{userId}:
* get:
* summary: Get a user by ID
* parameters:
* - in: path
* name: userId
* required: true
* schema:
* type: string
* description: ID of the user to retrieve
* responses:
* '200':
* description: User details
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/User'
* '400':
* description: Bad request
* '404':
* description: User not found
* '500':
* description: Internal server error
* put:
* summary: Update a user by ID
* parameters:
* - in: path
* name: userId
* required: true
* schema:
* type: string
* description: ID of the user to update
* requestBody:
* required: true
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/UserInput'
* responses:
* '200':
* description: User updated successfully
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/User'
* '400':
* description: Bad request
* '404':
* description: User not found
* '500':
* description: Internal server error
* delete:
* summary: Delete a user by ID
* parameters:
* - in: path
* name: userId
* required: true
* schema:
* type: string
* description: ID of the user to delete
* responses:
* '200':
* description: User deleted successfully
* '400':
* description: Bad request
* '404':
* description: User not found
* '500':
* description: Internal server error
*components:
* schemas:
* User:
* type: object
* properties:
* _id:
* type: string
* description: User ID
* userName:
* type: string
* description: User name
* email:
* type: string
* description: User email
* createdAt:
* type: string
* format: date-time
* description: User creation timestamp
* updatedAt:
* type: string
* format: date-time
* description: User last update timestamp
* required:
* - _id
* - userName
* - email
* - createdAt
* - updatedAt
* UserInput:
* type: object
* properties:
* userName:
* type: string
* description: User name
* email:
* type: string
* description: User email
* required:
* - userName
* - email
*/

// Define routes without '/users' at the beginning
router.get('/', checkAuth.authenticate, userController.getAllUsers);
router.post('/', userController.createUser);
router.get('/:id', checkAuth.authenticate, userController.getUserById);
router.put('/:id', checkAuth.authenticate, userController.updateUser);
router.delete('/:id', checkAuth.authenticate, userController.deleteUser);
router.put('/:id/address', checkAuth.authenticate, userController.updateAddress);
router.get('/:id/address',checkAuth.authenticate, userController.getAddress);

module.exports = router;