50 pass output interface to commands (#51)Unverified
* added output to add command * added output to play and show commands --------- Co-authored-by: Adrian Duraj <adrian.duraj@onmail.com>
6c77d0e parent: bed72a2 modified
app/include/commands/addCommand.hpp +7 -2 | @@ -5,19 +5,23 @@ | ||
| 5 | 5 | |
| 6 | 6 | #include <command.hpp> |
| 7 | 7 | #include <gameRepository.hpp> |
| 8 | +#include <output.hpp> | |
| 8 | 9 | |
| 9 | 10 | class AddCommand: public Command { |
| 10 | 11 | private: |
| 11 | 12 | std::weak_ptr<GameRepositoryInterface> gameRepository; |
| 13 | + std::weak_ptr<Output> output; | |
| 12 | 14 | std::string gameName; |
| 13 | 15 | std::string path; |
| 14 | 16 | public: |
| 15 | 17 | AddCommand( |
| 16 | 18 | std::weak_ptr<GameRepositoryInterface> gameRepository, |
| 19 | + std::weak_ptr<Output> output, | |
| 17 | 20 | std::string gameName, |
| 18 | 21 | std::string path |
| 19 | 22 | ) { |
| 20 | 23 | this->gameRepository = gameRepository; |
| 24 | + this->output = output; | |
| 21 | 25 | this->gameName = gameName; |
| 22 | 26 | this->path = path; |
| 23 | 27 | } |
| @@ -37,8 +41,9 @@ class AddCommand: public Command { | ||
| 37 | 41 | if (std::shared_ptr<GameRepositoryInterface> gr = gameRepository.lock()) { |
| 38 | 42 | Game currentGame = gr->getGameByName(gameName); |
| 39 | 43 | if (currentGame.getName().length() != 0) { |
| 40 | - std::cout << "Game already exist" << std::endl; | |
| 41 | - // output->printMessage("Game already exist"); | |
| 44 | + if (auto out = output.lock()) { | |
| 45 | + out->printMessage("Game already exist"); | |
| 46 | + } | |
| 42 | 47 | } |
| 43 | 48 | else { |
| 44 | 49 | gr->addGame(gameName, temp); |
| @@ -5,19 +5,23 @@ | |||
| 5 | 5 | ||
| 6 | #include <command.hpp> | 6 | #include <command.hpp> |
| 7 | #include <gameRepository.hpp> | 7 | #include <gameRepository.hpp> |
| 8 | +#include <output.hpp> | ||
| 8 | 9 | ||
| 9 | class AddCommand: public Command { | 10 | class AddCommand: public Command { |
| 10 | private: | 11 | private: |
| 11 | std::weak_ptr<GameRepositoryInterface> gameRepository; | 12 | std::weak_ptr<GameRepositoryInterface> gameRepository; |
| 13 | + std::weak_ptr<Output> output; | ||
| 12 | std::string gameName; | 14 | std::string gameName; |
| 13 | std::string path; | 15 | std::string path; |
| 14 | public: | 16 | public: |
| 15 | AddCommand( | 17 | AddCommand( |
| 16 | std::weak_ptr<GameRepositoryInterface> gameRepository, | 18 | std::weak_ptr<GameRepositoryInterface> gameRepository, |
| 19 | + std::weak_ptr<Output> output, | ||
| 17 | std::string gameName, | 20 | std::string gameName, |
| 18 | std::string path | 21 | std::string path |
| 19 | ) { | 22 | ) { |
| 20 | this->gameRepository = gameRepository; | 23 | this->gameRepository = gameRepository; |
| 24 | + this->output = output; | ||
| 21 | this->gameName = gameName; | 25 | this->gameName = gameName; |
| 22 | this->path = path; | 26 | this->path = path; |
| 23 | } | 27 | } |
| @@ -37,8 +41,9 @@ class AddCommand: public Command { | |||
| 37 | if (std::shared_ptr<GameRepositoryInterface> gr = gameRepository.lock()) { | 41 | if (std::shared_ptr<GameRepositoryInterface> gr = gameRepository.lock()) { |
| 38 | Game currentGame = gr->getGameByName(gameName); | 42 | Game currentGame = gr->getGameByName(gameName); |
| 39 | if (currentGame.getName().length() != 0) { | 43 | if (currentGame.getName().length() != 0) { |
| 40 | - std::cout << "Game already exist" << std::endl; | 44 | + if (auto out = output.lock()) { |
| 41 | - // output->printMessage("Game already exist"); | 45 | + out->printMessage("Game already exist"); |
| 46 | + } | ||
| 42 | } | 47 | } |
| 43 | else { | 48 | else { |
| 44 | gr->addGame(gameName, temp); | 49 | gr->addGame(gameName, temp); |
modified
app/include/commands/playCommand.hpp +7 -1 | @@ -4,18 +4,22 @@ | ||
| 4 | 4 | |
| 5 | 5 | #include <command.hpp> |
| 6 | 6 | #include <gameRepository.hpp> |
| 7 | +#include <output.hpp> | |
| 7 | 8 | |
| 8 | 9 | class PlayCommand: public Command { |
| 9 | 10 | private: |
| 10 | 11 | std::weak_ptr<GameRepositoryInterface> gameRepository; |
| 12 | + std::weak_ptr<Output> output; | |
| 11 | 13 | std::string gameName; |
| 12 | 14 | |
| 13 | 15 | public: |
| 14 | 16 | PlayCommand( |
| 15 | 17 | std::weak_ptr<GameRepositoryInterface> gameRepository, |
| 18 | + std::weak_ptr<Output> output, | |
| 16 | 19 | std::string gameName |
| 17 | 20 | ) { |
| 18 | 21 | this->gameRepository = gameRepository; |
| 22 | + this->output = output; | |
| 19 | 23 | this->gameName = gameName; |
| 20 | 24 | } |
| 21 | 25 | |
| @@ -23,7 +27,9 @@ class PlayCommand: public Command { | ||
| 23 | 27 | if (std::shared_ptr<GameRepositoryInterface> gr = gameRepository.lock()) { |
| 24 | 28 | std::string pathToRun = gr->getGameByName(gameName).getPath(); |
| 25 | 29 | if(pathToRun.empty()) { |
| 26 | - std::cout << "no application found" << std::endl; | |
| 30 | + if (auto out = output.lock()) { | |
| 31 | + out->printMessage("No application found"); | |
| 32 | + } | |
| 27 | 33 | } |
| 28 | 34 | else { |
| 29 | 35 | system(pathToRun.c_str()); |
| @@ -4,18 +4,22 @@ | |||
| 4 | 4 | ||
| 5 | #include <command.hpp> | 5 | #include <command.hpp> |
| 6 | #include <gameRepository.hpp> | 6 | #include <gameRepository.hpp> |
| 7 | +#include <output.hpp> | ||
| 7 | 8 | ||
| 8 | class PlayCommand: public Command { | 9 | class PlayCommand: public Command { |
| 9 | private: | 10 | private: |
| 10 | std::weak_ptr<GameRepositoryInterface> gameRepository; | 11 | std::weak_ptr<GameRepositoryInterface> gameRepository; |
| 12 | + std::weak_ptr<Output> output; | ||
| 11 | std::string gameName; | 13 | std::string gameName; |
| 12 | 14 | ||
| 13 | public: | 15 | public: |
| 14 | PlayCommand( | 16 | PlayCommand( |
| 15 | std::weak_ptr<GameRepositoryInterface> gameRepository, | 17 | std::weak_ptr<GameRepositoryInterface> gameRepository, |
| 18 | + std::weak_ptr<Output> output, | ||
| 16 | std::string gameName | 19 | std::string gameName |
| 17 | ) { | 20 | ) { |
| 18 | this->gameRepository = gameRepository; | 21 | this->gameRepository = gameRepository; |
| 22 | + this->output = output; | ||
| 19 | this->gameName = gameName; | 23 | this->gameName = gameName; |
| 20 | } | 24 | } |
| 21 | 25 | ||
| @@ -23,7 +27,9 @@ class PlayCommand: public Command { | |||
| 23 | if (std::shared_ptr<GameRepositoryInterface> gr = gameRepository.lock()) { | 27 | if (std::shared_ptr<GameRepositoryInterface> gr = gameRepository.lock()) { |
| 24 | std::string pathToRun = gr->getGameByName(gameName).getPath(); | 28 | std::string pathToRun = gr->getGameByName(gameName).getPath(); |
| 25 | if(pathToRun.empty()) { | 29 | if(pathToRun.empty()) { |
| 26 | - std::cout << "no application found" << std::endl; | 30 | + if (auto out = output.lock()) { |
| 31 | + out->printMessage("No application found"); | ||
| 32 | + } | ||
| 27 | } | 33 | } |
| 28 | else { | 34 | else { |
| 29 | system(pathToRun.c_str()); | 35 | system(pathToRun.c_str()); |
modified
app/include/commands/showCommand.hpp +10 -3 | @@ -4,20 +4,27 @@ | ||
| 4 | 4 | #include <iostream> |
| 5 | 5 | |
| 6 | 6 | #include <command.hpp> |
| 7 | - | |
| 7 | +#include <output.hpp> | |
| 8 | 8 | |
| 9 | 9 | class ShowCommand: public Command { |
| 10 | 10 | private: |
| 11 | 11 | std::weak_ptr<GameRepositoryInterface> gameRepository; |
| 12 | + std::weak_ptr<Output> output; | |
| 12 | 13 | public: |
| 13 | - ShowCommand(std::weak_ptr<GameRepositoryInterface> gameRepository) { | |
| 14 | + ShowCommand( | |
| 15 | + std::weak_ptr<GameRepositoryInterface> gameRepository, | |
| 16 | + std::weak_ptr<Output> output | |
| 17 | + ) { | |
| 14 | 18 | this->gameRepository = gameRepository; |
| 19 | + this->output = output; | |
| 15 | 20 | } |
| 16 | 21 | |
| 17 | 22 | void execute() override { |
| 18 | 23 | if (std::shared_ptr<GameRepositoryInterface> repository = gameRepository.lock()) { |
| 19 | 24 | for (auto &game : repository->getGames()) { |
| 20 | - std::cout << game.getName() << ": " << game.getPath() << std::endl; | |
| 25 | + if (auto out = output.lock()) { | |
| 26 | + out->printMessage(game.getName() + ": " + game.getPath()); | |
| 27 | + } | |
| 21 | 28 | } |
| 22 | 29 | } |
| 23 | 30 | } |
| @@ -4,20 +4,27 @@ | |||
| 4 | #include <iostream> | 4 | #include <iostream> |
| 5 | 5 | ||
| 6 | #include <command.hpp> | 6 | #include <command.hpp> |
| 7 | - | 7 | +#include <output.hpp> |
| 8 | 8 | ||
| 9 | class ShowCommand: public Command { | 9 | class ShowCommand: public Command { |
| 10 | private: | 10 | private: |
| 11 | std::weak_ptr<GameRepositoryInterface> gameRepository; | 11 | std::weak_ptr<GameRepositoryInterface> gameRepository; |
| 12 | + std::weak_ptr<Output> output; | ||
| 12 | public: | 13 | public: |
| 13 | - ShowCommand(std::weak_ptr<GameRepositoryInterface> gameRepository) { | 14 | + ShowCommand( |
| 15 | + std::weak_ptr<GameRepositoryInterface> gameRepository, | ||
| 16 | + std::weak_ptr<Output> output | ||
| 17 | + ) { | ||
| 14 | this->gameRepository = gameRepository; | 18 | this->gameRepository = gameRepository; |
| 19 | + this->output = output; | ||
| 15 | } | 20 | } |
| 16 | 21 | ||
| 17 | void execute() override { | 22 | void execute() override { |
| 18 | if (std::shared_ptr<GameRepositoryInterface> repository = gameRepository.lock()) { | 23 | if (std::shared_ptr<GameRepositoryInterface> repository = gameRepository.lock()) { |
| 19 | for (auto &game : repository->getGames()) { | 24 | for (auto &game : repository->getGames()) { |
| 20 | - std::cout << game.getName() << ": " << game.getPath() << std::endl; | 25 | + if (auto out = output.lock()) { |
| 26 | + out->printMessage(game.getName() + ": " + game.getPath()); | ||
| 27 | + } | ||
| 21 | } | 28 | } |
| 22 | } | 29 | } |
| 23 | } | 30 | } |
modified
app/src/fluffyApplication.cpp +8 -3 | @@ -17,7 +17,7 @@ auto FluffyApplication::handleCommand( | ||
| 17 | 17 | const std::vector<std::string>& arguments |
| 18 | 18 | ) -> void { |
| 19 | 19 | if (command == "show") { |
| 20 | - ShowCommand showCommand = ShowCommand(gameRepository); | |
| 20 | + ShowCommand showCommand = ShowCommand(gameRepository, output); | |
| 21 | 21 | showCommand.execute(); |
| 22 | 22 | } |
| 23 | 23 | else if (command == "rm") { |
| @@ -28,14 +28,19 @@ auto FluffyApplication::handleCommand( | ||
| 28 | 28 | else if (command == "add") { |
| 29 | 29 | std::string gameName = arguments[0]; |
| 30 | 30 | std::string gamePath = arguments[1]; |
| 31 | - AddCommand addCommand = AddCommand(gameRepository, gameName, gamePath); | |
| 31 | + AddCommand addCommand = AddCommand(gameRepository, output, gameName, gamePath); | |
| 32 | 32 | addCommand.execute(); |
| 33 | 33 | } |
| 34 | 34 | else if (command == "play") { |
| 35 | 35 | std::string gameName{arguments[0]}; |
| 36 | - PlayCommand playCommand = PlayCommand(gameRepository, gameName); | |
| 36 | + PlayCommand playCommand = PlayCommand(gameRepository, output, gameName); | |
| 37 | 37 | playCommand.execute(); |
| 38 | 38 | } |
| 39 | + else if (command == "only-games") { | |
| 40 | + for (auto &game : gameRepository->getGames()) { | |
| 41 | + output->printMessage(game.getName()); | |
| 42 | + } | |
| 43 | + } | |
| 39 | 44 | else if (command == "--version") { |
| 40 | 45 | output->printMessage("fluffy " + version); |
| 41 | 46 | } |
| @@ -17,7 +17,7 @@ auto FluffyApplication::handleCommand( | |||
| 17 | const std::vector<std::string>& arguments | 17 | const std::vector<std::string>& arguments |
| 18 | ) -> void { | 18 | ) -> void { |
| 19 | if (command == "show") { | 19 | if (command == "show") { |
| 20 | - ShowCommand showCommand = ShowCommand(gameRepository); | 20 | + ShowCommand showCommand = ShowCommand(gameRepository, output); |
| 21 | showCommand.execute(); | 21 | showCommand.execute(); |
| 22 | } | 22 | } |
| 23 | else if (command == "rm") { | 23 | else if (command == "rm") { |
| @@ -28,14 +28,19 @@ auto FluffyApplication::handleCommand( | |||
| 28 | else if (command == "add") { | 28 | else if (command == "add") { |
| 29 | std::string gameName = arguments[0]; | 29 | std::string gameName = arguments[0]; |
| 30 | std::string gamePath = arguments[1]; | 30 | std::string gamePath = arguments[1]; |
| 31 | - AddCommand addCommand = AddCommand(gameRepository, gameName, gamePath); | 31 | + AddCommand addCommand = AddCommand(gameRepository, output, gameName, gamePath); |
| 32 | addCommand.execute(); | 32 | addCommand.execute(); |
| 33 | } | 33 | } |
| 34 | else if (command == "play") { | 34 | else if (command == "play") { |
| 35 | std::string gameName{arguments[0]}; | 35 | std::string gameName{arguments[0]}; |
| 36 | - PlayCommand playCommand = PlayCommand(gameRepository, gameName); | 36 | + PlayCommand playCommand = PlayCommand(gameRepository, output, gameName); |
| 37 | playCommand.execute(); | 37 | playCommand.execute(); |
| 38 | } | 38 | } |
| 39 | + else if (command == "only-games") { | ||
| 40 | + for (auto &game : gameRepository->getGames()) { | ||
| 41 | + output->printMessage(game.getName()); | ||
| 42 | + } | ||
| 43 | + } | ||
| 39 | else if (command == "--version") { | 44 | else if (command == "--version") { |
| 40 | output->printMessage("fluffy " + version); | 45 | output->printMessage("fluffy " + version); |
| 41 | } | 46 | } |