-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(batiments) : creation de l'entité batiment et son crud associé (#75
- Loading branch information
1 parent
143e0ad
commit aac3c19
Showing
22 changed files
with
622 additions
and
98 deletions.
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
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,6 @@ | ||
# frozen_string_literal: true | ||
|
||
class BuildingBlueprint < Base | ||
# Fields | ||
fields :name, :description | ||
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,49 @@ | ||
# frozen_string_literal: true | ||
|
||
class Api::V1::BuildingsController < ApiController | ||
before_action :set_building, only: %i[show update destroy] | ||
|
||
def index | ||
buildings = policy_scope(Building) | ||
authorize buildings | ||
render json: apply_fetcheable(buildings).to_blueprint, status: :ok | ||
end | ||
|
||
def show | ||
render json: @building.to_blueprint, status: :ok | ||
end | ||
|
||
def create | ||
authorize Building | ||
building = Building.new(building_params) | ||
if building.save | ||
render json: building.to_blueprint, status: :created | ||
else | ||
render_validation_error(building) | ||
end | ||
end | ||
|
||
def update | ||
if @building.update(building_params) | ||
render json: @building.to_blueprint, status: :ok | ||
else | ||
render_validation_error(@building) | ||
end | ||
end | ||
|
||
def destroy | ||
@building.destroy | ||
head :no_content | ||
end | ||
|
||
private | ||
|
||
def set_building | ||
@building = policy_scope(Building).find(params[:id]) | ||
authorize @building | ||
end | ||
|
||
def building_params | ||
params.require(:building).permit(%i[name description]) | ||
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
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,20 @@ | ||
# frozen_string_literal: true | ||
|
||
class Building < ApplicationRecord | ||
# Associations | ||
has_many :greenhouses, dependent: :destroy | ||
|
||
# Validations | ||
validates :name, presence: true | ||
end | ||
|
||
# == Schema Information | ||
# | ||
# Table name: buildings | ||
# | ||
# id :bigint not null, primary key | ||
# name :string | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# description :text | ||
# |
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,34 @@ | ||
# frozen_string_literal: true | ||
|
||
class BuildingPolicy < ApplicationPolicy | ||
def index? | ||
grower? | ||
end | ||
|
||
def show? | ||
grower? | ||
end | ||
|
||
def create? | ||
grower? | ||
end | ||
|
||
def update? | ||
grower? | ||
end | ||
|
||
def destroy? | ||
return false unless grower? | ||
|
||
return true if record.greenhouses.flat_map(&:benches).flat_map(&:request_distributions).empty? | ||
|
||
record.errors.add(:request_distributions, 'can\'t delete a building with ongoing requests') | ||
false | ||
end | ||
|
||
class Scope < Scope | ||
def resolve | ||
grower? ? scope.all : scope.none | ||
end | ||
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
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 @@ | ||
# frozen_string_literal: true | ||
|
||
class CreateBuildings < ActiveRecord::Migration[7.2] | ||
def change | ||
create_table :buildings do |t| | ||
t.string :name | ||
t.string :description | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
Oops, something went wrong.