add rfid tags controller
This commit is contained in:
parent
240c94d1dd
commit
1d07af2882
5 changed files with 43 additions and 0 deletions
30
app/controllers/rfid_tags_controller.rb
Normal file
30
app/controllers/rfid_tags_controller.rb
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
class RfidTagsController < ApplicationController
|
||||
skip_forgery_protection
|
||||
|
||||
def create
|
||||
identifier = params.expect(:identifier)
|
||||
rfid_tag = RfidTag.find_or_initialize_by(identifier:)
|
||||
if rfid_tag.new_record?
|
||||
register rfid_tag
|
||||
return
|
||||
end
|
||||
|
||||
create_mood(rfid_tag)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def register(rfid_tag)
|
||||
rfid_tag.save!
|
||||
head :created, code: :registered
|
||||
end
|
||||
|
||||
def create_mood(rfid_tag)
|
||||
if rfid_tag.mode
|
||||
Mood.create!(recorded_at: DateTime.now, mode: rfid_tag.mode)
|
||||
head :created, code: :recorded
|
||||
else
|
||||
head :unprocessable_entity
|
||||
end
|
||||
end
|
||||
end
|
||||
2
app/helpers/rfid_tags_helper.rb
Normal file
2
app/helpers/rfid_tags_helper.rb
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
module RfidTagsHelper
|
||||
end
|
||||
2
app/views/rfid_tags/post.html.erb
Normal file
2
app/views/rfid_tags/post.html.erb
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
<h1>RfidTags#post</h1>
|
||||
<p>Find me in app/views/rfid_tags/post.html.erb</p>
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
Rails.application.routes.draw do
|
||||
post '/rfid_tags' => 'rfid_tags#create'
|
||||
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
|
||||
|
||||
# Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500.
|
||||
|
|
|
|||
8
test/controllers/rfid_tags_controller_test.rb
Normal file
8
test/controllers/rfid_tags_controller_test.rb
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
require "test_helper"
|
||||
|
||||
class RfidTagsControllerTest < ActionDispatch::IntegrationTest
|
||||
test "should get post" do
|
||||
get rfid_tags_post_url
|
||||
assert_response :success
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Reference in a new issue