diff --git a/app/controllers/v1/contacts_controller.rb b/app/controllers/v1/contacts_controller.rb new file mode 100644 index 0000000000000000000000000000000000000000..d1e589b425c6a77f931d9036f71b966822524c5d --- /dev/null +++ b/app/controllers/v1/contacts_controller.rb @@ -0,0 +1,52 @@ +class V1::ContactsController < ApplicationController + include ::Paginator + + before_action :set_contact, only: [:show, :update, :destroy] + + # GET v1/contacts + def index + contacts = paginate Contact + render json: contacts + end + + # GET v1/contacts/1 + def show + render json: @contact + end + + # POST v1/contacts + def create + @contact = Contact.new(contact_params) + if @contact.save + render json: @contact, status: :created + else + render json: @contact.errors, status: :unprocessable_entity + end + end + + # PATCH/PUT v1/contacts/1 + def update + if @contact.update(contact_params) + render json: @contact + else + render json: @contact.errors, status: :unprocessable_entity + end + end + + # DELETE v1/contacts/1 + def destroy + @contact.destroy + render status: :ok + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_contact + @contact = Contact.find(params[:id]) + end + + # Only allow a trusted parameter "white list" through. + def contact_params + params.require(:contact).permit(:name, :email, :message) + end +end diff --git a/app/models/contact.rb b/app/models/contact.rb new file mode 100644 index 0000000000000000000000000000000000000000..4d63e81922f4fcde4827c07dcc45635e15fc7989 --- /dev/null +++ b/app/models/contact.rb @@ -0,0 +1,11 @@ +# == Schema Information +# +# Table name: contacts +# +# id :integer not null, primary key +# name :string +# email :string +# message :text +# +class Contact < ApplicationRecord +end diff --git a/app/serializers/contact_serializer.rb b/app/serializers/contact_serializer.rb new file mode 100644 index 0000000000000000000000000000000000000000..b3eca6681df75dec18dc84a302ac7740c0d724f2 --- /dev/null +++ b/app/serializers/contact_serializer.rb @@ -0,0 +1,4 @@ +class ContactSerializer < ActiveModel::Serializer + cache key: 'contact', expires_in: 24.hours + attributes :id, :name, :email, :message +end diff --git a/config/routes.rb b/config/routes.rb index c799fa8d47ee6d0fa4e7c88aba4bfcde0bc5ad90..257e272eceec7082689b97c85f9b3f00cd2d0228 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -139,6 +139,7 @@ Rails.application.routes.draw do resources :roles, except: [:new, :edit] resources :scores, only: [:index, :show, :update] resources :ratings, except: [:new, :edit] + resources :contacts post '/package', to: 'packages#link' get '/subjects', to: 'subjects#index' diff --git a/db/migrate/20170207122721_create_contacts.rb b/db/migrate/20170207122721_create_contacts.rb new file mode 100644 index 0000000000000000000000000000000000000000..7c7f4d52e9b2ad186f03fdccd5a630fa54fcb812 --- /dev/null +++ b/db/migrate/20170207122721_create_contacts.rb @@ -0,0 +1,11 @@ +class CreateContacts < ActiveRecord::Migration[5.0] + def change + create_table :contacts do |t| + t.string :name + t.string :email + t.text :message + + t.timestamps + end + end +end