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.

users_controller.rb 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. class Api::V1::UsersController < ApplicationController
  2. before_action :set_user, only: %i[show update destroy]
  3. before_action :check_owner, only: %i[update destroy]
  4. def index
  5. render json: UserSerializer.new(User.all).serializable_hash.to_json
  6. end
  7. def show
  8. if params[:included] === "true"
  9. options = { include: [:activities] }
  10. render json: UserSerializer.new(@user, options).serializable_hash.to_json
  11. else
  12. render json: UserSerializer.new(@user).serializable_hash.to_json
  13. end
  14. end
  15. def create
  16. @user = User.new(user_params)
  17. if @user.save
  18. render json: UserSerializer.new(@user).serializable_hash.to_json, status: :created
  19. else
  20. render json: @user.errors, status: :unprocessable_entity
  21. end
  22. end
  23. def update
  24. if @user.update(user_params)
  25. render json: UserSerializer.new(@user).serializable_hash.to_json, status: :ok
  26. else
  27. render json: @user.errors, status: :unprocessable_entity
  28. end
  29. end
  30. def destroy
  31. @user.destroy
  32. head 204
  33. end
  34. private
  35. # Only allow a trusted parameter "white list" through.
  36. def user_params
  37. params.require(:user).permit(:email, :username, :password)
  38. end
  39. def set_user
  40. @user = User.find(params[:id])
  41. end
  42. def check_owner
  43. head :forbidden unless @user.id == current_user&.id
  44. end
  45. end