This is the backend API built with Ruby on Rails for managing tasks. The frontend communicates with this API to perform CRUD operations on tasks.
Before running the application, ensure you have the following installed:
-
Clone the repository:
git clone <repo-url> backend cd backend
-
Install Ruby dependencies:
bundle install
-
Set up the database:
rails db:create rails db:migrate
-
Seed the database (optional):
rails db:seed
-
Start the Rails server:
rails server
-
Visit
http://localhost:3000
in your browser or use it as an API endpoint for the frontend.
The backend will now be running, and it will handle requests related to task management.
The backend provides the following API endpoints for managing tasks:
GET /tasks
: Retrieves the list of tasks.POST /tasks
: Creates a new task.PATCH /tasks/:id
: Updates an existing task.DELETE /tasks/:id
: Deletes a task.
This project uses Swagger (via Rswag) to document and test the API endpoints. Swagger provides an interactive interface for exploring and testing the API.
Swagger is a toolset that generates interactive API documentation based on your controllers and endpoint definitions. With Swagger, you can:
- View all available API endpoints.
- Test API endpoints directly from the documentation.
- Understand request/response formats, parameters, and error codes.
To view the API documentation in your browser:
- Start the Rails server:
rails server
- Navigate to the Swagger UI:
http://localhost:3000/api-docs
Here, you can explore and test the API endpoints.
Swagger is configured using Rswag. The configuration file is located at:
spec/swagger_helper.rb
: Defines general Swagger settings.spec/swagger/v1/swagger.yaml
: Stores the generated API documentation.
To regenerate the Swagger YAML file, run:
bundle exec rake rswag:specs:swaggerize
Method | Endpoint | Description |
---|---|---|
GET | /tasks |
Fetches all tasks. |
POST | /tasks |
Creates a new task. |
GET | /tasks/:id |
Fetches a specific task. |
PUT | /tasks/:id |
Updates a specific task. |
DELETE | /tasks/:id |
Deletes a specific task. |
To customize the API documentation:
- Add Swagger annotations to your controller actions using
swagger_api
. - Regenerate the Swagger YAML file using the command mentioned above.
CORS (Cross-Origin Resource Sharing) is configured to allow communication between the frontend and the backend. The backend allows all origins for development purposes.
# config/initializers/cors.rb
Rails.application.config.middleware.insert_before 0, Rack::Cors do
allow do
origins "*"
resource "*", headers: :any, methods: [:get, :post, :put, :patch, :delete, :options, :head]
end
end
backend/
├── app/
│ ├── controllers/ # Controller files for handling requests
│ ├── models/ # Model files for tasks
│ └── serializers/ # Serializer for task responses (if applicable)
├── config/ # Configuration files
│ └── cors.rb # CORS configuration for allowing frontend requests
├── db/ # Database files and migrations
├── Gemfile # Ruby dependencies
├── Gemfile.lock # Lock file for Ruby dependencies
├── config.ru # Rack configuration file
└── routes.rb # Defines the routes (API endpoints)
- Ensure the Rails backend and Angular frontend are both running in parallel for seamless communication.
- The backend is designed to handle the task management logic, while the frontend handles the user interface.