API de comptabilité horaire.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

memberships_controller.rb 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. render json: MembershipSerializer.new(Membership.all).serializable_hash.to_json
  6. end
  7. def show
  8. render json: MembershipSerializer.new(@membership).serializable_hash.to_json
  9. end
  10. def create
  11. member_id = User.where(username: params[:membership][:username])[0].id
  12. team_id = Team.where(name: params[:membership][:team_name])[0].id
  13. membership = Membership.new(team_id: team_id, member_id: member_id, can_edit: false)
  14. if membership.save
  15. render json: MembershipSerializer.new(membership).serializable_hash.to_json, status: :created
  16. else
  17. render json: membership.errors, status: :unprocessable_entity
  18. end
  19. end
  20. def update
  21. if params[:membership][:can_edit]
  22. if @membership.update(memberships_params)
  23. render json: MembershipSerializer.new(@membership).serializable_hash.to_json, status: :ok
  24. else
  25. render json: @membership.errors, status: :unprocessable_entity
  26. end
  27. else
  28. head 422
  29. end
  30. end
  31. def destroy
  32. @membership.destroy
  33. head 204
  34. end
  35. private
  36. def memberships_params
  37. params.require(:membership).permit(:can_edit)
  38. end
  39. def set_memberships
  40. @membership = Membership.find(params[:id])
  41. end
  42. end