Skip to content

Commit

Permalink
Add devices endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrienNini committed Oct 13, 2018
1 parent a2b5d29 commit 5bd96b6
Show file tree
Hide file tree
Showing 34 changed files with 154 additions and 93 deletions.
58 changes: 58 additions & 0 deletions app/controllers/devices_controller.rb
@@ -1,3 +1,61 @@
class DevicesController < ApplicationController

def index
devices = Device.all
render json: { status: "SUCCESS", message: "All devices currently in DB", data: devices }, status: :ok
end

def show
devices = Device.find_by(user_id: params[:user_id])
if devices
render json: { status: "SUCCESS", message: "All devices currently assigned to requested user", data: devices }, status: :ok
else
render json: { status: "ERROR", message: "No device found for requested user" }, status: :not_found
end
end

def create
device = Device.new(device_params)
begin
device.save
rescue ActiveRecord::NotNullViolation => exception
render json: { status: 'ERROR', message: 'Saving failed: Missing deviceName value', data: exception.message }, status: :unprocessable_entity
rescue ActiveRecord::InvalidForeignKey => exception
render json: { status: 'ERROR', message: 'Saving failed: user_id does not exist', data: exception.message }, status: :not_found
rescue ActiveRecord::RecordNotUnique => exception
render json: { status: 'ERROR', message: 'Saving failed: device_id already exists', data: exception.message }, status: :unprocessable_entity
else
render json: { status: 'SUCCESS', message: "Device saved", data: device }, status: :ok
end
end

def update
device = Device.find_by(device_id: params[:id])
if device
if device.update_attributes(device_params)
render json: { status: "SUCCESS", message: "Device updated", data: device }, status: :ok
else
render json: { status: "SUCCESS", message: "Device update failed", data: device.errors }, status: :unprocessable_entity
end
else
render json: { status: "ERROR", message: "No device found for requested user" }, status: :not_found
end
end

def destroy
device = Device.find_by(device_id: params[:id])
if device
device.destroy
render json: { status: "SUCCESS", message: "Device deleted", data: device }, status: :ok
else
render json: { status: "ERROR", message: "No device found for requested user" }, status: :not_found
end
end

private

def device_params
params.permit(:user_id, :device_id, :deviceName)
end

end
34 changes: 27 additions & 7 deletions bin/rake
@@ -1,9 +1,29 @@
#!/usr/bin/env ruby
begin
load File.expand_path('../spring', __FILE__)
rescue LoadError => e
raise unless e.message.include?('spring')
# frozen_string_literal: true

#
# This file was generated by Bundler.
#
# The application 'rake' is installed as part of a gem, and
# this file is here to facilitate running it.
#

require "pathname"
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
Pathname.new(__FILE__).realpath)

bundle_binstub = File.expand_path("../bundle", __FILE__)

if File.file?(bundle_binstub)
if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
load(bundle_binstub)
else
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
end
end
require_relative '../config/boot'
require 'rake'
Rake.application.run

require "rubygems"
require "bundler/setup"

load Gem.bin_path("rake", "rake")
2 changes: 1 addition & 1 deletion config/boot.rb
@@ -1,4 +1,4 @@
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)

require 'bundler/setup' # Set up gems listed in the Gemfile.
require 'bootsnap/setup' # Speed up boot time by caching expensive operations.
#require 'bootsnap/setup' # Speed up boot time by caching expensive operations.
4 changes: 2 additions & 2 deletions config/routes.rb
Expand Up @@ -10,7 +10,7 @@
get 'devices' => 'devices#index'
get 'devices/:user_id' => 'devices#show' # Get all the devices of the user
post 'devices/:user_id' => 'devices#create'
patch 'devices/:user_id/:device_id' => 'devices#update'
delete 'devices/:user_id/:device_id' => 'devices#destroy'
patch 'devices/:id' => 'devices#update'
delete 'devices/:id' => 'devices#destroy'

end
2 changes: 1 addition & 1 deletion db/migrate/20181004065431_create_tables_migration.rb
Expand Up @@ -12,7 +12,7 @@ def change
t.string :deviceName, null: false, default: "Unnamed device"
end

add_index :devices, :device_id
add_index :devices, :device_id, unique: true
add_foreign_key :devices, :users

create_table :sensors do |t|
Expand Down
2 changes: 1 addition & 1 deletion db/schema.rb
Expand Up @@ -25,7 +25,7 @@
t.string "device_id", null: false
t.bigint "user_id", null: false
t.string "deviceName", default: "Unnamed device", null: false
t.index ["device_id"], name: "index_devices_on_device_id"
t.index ["device_id"], name: "index_devices_on_device_id", unique: true
t.index ["user_id"], name: "fk_rails_410b63ef65"
end

Expand Down
39 changes: 11 additions & 28 deletions spec/requests/devices_spec.rb
Expand Up @@ -17,7 +17,6 @@
end
end

=begin

RSpec.describe 'Devices management' do
describe 'GET /devices' do
Expand Down Expand Up @@ -45,7 +44,7 @@

describe 'POST /devices/:user_id' do
it 'returns a status message' do
post '/devices/1', :params => { id: 'ABC000222', deviceName: "Test device", timestamp: DateTime.now }
post '/devices/1', :params => { device_id: 'ABC000222', deviceName: "Test device" }
json = JSON.parse response.body
expect(json['status']).to eql('SUCCESS')
expect(response.status).to eql(200)
Expand All @@ -59,72 +58,56 @@
end

it 'returns a status message (ERROR) if missing argument' do
post '/devices/1', :params => { id: 'ABC000222', deviceName: "Test device" }
post '/devices/1', :params => { deviceName: 'Test device' }
json = JSON.parse response.body
expect(json['status']).to eql('ERROR')
expect(response.status).to eql(422)
end

it 'returns a status message (ERROR) if device_id already exists' do
post 'devices/1', :params => { id: 'ABC000111', deviceName: "Test device", timestamp: DateTime.now }
post '/devices/1', :params => { device_id: 'ABC000111', deviceName: "Test device" }
json = JSON.parse response.body
expect(json['status']).to eql('ERROR')
expect(response.status).to eql(422)
end

it "retuns a status message (ERROR) if the requested user_id doesn't exists" do
post 'device/42', :params => { id: 'ABC000222', deviceName: "Test device", timestamp: DateTime.now }
post '/devices/42', :params => { device_id: 'DEF000333', deviceName: "Test device" }
json = JSON.parse response.body
expect(json['status']).to eql('ERROR')
expect(response.status).to eql(404)
end
end

describe 'PATCH /devices/:user_id/:device_id' do
describe 'PATCH /devices/:id' do
it 'returns a status message' do
patch '/devices/1/ABC000111', :params => { deviceName: "Test device 1" }
patch '/devices/ABC000111', :params => { deviceName: "Test device 1" }
json = JSON.parse response.body
expect(json['status']).to eql('SUCCESS')
expect(response.status).to eql(200)
end

it "returns a status message (Error) if the requested user_id doesn't exists" do
patch '/devices/42/ABC000111', :params => { deviceName: "Test device 1" }
json = JSON.parse response.body
expect(json['status']).to eql('ERROR')
expect(response.status).to eql(404)
end
it "returns a status message (Error) if the requested device_id doesn't exists" do
patch '/devices/1/ABC696969', :params => { deviceName: "Test device 1" }
patch '/devices/ABC696969', :params => { deviceName: "Test device 1" }
json = JSON.parse response.body
expect(json['status']).to eql('ERROR')
expect(response.status).to eql(404)
end
end

describe 'DELETE /devices/:user_id/:device_id' do
describe 'DELETE /devices/:id' do
it 'returns a status message' do
delete '/devices/1/ABC000111'
delete '/devices/ABC000111'
json = JSON.parse response.body
expect(json['status']).to eql('SUCCESS')
expect(response.status).to eql(200)
end

it "returns a status message (ERROR) if the requested user_id doesn't exists" do
delete '/devices/42/ABC000111'
json = JSON.parse response.body
expect(json['status']).to eql('ERROR')
expect(response.status).to eql(404)
end
it "returns a status message (ERROR) if the requested device_id doesn't exists" do
delete '/devices/1/ABC696969'
delete '/devices/ABC696969'
json = JSON.parse response.body
expect(json['status']).to eql('ERROR')
expect(response.status).to eql(404)
end
end
end
=end
end
@@ -1,5 +1,5 @@
current directory: /Users/Adrien/Dev/projet-integration/sensorygarden-api/vendor/bundle/ruby/2.5.0/gems/bootsnap-1.3.2/ext/bootsnap
/Users/Adrien/.rbenv/versions/2.5.1/bin/ruby -r ./siteconf20181011-3510-1v6125a.rb extconf.rb
/Users/Adrien/.rbenv/versions/2.5.1/bin/ruby -r ./siteconf20181013-20035-1m4gfnz.rb extconf.rb
creating Makefile

current directory: /Users/Adrien/Dev/projet-integration/sensorygarden-api/vendor/bundle/ruby/2.5.0/gems/bootsnap-1.3.2/ext/bootsnap
Expand All @@ -12,4 +12,4 @@ linking shared-object bootsnap/bootsnap.bundle

current directory: /Users/Adrien/Dev/projet-integration/sensorygarden-api/vendor/bundle/ruby/2.5.0/gems/bootsnap-1.3.2/ext/bootsnap
make "DESTDIR=" install
/usr/bin/install -c -m 0755 bootsnap.bundle ./.gem.20181011-3510-kzmtgd/bootsnap
/usr/bin/install -c -m 0755 bootsnap.bundle ./.gem.20181013-20035-ph0ap0/bootsnap
@@ -1,5 +1,5 @@
current directory: /Users/Adrien/Dev/projet-integration/sensorygarden-api/vendor/bundle/ruby/2.5.0/gems/byebug-10.0.2/ext/byebug
/Users/Adrien/.rbenv/versions/2.5.1/bin/ruby -r ./siteconf20181011-3510-7h5gr.rb extconf.rb
/Users/Adrien/.rbenv/versions/2.5.1/bin/ruby -r ./siteconf20181013-20035-1s5s4k0.rb extconf.rb
creating Makefile

current directory: /Users/Adrien/Dev/projet-integration/sensorygarden-api/vendor/bundle/ruby/2.5.0/gems/byebug-10.0.2/ext/byebug
Expand All @@ -16,4 +16,4 @@ linking shared-object byebug/byebug.bundle

current directory: /Users/Adrien/Dev/projet-integration/sensorygarden-api/vendor/bundle/ruby/2.5.0/gems/byebug-10.0.2/ext/byebug
make "DESTDIR=" install
/usr/bin/install -c -m 0755 byebug.bundle ./.gem.20181011-3510-11pxtxd/byebug
/usr/bin/install -c -m 0755 byebug.bundle ./.gem.20181013-20035-f89vnl/byebug
@@ -1,5 +1,5 @@
current directory: /Users/Adrien/Dev/projet-integration/sensorygarden-api/vendor/bundle/ruby/2.5.0/gems/ffi-1.9.25/ext/ffi_c
/Users/Adrien/.rbenv/versions/2.5.1/bin/ruby -r ./siteconf20181011-3510-quy44p.rb extconf.rb
/Users/Adrien/.rbenv/versions/2.5.1/bin/ruby -r ./siteconf20181013-20035-a82wmb.rb extconf.rb
checking for ffi.h... no
checking for ffi.h in /usr/local/include,/usr/include/ffi... yes
checking for ffi_call() in -lffi... yes
Expand Down Expand Up @@ -136,4 +136,4 @@ compiling Variadic.c
compiling ffi.c
linking shared-object ffi_c.bundle
ld: warning: could not create compact unwind for _ffi_call_unix64: does not use RBP or RSP based frame
/usr/bin/install -c -m 0755 ffi_c.bundle ./.gem.20181011-3510-1bekpi5
/usr/bin/install -c -m 0755 ffi_c.bundle ./.gem.20181013-20035-d64ebq
Expand Up @@ -120,7 +120,7 @@ ld: warning: text-based stub file /System/Library/Frameworks//Foundation.framewo
ld: warning: text-based stub file /System/Library/Frameworks//CoreFoundation.framework/Versions/A/CoreFoundation.tbd and library file /System/Library/Frameworks//CoreFoundation.framework/Versions/A/CoreFoundation are out of sync. Falling back to library file for linking.
Undefined symbols for architecture x86_64:
"_ffi_closure_alloc", referenced from:
_t in conftest-0bc1d9.o
_t in conftest-33c322.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
checked program was:
Expand Down Expand Up @@ -172,7 +172,7 @@ ld: warning: text-based stub file /System/Library/Frameworks//Foundation.framewo
ld: warning: text-based stub file /System/Library/Frameworks//CoreFoundation.framework/Versions/A/CoreFoundation.tbd and library file /System/Library/Frameworks//CoreFoundation.framework/Versions/A/CoreFoundation are out of sync. Falling back to library file for linking.
Undefined symbols for architecture x86_64:
"_ffi_raw_call", referenced from:
_t in conftest-ec1784.o
_t in conftest-1606c5.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
checked program was:
Expand Down Expand Up @@ -252,7 +252,7 @@ ld: warning: text-based stub file /System/Library/Frameworks//Foundation.framewo
ld: warning: text-based stub file /System/Library/Frameworks//CoreFoundation.framework/Versions/A/CoreFoundation.tbd and library file /System/Library/Frameworks//CoreFoundation.framework/Versions/A/CoreFoundation are out of sync. Falling back to library file for linking.
Undefined symbols for architecture x86_64:
"_rb_thread_blocking_region", referenced from:
_t in conftest-2524d5.o
_t in conftest-ae781d.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
checked program was:
Expand Down
@@ -1,5 +1,5 @@
current directory: /Users/Adrien/Dev/projet-integration/sensorygarden-api/vendor/bundle/ruby/2.5.0/gems/kgio-2.11.2/ext/kgio
/Users/Adrien/.rbenv/versions/2.5.1/bin/ruby -r ./siteconf20181011-3510-nqyou2.rb extconf.rb
/Users/Adrien/.rbenv/versions/2.5.1/bin/ruby -r ./siteconf20181013-20035-pevd2w.rb extconf.rb
checking for CLOCK_MONOTONIC in time.h... yes
checking for clockid_t in time.h... yes
checking for clock_gettime() in -lrt... no
Expand Down Expand Up @@ -56,4 +56,4 @@ linking shared-object kgio_ext.bundle

current directory: /Users/Adrien/Dev/projet-integration/sensorygarden-api/vendor/bundle/ruby/2.5.0/gems/kgio-2.11.2/ext/kgio
make "DESTDIR=" install
/usr/bin/install -c -m 0755 kgio_ext.bundle ./.gem.20181011-3510-11skh3m
/usr/bin/install -c -m 0755 kgio_ext.bundle ./.gem.20181013-20035-1mpklvk
Expand Up @@ -340,7 +340,7 @@ ld: warning: text-based stub file /System/Library/Frameworks//Foundation.framewo
ld: warning: text-based stub file /System/Library/Frameworks//CoreFoundation.framework/Versions/A/CoreFoundation.tbd and library file /System/Library/Frameworks//CoreFoundation.framework/Versions/A/CoreFoundation are out of sync. Falling back to library file for linking.
Undefined symbols for architecture x86_64:
"_accept4", referenced from:
_t in conftest-05823d.o
_t in conftest-3360f0.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
checked program was:
Expand Down Expand Up @@ -811,7 +811,7 @@ ld: warning: text-based stub file /System/Library/Frameworks//Foundation.framewo
ld: warning: text-based stub file /System/Library/Frameworks//CoreFoundation.framework/Versions/A/CoreFoundation.tbd and library file /System/Library/Frameworks//CoreFoundation.framework/Versions/A/CoreFoundation are out of sync. Falling back to library file for linking.
Undefined symbols for architecture x86_64:
"_rb_thread_blocking_region", referenced from:
_t in conftest-db5e12.o
_t in conftest-f2cebf.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
checked program was:
Expand Down
@@ -1,5 +1,5 @@
current directory: /Users/Adrien/Dev/projet-integration/sensorygarden-api/vendor/bundle/ruby/2.5.0/gems/msgpack-1.2.4/ext/msgpack
/Users/Adrien/.rbenv/versions/2.5.1/bin/ruby -r ./siteconf20181011-3510-eg24xs.rb extconf.rb
/Users/Adrien/.rbenv/versions/2.5.1/bin/ruby -r ./siteconf20181013-20035-hjz85i.rb extconf.rb
checking for ruby/st.h... yes
checking for st.h... yes
checking for rb_str_replace() in ruby.h... yes
Expand Down Expand Up @@ -36,4 +36,4 @@ linking shared-object msgpack/msgpack.bundle

current directory: /Users/Adrien/Dev/projet-integration/sensorygarden-api/vendor/bundle/ruby/2.5.0/gems/msgpack-1.2.4/ext/msgpack
make "DESTDIR=" install
/usr/bin/install -c -m 0755 msgpack.bundle ./.gem.20181011-3510-1w5tq49/msgpack
/usr/bin/install -c -m 0755 msgpack.bundle ./.gem.20181013-20035-3hu1q4/msgpack
Binary file not shown.
@@ -1,5 +1,5 @@
current directory: /Users/Adrien/Dev/projet-integration/sensorygarden-api/vendor/bundle/ruby/2.5.0/gems/mysql2-0.5.2/ext/mysql2
/Users/Adrien/.rbenv/versions/2.5.1/bin/ruby -r ./siteconf20181011-3510-12tsc7o.rb extconf.rb
/Users/Adrien/.rbenv/versions/2.5.1/bin/ruby -r ./siteconf20181013-20035-xa5djf.rb extconf.rb
checking for rb_absint_size()... yes
checking for rb_absint_singlebit_p()... yes
checking for rb_wait_for_single_fd()... yes
Expand Down Expand Up @@ -41,4 +41,4 @@ linking shared-object mysql2/mysql2.bundle

current directory: /Users/Adrien/Dev/projet-integration/sensorygarden-api/vendor/bundle/ruby/2.5.0/gems/mysql2-0.5.2/ext/mysql2
make "DESTDIR=" install
/usr/bin/install -c -m 0755 mysql2.bundle ./.gem.20181011-3510-y6fufh/mysql2
/usr/bin/install -c -m 0755 mysql2.bundle ./.gem.20181013-20035-1qdwa4o/mysql2
@@ -1,5 +1,5 @@
current directory: /Users/Adrien/Dev/projet-integration/sensorygarden-api/vendor/bundle/ruby/2.5.0/gems/nio4r-2.3.1/ext/nio4r
/Users/Adrien/.rbenv/versions/2.5.1/bin/ruby -r ./siteconf20181011-3510-77gm8x.rb extconf.rb
/Users/Adrien/.rbenv/versions/2.5.1/bin/ruby -r ./siteconf20181013-20035-o5nqk8.rb extconf.rb
checking for unistd.h... yes
checking for sys/select.h... yes
checking for port_event_t in poll.h... no
Expand Down Expand Up @@ -247,4 +247,4 @@ linking shared-object nio4r_ext.bundle

current directory: /Users/Adrien/Dev/projet-integration/sensorygarden-api/vendor/bundle/ruby/2.5.0/gems/nio4r-2.3.1/ext/nio4r
make "DESTDIR=" install
/usr/bin/install -c -m 0755 nio4r_ext.bundle ./.gem.20181011-3510-ffk1cn
/usr/bin/install -c -m 0755 nio4r_ext.bundle ./.gem.20181013-20035-1a1k86c
@@ -1,5 +1,5 @@
current directory: /Users/Adrien/Dev/projet-integration/sensorygarden-api/vendor/bundle/ruby/2.5.0/gems/nokogiri-1.8.4/ext/nokogiri
/Users/Adrien/.rbenv/versions/2.5.1/bin/ruby -r ./siteconf20181011-3510-12cvh67.rb extconf.rb
/Users/Adrien/.rbenv/versions/2.5.1/bin/ruby -r ./siteconf20181013-20035-jfhci3.rb extconf.rb
checking if the C compiler accepts ... yes
checking if the C compiler accepts -Wno-error=unused-command-line-argument-hard-error-in-future... no
Building nokogiri using packaged libraries.
Expand Down Expand Up @@ -120,4 +120,4 @@ rm -rf /Users/Adrien/Dev/projet-integration/sensorygarden-api/vendor/bundle/ruby

current directory: /Users/Adrien/Dev/projet-integration/sensorygarden-api/vendor/bundle/ruby/2.5.0/gems/nokogiri-1.8.4/ext/nokogiri
make "DESTDIR=" install
/usr/bin/install -c -m 0755 nokogiri.bundle ./.gem.20181011-3510-1d64wpj/nokogiri
/usr/bin/install -c -m 0755 nokogiri.bundle ./.gem.20181013-20035-1xbpexh/nokogiri
Expand Up @@ -73,9 +73,9 @@ ld: warning: text-based stub file /System/Library/Frameworks//Foundation.framewo
ld: warning: text-based stub file /System/Library/Frameworks//CoreFoundation.framework/Versions/A/CoreFoundation.tbd and library file /System/Library/Frameworks//CoreFoundation.framework/Versions/A/CoreFoundation are out of sync. Falling back to library file for linking.
Undefined symbols for architecture x86_64:
"_iconv", referenced from:
_main in conftest-85f9c5.o
_main in conftest-78346e.o
"_iconv_open", referenced from:
_main in conftest-85f9c5.o
_main in conftest-78346e.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
checked program was:
Expand Down

0 comments on commit 5bd96b6

Please sign in to comment.