# frozen_string_literal: true

module Api
  class UsersController < Api::ApplicationController
    def index
      @users = User.all
      render json: @users
    end

    def create
      @user = User.new(user_params)
      if @user.save
        render json: @user
      else
        render json: { message: @user.errors.full_message.join(',') }, status: 422
      end
    end

    def update
      @user = User.find(params[:id])
      if @user.update(user_params)
        render json: @user
      else
        render json: { message: @user.errors.full_message.join(',') }, status: 422
      end
    end

    def destroy
      @user = User.find(params[:id])
      @user.destroy
      render json: @user
    end

    private

    def user_params
      params.require(:user).permit(:name, :email)
    end
  end
end
