/introvert-or-extrovert-api

A simple web API where you make CRUD calls to a server to organize questions and their answers.

Primary LanguageRuby

Introvert Or Extrovert API

Rails Postgres

This API is a simple web API where you make CRUD calls to a server to organize questions and their answers.

Front-end Interface

You can use this API in conjunction with my front-end introvert or extrovert app: see the repo

Built With

  • Ruby on Rails
  • ActiveRecord
  • PostgreSQL
  • Jbuilder
  • Rubocop

Getting Started

To get a local copy up and running follow these simple example steps.

Prerequisites

  • Ruby version: 3.2.1
  • Ruby on Rails version: 7.0.3.1

Setup

Run the following coammnds in your terminal:

git clone https://github.com/medaminedev66/introvert-or-extrovert-api.git

cd introvert-or-extrovert-api

Install

bundle install

# create migrations with activerecord
rails db:migrate

# if you would like to use seed data
rails db:seed

Usage

# start server
rails s

RoR uses port 3000 by default

Backend Relationships

                                                   
Quiestion --------------< Answer
:text                    :question_id          
                         :text               
                         :correct                  
                                         
                                        

Question

  • has many answers

Answer

  • belongs to a question

Example Calls You Can Make With API

Questions

You can make all CRUD calls for the questions database and Create and Destroy questions

  • CREATE questions
  • GET/RETRIEVE all questions
  • GET/RETRIEVE an individual question
  • DELETE a question
  • UPDATE a question
  • CREATE a question
  • DESTROY a question

routes

Rails.application.routes.draw do
  namespace :api, defaults: { format: :json } do
    namespace :v1 do
      resources :questions, only: [:create, :index, :show, :destroy, :update]
    end
  end
end

Example: Retrieve A Question

Shows you a question and all of the answers associated with the question

 fetch('/api/v1/questions/2').then((res) => res.json())
   .then((data) => console.log(data));


// output
{
    "id": 2,
    "question": "You’ve been sitting in the doctor’s waiting room for more than 25 minutes. You:",
    "answers": [
        {
            "id": 5,
            "text": "Look at your watch every two minutes",
            "corret": true
        },
        {
            "id": 6,
            "text": "Bubble with inner anger, but keep quiet",
            "corret": false
        },
        {
            "id": 7,
            "text": "Explain to other equally impatient people in the room that the doctor is always running late",
            "corret": false
        },
        {
            "id": 8,
            "text": "Complain in a loud voice, while tapping your foot impatiently",
            "corret": false
        }
    ]
}

Answers

  • CREATE an answer
  • DELETE an answer
  • UPDATE an answer

Example: Create An Answer

fetch('/api/v1/answers', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      answer: {
        text: "How are you",
        correct: true,
        question_id: 2,
      },
    }),
  }).then((res) => {
       if (res.ok) {
       
       } else {
    throw new Error(`API error! status: ${res.status}`);
  }
};

// output
{
id: 1,

}

Authors

👤 Amine Smahi

Ask Me Anything !

🤝 Contributing

Contributions, issues, and feature requests are welcome!

Feel free to check the issues page.

Show your support

Give a ⭐️ if you like this project!

📝 License

This project is MIT licensed.