46 prevent from duplication when adding new game #47
Merged@github-juk3n wants to merge
46-prevent-from-duplication-when-adding-new-game into mainMerged as
1fbb4dceb7ab.2 files changed
modified
app/include/fluffyApplication.hpp +1 -1 | @@ -36,7 +36,7 @@ fluffy commands:\n\ | ||
| 36 | 36 | show Show all games added to library\n\ |
| 37 | 37 | rm <game_name> Remove game from a library"}; |
| 38 | 38 | |
| 39 | - auto addGame(Database& database, std::string name, std::string path) -> void; | |
| 39 | + auto addGame(std::string name, std::string path) -> void; | |
| 40 | 40 | |
| 41 | 41 | auto removeGame(Database& database, std::string name) -> void; |
| 42 | 42 | |
| @@ -36,7 +36,7 @@ fluffy commands:\n\ | |||
| 36 | show Show all games added to library\n\ | 36 | show Show all games added to library\n\ |
| 37 | rm <game_name> Remove game from a library"}; | 37 | rm <game_name> Remove game from a library"}; |
| 38 | 38 | ||
| 39 | - auto addGame(Database& database, std::string name, std::string path) -> void; | 39 | + auto addGame(std::string name, std::string path) -> void; |
| 40 | 40 | ||
| 41 | auto removeGame(Database& database, std::string name) -> void; | 41 | auto removeGame(Database& database, std::string name) -> void; |
| 42 | 42 | ||
modified
app/src/fluffyApplication.cpp +25 -14 | @@ -31,20 +31,9 @@ auto FluffyApplication::handleCommand( | ||
| 31 | 31 | gameRepository->deleteGame(gameName); |
| 32 | 32 | } |
| 33 | 33 | else if (command == "add") { |
| 34 | - std::string gameName{arguments[0]}; | |
| 35 | - auto localPath = std::filesystem::path(arguments[1]); | |
| 36 | - std::string globalPath = | |
| 37 | - std::filesystem::absolute(localPath).lexically_normal().string(); | |
| 38 | - std::string temp{}; | |
| 39 | - for (const auto a : globalPath) { | |
| 40 | - if (a != ' ') { | |
| 41 | - temp += a; | |
| 42 | - } else { | |
| 43 | - temp += "\' \'"; | |
| 44 | - } | |
| 45 | - } | |
| 46 | - | |
| 47 | - gameRepository->addGame(gameName, temp); | |
| 34 | + std::string gameName = arguments[0]; | |
| 35 | + std::string gamePath = arguments[1]; | |
| 36 | + this->addGame(gameName, gamePath); | |
| 48 | 37 | } |
| 49 | 38 | else if (command == "play") { |
| 50 | 39 | std::string gameName{arguments[0]}; |
| @@ -61,6 +50,28 @@ auto FluffyApplication::handleCommand( | ||
| 61 | 50 | } |
| 62 | 51 | } |
| 63 | 52 | |
| 53 | +auto FluffyApplication::addGame(std::string name, std::string path) -> void { | |
| 54 | + std::string gameName{name}; | |
| 55 | + auto localPath = std::filesystem::path(path); | |
| 56 | + std::string globalPath = | |
| 57 | + std::filesystem::absolute(localPath).lexically_normal().string(); | |
| 58 | + std::string temp{}; | |
| 59 | + for (const auto a : globalPath) { | |
| 60 | + if (a != ' ') { | |
| 61 | + temp += a; | |
| 62 | + } else { | |
| 63 | + temp += "\' \'"; | |
| 64 | + } | |
| 65 | + } | |
| 66 | + Game currentGame = gameRepository->getGameByName(gameName); | |
| 67 | + if (currentGame.getName().length() != 0) { | |
| 68 | + output->printMessage("Game already exist"); | |
| 69 | + } | |
| 70 | + else { | |
| 71 | + gameRepository->addGame(gameName, temp); | |
| 72 | + } | |
| 73 | + | |
| 74 | +} | |
| 64 | 75 | |
| 65 | 76 | auto FluffyApplication::handleFlags(std::vector<std::string> flags) -> void { |
| 66 | 77 | for (const auto& flag : flags) { |
| @@ -31,20 +31,9 @@ auto FluffyApplication::handleCommand( | |||
| 31 | gameRepository->deleteGame(gameName); | 31 | gameRepository->deleteGame(gameName); |
| 32 | } | 32 | } |
| 33 | else if (command == "add") { | 33 | else if (command == "add") { |
| 34 | - std::string gameName{arguments[0]}; | 34 | + std::string gameName = arguments[0]; |
| 35 | - auto localPath = std::filesystem::path(arguments[1]); | 35 | + std::string gamePath = arguments[1]; |
| 36 | - std::string globalPath = | 36 | + this->addGame(gameName, gamePath); |
| 37 | - std::filesystem::absolute(localPath).lexically_normal().string(); | ||
| 38 | - std::string temp{}; | ||
| 39 | - for (const auto a : globalPath) { | ||
| 40 | - if (a != ' ') { | ||
| 41 | - temp += a; | ||
| 42 | - } else { | ||
| 43 | - temp += "\' \'"; | ||
| 44 | - } | ||
| 45 | - } | ||
| 46 | - | ||
| 47 | - gameRepository->addGame(gameName, temp); | ||
| 48 | } | 37 | } |
| 49 | else if (command == "play") { | 38 | else if (command == "play") { |
| 50 | std::string gameName{arguments[0]}; | 39 | std::string gameName{arguments[0]}; |
| @@ -61,6 +50,28 @@ auto FluffyApplication::handleCommand( | |||
| 61 | } | 50 | } |
| 62 | } | 51 | } |
| 63 | 52 | ||
| 53 | +auto FluffyApplication::addGame(std::string name, std::string path) -> void { | ||
| 54 | + std::string gameName{name}; | ||
| 55 | + auto localPath = std::filesystem::path(path); | ||
| 56 | + std::string globalPath = | ||
| 57 | + std::filesystem::absolute(localPath).lexically_normal().string(); | ||
| 58 | + std::string temp{}; | ||
| 59 | + for (const auto a : globalPath) { | ||
| 60 | + if (a != ' ') { | ||
| 61 | + temp += a; | ||
| 62 | + } else { | ||
| 63 | + temp += "\' \'"; | ||
| 64 | + } | ||
| 65 | + } | ||
| 66 | + Game currentGame = gameRepository->getGameByName(gameName); | ||
| 67 | + if (currentGame.getName().length() != 0) { | ||
| 68 | + output->printMessage("Game already exist"); | ||
| 69 | + } | ||
| 70 | + else { | ||
| 71 | + gameRepository->addGame(gameName, temp); | ||
| 72 | + } | ||
| 73 | + | ||
| 74 | +} | ||
| 64 | 75 | ||
| 65 | auto FluffyApplication::handleFlags(std::vector<std::string> flags) -> void { | 76 | auto FluffyApplication::handleFlags(std::vector<std::string> flags) -> void { |
| 66 | for (const auto& flag : flags) { | 77 | for (const auto& flag : flags) { |