Skip to content

Fs_Challenge/ views and migrations #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@
/yarn-error.log
yarn-debug.log*
.yarn-integrity
.rubocop.yml
34 changes: 27 additions & 7 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ GEM
zeitwerk (~> 2.3)
addressable (2.8.0)
public_suffix (>= 2.0.2, < 5.0)
ast (2.4.2)
bcrypt (3.1.16)
bindex (0.8.1)
bootsnap (1.8.1)
Expand Down Expand Up @@ -91,7 +92,7 @@ GEM
ffi (1.15.3)
globalid (0.5.2)
activesupport (>= 5.0)
i18n (1.8.10)
i18n (1.8.11)
concurrent-ruby (~> 1.0)
jbuilder (2.11.2)
activesupport (>= 5.0.0)
Expand All @@ -110,17 +111,20 @@ GEM
minitest (5.14.4)
msgpack (1.4.2)
nio4r (2.5.8)
nokogiri (1.12.4)
nokogiri (1.12.5)
mini_portile2 (~> 2.6.1)
racc (~> 1.4)
nokogiri (1.12.4-x86_64-linux)
nokogiri (1.12.5-x86_64-linux)
racc (~> 1.4)
orm_adapter (0.5.0)
parallel (1.21.0)
parser (3.0.2.0)
ast (~> 2.4.1)
pg (1.2.3)
public_suffix (4.0.6)
puma (5.4.0)
nio4r (~> 2.0)
racc (1.5.2)
racc (1.6.0)
rack (2.2.3)
rack-mini-profiler (2.3.3)
rack (>= 1.2.0)
Expand Down Expand Up @@ -154,6 +158,7 @@ GEM
method_source
rake (>= 0.13)
thor (~> 1.0)
rainbow (3.0.0)
rake (13.0.6)
rb-fsevent (0.11.0)
rb-inotify (0.10.1)
Expand All @@ -162,6 +167,19 @@ GEM
responders (3.0.1)
actionpack (>= 5.0)
railties (>= 5.0)
rexml (3.2.5)
rubocop (1.23.0)
parallel (~> 1.10)
parser (>= 3.0.0.0)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml
rubocop-ast (>= 1.12.0, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 3.0)
rubocop-ast (1.13.0)
parser (>= 3.0.1.1)
ruby-progressbar (1.11.0)
rubyzip (2.3.2)
sass-rails (6.0.0)
sassc-rails (~> 2.1, >= 2.1.1)
Expand Down Expand Up @@ -194,6 +212,7 @@ GEM
turbolinks-source (5.2.0)
tzinfo (2.0.4)
concurrent-ruby (~> 1.0)
unicode-display_width (2.1.0)
warden (1.2.9)
rack (>= 2.0.9)
web-console (4.1.0)
Expand All @@ -205,7 +224,7 @@ GEM
nokogiri (~> 1.6)
rubyzip (>= 1.3.0)
selenium-webdriver (>= 3.0, < 4.0)
webpacker (5.4.2)
webpacker (5.4.3)
activesupport (>= 5.2)
rack-proxy (>= 0.6.1)
railties (>= 5.2)
Expand All @@ -215,7 +234,7 @@ GEM
websocket-extensions (0.1.5)
xpath (3.2.0)
nokogiri (~> 1.8)
zeitwerk (2.4.2)
zeitwerk (2.5.1)

PLATFORMS
ruby
Expand All @@ -233,6 +252,7 @@ DEPENDENCIES
puma (~> 5.0)
rack-mini-profiler (~> 2.0)
rails (~> 6.1.4)
rubocop
sass-rails (>= 6)
selenium-webdriver
spring
Expand All @@ -247,4 +267,4 @@ RUBY VERSION
ruby 2.7.4p191

BUNDLED WITH
2.2.24
2.2.31
8 changes: 4 additions & 4 deletions app/controllers/books_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def create

respond_to do |format|
if @book.save
format.html { redirect_to @book, notice: "Book was successfully created." }
format.html { redirect_to @book, notice: 'Book was successfully created.' }
format.json { render :show, status: :created, location: @book }
else
format.html { render :new, status: :unprocessable_entity }
Expand All @@ -38,7 +38,7 @@ def create
def update
respond_to do |format|
if @book.update(book_params)
format.html { redirect_to @book, notice: "Book was successfully updated." }
format.html { redirect_to @book, notice: 'Book was successfully updated.' }
format.json { render :show, status: :ok, location: @book }
else
format.html { render :edit, status: :unprocessable_entity }
Expand All @@ -51,7 +51,7 @@ def update
def destroy
@book.destroy
respond_to do |format|
format.html { redirect_to books_url, notice: "Book was successfully destroyed." }
format.html { redirect_to books_url, notice: 'Book was successfully destroyed.' }
format.json { head :no_content }
end
end
Expand All @@ -64,6 +64,6 @@ def set_book

# Only allow a list of trusted parameters through.
def book_params
params.fetch(:book, {}).permit(:name)
params.fetch(:book, {}).permit(:name, :author)
end
end
43 changes: 36 additions & 7 deletions app/controllers/lends_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def index

# GET /lends/1 or /lends/1.json
def show
@lend = Lend.find(params[:id])
end

# GET /lends/new
Expand All @@ -17,15 +18,27 @@ def new

# GET /lends/1/edit
def edit
@lend = Lend.find(params[:id])
end

# POST /lends or /lends.json
def create
@lend = Lend.new(lend_params)

@lend = Lend.new

if User.find_by(email: lend_params[:user_email]).nil?
error = true
@lend.errors[:base] << "Invalid email"
else
error = false
user = User.find_by!(email: lend_params[:user_email])
@lend.book_id = lend_params[:book_id]
@lend.user_id = user.id
end
Copy link
Contributor

@thiagodiniz thiagodiniz Nov 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Como vocês faria isso utilizando uma abordagem de serviço?

Outra coisa, não vamos poder mergiar isso nesse repo, por que é dos proximos candidatos 🙄


respond_to do |format|
if @lend.save
format.html { redirect_to @lend, notice: "Lend was successfully created." }
if !error && @lend.save
format.html { redirect_to @lend, notice: 'Lend was successfully created.' }
format.json { render :show, status: :created, location: @lend }
else
format.html { render :new, status: :unprocessable_entity }
Expand All @@ -36,9 +49,25 @@ def create

# PATCH/PUT /lends/1 or /lends/1.json
def update

if User.find_by(email: lend_params[:user_email]).nil?
error = true
@lend.errors[:base] << 'Invalid email'
else
user = User.find_by!(email: lend_params[:user_email])
@lend.user_id = user.id
@lend.book_id = lend_params[:book_id]

if lend_params[:returned_bool] == 'Yes'
@lend.devolution = Time.new
else
@lend.devolution = nil
end
end

respond_to do |format|
if @lend.update(lend_params)
format.html { redirect_to @lend, notice: "Lend was successfully updated." }
if !error && @lend.save
format.html { redirect_to @lend, notice: 'Lend was successfully updated.' }
format.json { render :show, status: :ok, location: @lend }
else
format.html { render :edit, status: :unprocessable_entity }
Expand All @@ -51,7 +80,7 @@ def update
def destroy
@lend.destroy
respond_to do |format|
format.html { redirect_to lends_url, notice: "Lend was successfully destroyed." }
format.html { redirect_to lends_url, notice: 'Lend was successfully destroyed.' }
format.json { head :no_content }
end
end
Expand All @@ -64,6 +93,6 @@ def set_lend

# Only allow a list of trusted parameters through.
def lend_params
params.fetch(:lend, {}).permit(:book_id)
params.fetch(:lend, {}).permit(:book_id, :user_email, :returned_bool)
end
end
62 changes: 62 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
class UsersController < ApplicationController
before_action :set_user, only: %i[ edit update show ]

def index
@users = User.all
end

def show
@user = User.find(params[:id])
end

def new
@user = User.new
end

def create
@user = User.new(user_params)

respond_to do |format|
if @user.update(user_params)
format.html { redirect_to @user, notice: 'User was successfuly created!' }
format.json { render :show, status: :ok, location: @user }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end

def edit
end

def update

respond_to do |format|
if @user.update(user_params)
format.html { redirect_to @user, notice: 'User was successfuly updated!!' }
format.json { render :show, status: :ok, location: @user }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end

def destroy
@user.destroy
respond_to do |format|
format.html { redirect_to users_url, notice: 'user was successfully destroyed.' }
format.json { head :no_content }
end
end

private
def set_user
@user = User.find(params[:id])
end

def user_params
params.fetch(:user, {}).permit(:user_id, :name, :password, :email, :id)
end
end
2 changes: 2 additions & 0 deletions app/helpers/users_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module UsersHelper
end
2 changes: 2 additions & 0 deletions app/models/book.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
class Book < ApplicationRecord
has_many :lends

validates :name, uniqueness: true
end
5 changes: 4 additions & 1 deletion app/models/lend.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
class Lend < ApplicationRecord
belongs_to :book
end
belongs_to :user

validates_with LoanDevolutionValidator
end
20 changes: 20 additions & 0 deletions app/validators/loan_devolution_validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class LoanDevolutionValidator < ActiveModel::Validator
def validate(record)
unless record.devolution.nil?
months = ((record.devolution.year * 12 + record.devolution.month) -
(record.created_at.year * 12 + record.created_at.month))

if record.devolution < record.created_at
record.erros[:base] << 'The return date cannot be less than loan date'
elsif months >= 6
record.erros[:base] << 'The datetime cannot be greater of six months'
end
end

lends = Lend.where({ user_id: record.user_id, devolution: nil })

if lends.size >= 2 # check if exists two or more lends not closed
record.errors[:base] << 'The user has more than 2 loans not closed'
end
end
end
6 changes: 6 additions & 0 deletions app/views/books/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
<%= form.text_field :name, autocomplete: "given-name", class: "max-w-lg block w-full shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:max-w-xs sm:text-sm border-gray-300 rounded-md" %>
</div>
</div>
<%= form.label :author, class: "block text-sm font-medium text-gray-700 sm:mt-px sm:pt-2" %>
<div class="mt-1 sm:mt-0 sm:col-span-2">
<div class="max-w-lg flex rounded-md shadow-sm">
<%= form.text_field :author, autocomplete: "given-name", class: "max-w-lg block w-full shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:max-w-xs sm:text-sm border-gray-300 rounded-md" %>
</div>
</div>
</div>
</div>
</div>
Expand Down
6 changes: 6 additions & 0 deletions app/views/books/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
<dd class="mt-1 text-sm text-gray-900 sm:mt-0 sm:col-span-2">
<%= @book.name %>
</dd>
<dt class="text-sm font-medium text-gray-500">
Book Author
</dt>
<dd class="mt-1 text-sm text-gray-900 sm:mt-0 sm:col-span-2">
<%= @book.author %>
</dd>
</div>
</dl>
</div>
Expand Down
4 changes: 2 additions & 2 deletions app/views/layouts/_sidebar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
Books
<% end %>

<a href="#" class="text-gray-300 hover:bg-gray-700 hover:text-white group flex items-center px-2 py-2 text-sm font-medium rounded-md">
<%= link_to users_path, class: "text-gray-300 hover:bg-gray-700 hover:text-white group flex items-center px-2 py-2 text-sm font-medium rounded-md" do %>
<!-- Heroicon name: outline/users -->
<svg class="text-gray-400 group-hover:text-gray-300 mr-3 flex-shrink-0 h-6 w-6" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor" aria-hidden="true">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4.354a4 4 0 110 5.292M15 21H3v-1a6 6 0 0112 0v1zm0 0h6v-1a6 6 0 00-9-5.197M13 7a4 4 0 11-8 0 4 4 0 018 0z" />
</svg>
Users
</a>
<% end %>
</nav>
</div>
<div class="flex-shrink-0 flex bg-gray-700 p-4">
Expand Down
11 changes: 11 additions & 0 deletions app/views/lends/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@
<div class="mt-1 sm:mt-0 sm:col-span-2">
<%= form.select :book_id, Book.all.collect { |b| [b.name, b.id] }, {}, class: "max-w-lg block focus:ring-indigo-500 focus:border-indigo-500 w-full shadow-sm sm:max-w-xs sm:text-sm border-gray-300 rounded-md" %>
</div>
<%= form.label :user_email, class: "block text-sm font-medium text-gray-700 sm:mt-px sm:pt-2" %>
<div class="mt-1 sm:mt-0 sm:col-span-2">
<%= form.text_field :user_email, class: "max-w-lg block focus:ring-indigo-500 focus:border-indigo-500 w-full shadow-sm sm:max-w-xs sm:text-sm border-gray-300 rounded-md" %>
</div>
<%= form.label :returned, class: "block text-sm font-medium text-gray-700 sm:mt-px sm:pt-2" %>
<div class="mt-1 sm:mt-0 sm:col-span-2">
<%= form.radio_button :returned_bool, "Yes", checked: false, class: "col-span-2"%>
<%= form.label :returned_yes, "Yes", class: "max-w-lg focus:ring-indigo-500 focus:border-indigo-500 w-full shadow-sm sm:max-w-xs sm:text-sm border-gray-300 rounded-md col-span-3"%>
<%= form.radio_button :returned_bool, "No", checked: true, class: "col-span-2"%>
<%= form.label :returned_no, "No", class: "max-w-lg focus:ring-indigo-500 focus:border-indigo-500 w-full shadow-sm sm:max-w-xs sm:text-sm border-gray-300 rounded-md col-span-3"%>
</div>
</div>
</div>
</div>
Expand Down
Loading