require_dependency "<%= module_snake %>/api/v<%= api_version %>/application_controller" require 'authorization' module <%= module_camel %> class Api::V<%= api_version %>::<%= resource_camel.pluralize %>Controller < Api::V<%= api_version %>::ApplicationController before_action :set_<%= resource_singular %>, only: [:show, :update, :destroy] # GET /api/<%= api_version %>/<%= resource_plural %> def index @<%= resource_plural %> = ::<%= module_camel %>::V<%= api_version %>::<%= resource_camel %>.all render json: @<%= resource_plural %> end # GET /api/<%= api_version %>/<%= resource_plural %>/1 def show render json: @<%= resource_singular %> end # POST /api/<%= api_version %>/<%= resource_plural %> def create @<%= resource_singular %> = ::<%= module_camel %>::V<%= api_version %>::<%= resource_camel %>.new(<%= resource_singular %>_params) if @<%= resource_singular %>.save render json: @<%= resource_singular %> else render :json => {errors: @<%= resource_singular %>.errors.full_messages}, status: :unprocessable_entity end end # PATCH/PUT /api/<%= api_version %>/<%= resource_plural %>/1 def update if @<%= resource_singular %>.update(<%= resource_singular %>_params) render json: @<%= resource_singular %> else render :json => {errors: @<%= resource_singular %>.errors.full_messages}, status: :unprocessable_entity end end # DELETE /api/<%= api_version %>/<%= resource_plural %>/1 def destroy @<%= resource_singular %>.destroy render json: {} end private # Use callbacks to share common setup or constraints between actions. def set_<%= resource_singular %> @<%= resource_singular %> = ::<%= module_camel %>::V<%= api_version %>::<%= resource_camel %>.find_by_id(params[:id]) if @<%= resource_singular %>.nil? render :json => {errors: "<%= resource_camel %> was not found"}, status: :not_found end end # Only allow a trusted parameter "white list" through. def <%= resource_singular %>_params params.require(:<%= resource_singular %>).permit(<%= params_list %>) end end end