From 8aa2521787bb4491d6634b387efcd506440de1e3 Mon Sep 17 00:00:00 2001 From: Matt Huntington Date: Thu, 15 Mar 2018 22:45:53 -0400 Subject: [PATCH] delete fruit route --- app/controllers/fruits_controller.rb | 4 ++++ app/models/fruit.rb | 5 +++++ config/routes.rb | 1 + 3 files changed, 10 insertions(+) diff --git a/app/controllers/fruits_controller.rb b/app/controllers/fruits_controller.rb index a43833f..637caa0 100644 --- a/app/controllers/fruits_controller.rb +++ b/app/controllers/fruits_controller.rb @@ -14,4 +14,8 @@ class FruitsController < ApplicationController def create render json: Fruit.create(params["fruit"]) end + + def delete + render json: Fruit.delete(params[:id]) + end end diff --git a/app/models/fruit.rb b/app/models/fruit.rb index 04f32be..30a533d 100644 --- a/app/models/fruit.rb +++ b/app/models/fruit.rb @@ -27,4 +27,9 @@ class Fruit results = DB.exec("INSERT INTO fruits (name, color, readytoeat) VALUES ( '#{opts["name"]}', '#{opts["color"]}', #{opts["readyToEat"]} );") return Fruit.new(opts) end + + def self.delete(id) + results = DB.exec("DELETE FROM fruits WHERE id=#{id};") + return { deleted: true } + end end diff --git a/config/routes.rb b/config/routes.rb index 598e466..caddaa0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,4 +4,5 @@ Rails.application.routes.draw do # get('/fruits', { :to => 'fruits#index' }) get '/fruits/:id', to: 'fruits#show' post '/fruits', to: 'fruits#create' + delete '/fruits/:id', to: 'fruits#delete' end