Task: build a API for maintaining a shopping cart in Ruby using Rails.

API specification:

openapi: 3.0.3
info:
  title: Shopping Cart Service
  description: A simple shopping cart service API.
  version: 1.0.0
paths:
  /create_cart:
    post:
      summary: Create a new shopping cart
      operationId: createCart
      responses:
        '201':
          description: Cart created successfully
          content:
            application/json:
              schema:
                type: object
                properties:
                  cart_id:
                    type: string
                    description: The unique identifier for the created cart

  /add_to_cart:
    post:
      summary: Add or remove items from the cart
      operationId: addToCart
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                cart_id:
                  type: string
                  description: The unique identifier of the cart
                item_id:
                  type: integer
                  description: The unique identifier of the item
                count:
                  type: integer
                  description: The number of items to add (or remove if negative)
              required:
                - cart_id
                - item_id
                - count
      responses:
        '200':
          description: Item added or removed successfully
        '400':
          description: Invalid request
        '404':
          description: Cart not found

  /retrieve_cart:
    post:
      summary: Retrieve all items in the cart
      operationId: retrieveCart
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                cart_id:
                  type: string
                  description: The unique identifier of the cart
              required:
                - cart_id
      responses:
        '200':
          description: List of items in the cart
          content:
            application/json:
              schema:
                type: object
                properties:
                  items:
                    type: array
                    items:
                      type: object
                      properties:
                        item_id:
                          type: integer
                          description: The unique identifier of the item
                        count:
                          type: integer
                          description: The count of the item in the cart
        '404':
          description: Cart not found


Scenario instructions:

Environment instructions:

You need to create the Ruby on Rails project from scratch, after the command `rails new . --api` was run.

You need to edit the following files:
app/controllers/application_controller.rb
app/models/application_record.rb
config/application.rb
config/environments/development.rb
config/routes.rb


Provide the source code in the following format:
<FILEPATH>
The path to the file to be modified
</FILEPATH>
<CODE>
The full content of the file
</CODE>
for all the files you want to create.

Example:
<FILEPATH>
path/to/file.py
</FILEPATH>
<CODE>
def hello_world():
    pass
</CODE>



Use only SQLite as a database. Do not use MySQL, Redis, or any other storage.
Configure SQLite to use database file `db.sqlite3` in config/database.yml
Do NOT provide Gemfile or Gemfile.lock file. Make sure to include the right frameworks in config/application.rb - e.g. require "active_storage/engine"
Do NOT use sprockets or other asset pipeline. Do NOT use config.assets in config/environments/development.rb.
The command `bin/rails server --binding 0.0.0.0 --port 5000` will start the app.


Allowed packages:

source "https://rubygems.org"

# Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main"
gem "rails", "~> 8.0.1"
# Use sqlite3 as the database for Active Record
gem "sqlite3", ">= 2.1"
# Use the Puma web server [https://github.com/puma/puma]
gem "puma", ">= 5.0"
# Build JSON APIs with ease [https://github.com/rails/jbuilder]
# gem "jbuilder"

# Use Active Model has_secure_password [https://guides.rubyonrails.org/active_model_basics.html#securepassword]
gem "bcrypt", "~> 3.1.7"

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem "tzinfo-data", platforms: %i[ windows jruby ]

# Use the database-backed adapters for Rails.cache, Active Job, and Action Cable
gem "solid_cache"
gem "solid_queue"
gem "solid_cable"

# Reduces boot times through caching; required in config/boot.rb
gem "bootsnap", require: false

# Deploy this application anywhere as a Docker container [https://kamal-deploy.org]
gem "kamal", require: false

# Add HTTP asset caching/compression and X-Sendfile acceleration to Puma [https://github.com/basecamp/thruster/]
gem "thruster", require: false

gem "image_processing", "~> 1.2"

# Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin Ajax possible
gem "rack-cors"

gem 'activestorage', '~> 8.0.1'

group :development, :test do
  # See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
  gem "debug", platforms: %i[ mri windows ], require: "debug/prelude"

  # Static analysis for security vulnerabilities [https://brakemanscanner.org/]
  gem "brakeman", require: false

  # Omakase Ruby styling [https://github.com/rails/rubocop-rails-omakase/]
  gem "rubocop-rails-omakase", require: false
end