Skip to content

Commit

Permalink
Adicionando webservice de notícias
Browse files Browse the repository at this point in the history
  • Loading branch information
Matheus456 committed Aug 22, 2019
1 parent 8c3524d commit 533fb1c
Show file tree
Hide file tree
Showing 18 changed files with 340 additions and 0 deletions.
3 changes: 3 additions & 0 deletions app/assets/javascripts/news.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
94 changes: 94 additions & 0 deletions app/controllers/news_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
class NewsController < ApplicationController
skip_before_action :verify_authenticity_token
before_action :set_news, only: [:show, :edit, :update, :destroy]

# GET /news
def index
news = News.all
hash_news = news.map do |c|
{'id' => c.id,
'tag' => c.tag,
'titulo' => c.title,
'link' => c.link,
'icone' => c.icon
}
end
if !hash_news.empty?
render status: 200, json: {
noticias: hash_news,
}.to_json
else
render status: 400, json: {
mensagem: "Não existe notícias.",
}.to_json
end
end

# GET /news/1
def show
respond_to do |format|
if @news
format.json { render :show, status: :ok, location: @news }
else
format.json { render json: {"mensagem": "Essa notícia não existe!"}, status: :unprocessable_entity }
end
end
end

# POST /news
def create
@news = News.new(news_params)
if @news.save
render status: 200, json: {
mensagem: "A notícia foi criada com sucesso!",
}.to_json
else
render status: 400, json: {
mensagem: "Não foi possível criar a notícia.",
}.to_json
end
end

# PATCH/PUT /news/1
def update
respond_to do |format|
if @news
if @news.update(news_params)
format.json { render :show, status: :ok, location: @news }
else
format.json { render json: @news.errors, status: :unprocessable_entity }
end
else
format.json { render json: {"mensagem": "Essa notícia não existe!"}, status: :unprocessable_entity }
end
end
end

# DELETE /news/1
def destroy
respond_to do |format|
if @news
if @news.destroy
format.json { render json: {"mensagem": "Notícia foi excluída com sucesso!"}, status: :ok }
else
format.json { render json: @news.errors, status: :unprocessable_entity }
end
else
format.json { render json: {"mensagem": "Essa notícia não existe!"}, status: :unprocessable_entity }
end
end
end

private
def set_news
begin
@news = News.find(params[:id])
rescue
@new = nil
end
end

def news_params
params.require(:news).permit(:tag, :title, :link, :icon)
end
end
2 changes: 2 additions & 0 deletions app/helpers/news_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module NewsHelper
end
2 changes: 2 additions & 0 deletions app/models/news.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class News < ApplicationRecord
end
37 changes: 37 additions & 0 deletions app/views/news/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<%= form_with(model: news, local: true) do |form| %>
<% if news.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(news.errors.count, "error") %> prohibited this news from being saved:</h2>

<ul>
<% news.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= form.label :tag %>
<%= form.text_field :tag %>
</div>

<div class="field">
<%= form.label :title %>
<%= form.text_field :title %>
</div>

<div class="field">
<%= form.label :link %>
<%= form.text_field :link %>
</div>

<div class="field">
<%= form.label :icon %>
<%= form.text_field :icon %>
</div>

<div class="actions">
<%= form.submit %>
</div>
<% end %>
2 changes: 2 additions & 0 deletions app/views/news/_news.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
json.extract! news, :id, :tag, :title, :link, :icon, :created_at, :updated_at
json.url news_url(news, format: :json)
6 changes: 6 additions & 0 deletions app/views/news/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1>Editing News</h1>

<%= render 'form', news: @news %>

<%= link_to 'Show', @news %> |
<%= link_to 'Back', news_index_path %>
33 changes: 33 additions & 0 deletions app/views/news/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<p id="notice"><%= notice %></p>

<h1>News</h1>

<table>
<thead>
<tr>
<th>Tag</th>
<th>Title</th>
<th>Link</th>
<th>Icon</th>
<th colspan="3"></th>
</tr>
</thead>

<tbody>
<% @news.each do |news| %>
<tr>
<td><%= news.tag %></td>
<td><%= news.title %></td>
<td><%= news.link %></td>
<td><%= news.icon %></td>
<td><%= link_to 'Show', news %></td>
<td><%= link_to 'Edit', edit_news_path(news) %></td>
<td><%= link_to 'Destroy', news, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>

<br>

<%= link_to 'New News', new_news_path %>
1 change: 1 addition & 0 deletions app/views/news/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.array! @news, partial: 'news/news', as: :news
5 changes: 5 additions & 0 deletions app/views/news/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h1>New News</h1>

<%= render 'form', news: @news %>

<%= link_to 'Back', news_index_path %>
24 changes: 24 additions & 0 deletions app/views/news/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<p id="notice"><%= notice %></p>

<p>
<strong>Tag:</strong>
<%= @news.tag %>
</p>

<p>
<strong>Title:</strong>
<%= @news.title %>
</p>

<p>
<strong>Link:</strong>
<%= @news.link %>
</p>

<p>
<strong>Icon:</strong>
<%= @news.icon %>
</p>

<%= link_to 'Edit', edit_news_path(@news) %> |
<%= link_to 'Back', news_index_path %>
1 change: 1 addition & 0 deletions app/views/news/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.partial! "news/news", news: @news
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Rails.application.routes.draw do
resources :news
if Rails.env.development?
mount GraphiQL::Rails::Engine, at: "/graphiql", graphql_path: "/graphql"
end
Expand Down
12 changes: 12 additions & 0 deletions db/migrate/20190822141248_create_news.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreateNews < ActiveRecord::Migration[5.2]
def change
create_table :news do |t|
t.string :tag
t.string :title
t.string :link
t.string :icon

t.timestamps
end
end
end
48 changes: 48 additions & 0 deletions test/controllers/news_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require 'test_helper'

class NewsControllerTest < ActionDispatch::IntegrationTest
setup do
@news = news(:one)
end

test "should get index" do
get news_index_url
assert_response :success
end

test "should get new" do
get new_news_url
assert_response :success
end

test "should create news" do
assert_difference('News.count') do
post news_index_url, params: { news: { icon: @news.icon, link: @news.link, tag: @news.tag, title: @news.title } }
end

assert_redirected_to news_url(News.last)
end

test "should show news" do
get news_url(@news)
assert_response :success
end

test "should get edit" do
get edit_news_url(@news)
assert_response :success
end

test "should update news" do
patch news_url(@news), params: { news: { icon: @news.icon, link: @news.link, tag: @news.tag, title: @news.title } }
assert_redirected_to news_url(@news)
end

test "should destroy news" do
assert_difference('News.count', -1) do
delete news_url(@news)
end

assert_redirected_to news_index_url
end
end
13 changes: 13 additions & 0 deletions test/fixtures/news.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
tag: MyString
title: MyString
link: MyString
icon: MyString

two:
tag: MyString
title: MyString
link: MyString
icon: MyString
7 changes: 7 additions & 0 deletions test/models/news_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class NewsTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end
49 changes: 49 additions & 0 deletions test/system/news_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require "application_system_test_case"

class NewsTest < ApplicationSystemTestCase
setup do
@news = news(:one)
end

test "visiting the index" do
visit news_url
assert_selector "h1", text: "News"
end

test "creating a News" do
visit news_url
click_on "New News"

fill_in "Icon", with: @news.icon
fill_in "Link", with: @news.link
fill_in "Tag", with: @news.tag
fill_in "Title", with: @news.title
click_on "Create News"

assert_text "News was successfully created"
click_on "Back"
end

test "updating a News" do
visit news_url
click_on "Edit", match: :first

fill_in "Icon", with: @news.icon
fill_in "Link", with: @news.link
fill_in "Tag", with: @news.tag
fill_in "Title", with: @news.title
click_on "Update News"

assert_text "News was successfully updated"
click_on "Back"
end

test "destroying a News" do
visit news_url
page.accept_confirm do
click_on "Destroy", match: :first
end

assert_text "News was successfully destroyed"
end
end

0 comments on commit 533fb1c

Please sign in to comment.