Skip to content

Commit

Permalink
introduced app interactors and switched controllers to use
Browse files Browse the repository at this point in the history
  • Loading branch information
suprjinx committed Aug 27, 2024
1 parent c3f823b commit 1f316c3
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 5 deletions.
9 changes: 6 additions & 3 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ def info
end

def authenticate_request
token = request.headers["Authorization"]
token = token.split(" ").last if token
@identity = Services::AuthService.new.authenticate!(token)
result = AuthenticateIdentity.call(request: request)
if result.success?
@identity = result.identity
else
raise AuthError
end
end

private
Expand Down
7 changes: 5 additions & 2 deletions app/controllers/certificates_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ def create
req = CertIssueRequest.new(params_permitted)
if !req.valid?
render json: { error: req.errors }, status: :bad_request
end
result = IssueCert.call(request: req)
if result.success?
render json: result.cert
else
cert = Services::CertificateService.new.issue_cert(req)
render json: cert
raise StandardError.new result.message
end
end

Expand Down
16 changes: 16 additions & 0 deletions app/interactors/authenticate_identity.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class AuthenticateIdentity
include Interactor

before do
token = context.request.headers["Authorization"]
context.token = token.split(" ").last if token
end

def call
if identity = Services::AuthService.new.authenticate!(context.token)
context.identity = identity
else
context.fail!
end
end
end
6 changes: 6 additions & 0 deletions app/interactors/check_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class CheckPolicy
include Interactor

def call
end
end
5 changes: 5 additions & 0 deletions app/interactors/issue_cert.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class IssueCert
include Interactor::Organizer

organize CheckPolicy, ObtainCert
end
7 changes: 7 additions & 0 deletions app/interactors/obtain_cert.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class ObtainCert
include Interactor

def call
context.cert = Services::CertificateService.new.issue_cert(context.request)
end
end

0 comments on commit 1f316c3

Please sign in to comment.