From c34efb386cf0081227182ec33107755fbfa21b9c Mon Sep 17 00:00:00 2001 From: michael-mbugua Date: Tue, 8 Nov 2022 10:18:55 +0300 Subject: [PATCH 1/5] add model files --- app/models/buyer.rb | 3 +++ app/models/product.rb | 3 +++ app/models/seller.rb | 3 +++ 3 files changed, 9 insertions(+) create mode 100644 app/models/buyer.rb create mode 100644 app/models/product.rb create mode 100644 app/models/seller.rb diff --git a/app/models/buyer.rb b/app/models/buyer.rb new file mode 100644 index 0000000..3a98619 --- /dev/null +++ b/app/models/buyer.rb @@ -0,0 +1,3 @@ +class Buyer Date: Tue, 8 Nov 2022 10:19:32 +0300 Subject: [PATCH 2/5] run the bundle install --- Gemfile.lock | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 Gemfile.lock diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..e0e606c --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,100 @@ +GEM + remote: https://rubygems.org/ + specs: + activemodel (6.1.7) + activesupport (= 6.1.7) + activerecord (6.1.7) + activemodel (= 6.1.7) + activesupport (= 6.1.7) + activesupport (6.1.7) + concurrent-ruby (~> 1.0, >= 1.0.2) + i18n (>= 1.6, < 2) + minitest (>= 5.1) + tzinfo (~> 2.0) + zeitwerk (~> 2.3) + coderay (1.1.3) + concurrent-ruby (1.1.10) + database_cleaner (2.0.1) + database_cleaner-active_record (~> 2.0.0) + database_cleaner-active_record (2.0.1) + activerecord (>= 5.a) + database_cleaner-core (~> 2.0.0) + database_cleaner-core (2.0.1) + diff-lcs (1.5.0) + faker (2.23.0) + i18n (>= 1.8.11, < 2) + ffi (1.15.5) + i18n (1.12.0) + concurrent-ruby (~> 1.0) + listen (3.7.1) + rb-fsevent (~> 0.10, >= 0.10.3) + rb-inotify (~> 0.9, >= 0.9.10) + method_source (1.0.0) + minitest (5.16.3) + mustermann (2.0.2) + ruby2_keywords (~> 0.0.1) + pry (0.14.1) + coderay (~> 1.1) + method_source (~> 1.0) + rack (2.2.4) + rack-contrib (2.3.0) + rack (~> 2.0) + rack-protection (2.2.2) + rack + rack-test (1.1.0) + rack (>= 1.0, < 3) + rake (13.0.6) + rb-fsevent (0.11.2) + rb-inotify (0.10.1) + ffi (~> 1.0) + require_all (3.0.0) + rerun (0.13.1) + listen (~> 3.0) + rspec (3.12.0) + rspec-core (~> 3.12.0) + rspec-expectations (~> 3.12.0) + rspec-mocks (~> 3.12.0) + rspec-core (3.12.0) + rspec-support (~> 3.12.0) + rspec-expectations (3.12.0) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.12.0) + rspec-mocks (3.12.0) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.12.0) + rspec-support (3.12.0) + ruby2_keywords (0.0.5) + sinatra (2.2.2) + mustermann (~> 2.0) + rack (~> 2.2) + rack-protection (= 2.2.2) + tilt (~> 2.0) + sinatra-activerecord (2.0.26) + activerecord (>= 4.1) + sinatra (>= 1.0) + sqlite3 (1.5.3-x86_64-linux) + tilt (2.0.11) + tzinfo (2.0.5) + concurrent-ruby (~> 1.0) + zeitwerk (2.6.5) + +PLATFORMS + x86_64-linux + +DEPENDENCIES + activerecord (~> 6.1) + database_cleaner + faker (~> 2.18) + pry + rack-contrib (~> 2.3) + rack-test (~> 1.1) + rake + require_all + rerun + rspec + sinatra (~> 2.1) + sinatra-activerecord + sqlite3 (~> 1.4) + +BUNDLED WITH + 2.3.19 From 894feb77fdf345797a12a6d6f74d75b54bd1ebd4 Mon Sep 17 00:00:00 2001 From: michael-mbugua Date: Tue, 8 Nov 2022 12:19:10 +0300 Subject: [PATCH 3/5] create the POST DELETE and GET commands --- app/controllers/application_controller.rb | 54 ++++++++++++++++++++++- 1 file changed, 52 insertions(+), 2 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index cf1fcf2..f795a69 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,5 +1,55 @@ class ApplicationController < Sinatra::Base - get '/' do # this is the root route of the application (the homepage) but you can have as many routes as you want - {hello: "Just a starting code 😃"}.to_json + set :default_content_type, "application/json" + before do + response.headers["Access-Control-Allow-Origin"]="*" end + + options "*" do + response.headers["Access-Control-Allow-Methods"]="GET,POST,PUT,PATCH,DELETE,OPTIONS" + end + + # to get all buyers + get "/buyers" do + Buyer.all.to_json + end + + # to get all sellers + get "/sellers" do + Seller.all.to_json + end + + +# POST IN buyers + post "/buyers" do + buyers=Buyer.create( + name: params[:name], + email: params[:email], + password: params[:password] + ) + buyers.to_json + end + +# DELETE IN Buyers + delete "/buyers/:id" do + buyers = Buyer.find(params[:id]) + buyers.destroy + {message: "Buyer '#{buyers.name}' has been deleted."}.to_json + end + +# POST in sellers + post "/sellers" do + sellers=Seller.create( + name: params[:name], + email: params[:email], + password: params[:password] + ) + sellers.to_json + end +# DELETE IN SELLERS + delete "/sellers/:id" do + sellers=Seller.find(params[:id]) + sellers.destroy + {message: "seller '#{sellers.name}' has been deleted."}.to_json + end + end \ No newline at end of file From 80b8eece377944fb83135d614095ecadff0d8492 Mon Sep 17 00:00:00 2001 From: michael-mbugua Date: Tue, 8 Nov 2022 12:19:34 +0300 Subject: [PATCH 4/5] auto generated file containing the snapshot --- db/schema.rb | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 db/schema.rb diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 0000000..013c111 --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,31 @@ +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# This file is the source Rails uses to define your schema when running `bin/rails +# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to +# be faster and is potentially less error prone than running all of your +# migrations from scratch. Old migrations may fail to apply correctly if those +# migrations use external dependencies or application code. +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema.define(version: 2022_11_08_072947) do + + create_table "buyers", force: :cascade do |t| + t.string "name" + t.text "email" + t.text "password" + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + end + + create_table "sellers", force: :cascade do |t| + t.string "name" + t.text "email" + t.text "password" + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + end + +end From 01919105dc5a4ed3462dacdbcc80812f5d7246e6 Mon Sep 17 00:00:00 2001 From: michael-mbugua Date: Tue, 8 Nov 2022 12:20:06 +0300 Subject: [PATCH 5/5] create migrations and creating tables --- db/migrate/20221108072722_create_sellers.rb | 10 ++++++++++ db/migrate/20221108072947_create_buyers.rb | 10 ++++++++++ 2 files changed, 20 insertions(+) create mode 100644 db/migrate/20221108072722_create_sellers.rb create mode 100644 db/migrate/20221108072947_create_buyers.rb diff --git a/db/migrate/20221108072722_create_sellers.rb b/db/migrate/20221108072722_create_sellers.rb new file mode 100644 index 0000000..0f633fd --- /dev/null +++ b/db/migrate/20221108072722_create_sellers.rb @@ -0,0 +1,10 @@ +class CreateSellers < ActiveRecord::Migration[6.1] + def change + create_table :sellers do |t| + t.string :name + t.text :email + t.text :password + t.timestamps + end + end +end diff --git a/db/migrate/20221108072947_create_buyers.rb b/db/migrate/20221108072947_create_buyers.rb new file mode 100644 index 0000000..c005fcb --- /dev/null +++ b/db/migrate/20221108072947_create_buyers.rb @@ -0,0 +1,10 @@ +class CreateBuyers < ActiveRecord::Migration[6.1] + def change + create_table :buyers do |t| + t.string :name + t.text :email + t.text :password + t.timestamps + end + end +end