Skip to content

Commit

Permalink
Fix bug where controller would keep going after rendering an error, a…
Browse files Browse the repository at this point in the history
…lso add new error detection for db param
  • Loading branch information
n8ta committed Dec 3, 2023
1 parent f33ab89 commit 869b10a
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions app/controllers/mofs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ def count
# Finding the count of MOFs is the slowest part of the search
# to speedup the frontend we decouple this from main search query
# into a separate request to /mofs/count?normal_query_params
@mofs = get_mofs
success, @mofs = get_mofs
unless success
return render status: 400, :json => @mofs
end
@status = "success"
@error_message = ""
begin
Expand All @@ -38,7 +41,10 @@ def homepage
# GET /mofs
# GET /mofs.json
def index
@mofs = get_mofs
success, @mofs = get_mofs
unless success
return render status: 400, :json => @mofs
end

bulk = params[:bulk] && params[:bulk] == "true"
cifs = params[:cifs] && params[:cifs] == "true"
Expand Down Expand Up @@ -166,6 +172,8 @@ def version

private

# Returns Tuple(bool, List<mof>)
# If success bool is false an error has already been rendered and caller
def get_mofs
mofs = Mof.all.distinct.visible
## GASES
Expand All @@ -175,12 +183,13 @@ def get_mofs
gases.each do |gas_name|
gas = Gas.find_gas(gas_name)
if gas.nil?
return render status: 400, json: { status: RESULTS[:error], error: "Gas '#{gas_name}' not found" }.to_json
return false, { status: RESULTS[:error], error: "Gas '#{gas_name}' not found" }.to_json
end
gas_ids << gas.id
end
gas_ids = gases.map { |gas_name| Gas.find_gas(gas_name).id }.uniq
mofs = mofs.joins(:gases).where("gases_mofs.gas_id in (?)", gas_ids)
return true, mofs
end

## Elements in MOF
Expand Down Expand Up @@ -243,6 +252,9 @@ def get_mofs
# DB
if params[:database] && params[:database] != "Any" && !params[:database].empty?
database = Database.find_by(name: params[:database])
if database.nil?
return false, { status: RESULTS[:error], error: "Database '#{params[:database]}' not found" }.to_json
end
mofs = mofs.where(database: database)
end

Expand Down

0 comments on commit 869b10a

Please sign in to comment.