Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pull requests #388

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,5 @@ group :test do
gem "rspec", "~> 3.10"
gem "rspec-json_expectations", "~> 2.2"
end

gem 'faker'
6 changes: 5 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ GEM
database_cleaner-core (2.0.1)
diff-lcs (1.4.4)
eventmachine (1.2.7)
faker (2.19.0)
i18n (>= 1.6, < 2)
ffi (1.15.3)
i18n (1.8.10)
concurrent-ruby (~> 1.0)
Expand Down Expand Up @@ -87,10 +89,12 @@ GEM

PLATFORMS
x86_64-darwin-19
x86_64-linux

DEPENDENCIES
activerecord (~> 6.1)
database_cleaner (~> 2.0)
faker
pry (~> 0.14.1)
rack-contrib (~> 2.3)
rack-cors (~> 1.1)
Expand All @@ -106,4 +110,4 @@ DEPENDENCIES
thin (~> 1.8)

BUNDLED WITH
2.2.23
2.4.13
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ After creating the project locally, you should also
[create a repository on GitHub][create repo] to host your repo and help
collaborate, if you're working with a partner.

Heres a link to My front end [front end]

### Fetch Example

Your React app should make fetch requests to your Sinatra backend! Here's an
Expand Down Expand Up @@ -133,6 +135,7 @@ fetch("http://localhost:9292/test")

[create-react-app]: https://create-react-app.dev/docs/getting-started
[create repo]: https://docs.github.com/en/get-started/quickstart/create-a-repo
[front end]: https://github.com/Naulikha/phase-3-sinatra-react-project-front-end.git
[dbdiagram.io]: https://dbdiagram.io/
[postman download]: https://www.postman.com/downloads/
[network tab]: https://developer.chrome.com/docs/devtools/network/
5 changes: 2 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ require_relative "./config/environment"
require "sinatra/activerecord/rake"

desc "Start the server"
task :server do
task :server do
if ActiveRecord::Base.connection.migration_context.needs_migration?
puts "Migrations are pending. Make sure to run `rake db:migrate` first."
return
end

# rackup -p PORT will run on the port specified (9292 by default)
ENV["PORT"] ||= "9292"
rackup = "rackup -p #{ENV['PORT']}"
rackup = "rackup -p #{ENV["PORT"]}"

# rerun allows auto-reloading of server when files are updated
# -b runs in the background (include it or binding.pry won't work)
Expand All @@ -19,6 +19,5 @@ end

desc "Start the console"
task :console do
ActiveRecord::Base.logger = Logger.new(STDOUT)
Pry.start
end
83 changes: 78 additions & 5 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,82 @@
class ApplicationController < Sinatra::Base
set :default_content_type, 'application/json'

# Add your routes here
get "/" do
{ message: "Good luck with your project!" }.to_json
set :default_content_type, "application/json"

configure do
enable :cross_origin
end

before do
response.headers['Access-Control-Allow-Origin'] = '*'
end

options "*" do
response.headers["Access-Control-Allow-Methods"] = "GET, PATCH, PUT, POST, DELETE"
response.headers["Access-Control-Allow-Headers"] = "Authorization, Content-Type, Accept, X-User-Email, X-Auth-Token"
response.headers["Access-Control-Allow-Origin"] = "*"
200
end


get '/hello' do
"hello my brother"
end

# user sign in
patch "/login" do
user = User.find_by(name: params[:name], password: params[:password])
if !!user
{ isRegistered: "#{!!user}", userId: user.id }.to_json
else
{ isRegistered: "#{!!user}"}.to_json
end
end

# user sign up
post "/signup" do
is_signed_up = User.find_by(email: params[:email])
if is_signed_up
{ isAlreadyRegistered: "#{!!is_signed_up}" }.to_json
else
user = User.create(name: params[:name], email: params[:email], password: params[:password])
user.to_json
end
end

# user create task
post "/tasks/create" do
task = Task.create(
name: params[:name],
description: params[:description],
due: params[:due],
status: params[:status],
user_id: params[:user_id]
)
task.to_json
end

# user updates task
patch "/tasks/update/:id" do
task = Task.find(params[:id])
task.update(status: params[:status])
task.to_json
end

# user gets individual task
get "/tasks/:id" do
task = Task.find(params[:id])
task.to_json
end

# user lists all their own tasks
patch "/tasks" do
task = Task.where(user_id: params[:user_id])
task.to_json
end

# user delete specific tasks
delete "/tasks/:id" do
task = Task.find(params[:id])
task.destroy
task.to_json
end
end
Empty file removed app/models/.gitkeep
Empty file.
3 changes: 3 additions & 0 deletions app/models/task.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Task < ActiveRecord::Base
belongs_to :user
end
3 changes: 3 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class User < ActiveRecord::Base
has_many :tasks
end
9 changes: 1 addition & 8 deletions config.ru
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
require_relative "./config/environment"

# Allow CORS (Cross-Origin Resource Sharing) requests
use Rack::Cors do
allow do
origins '*'
resource '*', headers: :any, methods: [:get, :post, :delete, :put, :patch, :options, :head]
end
end

# Parse JSON from the request body into the params hash
use Rack::JSONBodyParser

# Our application
use ApplicationController
run ApplicationController
8 changes: 2 additions & 6 deletions config/database.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
#connection between database and models
default: &default
adapter: sqlite3
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000

development:
<<: *default
database: db/development.sqlite3

test:
<<: *default
database: db/test.sqlite3

production:
<<: *default
database: db/production.sqlite3
database: db/task-prod.db
9 changes: 9 additions & 0 deletions db/migrate/20230609043400_create_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateUsers < ActiveRecord::Migration[6.1]
def change
create_table :users do |t|
t.string :name
t.string :email
t.string :password
end
end
end
12 changes: 12 additions & 0 deletions db/migrate/20230609043603_create_tasks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreateTasks < ActiveRecord::Migration[6.1]
def change
create_table :tasks do |t|
t.string :name
t.string :description
t.datetime :due
t.string :status
t.integer :user_id
t.timestamps
end
end
end
31 changes: 31 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
@@ -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: 2023_06_09_043603) do

create_table "tasks", force: :cascade do |t|
t.string "name"
t.string "description"
t.datetime "due"
t.string "status"
t.integer "user_id"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end

create_table "users", force: :cascade do |t|
t.string "name"
t.string "email"
t.string "password"
end

end
13 changes: 11 additions & 2 deletions db/seeds.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
puts "🌱 Seeding spices..."
puts "🌱 Seeding users..."
User.destroy_all
Tasks.destroy_all

# Seed your database here
5.times do
user = User.create(
name: Faker::Name.name,
email: Faker::Internet.email,
password: Faker::Internet.password,
)
end

puts "✅ Done seeding!"
puts "create your own tasks"