Skip to content

Commit

Permalink
✨ create Users::FindByUsername operation
Browse files Browse the repository at this point in the history
  • Loading branch information
karinevieira committed Feb 21, 2024
1 parent 404f215 commit 161656a
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
4 changes: 2 additions & 2 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ class UsersController < ApplicationController
skip_before_action :authenticate_user!, only: %i[show]

def show
user = UserProfile.find_by(username: params[:username]).user
result = Users::FindByUsername.result(username: params[:username])

render Users::ShowPage.new(user: user)
render Users::ShowPage.new(user: result.user)
end
end
17 changes: 17 additions & 0 deletions app/operations/users/find_by_username.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

module Users
class FindByUsername < Actor
input :username, type: String

output :user, type: User

def call
profile = UserProfile.find_by(username: username)

fail!(error: :not_found) if profile.blank?

self.user = profile.user
end
end
end
39 changes: 39 additions & 0 deletions spec/operations/users/find_by_username_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe Users::FindByUsername, type: :operation do
context "when user with given username exists" do
it "is a success" do
user = create(:user)
create(:user_profile, username: "johndoe", user: user)

result = described_class.result(username: "johndoe")

expect(result.success?).to be true
end

it "returns found user for given username" do
user = create(:user)
create(:user_profile, username: "johndoe", user: user)

result = described_class.result(username: "johndoe")

expect(result.user).to eq(user)
end
end

context "when user with given username doesn't exist" do
it "is a failure" do
result = described_class.result(username: "not-found-username")

expect(result.failure?).to be true
end

it "returns not found error" do
result = described_class.result(username: "not-found-username")

expect(result.error).to be :not_found
end
end
end

0 comments on commit 161656a

Please sign in to comment.