add party share errors and blueprint
This commit is contained in:
parent
329f86df20
commit
d09d370212
2 changed files with 134 additions and 0 deletions
57
app/blueprints/api/v1/party_share_blueprint.rb
Normal file
57
app/blueprints/api/v1/party_share_blueprint.rb
Normal file
|
|
@ -0,0 +1,57 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
module Api
|
||||||
|
module V1
|
||||||
|
class PartyShareBlueprint < ApiBlueprint
|
||||||
|
identifier :id
|
||||||
|
|
||||||
|
fields :created_at
|
||||||
|
|
||||||
|
field :shareable_type do |share|
|
||||||
|
share.shareable_type.downcase
|
||||||
|
end
|
||||||
|
|
||||||
|
field :shareable_id do |share|
|
||||||
|
share.shareable_id
|
||||||
|
end
|
||||||
|
|
||||||
|
view :with_shareable do
|
||||||
|
fields :created_at
|
||||||
|
|
||||||
|
field :shareable_type do |share|
|
||||||
|
share.shareable_type.downcase
|
||||||
|
end
|
||||||
|
|
||||||
|
field :shareable do |share|
|
||||||
|
case share.shareable_type
|
||||||
|
when 'Crew'
|
||||||
|
CrewBlueprint.render_as_hash(share.shareable, view: :minimal)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
field :shared_by do |share|
|
||||||
|
UserBlueprint.render_as_hash(share.shared_by, view: :minimal)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
view :with_party do
|
||||||
|
fields :created_at
|
||||||
|
|
||||||
|
field :shareable_type do |share|
|
||||||
|
share.shareable_type.downcase
|
||||||
|
end
|
||||||
|
|
||||||
|
field :party do |share|
|
||||||
|
PartyBlueprint.render_as_hash(share.party, view: :preview)
|
||||||
|
end
|
||||||
|
|
||||||
|
field :shareable do |share|
|
||||||
|
case share.shareable_type
|
||||||
|
when 'Crew'
|
||||||
|
CrewBlueprint.render_as_hash(share.shareable, view: :minimal)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
77
app/errors/party_share_errors.rb
Normal file
77
app/errors/party_share_errors.rb
Normal file
|
|
@ -0,0 +1,77 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
module PartyShareErrors
|
||||||
|
# Base class for all party share-related errors
|
||||||
|
class PartyShareError < StandardError
|
||||||
|
def http_status
|
||||||
|
:unprocessable_entity
|
||||||
|
end
|
||||||
|
|
||||||
|
def code
|
||||||
|
self.class.name.demodulize.underscore
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_hash
|
||||||
|
{
|
||||||
|
message: message,
|
||||||
|
code: code
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class NotInCrewError < PartyShareError
|
||||||
|
def http_status
|
||||||
|
:unprocessable_entity
|
||||||
|
end
|
||||||
|
|
||||||
|
def code
|
||||||
|
'not_in_crew'
|
||||||
|
end
|
||||||
|
|
||||||
|
def message
|
||||||
|
'You must be in a crew to share parties'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class NotPartyOwnerError < PartyShareError
|
||||||
|
def http_status
|
||||||
|
:forbidden
|
||||||
|
end
|
||||||
|
|
||||||
|
def code
|
||||||
|
'not_party_owner'
|
||||||
|
end
|
||||||
|
|
||||||
|
def message
|
||||||
|
'Only the party owner can share this party'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class AlreadySharedError < PartyShareError
|
||||||
|
def http_status
|
||||||
|
:conflict
|
||||||
|
end
|
||||||
|
|
||||||
|
def code
|
||||||
|
'already_shared'
|
||||||
|
end
|
||||||
|
|
||||||
|
def message
|
||||||
|
'This party is already shared with this crew'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class CanOnlyShareToOwnCrewError < PartyShareError
|
||||||
|
def http_status
|
||||||
|
:forbidden
|
||||||
|
end
|
||||||
|
|
||||||
|
def code
|
||||||
|
'can_only_share_to_own_crew'
|
||||||
|
end
|
||||||
|
|
||||||
|
def message
|
||||||
|
'You can only share parties with your own crew'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Loading…
Reference in a new issue