class Api::V1::MembershipsController < ApplicationController before_action :set_memberships, only: %i[show update destroy] before_action :check_login def index render json: MembershipSerializer.new(Membership.all).serializable_hash.to_json end def show render json: MembershipSerializer.new(@membership).serializable_hash.to_json end def create member_id = User.where(username: params[:membership][:username])[0].id team_id = Team.where(name: params[:membership][:team_name])[0].id membership = Membership.new(team_id: team_id, member_id: member_id, can_edit: false) if membership.save render json: MembershipSerializer.new(membership).serializable_hash.to_json, status: :created else render json: membership.errors, status: :unprocessable_entity end end def update if params[:membership][:can_edit] if @membership.update(memberships_params) render json: MembershipSerializer.new(@membership).serializable_hash.to_json, status: :ok else render json: @membership.errors, status: :unprocessable_entity end else head 422 end end def destroy @membership.destroy head 204 end private def memberships_params params.require(:membership).permit(:can_edit) end def set_memberships @membership = Membership.find(params[:id]) end end