-
Notifications
You must be signed in to change notification settings - Fork 2
#Sn-41-Create user interests API endpoint #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
b365735
e1772eb
087a752
9b41e70
568e62f
4ef8e10
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,13 +4,15 @@ module API | |
| module V1 | ||
| # Controller class that is responsible for handling profile requests. | ||
| class ProfilesController < ApplicationController | ||
| before_action :set_profile, only: %i[show update] | ||
| before_action :authorize_profile! | ||
| before_action :set_profile, except: %i[index create] | ||
| before_action :set_tag, only: %i[remove_tag] | ||
| after_action :verify_authorized | ||
|
|
||
| # GET /profiles or /profiles.json | ||
| def index | ||
| render json: Profile.all, status: :ok | ||
| profiles = Profile.all | ||
| render json: profiles, status: :ok | ||
| end | ||
|
|
||
| # GET /profiles/:id or /profiles/:id.json | ||
|
|
@@ -37,16 +39,45 @@ def update | |
| end | ||
| end | ||
|
|
||
| private | ||
| # POST /profiles/:id/add_tag or /profiles/:id/add_tag.json | ||
| def add_tag | ||
| tag = Tag.find_or_create_by(params[:tag]) | ||
| if tag_params.blank? | ||
| render json: { error: 'There was no tag data passed in so your profile could not be saved.' }, | ||
| status: :unprocessable_entity | ||
| else | ||
| if @profile.tags << tag | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/IfInsideElse: Convert if nested inside else to elsif. |
||
| render json: @profile, status: :created | ||
| else | ||
| render json: @profile.errors, status: :unprocessable_entity | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def set_profile | ||
| @profile = Profile.find_by(user_id: params[:user_id]) | ||
| end | ||
|
|
||
| # DELETE /profiles/:profile_id/remove_tag/:id or /profiles/:profile_id/remove_tag/:id.json | ||
| def remove_tag | ||
| @profile.tags.destroy(@tag) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I worry this removes the tag completely from db but it should just unassigne tag from profile/post ... |
||
| head 204 | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def set_tag | ||
| @tag = Tag.find_by(name: params[:name]) | ||
| end | ||
|
|
||
| def profile_params | ||
| params.permit(:surname, :name, :patronymic, :birthday, :phone, :about, :user_id) | ||
| end | ||
|
|
||
| def tag_params | ||
| params.permit(:name) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not clear which name is that.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| end | ||
|
|
||
| def authorize_profile! | ||
| authorize(@profile || Profile) | ||
| end | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,6 @@ | |
| # | ||
| FactoryBot.define do | ||
| factory :tag do | ||
| name { Faker::Hobby.unique.activity } | ||
| name { Faker::Hobby.unique.activity.downcase.gsub(' ','_') } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Performance/StringReplacement: Use tr instead of gsub. |
||
| end | ||
| end | ||
Uh oh!
There was an error while loading. Please reload this page.