API de comptabilité horaire.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

memberships_controller.rb 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. class Api::V1::MembershipsController < ApplicationController
  2. before_action :set_memberships, only: %i[show update destroy]
  3. before_action :check_login
  4. def index
  5. if !params[:team_id]
  6. render json: MembershipSerializer.new(Membership.all).serializable_hash.to_json
  7. else
  8. render json: MembershipSerializer.new(Team.find(params[:team_id]).memberships).serializable_hash.to_json
  9. end
  10. end
  11. def show
  12. render json: MembershipSerializer.new(@membership).serializable_hash.to_json
  13. end
  14. def create
  15. membership = current_user.memberships.build(membership_params)
  16. membership.team_id = params[:team_id]
  17. if membership.save
  18. render json: MembershipSerializer.new(membership).serializable_hash.to_json, status: :created
  19. else
  20. render json: membership.errors, status: :unprocessable_entity
  21. end
  22. end
  23. def update
  24. if params[:membership][:can_edit]
  25. if @membership.update(membership_params)
  26. render json: MembershipSerializer.new(@membership).serializable_hash.to_json, status: :ok
  27. else
  28. render json: @membership.errors, status: :unprocessable_entity
  29. end
  30. else
  31. head 422
  32. end
  33. end
  34. def destroy
  35. @membership.destroy
  36. head 204
  37. end
  38. private
  39. def membership_params
  40. params.require(:membership).permit(:can_edit)
  41. end
  42. def set_memberships
  43. @membership = Membership.find(params[:id])
  44. end
  45. end