Skip to content
Merged
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
5 changes: 4 additions & 1 deletion app/controllers/api/v1/feed_movements_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class FeedMovementsController < BaseController

def create_entry
movement = FeedMovements::CreateEntry.call(
user: current_user,
feed_id: params[:feed_id],
quantity: entry_params[:quantity],
value: entry_params[:value],
Expand All @@ -15,6 +16,7 @@ def create_entry

def create_exit
movement = FeedMovements::CreateExit.call(
user: current_user,
feed_id: params[:feed_id],
quantity: exit_params[:quantity],
reason: exit_params[:reason],
Expand All @@ -24,8 +26,9 @@ def create_exit
end

def index
feed = Feed.active.find(params[:feed_id])
feed = Feeds::Find.call(user: current_user, id: params[:feed_id])
result = FeedMovements::History.call(
user: current_user,
feed_id: feed.id,
page: params[:page], per_page: params[:per_page]
)
Expand Down
9 changes: 5 additions & 4 deletions app/controllers/api/v1/feeds_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ class FeedsController < BaseController
before_action :authenticate_request!

def create
feed = Feeds::Create.call(name: feed_params[:name], initial_stock_quantity: feed_params[:initial_stock_quantity])
feed = Feeds::Create.call(user: current_user, name: feed_params[:name], initial_stock_quantity: feed_params[:initial_stock_quantity])
render json: feed, serializer: FeedSerializer, status: :created
end

def index
result = Feeds::List.call(
user: current_user,
filters: params.permit(:name, :min_stock_quantity, :max_stock_quantity).to_h,
sort: params[:sort],
direction: params[:direction],
Expand All @@ -18,17 +19,17 @@ def index
end

def show
feed = Feeds::Find.call(id: params[:id])
feed = Feeds::Find.call(user: current_user, id: params[:id])
render json: feed, serializer: FeedSerializer
end

def update
feed = Feeds::Update.call(id: params[:id], name: feed_params[:name])
feed = Feeds::Update.call(user: current_user, id: params[:id], name: feed_params[:name])
render json: feed, serializer: FeedSerializer
end

def destroy
Feeds::Deactivate.call(id: params[:id])
Feeds::Deactivate.call(user: current_user, id: params[:id])
head :no_content
end

Expand Down
9 changes: 5 additions & 4 deletions app/controllers/api/v1/groups_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ class GroupsController < BaseController
before_action :authenticate_request!

def create
group = Groups::Create.call(attributes: group_params)
group = Groups::Create.call(user: current_user, attributes: group_params)
render json: group, serializer: GroupSerializer, status: :created
end

def index
result = Groups::List.call(
user: current_user,
filters: params.permit(:name, :min_animal_count, :max_animal_count).to_h,
sort: params[:sort],
direction: params[:direction],
Expand All @@ -18,17 +19,17 @@ def index
end

def show
group = Groups::Find.call(id: params[:id])
group = Groups::Find.call(user: current_user, id: params[:id])
render json: group, serializer: GroupSerializer, status: :ok
end

def update
group = Groups::Update.call(id: params[:id], attributes: group_params)
group = Groups::Update.call(user: current_user, id: params[:id], attributes: group_params)
render json: group, serializer: GroupSerializer, status: :ok
end

def destroy
Groups::Deactivate.call(id: params[:id])
Groups::Deactivate.call(user: current_user, id: params[:id])
head :no_content
end

Expand Down
10 changes: 5 additions & 5 deletions app/controllers/api/v1/occupations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@ class OccupationsController < BaseController
before_action :authenticate_request!

def create
group = Groups::Find.call(id: occupation_params[:group_id])
destination = Terrains::Find.call(id: occupation_params[:terrain_id])
group = Groups::Find.call(user: current_user, id: occupation_params[:group_id])
destination = Terrains::Find.call(user: current_user, id: occupation_params[:terrain_id])

occupation = Occupations::Move.call(group:, destination:)
occupation = Occupations::Move.call(user: current_user, group:, destination:)

render json: occupation, serializer: OccupationSerializer, status: :created
end

def by_group
group = Groups::Find.call(id: params[:id])
group = Groups::Find.call(user: current_user, id: params[:id])
result = Occupations::HistoryByGroup.call(group:, page: params[:page], per_page: params[:per_page])
paginated = PaginatedCollectionSerializer.call(result:, serializer: GroupOccupationHistorySerializer)

render json: { group: GroupSummarySerializer.new(group).attributes, **paginated }
end

def by_terrain
terrain = Terrains::Find.call(id: params[:id])
terrain = Terrains::Find.call(user: current_user, id: params[:id])
result = Occupations::HistoryByTerrain.call(terrain:, page: params[:page], per_page: params[:per_page])
paginated = PaginatedCollectionSerializer.call(result:, serializer: TerrainOccupationHistorySerializer)

Expand Down
9 changes: 5 additions & 4 deletions app/controllers/api/v1/terrains_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ class TerrainsController < BaseController
before_action :authenticate_request!

def create
terrain = Terrains::Create.call(attributes: terrain_params)
terrain = Terrains::Create.call(user: current_user, attributes: terrain_params)
render json: terrain, serializer: TerrainSerializer, status: :created
end

def index
result = Terrains::List.call(
user: current_user,
filters: params.permit(:name, :status, :min_rest_days, :max_rest_days).to_h,
sort: params[:sort],
direction: params[:direction],
Expand All @@ -18,17 +19,17 @@ def index
end

def show
terrain = Terrains::Find.call(id: params[:id])
terrain = Terrains::Find.call(user: current_user, id: params[:id])
render json: terrain, serializer: TerrainSerializer, status: :ok
end

def update
terrain = Terrains::Update.call(id: params[:id], attributes: terrain_params)
terrain = Terrains::Update.call(user: current_user, id: params[:id], attributes: terrain_params)
render json: terrain, serializer: TerrainSerializer, status: :ok
end

def destroy
Terrains::Deactivate.call(id: params[:id])
Terrains::Deactivate.call(user: current_user, id: params[:id])
head :no_content
end

Expand Down
2 changes: 2 additions & 0 deletions app/models/feed.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class Feed < ApplicationRecord
belongs_to :user

has_many :feed_movements, dependent: :restrict_with_error
validates :name, presence: true
validates :stock_quantity, presence: true, numericality: { greater_than_or_equal_to: 0 }
Expand Down
2 changes: 2 additions & 0 deletions app/models/group.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class Group < ApplicationRecord
belongs_to :user

has_many :occupations, dependent: :restrict_with_error

validates :name, presence: true
Expand Down
2 changes: 2 additions & 0 deletions app/models/terrain.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class Terrain < ApplicationRecord
belongs_to :user

has_many :occupations, dependent: :restrict_with_error

enum :status, {
Expand Down
3 changes: 3 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ class User < ApplicationRecord
:recoverable, :validatable

has_many :sessions, dependent: :destroy
has_many :groups, dependent: :destroy
has_many :terrains, dependent: :destroy
has_many :feeds, dependent: :destroy

validates :name, presence: true

Expand Down
4 changes: 2 additions & 2 deletions app/queries/feeds/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class Query
DEFAULT_SORT = { updated_at: :desc }.freeze

class << self
def call(filters: {}, sort: nil, direction: nil)
scope = Filtering::Filterer.call(scope: Feed.active, filters:, definitions: FILTERS)
def call(scope:, filters: {}, sort: nil, direction: nil)
scope = Filtering::Filterer.call(scope:, filters:, definitions: FILTERS)
Sorting::Sorter.call(scope: scope, sort:, direction:, allowed: SORTS, default: DEFAULT_SORT)
end
end
Expand Down
4 changes: 2 additions & 2 deletions app/queries/groups/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class Query
DEFAULT_SORT = { updated_at: :desc }.freeze

class << self
def call(filters: {}, sort: nil, direction: nil)
scope = Filtering::Filterer.call(scope: Group.active, filters:, definitions: FILTERS)
def call(scope:, filters: {}, sort: nil, direction: nil)
scope = Filtering::Filterer.call(scope:, filters:, definitions: FILTERS)
Sorting::Sorter.call(scope: scope, sort:, direction:, allowed: SORTS, default: DEFAULT_SORT)
end
end
Expand Down
4 changes: 2 additions & 2 deletions app/queries/terrains/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class Query
DEFAULT_SORT = { updated_at: :desc }.freeze

class << self
def call(filters: {}, sort: nil, direction: nil)
scope = Filtering::Filterer.call(scope: Terrain.active, filters:, definitions: FILTERS)
def call(scope:, filters: {}, sort: nil, direction: nil)
scope = Filtering::Filterer.call(scope:, filters:, definitions: FILTERS)
Sorting::Sorter.call(scope: scope, sort:, direction:, allowed: SORTS, default: DEFAULT_SORT)
end
end
Expand Down
4 changes: 2 additions & 2 deletions app/services/feed_movements/create_entry.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module FeedMovements
class CreateEntry
class << self
def call(feed_id:, quantity:, value:, occurred_on:)
def call(user:, feed_id:, quantity:, value:, occurred_on:)
ActiveRecord::Base.transaction do
feed = Feed.active.lock.find(feed_id)
feed = user.feeds.active.lock.find(feed_id)

movement = FeedMovement.create!(
feed:,
Expand Down
4 changes: 2 additions & 2 deletions app/services/feed_movements/create_exit.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module FeedMovements
class CreateExit
class << self
def call(feed_id:, quantity:, reason:, occurred_on:)
def call(user:, feed_id:, quantity:, reason:, occurred_on:)
ActiveRecord::Base.transaction do
feed = Feed.active.lock.find(feed_id)
feed = user.feeds.active.lock.find(feed_id)

movement = FeedMovement.new(
feed:,
Expand Down
4 changes: 2 additions & 2 deletions app/services/feed_movements/history.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module FeedMovements
class History
class << self
def call(feed_id:, page:, per_page:)
feed = Feeds::Find.call(id: feed_id)
def call(user:, feed_id:, page:, per_page:)
feed = Feeds::Find.call(user:, id: feed_id)
scope = feed.feed_movements.order(occurred_on: :desc, created_at: :desc)
Pagination::Paginator.call(
scope:,
Expand Down
4 changes: 2 additions & 2 deletions app/services/feeds/create.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module Feeds
class Create
class << self
def call(name:, initial_stock_quantity:)
Feed.create!(name:, stock_quantity: initial_stock_quantity)
def call(user:, name:, initial_stock_quantity:)
user.feeds.create!(name:, stock_quantity: initial_stock_quantity)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions app/services/feeds/deactivate.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module Feeds
class Deactivate
class << self
def call(id:)
feed = Feeds::Find.call(id:)
def call(user:, id:)
feed = Feeds::Find.call(user:, id:)
feed.soft_delete!
feed
end
Expand Down
4 changes: 2 additions & 2 deletions app/services/feeds/find.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module Feeds
class Find
class << self
def call(id:)
Feed.active.find(id)
def call(user:, id:)
user.feeds.active.find(id)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions app/services/feeds/list.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module Feeds
class List
class << self
def call(filters: {}, sort: nil, direction: nil, page:, per_page:)
scope = Feeds::Query.call(filters:, sort:, direction:)
def call(user:, filters: {}, sort: nil, direction: nil, page:, per_page:)
scope = Feeds::Query.call(scope: user.feeds.active, filters:, sort:, direction:)
Pagination::Paginator.call(
scope: scope,
page:,
Expand Down
4 changes: 2 additions & 2 deletions app/services/feeds/update.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module Feeds
class Update
class << self
def call(id:, name:)
feed = Feeds::Find.call(id:)
def call(user:, id:, name:)
feed = Feeds::Find.call(user:, id:)
feed.update!(name:)
feed
end
Expand Down
4 changes: 2 additions & 2 deletions app/services/groups/create.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module Groups
class Create
class << self
def call(attributes:)
Group.create!(attributes)
def call(user:, attributes:)
user.groups.create!(attributes.merge(user:))
end
end
end
Expand Down
12 changes: 6 additions & 6 deletions app/services/groups/deactivate.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
module Groups
class Deactivate
class << self
def call(id:)
def call(user:, id:)
ActiveRecord::Base.transaction do
group = Group.active.lock.find(id)
group = user.groups.active.lock.find(id)
occupation = group.occupations.active.lock.first
close_occupation(occupation) if occupation
close_occupation(user:, occupation:) if occupation
group.soft_delete!
group
end
end

private

def close_occupation(occupation)
occupation.terrain.lock!
def close_occupation(user:, occupation:)
terrain = user.terrains.lock.find(occupation.terrain_id)
Occupations::Close.call(occupation:)
Terrains::StartRest.call(terrain: occupation.terrain)
Terrains::StartRest.call(terrain:)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions app/services/groups/find.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module Groups
class Find
class << self
def call(id:)
Group.active.find(id)
def call(user:, id:)
user.groups.active.find(id)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions app/services/groups/list.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module Groups
class List
class << self
def call(filters: {}, sort: nil, direction: nil, page:, per_page:)
scope = Groups::Query.call(filters:, sort:, direction:)
def call(user:, filters: {}, sort: nil, direction: nil, page:, per_page:)
scope = Groups::Query.call(scope: user.groups.active, filters:, sort:, direction:)
Pagination::Paginator.call(
scope: scope,
page:,
Expand Down
4 changes: 2 additions & 2 deletions app/services/groups/update.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module Groups
class Update
class << self
def call(id:, attributes:)
group = Groups::Find.call(id:)
def call(user:, id:, attributes:)
group = Groups::Find.call(user:, id:)
group.update!(attributes)
group
end
Expand Down
Loading
Loading