-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
37adcb7
commit c707b11
Showing
8 changed files
with
138 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
class MessagesController < ApplicationController | ||
before_action :set_message, only: %i[ show update destroy ] | ||
|
||
# GET /messages | ||
def index | ||
@messages = Message.all | ||
|
||
render json: @messages | ||
end | ||
|
||
# GET /messages/1 | ||
def show | ||
render json: @message | ||
end | ||
|
||
# POST /messages | ||
def create | ||
@message = Message.new(message_params) | ||
|
||
if @message.save | ||
render json: @message, status: :created, location: @message | ||
else | ||
render json: @message.errors, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
# PATCH/PUT /messages/1 | ||
def update | ||
if @message.update(message_params) | ||
render json: @message | ||
else | ||
render json: @message.errors, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
# DELETE /messages/1 | ||
def destroy | ||
@message.destroy! | ||
end | ||
|
||
private | ||
# Use callbacks to share common setup or constraints between actions. | ||
def set_message | ||
@message = Message.find(params[:id]) | ||
end | ||
|
||
# Only allow a list of trusted parameters through. | ||
def message_params | ||
params.require(:message).permit(:name, :email, :subject, :content) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class Message < ApplicationRecord | ||
validates :name, presence: true | ||
validates :email, presence: true, format: { with: URI::MailTo::EMAIL_REGEXP } | ||
validates :subject, presence: true | ||
validates :content, presence: true | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class CreateMessages < ActiveRecord::Migration[7.2] | ||
def change | ||
create_table :messages do |t| | ||
t.string :name | ||
t.string :email | ||
t.string :subject | ||
t.text :content | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
require "test_helper" | ||
|
||
class MessagesControllerTest < ActionDispatch::IntegrationTest | ||
setup do | ||
@message = messages(:one) | ||
end | ||
|
||
test "should get index" do | ||
get messages_url, as: :json | ||
assert_response :success | ||
end | ||
|
||
test "should create message" do | ||
assert_difference("Message.count") do | ||
post messages_url, params: { message: { content: @message.content, email: @message.email, name: @message.name, subject: @message.subject } }, as: :json | ||
end | ||
|
||
assert_response :created | ||
end | ||
|
||
test "should show message" do | ||
get message_url(@message), as: :json | ||
assert_response :success | ||
end | ||
|
||
test "should update message" do | ||
patch message_url(@message), params: { message: { content: @message.content, email: @message.email, name: @message.name, subject: @message.subject } }, as: :json | ||
assert_response :success | ||
end | ||
|
||
test "should destroy message" do | ||
assert_difference("Message.count", -1) do | ||
delete message_url(@message), as: :json | ||
end | ||
|
||
assert_response :no_content | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html | ||
|
||
one: | ||
name: MyString | ||
email: MyString | ||
subject: MyString | ||
content: MyText | ||
|
||
two: | ||
name: MyString | ||
email: MyString | ||
subject: MyString | ||
content: MyText |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
require "test_helper" | ||
|
||
class MessageTest < ActiveSupport::TestCase | ||
# test "the truth" do | ||
# assert true | ||
# end | ||
end |