diff --git a/app/controllers/rfid_tags_controller.rb b/app/controllers/rfid_tags_controller.rb new file mode 100644 index 0000000..1fc49b0 --- /dev/null +++ b/app/controllers/rfid_tags_controller.rb @@ -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 diff --git a/app/helpers/rfid_tags_helper.rb b/app/helpers/rfid_tags_helper.rb new file mode 100644 index 0000000..bc6b482 --- /dev/null +++ b/app/helpers/rfid_tags_helper.rb @@ -0,0 +1,2 @@ +module RfidTagsHelper +end diff --git a/app/views/rfid_tags/post.html.erb b/app/views/rfid_tags/post.html.erb new file mode 100644 index 0000000..e94c238 --- /dev/null +++ b/app/views/rfid_tags/post.html.erb @@ -0,0 +1,2 @@ +

RfidTags#post

+

Find me in app/views/rfid_tags/post.html.erb

diff --git a/config/routes.rb b/config/routes.rb index e4ed79a..a0ccd3f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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. diff --git a/test/controllers/rfid_tags_controller_test.rb b/test/controllers/rfid_tags_controller_test.rb new file mode 100644 index 0000000..3ccfbe3 --- /dev/null +++ b/test/controllers/rfid_tags_controller_test.rb @@ -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