Coder Social home page Coder Social logo

prechak / question-and-answer-api Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 128 KB

This project is an Express-based REST API for managing questions and answers. Users can create, read, update, delete questions and answers, as well as upvote or downvote them.

JavaScript 100.00%

question-and-answer-api's Introduction

Question and Answer API

This project is an Express-based REST API for managing questions and answers. Users can create, read, update, delete questions and answers, as well as upvote or downvote them. Swagger

Table of Contents

Installation

  1. Clone the repository:

    mkdir question-answer-api
    cd question-answer-api
    git clone [email protected]:prechak/Question-and-Answer-API.git
  2. Install dependencies:

    npm install
  3. Set up the database:

    • Ensure you have a PostgreSQL server running.
    • Create a database and update the connection details in utils/db.mjs.
  4. Start the server:

    npm start

Usage

The API provides endpoints to manage questions and answers. Below is a list of available endpoints and their usage.

API Endpoints

Questions

  • Create a question

    • POST /questions
    • Body: { "title": "Question title", "description": "Question description", "category": "Question category" }
    • Response: 201 Created
  • Get all questions

    • GET /questions?keywords=keyword1 keyword2
    • Response: 200 OK with a list of questions matching the keywords
  • Get a question by ID

    • GET /questions/:id
    • Response: 200 OK with the question details
  • Update a question

    • PUT /questions/:id
    • Body: { "title": "Updated title", "description": "Updated description", "category": "Updated category" }
    • Response: 201 Created
  • Delete a question

    • DELETE /questions/:id
    • Response: 201 Created
  • Upvote a question

    • POST /questions/:id/upvote
    • Body: { "vote": 1 }
    • Response: 200 OK
  • Downvote a question

    • POST /questions/:id/downvote
    • Body: { "vote": -1 }
    • Response: 200 OK

Answers

  • Create an answer for a question

    • POST /questions/:id/answers
    • Body: { "content": "Answer content" }
    • Response: 201 Created
  • Get answers for a question

    • GET /questions/:id/answers
    • Response: 200 OK with a list of answers
  • Upvote an answer

    • POST /answers/:id/upvote
    • Body: { "vote": 1 }
    • Response: 200 OK
  • Downvote an answer

    • POST /answers/:id/downvote
    • Body: { "vote": -1 }
    • Response: 200 OK

Middleware

  • post.validation.mjs: Validates the creation of questions.
  • put.validation.mjs: Validates the update of questions.
  • postanswers.validation.mjs: Validates the creation of answers.

Database

The database schema includes two main tables, questions and answers, with additional tables for votes:

  • questions: Stores question details.
  • answers: Stores answer details.
  • question_votes: Tracks votes for questions.
  • answer_votes: Tracks votes for answers.

Swagger Documentation

The API documentation is available using Swagger. Follow the steps below to access the documentation:

  1. Ensure the server is running:

    npm start
  2. Open your browser and navigate to:

    http://localhost:7777/api-docs
    
  3. You will see the interactive API documentation generated by Swagger.

License

This project is licensed under the MIT License.

question-and-answer-api's People

Contributors

prechak avatar github-classroom[bot] avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.