add rfid tags controller

This commit is contained in:
Christophe Robillard 2025-08-07 15:56:30 +02:00
parent 240c94d1dd
commit 1d07af2882
5 changed files with 43 additions and 0 deletions

View 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

View file

@ -0,0 +1,2 @@
module RfidTagsHelper
end

View file

@ -0,0 +1,2 @@
<h1>RfidTags#post</h1>
<p>Find me in app/views/rfid_tags/post.html.erb</p>

View file

@ -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.

View 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