Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Ruby Sinatra Help I need help with Function 1, 3, and 4 (I covered them in pound signs so its easier for you to see.

Ruby Sinatra Help

I need help with Function 1, 3, and 4 (I covered them in pound signs so its easier for you to see. I also left comments about what the request should do.).

I left my code to show that I was trying.

I believe the post request is correct. (Function 2, if you will)

The code is in two separate images because it did not fit.

I will paste the instruction of this assignment below.

image text in transcribed

image text in transcribed

Part 1 Instructions

  1. Make requests to GET /dogs display a line for each dog in the database with the following format: id: name - breed - weight
  2. Make requests to POST /dogs create a new dog where the name, breed, and weight of the dog to be created are grabbed from parameters name, breed, and weight respectively.
  3. Make requests to PATCH /dogs update the information for an existing dog, when given new information (from the parameters) and the id of the dog to update (from the parameters).
  4. Make requests to DELETE /dogs delete an existing dog from the database (if it exists) when given the id of the dog to delete.

Part 2 Instructions

  1. Make sure POST requests to /dogs don't make a new dog unless the name, breed, and weight parameters are all set.
  2. Make sure PATCH requests to /dogs work as long as the parameter id is provided and at least one other parameter (name, breed, or weight).
  3. Make sure DELETE requests to /dogs don't delete anything if the parameter id is not specified or the dog is not found in the database.

Thank you so so much!

require 'sinatra" require 'data mapper DataMapper: : setup ( : default, class Dog "sqlite3://# { Dir.pwd } /dogs.db") include DataMapper: :Resource property :id, Serial property:name, Text property :breed, Text property :weight, Integer end DataMapper.finalize # automatically create the post table Dog.auto upgrade! # FUNCTION 1 # DISPLAY THE ID, NAME, BREED, AND WEIGHT OF EVERY DOG # Follow the following format : # id: name - breed -weight # 1: Bonny- Pitbull - 500 get '/dogs' do # dogs is an array of Dog objects dogs Dog.all output = "" dogs . each do I d I '' # { d. id):" " " " #{d.name } -" " #{d.breed) -" " output +- + + + #{d.weight)" + end puts output end # # CREATE A NEW DOG WHERE NAME, BREED, AND WEIGHT ARE GRABBED FROM PARAMETERS name, breed, AND weight RESPECETIVELY. post is used to create something post '/dogs' do d = Dog.new d. name = params ["name " ] d,breed = params ["breed "] d.weightparams [ "weight"].to_i d.save end # FUNCTION 3 #update an existing dog, given its id and new information L# patch is to modify something patch "/dogs' do output = "" dogs.each do Id output+"d.id] In" end puts output end # FUNCTION 4 #delete a dog object, given its id delete '/dogs' do end require 'sinatra" require 'data mapper DataMapper: : setup ( : default, class Dog "sqlite3://# { Dir.pwd } /dogs.db") include DataMapper: :Resource property :id, Serial property:name, Text property :breed, Text property :weight, Integer end DataMapper.finalize # automatically create the post table Dog.auto upgrade! # FUNCTION 1 # DISPLAY THE ID, NAME, BREED, AND WEIGHT OF EVERY DOG # Follow the following format : # id: name - breed -weight # 1: Bonny- Pitbull - 500 get '/dogs' do # dogs is an array of Dog objects dogs Dog.all output = "" dogs . each do I d I '' # { d. id):" " " " #{d.name } -" " #{d.breed) -" " output +- + + + #{d.weight)" + end puts output end # # CREATE A NEW DOG WHERE NAME, BREED, AND WEIGHT ARE GRABBED FROM PARAMETERS name, breed, AND weight RESPECETIVELY. post is used to create something post '/dogs' do d = Dog.new d. name = params ["name " ] d,breed = params ["breed "] d.weightparams [ "weight"].to_i d.save end # FUNCTION 3 #update an existing dog, given its id and new information L# patch is to modify something patch "/dogs' do output = "" dogs.each do Id output+"d.id] In" end puts output end # FUNCTION 4 #delete a dog object, given its id delete '/dogs' do end

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Relational Database And SQL

Authors: Lucy Scott

3rd Edition

1087899699, 978-1087899695

More Books

Students also viewed these Databases questions