Skip to content
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

Make onyx multi-query and updates test implementations compliant #4845

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions frameworks/Crystal/onyx/src/endpoints/worlds/many.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ struct Endpoints::Worlds::Many
include Onyx::HTTP::Endpoint
include RandomID

QUERY = Models::World.where("id = ANY(?)", 0).build(true)[0]
QUERY = Models::World.where(id: 0).build(true)[0]

params do
query do
Expand All @@ -12,13 +12,14 @@ struct Endpoints::Worlds::Many

def call
queries = params.query.queries.is_a?(String) ? 1 : params.query.queries.as(Int32)
ids = queries.clamp(1..500).times.map do
random_id
end.join(',').try do |s|
"{#{s}}"

worlds = Array(Models::World).new

queries.clamp(1..500).times.each do
world = Onyx.query(Models::World, QUERY, random_id).first
worlds << world
end

worlds = Onyx.query(Models::World, QUERY, ids)
return Views::Worlds.new(worlds)
end
end
11 changes: 5 additions & 6 deletions frameworks/Crystal/onyx/src/endpoints/worlds/update.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ struct Endpoints::Worlds::Update
include Onyx::HTTP::Endpoint
include RandomID

QUERY = Models::World.update.set(random_number: 0).where(id: 0).build[0]
SELECT_QUERY = Models::World.where(id: 0).build(true)[0]
UPDATE_QUERY = Models::World.update.set(random_number: 0).where(id: 0).build[0]

params do
query do
Expand All @@ -16,12 +17,10 @@ struct Endpoints::Worlds::Update
worlds = Array(Models::World).new

queries.clamp(1..500).times.each do
id, number = {random_id, random_id}

world = Models::World.new(id: id, random_number: number)
world = Onyx.query(Models::World, SELECT_QUERY, random_id).first
world.random_number = random_id
Onyx.exec(UPDATE_QUERY, world.random_number, world.id)
worlds << world

Onyx.exec(QUERY, world.random_number, world.id)
end

return Views::Worlds.new(worlds)
Expand Down