diff --git a/Diagramme sans nom.drawio b/Diagramme sans nom.drawio new file mode 100644 index 00000000..1c48b8b7 --- /dev/null +++ b/Diagramme sans nom.drawio @@ -0,0 +1,483 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/api.yml b/api.yml index 8a366fd9..d4d501c9 100644 --- a/api.yml +++ b/api.yml @@ -1,767 +1,453 @@ -swagger: "2.0" +openapi: 3.0.3 info: - description: "This a Documentation of your API for the Assignment" - version: "1.0.0" - title: "Storify" - termsOfService: "http://swagger.io/terms/" - contact: - email: "omar.belguith@alumni.medtech.tn" - license: - name: "Apache 2.0" - url: "http://www.apache.org/licenses/LICENSE-2.0.html" -host: "localhost:3000" -basePath: "/api" -tags: -- name: "Auth" - description: "Everything about Authentication" -- name: "Lists" - description: "Access to Storify Lists" -- name: "Items" - description: "Operations about Items" -- name: "Categories" - description: "Operations about Items" -schemes: -- "https" -- "http" + title: Neuronic Device API + description: API for controlling and interacting with the Neuronic device + version: 1.0.0 + paths: - /login: - post: - tags: - - "Auth" - summary: "Authenticate a user to your Platform" - description: "" - operationId: "login" - consumes: - - "application/json" - produces: - - "application/json" - parameters: - - in: "body" - name: "body" - description: "User Credentials" - required: true - schema: - $ref: "#/definitions/LoginRequest" + /programs: + get: + summary: Get a list of available programs responses: - 201: - description: "successful operation" - schema: - $ref: "#/definitions/LoginResponse" - 401: - $ref: "#/responses/401" - 422: - $ref: "#/responses/422" - 500: - $ref: "#/responses/500" - /signup: + "200": + description: Successful response + content: + application/json: + schema: + type: array + items: + $ref: "#/components/schemas/Program" + post: - tags: - - "Auth" - summary: "Registering a User" - operationId: "signup" - consumes: - - "application/json" - produces: - - "application/json" - parameters: - - in: "body" - name: "body" - description: "Email and password to authenticate" - required: true - schema: - $ref: "#/definitions/User" - responses: - 201: - description: "successful operation" - 422: - $ref: "#/responses/422" - 500: - $ref: "#/responses/500" - /getUser: - get: - operationId: "getUser" - summary: "Get the connected user informations" - tags: - - "Auth" + summary: Create a custom program + requestBody: + required: true + content: + application/json: + schema: + $ref: "#/components/schemas/ProgramInput" responses: - 200: - description: "Success" - schema: - $ref: "#/definitions/User" - 401: - $ref: "#/responses/401" - 404: - $ref: "#/responses/404" - 500: - $ref: "#/responses/500" - security: - - basicAuth: [] - - /lists: + "201": + description: Program created successfully + "400": + description: Bad request + + /programs/{programId}: get: - summary: "Fetch all the connected user lists" - operationId: "getLists" - tags: - - "Lists" - produces: - - "applicatin/json" - responses: - 201: - description: "Success" - schema: - $ref: "#/definitions/ListArray" - 401: - $ref: "#/responses/401" - 500: - $ref: "#/responses/500" - security: - - basicAuth: [] - - post: - summary: "Create a list for the connected user" - operationId: "createList" - tags: - - "Lists" - description: "This endpoint allows user to create a list" - consumes: - - "application/json" - produces: - - "application/json" + summary: Get details of a specific program parameters: - - in: "body" - name: "body" + - name: programId + in: path + required: true + description: ID of the program schema: - type: "object" - required: - - name - properties: - name: - type: string - example: "List name" + type: integer responses: - 201: - description: "Success" - schema: - $ref: "#/definitions/List" - 401: - $ref: "#/responses/401" - 422: - $ref: "#/responses/422" - 500: - $ref: "#/responses/500" - security: - - basicAuth: [] - /lists/{list_id}: + "200": + description: Successful response + content: + application/json: + schema: + $ref: "#/components/schemas/Program" + "404": + description: Program not found + put: - operationId: "updateList" - summary: "Update a list for the connected user" - tags: - - "Lists" - description: "This endpoint allows user to update a list" - consumes: - - "application/json" - produces: - - "application/json" + summary: Update an existing program parameters: - - name: list_id + - name: programId in: path - description: ID of list to fetch required: true - type: integer - format: int64 - - in: "body" - name: "body" + description: ID of the program schema: - type: "object" - required: - - name - properties: - name: - type: string - example: "List name" + type: integer + requestBody: + required: true + content: + application/json: + schema: + $ref: "#/components/schemas/ProgramInput" responses: - 201: - description: "Success" - schema: - $ref: "#/definitions/List" - 401: - $ref: "#/responses/401" - 422: - $ref: "#/responses/422" - 500: - $ref: "#/responses/500" - security: - - basicAuth: [] - delete: - summary: "Delete a list for the connected user" - tags: - - "Lists" - description: "This endpoint allows user to delete a list" - consumes: - - "application/json" - produces: - - "application/json" + "200": + description: Program updated successfully + "400": + description: Bad request + "404": + description: Program not found + /programs/{programId}/usage: + get: + summary: Get usage details of a specific program + description: Retrieve the usage details and status of a program based on its ID. parameters: - - name: list_id + - name: programId in: path - description: ID of list to fetch required: true - type: integer - format: int64 + description: ID of the program + schema: + type: integer responses: - 200: - description: "Success" - 401: - $ref: "#/responses/401" - 500: - $ref: "#/responses/500" - security: - - basicAuth: [] - - /lists/{list_id}/items: + "200": + description: Successful response + content: + application/json: + schema: + $ref: "#/components/schemas/ProgramUsage" + "404": + description: Program not found + /clinical-protocol: get: - summary: "Fetch all the connected user items of a certain list" - operationId: "getItemsOfList" - description: "This endpoint allows user to get an item of a specific list" - - tags: - - "Lists" - produces: - - "applicatin/json" - parameters: - - name: list_id - in: path - description: ID of list to fetch - required: true - type: integer - format: int64 + summary: Get all clinical protocols responses: - 201: - description: "Success" - schema: - type: "array" - items: - $ref: "#/definitions/Item" - 401: - $ref: "#/responses/401" - 500: - $ref: "#/responses/500" - security: - - basicAuth: [] + "200": + description: Successful response + content: + application/json: + schema: + type: array + items: + $ref: "#/components/schemas/ClinicalProtocolResponse" post: - summary: "Add an Item to a specific list for the user" - operationId: "createItemInList" - tags: - - "Lists" - description: "This endpoint allows user to add an item to a specific list" - consumes: - - "application/json" - produces: - - "application/json" + summary: Start a clinical protocol session + requestBody: + required: true + content: + application/json: + schema: + $ref: "#/components/schemas/ClinicalProtocolInput" + responses: + "200": + description: Successful response + content: + application/json: + schema: + $ref: "#/components/schemas/ClinicalProtocolResponse" + "400": + description: Bad request + /clinical-protocol/{sessionId}: + get: + summary: Get details of a specific clinical protocol session parameters: - - name: list_id + - name: sessionId in: path - description: ID of list to fetch required: true - type: integer - format: int64 - - in: "body" - name: "body" + description: ID of the clinical protocol session schema: - type: "object" - required: - - item_id - - list_id - properties: - item_id: - type: "number" - list_id: - type: "number" + type: string responses: - 201: - description: "Success" - schema: - $ref: "#/definitions/List" - 401: - $ref: "#/responses/401" - 422: - $ref: "#/responses/422" - 500: - $ref: "#/responses/500" - security: - - basicAuth: [] + "200": + description: Successful response + content: + application/json: + schema: + $ref: "#/components/schemas/ClinicalProtocolResponse" + "404": + description: Clinical protocol session not found + put: - summary: "Update an Item of a specific list for the user" - operationId: "updateItemOfList" - tags: - - "Lists" - description: "This endpoint allows user to update an item in a list" - consumes: - - "application/json" - produces: - - "application/json" + summary: Update an existing clinical protocol session parameters: - - name: list_id + - name: sessionId in: path - description: ID of list to fetch required: true - type: integer - format: int64 - - in: "body" - name: "body" + description: ID of the clinical protocol session schema: - type: "object" - required: - - item_id - - list_id - properties: - item_id: - type: "number" - list_id: - type: "number" - name: - type: "string" - image: - type: "string" - category_id: - type: "number" - note: - type: "string" - responses: - 201: - description: "Success" - schema: - $ref: "#/definitions/Item" - 401: - $ref: "#/responses/401" - 422: - $ref: "#/responses/422" - 500: - $ref: "#/responses/500" - security: - - basicAuth: [] - delete: - summary: "Delete an Item of a specific list for the user" - operationId: "DeleteItemOfList" - tags: - - "Lists" - description: "This endpoint allows user to delete an item in a list" - consumes: - - "application/json" - produces: - - "application/json" - parameters: - - name: list_id - in: path - description: ID of list to fetch - required: true - type: integer - format: int64 + type: string + requestBody: + required: true + content: + application/json: + schema: + $ref: "#/components/schemas/ClinicalProtocolInput" responses: - 200: - description: "Success" - 401: - $ref: "#/responses/401" - 500: - $ref: "#/responses/500" - security: - - basicAuth: [] - - /items: + "200": + description: Clinical protocol session updated successfully + "400": + description: Bad request + "404": + description: Clinical protocol session not found + + /notifications: get: - summary: "Fetch all the connected user items" - operationId: "getItems" - tags: - - "Items" - produces: - - "applicatin/json" + summary: Get all notifications responses: - 201: - description: "Success" - schema: - type: "array" - items: - $ref: "#/definitions/Item" - 401: - $ref: "#/responses/401" - 500: - $ref: "#/responses/500" - security: - - basicAuth: [] - + "200": + description: Successful response + content: + application/json: + schema: + type: array + items: + $ref: "#/components/schemas/Notification" + post: - summary: "Create an Item for the connected user" - operationId: "createItem" - tags: - - "Items" - description: "This endpoint allows user to create an item" - consumes: - - "application/json" - produces: - - "application/json" - parameters: - - in: "body" - name: "body" - schema: - type: "object" - required: - - name - - image - - note - - category_id - properties: - name: - type: "string" - image: - type: "string" - note: - type: "string" - category_id: - type: "number" + summary: Create a new notification + requestBody: + required: true + content: + application/json: + schema: + $ref: "#/components/schemas/NotificationCreate" responses: - 200: - description: "Success" - schema: - $ref: "#/definitions/Item" - 401: - $ref: "#/responses/401" - 422: - $ref: "#/responses/422" - 500: - $ref: "#/responses/500" - security: - - basicAuth: [] - /items/{item_id}: - put: - summary: "Update an Item of a user" - operationId: "updateItem" - tags: - - "Items" - description: "This endpoint allows user to update an item" - consumes: - - "application/json" - produces: - - "application/json" + "201": + description: Notification created successfully + "400": + description: Bad request + /notifications/{notificationId}: + get: + summary: Get details of a specific notification parameters: - - name: item_id + - name: notificationId in: path - description: ID of item to fetch required: true - type: integer - format: int64 - - in: "body" - name: "body" + description: ID of the notification schema: - type: "object" - required: - - name - - image - - note - - category_id - properties: - name: - type: "string" - image: - type: "string" - category_id: - type: "number" - note: - type: "string" + type: string responses: - 201: - description: "Success" - schema: - $ref: "#/definitions/Item" - 401: - $ref: "#/responses/401" - 422: - $ref: "#/responses/422" - 500: - $ref: "#/responses/500" - security: - - basicAuth: [] - delete: - summary: "Delete an Item of a user" - operationId: "DeleteItem" - tags: - - "Items" - description: "This endpoint allows user to delete an item" - consumes: - - "application/json" - produces: - - "application/json" + "200": + description: Successful response + content: + application/json: + schema: + $ref: "#/components/schemas/Notification" + "404": + description: Notification not found + + put: + summary: Update a specific notification parameters: - - name: item_id + - name: notificationId in: path - description: ID of item to fetch required: true - type: integer - format: int64 - responses: - 200: - description: "Success" - 401: - $ref: "#/responses/401" - 500: - $ref: "#/responses/500" - security: - - basicAuth: [] - /categories: - get: - summary: "Fetch all the connected user categories" - operationId: "getCategories" - tags: - - "Categories" - produces: - - "applicatin/json" - responses: - 201: - description: "Success" + description: ID of the notification schema: - type: "array" - items: - $ref: "#/definitions/Category" - 401: - $ref: "#/responses/401" - 500: - $ref: "#/responses/500" - security: - - basicAuth: [] - + type: string + requestBody: + required: true + content: + application/json: + schema: + $ref: "#/components/schemas/NotificationUpdate" + responses: + "200": + description: Notification updated successfully + "400": + description: Bad request + "404": + description: Notification not found + /upgrade: post: - summary: "Create a Category for the connected user" - operationId: "crateCategory" - tags: - - "Categories" - description: "This endpoint allows user to create a Category" - consumes: - - "application/json" - produces: - - "application/json" - parameters: - - in: "body" - name: "body" - schema: - type: "object" - required: - - name - properties: - name: - type: "string" + summary: Upgrade to Plus + description: | + Upgrade the user's account to the Plus version by entering the order number. + The server will validate the order number and grant access to additional clinical protocols. + requestBody: + required: true + content: + application/json: + schema: + properties: + orderNumber: + type: string + description: Order number for upgrading to Plus responses: - 200: - description: "Success" - schema: - $ref: "#/definitions/Category" - 401: - $ref: "#/responses/401" - 422: - $ref: "#/responses/422" - 500: - $ref: "#/responses/500" - security: - - basicAuth: [] - /categories/{category_id}: - put: - summary: "Update a Category of a user" - operationId: "updateCategory" - tags: - - "Categories" - description: "This endpoint allows user to update a Category" - consumes: - - "application/json" - produces: - - "application/json" - parameters: - - name: category_id - in: path - description: ID of item to category - required: true + "200": + description: Upgrade successful + "400": + description: Invalid order number +components: + schemas: + Program: + type: object + properties: + id: type: integer - format: int64 - - in: "body" - name: "body" - schema: - type: "object" - required: - - name - properties: - name: - type: "string" - responses: - 201: - description: "Success" - schema: - $ref: "#/definitions/Category" - 401: - $ref: "#/responses/401" - 422: - $ref: "#/responses/422" - 500: - $ref: "#/responses/500" - security: - - basicAuth: [] - delete: - summary: "Delete a Category of a user" - operationId: "deleteCategory" - tags: - - "Categories" - description: "This endpoint allows user to delete an item" - consumes: - - "application/json" - produces: - - "application/json" - parameters: - - name: category_id - in: path - description: ID of Category to fetch - required: true + description: Unique identifier of the program + title: + type: string + description: Title of the program + description: + type: string + description: Description of the program + image: + type: string + format: uri + description: URL of the background image for the program + time: type: integer - format: int64 - responses: - 200: - description: "Success" - 401: - $ref: "#/responses/401" - 500: - $ref: "#/responses/500" - security: - - basicAuth: [] - -securityDefinitions: - basicAuth: - type: basic - -responses: - 400: - description: Bad Request - schema: - $ref: "#/definitions/Error" - 401: - description: Unauthorized - schema: - $ref: "#/definitions/Error" - 404: - description: Not Found - schema: - $ref: "#/definitions/Error" - 422: - description: Validation Error - schema: - $ref: "#/definitions/ValidationError" - 500: - description: Internal Server Error - schema: - $ref: "#/definitions/Error" -definitions: - LoginRequest: - type: "object" - properties: - email: - type: "string" - example: "username@gmail.com" - password: - type: "string" - example: "myPassword123" - LoginResponse: - type: "object" - properties: - user: - type: "object" - example: - $ref: "#/definitions/User" - token: - type: "string" - example: "qlkjyzqsd325.!qspuezappz" - - User: - type: "object" - properties: - email: - type: "string" - example: "username@gmail.com" - password: - type: "string" - example: "MyPassword123" - userName: - type: "string" - example: "Username123" - - List: - type: "object" - properties: - id: - type: "integer" - name: - type: "string" - user_id: - type: "integer" - created_at: - type: "string" - updated_at: - type: "string" - - ListArray: - type: "array" - items: - $ref: "#/definitions/List" - - Item: - type: "object" - properties: - id: - type: "integer" - name: - type: "string" - category_id: - type: "integer" - user_id: - type: "integer" - note: - type: "string" - image: - type: "string" - created_at: - type: "string" - updated_at: - type: "string" - - Category: - type: "object" - properties: - id: - type: "integer" - example: 1 - name: - type: "string" - example: "Category" - user_id: - type: "integer" - example: 1 - created_at: - type: "string" - example: "2020-09-02T14:48:41.919Z" - updated_at: - type: "string" - example: "2020-09-02T14:48:41.919Z" - - Error: - type: "object" - properties: - status: - type: "string" - example: "error" - message: - type: "string" + description: Duration of the program in minutes + pulse_rate: + type: number + description: Desired pulse rate for the program in Hz + intensity: + type: number + description: Intensity level for the program as a percentage + + ProgramInput: + type: object + required: + - title + - description + - image + - time + - pulse_rate + - intensity + properties: + title: + type: string + description: Title of the program + description: + type: string + description: Description of the program + image: + type: string + format: uri + description: URL of the background image for the program + time: + type: integer + description: Duration of the program in minutes + pulse_rate: + type: number + description: Desired pulse rate for the program in Hz + intensity: + type: number + description: Intensity level for the program as a percentage + ClinicalProtocolInput: + type: object + required: + - title + - totalTime + - steps + - breaks + properties: + title: + type: string + description: Title of the clinical protocol + totalTime: + type: integer + description: Total duration of the clinical protocol in minutes + steps: + type: array + items: + $ref: "#/components/schemas/Step" + description: List of steps in the clinical protocol + breaks: + type: array + items: + $ref: "#/components/schemas/Break" + description: List of breaks in the clinical protocol + + Step: + type: object + required: + - id + - time + - qeegPoints + properties: + id: + type: string + description: ID of the step + time: + type: integer + description: Duration of the step in minutes + qeegPoints: + type: array + items: + $ref: "#/components/schemas/QeegPoint" + description: List of QEEG points for the step + + Break: + type: object + required: + - time + properties: + time: + type: integer + description: Duration of the break in minutes + + QeegPoint: + type: object + required: + - points + - pulseRate + - intensity + properties: + points: + type: string + description: The selected QEEG point in the brain + pulseRate: + type: number + description: Desired pulse rate for the QEEG point in Hz + intensity: + type: number + description: Intensity level for the QEEG point as a percentage + + ClinicalProtocolResponse: + type: object + properties: + sessionId: + type: string + description: ID of the clinical protocol session + title: + type: string + description: Title of the clinical protocol + totalTime: + type: integer + description: Total duration of the clinical protocol in minutes + steps: + type: array + items: + $ref: "#/components/schemas/Step" + description: List of steps in the clinical protocol + breaks: + type: array + items: + $ref: "#/components/schemas/Break" + description: List of breaks in the clinical protocol + Notification: + type: object + properties: + id: + type: string + description: Unique identifier of the notification + title: + type: string + description: Title of the notification + message: + type: string + description: Message content of the notification + isRead: + type: boolean + description: Indicates whether the notification has been read + + NotificationUpdate: + type: object + properties: + isRead: + type: boolean + description: Indicates whether the notification should be marked as read + NotificationCreate: + type: object + properties: + title: + type: string + description: Title of the notification + message: + type: string + description: Message content of the notification + ProgramUsage: + type: object + properties: + programId: + type: integer + description: ID of the program + status: + type: string + description: Status of the program (completed, pending) + remainingTime: + type: string + description: Remaining time to finish the program (formatted as HH:MM:SS) - ValidationError: - type: "object" - properties: - status: - type: "string" - example: "error" - field: - type: "string" - message: - type: "string" - +servers: + # Added by API Auto Mocking Plugin + - description: SwaggerHub API Auto Mocking + url: https://virtserver.swaggerhub.com/THAMEURKALAIZAR/programs/1.0.0 diff --git a/index.js b/index.js new file mode 100644 index 00000000..db669173 --- /dev/null +++ b/index.js @@ -0,0 +1,395 @@ +const express = require("express"); +const index = express(); +const morgan =require ("morgan"); +const createError =require('http-errors'); +const bcrypt = require('bcrypt'); +const jwt = require("jsonwebtoken"); +const fs = require('fs'); +index.use(express.json()); +const dotenv = require("dotenv"); +dotenv.config(); + +const mongoose = require('mongoose'); +const { json } = require("express"); +const { strict } = require("assert"); +mongoose.connect('mongodb://localhost:27017/students', {useNewUrlParser: true, useUnifiedTopology: true}); +mongoose.connection.on('error', console.error.bind(console, 'connection error:')); +mongoose.connection.once('open', function() { + console.log('Connected to database'); +}); + +// this function is to fix the findOneAndUpdate error +mongoose.set('useFindAndModify', false); + +// schemas needed in this assignment : + +const userSchema = new mongoose.Schema({ + email: String, + password: String, + username: String, + +}); +const userModel = mongoose.model('user', userSchema); + + +const listsSchema = new mongoose.Schema({ + name : String, + user_id: {type:mongoose.Schema.Types.ObjectId,ref:'users'} , + created_at: String, + updated_at: String +}); +const lists = mongoose.model('lists', listsSchema); +const categorySchema = new mongoose.Schema({ + + name: String, + user_id:{type:mongoose.Schema.Types.ObjectId,ref:'users'}, + created_at:String, + updated_at:String + +}) +const category = mongoose.model('category',categorySchema); + +const itemsSchema = new mongoose.Schema ({ + name: String, + category_id: {type:mongoose.Schema.Types.ObjectId,ref:'categories'}, + user_id: {type:mongoose.Schema.Types.ObjectId,ref:'users'}, + note: String, + image: String, + created_at: String, + updated_at: String, + list:[{type:mongoose.Schema.Types.ObjectId,ref:'lists'}] + + +}); +const items = mongoose.model('items',itemsSchema); + + + + +// everything about user and authentication + +index.post('/login', async (req, res) => { + try { + const user = await userModel.findOne({email: req.body.email}); + if(!user){ + res.send('Email Does Not Exist') + } + const match = await bcrypt.compare(req.body.password, user.password); + if(!match){ + res.send('Incorrect Password') + } + res.json({token: jwt.sign({id: user._id}, 'anysecret')}) + } catch (error) { + res.send(error) + } +}); + + + +index.post('/signup', async (req, res) => { + try { + req.body.password = await bcrypt.hash(req.body.password, 12); + const user = await userModel.create(req.body); + res.status(200).json({ + message: 'user Added Successfully', + user + }) + } catch (error) { + res.status(500).send('You have Validation Error') + } +}); + + + +const auth = (req, res, next) => { + const token = req.get('Authorization'); + console.log(token); + if(token && jwt.verify(token, 'thisisasecret')){ + next(); + }else { + res.send('Unauthorized Access') + } + }; + + + +index.get('/getUser',async(req,res)=>{ + try{ + const user= await userModel.find(); + res.status(200).json(user); + }catch(error){ + res.status(500).send(err) + } +}) + + + +// listss : + + +//1. get the list of users: +index.get('/getLists',async(req,res)=>{ + res.json(await lists.find()); +}); +index.get('/getLists/:id', async (req, res) =>{ +try { + const use = await lists.findById(req.params.id); + res.status(200).json(use); +} catch (error) { + res.status(500).send(error); +} +}); +//2. create lists : +index.post('/createList',async(req,res)=>{ +try{ +await lists.create(req.body); +res.status(200).send('lists created succesfully') + +}catch(error){ + res.status(500).send('problem with server ') +} +}) + +//3. delete list by id : +index.delete('/deletelist/:id', async (req, res) =>{ + try { + await lists.findByIdAndRemove(req.params.id); + res.status(200).send('Student Deleted'); + } catch (error) { + res.status(500).send(error); + } +}); + +//4. update list by id : +index.put('/updateList/:id',async(req,res,next)=>{ + + lists.findOneAndUpdate({_id:req.params.id}, + { + $set:{ + name:req.body.name, + user_id:req.body.user_id, + created_at:req.body.created_at, + updated_at:req.body.updated_at + } + } + + ).then(result=>{ + res.status(200).json({ + updated_product:result + }) + }) + .catch(err=>{ + res.status(500).json({error:err}) + }) + + +}) + + +//5. get items in a specefic list : +index.get('/getLists/:id/items', async (req, res) =>{ + try { + const use = await lists.findById(req.params.id); + const x = await items.find({ list:req.params.id }) + res.status(200).json({x,use}); + + + + } catch (error) { + res.status(500).json(error); + } +}); + +//6. create item in a specefic list : +index.post('/getLists/:id/items', async (req, res) =>{ + try{ + const use = await items.findById(req.body.id); + use.list.push(req.params.id); + await use.save(); + res.status(200).json(use); + }catch(error){ + res.status(500).send('error try again') + } +}) + + +//7. update item in a specefic list : + + index.put('/updateItemOfList/:id/items',async(req,res,next)=>{ + + await items.findByIdAndUpdate({_id:req.params.id},{ + + $set:{ + items:req.body.items + } + } + + ).then(result =>{ + res.status(200).json({updated_product:result}) + }).catch(err=>{ res.status(500).json({error:err}) + }) + + }) + + + + + + +//8. delete item in a specefic list : + +index.delete('/DeleteItemOfList/:id/items',async(req,res)=>{ + + try{ + await items.findOneAndRemove(req.params.id); + res.status(200).json('item deleted successfuly') + }catch(err){ + res.status(500).json('problem with ssystem'); + } + + +}) + + + + // items: + + +//1. get items: + +index.get('/getItems',async(req,res)=>{ + res.json(await items.find()); + }); + + +//2. create items : + +index.post('/createItem',async(req,res)=>{ + try{ + items.create(req.body); + res.status(200).json("items are created succesfuuly ") + }catch(error){ +res.status(500).json("problem with the system") + } +}) + + +//3. update items ! + +index.put('/updateitem/:id',async(req,res,next)=>{ + await items.findByIdAndUpdate({_id:req.params.id}, + { + $set:{ + name:req.body.name, + category_id:req.body.category_id, + user_id:req.body.user_id, + note:req.body.note, + image:req.body.image, + created_at:req.body.created_at, + updated_at:req.body.updated_at + } + + }).then (result =>{ + res.status(200).json({updated_product:result}) + }).catch(err=>{ res.status(500).json({error:err}) + + }) + + + + +}) + +//4. delete items : + +index.delete('/deleteitem/:id',async(req,res)=>{ +try{ + await items.findByIdAndRemove(req.params.id); + res.status(200).json("this item is deleted successfuly") +}catch(error){ + res.status(500).json("system erorr") +} + +}) + +index.delete('/delete',async(req,res)=>{ + + try{ + await items.deleteMany(); + res.status(200).send("this item is deleted successfuly") +}catch(error){ + res.status(500).send("system erorr") +} +}) + + + +// categoriess : + + + +//1. get categories : +index.get('/getCategories',async(req,res)=>{ + res.json( await category.find()); +}); + +//2. create category : +index.post('/createCategory',async(req,res)=>{ + try{ + await category.create(req.body); + res.status(200).json("category is created successfuly") + + + + }catch(error){ + res.status(500).json('problem with server') + } + + +}); + + +//3. update category by Id : + +index.put('/updateCategory/:id',async(req,res,next)=>{ + await category.findByIdAndUpdate({_id:req.params.id},{ + + $set:{ + name:req.body.name, + user_id:req.body.user_id, + created_at:req.body.created_at, + updated_at:req.body.updated_at + } + }).then(result=>{ +res.status(200).json({updated_product:result}) + + }).catch(err=>{res.status(500).json({error:err})}) + + +}) + + +//4. delete category by Id : + +index.delete('/deleteCategory/:id',async(req,res)=>{ + try{ + await category.findByIdAndRemove(req.params.id); + res.status(200).json("this category deleted is successufuly") + + }catch(error){ + res.status(500).json('there is a problem with your server pls check') + } + + +}); + + + + + + +index.listen(3000,()=>{ + console.log("ur own server is listening on port 3000"); +}) + + diff --git a/package.json b/package.json new file mode 100644 index 00000000..41f7ca5e --- /dev/null +++ b/package.json @@ -0,0 +1,30 @@ +{ + "name": "assignment-1", + "version": "1.0.0", + "description": "In this Assignment You need to code your own server using ExpressJS.", + "main": "index.js", + "scripts": { + "start": "nodemon index.js" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/MedTech-CS425/Assignment-1.git" + }, + "author": "", + "license": "ISC", + "bugs": { + "url": "https://github.com/MedTech-CS425/Assignment-1/issues" + }, + "homepage": "https://github.com/MedTech-CS425/Assignment-1#readme", + "dependencies": { + "bcrypt": "^5.0.1", + "dotenv": "^8.2.0", + "express": "^4.17.1", + "file-system": "^2.2.2", + "http-errors": "^1.8.0", + "jsonwebtoken": "^8.5.1", + "mongoose": "^5.12.7", + "morgan": "^1.10.0", + "nodemon": "^2.0.7" + } +}