Adapt API controllers to recent service changes

This commit is contained in:
Frank Rousseau 2017-11-05 22:34:03 +01:00
parent f23e947d0c
commit 2d40801bb4
4 changed files with 37 additions and 8 deletions

View file

@ -3,7 +3,7 @@
module Api module Api
module V0 module V0
class CommentsController < Api::V0::BaseController class CommentsController < Api::V0::BaseController
before_action only: %i[reate destroy] do before_action only: %i[create destroy] do
require_access_token %w[read write] require_access_token %w[read write]
end end
@ -16,18 +16,22 @@ module Api
end end
def create def create
@comment = CommentService.new(post_id: params[:post_id], text: params[:text], user: current_user).create_comment @comment = comment_service.create(params[:post_id], params[:text])
render json: CommentPresenter.new(@comment), status: 201 render json: CommentPresenter.new(@comment), status: 201
end end
def destroy def destroy
service = CommentService.new(comment_id: params[:id], user: current_user) if comment_service.destroy(params[:id])
if service.destroy_comment head :no_content
render json: I18n.t("comments.destroy.success", id: params[:id]), status: 200
else else
render json: I18n.t("comments.destroy.fail"), status: 403 render json: I18n.t("comments.destroy.fail"), status: 403
end end
end end
def comment_service
@comment_service ||= CommentService.new(current_user)
end
end end
end end
end end

View file

@ -27,7 +27,7 @@ module Api
def destroy def destroy
@like = Like.find_by!(id: params[:id], author_id: current_user.person.id) @like = Like.find_by!(id: params[:id], author_id: current_user.person.id)
current_user.retract(@like) current_user.retract(@like)
render nothing: true, status: 204 head :no_content, status: 204
end end
private private

View file

@ -26,7 +26,7 @@ module Api
def index def index
conversation = conversation_service.find!(params[:conversation_id]) conversation = conversation_service.find!(params[:conversation_id])
conversation.set_read(user) conversation.set_read(current_user)
render json: conversation.messages.map {|x| message_json(x) }, status: 201 render json: conversation.messages.map {|x| message_json(x) }, status: 201
end end

View file

@ -20,7 +20,7 @@ module Api
end end
def create def create
@status_message = StatusMessageCreationService.new(params, current_user).status_message @status_message = StatusMessageCreationService.new(current_user).create(normalized_params)
render json: PostPresenter.new(@status_message, current_user) render json: PostPresenter.new(@status_message, current_user)
end end
@ -29,6 +29,31 @@ module Api
post_service.retract_post post_service.retract_post
render nothing: true, status: 204 render nothing: true, status: 204
end end
def normalized_params
params.permit(
:location_address,
:location_coords,
:poll_question,
status_message: %i[text provider_display_name],
poll_answers: []
).to_h.merge(
services: [*params[:services]].compact,
aspect_ids: normalize_aspect_ids,
public: [*params[:aspect_ids]].first == "public",
photos: [*params[:photos]].compact
)
end
def normalize_aspect_ids
aspect_ids = [*params[:aspect_ids]]
if aspect_ids.first == "all_aspects"
current_user.aspect_ids
else
aspect_ids
end
end
end end
end end
end end