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

[refactor] DAO improvements #346

Merged
merged 17 commits into from
Jun 23, 2015
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
2 changes: 1 addition & 1 deletion .luacheckrc
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ files["kong/vendor/resty_http.lua"] = {
}

files["spec/"] = {
globals = {"describe", "it", "before_each", "setup", "after_each", "teardown", "stub", "mock", "spy", "finally"}
globals = {"describe", "it", "before_each", "setup", "after_each", "teardown", "stub", "mock", "spy", "finally", "pending"}
}
32 changes: 19 additions & 13 deletions kong/api/crud_helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ function _M.find_api_by_name_or_id(self, dao_factory, helpers)
}
self.params.name_or_id = nil

-- TODO: make the base_dao more flexible so we can query find_one with key/values
-- https://github.com/Mashape/kong/issues/103
local data, err = dao_factory.apis:find_by_keys(fetch_keys)
if err then
return helpers.yield_error(err)
Expand Down Expand Up @@ -74,13 +72,17 @@ function _M.paginated_set(self, dao_collection)
end

function _M.put(params, dao_collection)
local new_entity, err
if params.id then
new_entity, err = dao_collection:update(params)
if not err and new_entity then
local res, new_entity, err

res, err = dao_collection:find_by_primary_key(params)
if err then
return app_helpers.yield_error(err)
end

if res then
new_entity, err = dao_collection:update(params, true)
if not err then
return responses.send_HTTP_OK(new_entity)
elseif not new_entity then
return responses.send_HTTP_NOT_FOUND()
end
else
new_entity, err = dao_collection:insert(params)
Expand All @@ -104,17 +106,21 @@ function _M.post(params, dao_collection, success)
end
end

function _M.patch(params, dao_collection)
local new_entity, err = dao_collection:update(params)
function _M.patch(new_entity, old_entity, dao_collection)
for k, v in pairs(new_entity) do
old_entity[k] = v
end

local updated_entity, err = dao_collection:update(old_entity)
if err then
return app_helpers.yield_error(err)
else
return responses.send_HTTP_OK(new_entity)
return responses.send_HTTP_OK(updated_entity)
end
end

function _M.delete(entity_id, dao_collection)
local ok, err = dao_collection:delete(entity_id)
function _M.delete(where_t, dao_collection)
local ok, err = dao_collection:delete(where_t)
if not ok then
if err then
return app_helpers.yield_error(err)
Expand Down
10 changes: 4 additions & 6 deletions kong/api/routes/apis.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@ return {
end,

PATCH = function(self, dao_factory)
self.params.id = self.api.id
crud.patch(self.params, dao_factory.apis)
crud.patch(self.params, self.api, dao_factory.apis)
end,

DELETE = function(self, dao_factory)
crud.delete(self.api.id, dao_factory.apis)
crud.delete(self.api, dao_factory.apis)
end
},

Expand Down Expand Up @@ -79,12 +78,11 @@ return {
end,

PATCH = function(self, dao_factory, helpers)
self.params.id = self.plugin.id
crud.patch(self.params, dao_factory.plugins_configurations)
crud.patch(self.params, self.plugin, dao_factory.plugins_configurations)
end,

DELETE = function(self, dao_factory)
crud.delete(self.plugin.id, dao_factory.plugins_configurations)
crud.delete(self.plugin, dao_factory.plugins_configurations)
end
}
}
5 changes: 2 additions & 3 deletions kong/api/routes/consumers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@ return {
end,

PATCH = function(self, dao_factory, helpers)
self.params.id = self.consumer.id
crud.patch(self.params, dao_factory.consumers)
crud.patch(self.params, self.consumer, dao_factory.consumers)
end,

DELETE = function(self, dao_factory, helpers)
crud.delete(self.consumer.id, dao_factory.consumers)
crud.delete(self.consumer, dao_factory.consumers)
end
}
}
7 changes: 3 additions & 4 deletions kong/api/routes/plugins_configurations.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ return {
["/plugins_configurations/:id"] = {
before = function(self, dao_factory, helpers)
local err
self.plugin_conf, err = dao_factory.plugins_configurations:find_one(self.params.id)
self.plugin_conf, err = dao_factory.plugins_configurations:find_by_primary_key({ id = self.params.id })
if err then
return helpers.yield_error(err)
elseif not self.plugin_conf then
Expand All @@ -38,12 +38,11 @@ return {
end,

PATCH = function(self, dao_factory)
self.params.id = self.plugin_conf.id
crud.patch(self.params, dao_factory.plugins_configurations)
crud.patch(self.params, self.plugin_conf, dao_factory.plugins_configurations)
end,

DELETE = function(self, dao_factory)
crud.delete(self.plugin_conf.id, dao_factory.plugins_configurations)
crud.delete(self.plugin_conf, dao_factory.plugins_configurations)
end
}
}
4 changes: 0 additions & 4 deletions kong/constants.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ return {
UNIQUE = "unique",
FOREIGN = "foreign"
},
DATABASE_TYPES = {
ID = "id",
TIMESTAMP = "timestamp"
},
-- Non standard headers, specific to Kong
HEADERS = {
HOST_OVERRIDE = "X-Host-Override",
Expand Down
59 changes: 9 additions & 50 deletions kong/dao/cassandra/apis.lua
Original file line number Diff line number Diff line change
@@ -1,55 +1,19 @@
local BaseDao = require "kong.dao.cassandra.base_dao"
local apis_schema = require "kong.dao.schemas.apis"
local query_builder = require "kong.dao.cassandra.query_builder"

local Apis = BaseDao:extend()

function Apis:new(properties)
self._entity = "API"
self._table = "apis"
self._schema = apis_schema
self._queries = {
insert = {
args_keys = { "id", "name", "public_dns", "path", "strip_path", "target_url", "created_at" },
query = [[ INSERT INTO apis(id, name, public_dns, path, strip_path, target_url, created_at)
VALUES(?, ?, ?, ?, ?, ?, ?); ]]
},
update = {
args_keys = { "name", "public_dns", "path", "strip_path", "target_url", "id" },
query = [[ UPDATE apis SET name = ?, public_dns = ?, path = ?, strip_path = ?, target_url = ? WHERE id = ?; ]]
},
select = {
query = [[ SELECT * FROM apis %s; ]]
},
select_one = {
args_keys = { "id" },
query = [[ SELECT * FROM apis WHERE id = ?; ]]
},
delete = {
args_keys = { "id" },
query = [[ DELETE FROM apis WHERE id = ?; ]]
},
__unique = {
name = {
args_keys = { "name" },
query = [[ SELECT id FROM apis WHERE name = ?; ]]
},
path = {
args_keys = { "path" },
query = [[ SELECT id FROM apis WHERE path = ?; ]]
},
public_dns = {
args_keys = { "public_dns" },
query = [[ SELECT id FROM apis WHERE public_dns = ?; ]]
}
},
drop = "TRUNCATE apis;"
}

Apis.super.new(self, properties)
end

function Apis:find_all()
local apis = {}
for _, rows, page, err in Apis.super._execute_kong_query(self, self._queries.select.query, nil, {auto_paging=true}) do
local select_q = query_builder.select(self._table)
for _, rows, page, err in Apis.super.execute(self, select_q, nil, nil, {auto_paging=true}) do
if err then
return nil, err
end
Expand All @@ -63,28 +27,23 @@ function Apis:find_all()
end

-- @override
function Apis:delete(api_id)
local ok, err = Apis.super.delete(self, api_id)
function Apis:delete(where_t)
local ok, err = Apis.super.delete(self, where_t)
if not ok then
return false, err
end

-- delete all related plugins configurations
local plugins_dao = self._factory.plugins_configurations
local query, args_keys, errors = plugins_dao:_build_where_query(plugins_dao._queries.select.query, {
api_id = api_id
})
if errors then
return nil, errors
end
local select_q, columns = query_builder.select(plugins_dao._table, {api_id = where_t.id}, plugins_dao._column_family_details)

for _, rows, page, err in plugins_dao:_execute_kong_query({query=query, args_keys=args_keys}, {api_id=api_id}, {auto_paging=true}) do
for _, rows, page, err in plugins_dao:execute(select_q, columns, {api_id = where_t.id}, {auto_paging = true}) do
if err then
return nil, err
end

for _, row in ipairs(rows) do
local ok_del_plugin, err = plugins_dao:delete(row.id)
local ok_del_plugin, err = plugins_dao:delete({id = row.id})
if not ok_del_plugin then
return nil, err
end
Expand Down
Loading