diff --git a/Application/ApplicationApi.h b/Application/ApplicationApi.h index 87f19bb..bcd33cc 100644 --- a/Application/ApplicationApi.h +++ b/Application/ApplicationApi.h @@ -6,6 +6,12 @@ #define APPLICATION_API __declspec(dllimport) #endif +#ifndef PKEY +#define PKEY "Id" +#endif // !PKEY + +#define ASYNC + #define EQ(x) _EQ(x, x) #define NEQ(x) _NEQ(x, x) #define GT(x) _GT(x, x) diff --git a/Application/ApplicationUserServices.cpp b/Application/ApplicationUserServices.cpp index da69c88..691713d 100644 --- a/Application/ApplicationUserServices.cpp +++ b/Application/ApplicationUserServices.cpp @@ -34,16 +34,16 @@ std::string ApplicationUserService::CreateApplicationUser(ApplicationUser* appli return id; } -int ApplicationUserService::UpdateApplicationUser(ApplicationUser* applicationUser, const std::string& id) noexcept(false) +int ApplicationUserService::UpdateApplicationUser(ApplicationUser* applicationUser, const std::string& Id) noexcept(false) { ApplicationUserCommand cmd; - return cmd.Update(applicationUser, id); + return cmd.Update(applicationUser, EQ(Id)); } -int ApplicationUserService::DeleteApplicationUser(const std::string& id) +int ApplicationUserService::DeleteApplicationUser(const std::string& Id) { ApplicationUserCommand cmd; - return cmd.Delete(id); + return cmd.Delete(EQ(Id)); } ApplicationUserService::~ApplicationUserService() diff --git a/Application/BaseCommand.h b/Application/BaseCommand.h index ee0cd09..3b161ab 100644 --- a/Application/BaseCommand.h +++ b/Application/BaseCommand.h @@ -9,9 +9,6 @@ #include "CoreHelper.h" #include "TypeCheck.h" -#ifndef PKEY -#define PKEY "Id" -#endif // !PKEY template > class APPLICATION_API BaseCommand : public IBaseCommand @@ -124,16 +121,16 @@ class APPLICATION_API BaseCommand : public IBaseCommand return 0; } - virtual int Update(T* item, const std::string& id) noexcept(false) override + virtual int Update(T* item, const std::string& query) noexcept(false) override { auto con = db->GetConnection(); try { std::string table_name = typeid(T).name(); table_name = table_name.substr(table_name.find_last_of(' ') + 1); - std::string query = "UPDATE [dbo].[" + table_name + "] SET "; + std::string command = "UPDATE [dbo].[" + table_name + "] SET "; SACommand cmd(con); - cmd.setCommandText(_TSA(query.c_str())); + cmd.setCommandText(_TSA(command.c_str())); std::vector fields; boost::mp11::mp_for_each([&](auto D) { @@ -142,17 +139,17 @@ class APPLICATION_API BaseCommand : public IBaseCommand { if (field == "ModifiedDate") { - query += field + " = GETDATE(), "; + command += field + " = GETDATE(), "; } else { - query += field + " = :" + field + ", "; + command += field + " = :" + field + ", "; fields.push_back(field); } } }); - query = query.substr(0, query.length() - 2) + " WHERE Id = :id"; - cmd.setCommandText(_TSA(query.c_str())); + command = command.substr(0, command.length() - 2) + " WHERE " + query; + cmd.setCommandText(_TSA(command.c_str())); boost::mp11::mp_for_each([&](auto D) { for (auto& f : fields) @@ -192,7 +189,6 @@ class APPLICATION_API BaseCommand : public IBaseCommand } } }); - cmd.Param(_TSA("id")).setAsString() = id.c_str(); cmd.Execute(); return (int)cmd.RowsAffected(); } @@ -211,21 +207,17 @@ class APPLICATION_API BaseCommand : public IBaseCommand return 0; } - virtual int Delete(const std::string& id) noexcept(false) override + virtual int Delete(const std::string& query) noexcept(false) override { auto con = db->GetConnection(); try { - if (id.empty()) - { - throw std::exception("Internal error! Id is empty"); - } + std::string table_name = typeid(T).name(); table_name = table_name.substr(table_name.find_last_of(' ') + 1); - std::string query = "DELETE FROM [dbo].[" + table_name + "] WHERE Id = :id"; + std::string command = "DELETE FROM [dbo].[" + table_name + "] WHERE " + query; SACommand cmd(con); - cmd.setCommandText(_TSA(query.c_str())); - cmd.Param(_TSA("id")).setAsString() = id.c_str(); + cmd.setCommandText(_TSA(command.c_str())); cmd.Execute(); return (int)cmd.RowsAffected(); } diff --git a/Application/BaseQuery.h b/Application/BaseQuery.h index 12848b0..0e23838 100644 --- a/Application/BaseQuery.h +++ b/Application/BaseQuery.h @@ -16,12 +16,6 @@ #include #include -#define ASYNC - -#ifndef PKEY -#define PKEY "Id" -#endif // !PKEY - //TODO: Apply concepts template ::type> class APPLICATION_API BaseQuery @@ -46,7 +40,6 @@ class APPLICATION_API BaseQuery { throw std::exception("Id is empty"); } - std::string table_name = typeid(K).name(); table_name = table_name.substr(table_name.find_last_of(' ') + 1); std::string query = "SELECT * FROM [dbo].[" + table_name + "] WHERE " PKEY " = :id"; @@ -76,7 +69,6 @@ class APPLICATION_API BaseQuery auto inner_items = inner_items_future.get(); #else auto inner_items = GetAll(table_name + "Id = '" + std::string(cmd.Field(PKEY).asString().GetMultiByteChars()) + "'", includes); - #endif // ASYNC (item.get()->*(D).pointer) = std::any_cast(inner_items); } @@ -99,9 +91,7 @@ class APPLICATION_API BaseQuery (item.get()->*(D).pointer) = std::any_cast(inner_item); } } - } - }); } return item; @@ -364,7 +354,6 @@ class APPLICATION_API BaseQuery base_query = "SELECT * FROM ( SELECT ROW_NUMBER() OVER (ORDER BY CreatedDate) AS RowNum, * FROM [dbo].[" + table_name + "]"; - if (query.length() > 1) { base_query += " WHERE " + query; diff --git a/Application/TodoItemService.cpp b/Application/TodoItemService.cpp index e432bfd..266832e 100644 --- a/Application/TodoItemService.cpp +++ b/Application/TodoItemService.cpp @@ -29,16 +29,16 @@ std::string TodoItemService::CreateTodoItem(TodoItem* TodoItem) noexcept(false) return TodoItem->GetId(); } -int TodoItemService::UpdateTodoItem(TodoItem* TodoItem, const std::string& id) noexcept(false) +int TodoItemService::UpdateTodoItem(TodoItem* TodoItem, const std::string& Id) noexcept(false) { TodoItemCommand cmd; - return cmd.Update(TodoItem, id); + return cmd.Update(TodoItem, EQ(Id)); } -int TodoItemService::DeleteTodoItem(const std::string& id) +int TodoItemService::DeleteTodoItem(const std::string& Id) { TodoItemCommand cmd; - return cmd.Delete(id); + return cmd.Delete(EQ(Id)); } TodoItemService::~TodoItemService() diff --git a/Application/TodoListService.cpp b/Application/TodoListService.cpp index 4fead71..6927235 100644 --- a/Application/TodoListService.cpp +++ b/Application/TodoListService.cpp @@ -29,17 +29,17 @@ std::string TodoListService::CreateTodoList(TodoList* todo_list) noexcept(false) return todo_list->GetId(); } -int TodoListService::UpdateTodoList(TodoList* todo_list, const std::string& id) noexcept(false) +int TodoListService::UpdateTodoList(TodoList* todo_list, const std::string& Id) noexcept(false) { TodoListCommand cmd; - return cmd.Update(todo_list, id); + return cmd.Update(todo_list, EQ(Id)); } -int TodoListService::DeleteTodoList(const std::string& id) +int TodoListService::DeleteTodoList(const std::string& Id) { TodoListCommand cmd; - return cmd.Delete(id); + return cmd.Delete(EQ(Id)); } TodoListService::~TodoListService()