API de comptabilité horaire.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

teams_controller.rb 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. class Api::V1::TeamsController < ApplicationController
  2. before_action :set_team, only: %i[show update destroy]
  3. before_action :check_login
  4. before_action :can_edit?, only: %i[update destroy]
  5. def index
  6. render json: TeamSerializer.new(Team.all).serializable_hash.to_json
  7. end
  8. def show
  9. render json: TeamSerializer.new(@team).serializable_hash.to_json
  10. end
  11. def create
  12. team = current_user.teams.build(team_params)
  13. if team.save
  14. if current_user&.is_admin === false
  15. Membership.create!(
  16. member_id: current_user.id,
  17. team_id: team.id,
  18. can_edit: true
  19. )
  20. render json: TeamSerializer.new(team).serializable_hash.to_json, status: :created
  21. else
  22. render json: TeamSerializer.new(team).serializable_hash.to_json, status: :created
  23. end
  24. else
  25. render json: team.errors, status: :unprocessable_entity
  26. end
  27. end
  28. def update
  29. if @team.update(team_params)
  30. render json: TeamSerializer.new(@team).serializable_hash.to_json, status: :ok
  31. else
  32. render json: @team.errors, status: :unprocessable_entity
  33. end
  34. end
  35. def destroy
  36. @team.destroy
  37. head 204
  38. end
  39. private
  40. def team_params
  41. params.require(:team).permit(:name, :description, :created_at, :updated_at)
  42. end
  43. def set_team
  44. @team = Team.find(params[:id])
  45. end
  46. def can_edit?
  47. head :forbidden unless Membership.where(team_id: params[:id]).where(member_id: current_user.id)[0].can_edit
  48. end
  49. end