diff --git a/include/Redmine/Object.hpp b/include/Redmine/Object.hpp new file mode 100644 index 0000000..7e28710 --- /dev/null +++ b/include/Redmine/Object.hpp @@ -0,0 +1,87 @@ +#pragma once +/* +* Copyright (C) 2024 Dominik Meyer +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see . +*/ + +#include "nlohmann/json_fwd.hpp" +#include + + +/** +* @brief The main namespace of this library for Redmine related datatypes, classes, and functions +* +*/ +namespace Redmine +{ + + /** + * @brief interface class for all redmine objects + * + * All redmine objects are expected to use a json object as their main + * data store. This supports easy working with the json base api calls. + * + */ + class Object + { + protected: + + /** + * @brief verifies the validity of the provided json data + * + * This method has to check each value within the json data + * if it is valid for the object. It also has to enforce + * mandatory attributes. + * + * The method should throw exceptions if a non recoverable + * problem is found! + * + */ + virtual void _verify(const nlohmann::json &data) = 0; + + void _baseVerify(const nlohmann::json &data, const std::string &objectName); + + public: + + /** + * @brief set the internal data of the object + * + * This method has to set all valued of the object even if some already have been + * set but are missing in the new data. + * + * It is *expected* that you reset all old data + * + * @param data - the json data holding the objects new information + */ + virtual void set(const nlohmann::json &data) = 0; + + /** + * @brief returns the currently set data of the object as a json object + * + * @return nlohmann::json - the json object holding all object information + */ + virtual nlohmann::json get() const = 0; + + /** + * @brief return a string representation of the object + * + * @return std::string + */ + virtual std::string to_string() const = 0; + + + }; // class Object + +}; // namespace Redmine \ No newline at end of file diff --git a/include/Redmine/User.hpp b/include/Redmine/User.hpp new file mode 100644 index 0000000..dfad989 --- /dev/null +++ b/include/Redmine/User.hpp @@ -0,0 +1,154 @@ +#pragma once +/* +* Copyright (C) 2024 Dominik Meyer +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see . +*/ + +#include "nlohmann/json_fwd.hpp" +#include +#include +#include +#include + +/** + * @brief The main namespace of this library for Redmine related datatypes, classes, and functions + * + */ +namespace Redmine +{ + + /** + * @brief This class represents a user within the redmine server. + * + */ + class User : public Redmine::Object + { + private: + + /// the objects data store + nlohmann::json data_; + + /** + * @brief verify user data + * + * @param data - the json object containing the user data + */ + void _verify(const nlohmann::json &data); + + + + public: + + /** + * @brief Constructor creating a UserInfo object from json data + * + * @param userinfo + */ + explicit User(const nlohmann::json &user); + + /** + * @brief set the user object from a json object + * + * @param data + */ + void set(const nlohmann::json &data); + + /** + * @brief return a json object from the user object + * + * @return nlohmann::json + */ + nlohmann::json get() const; + + /** + * @brief Get the Full Name of the user + * + * @return std::string + */ + std::string getFullName() const; + + /** + * @brief Get the First Name of the user + * + * @return std::string + */ + std::string getFirstName() const; + + /** + * @brief Get the Last Name of the user + * + * @return std::string + */ + std::string getLastName() const; + + /** + * @brief Get the Email address of the user + * + * @return std::string + */ + std::string getEmail() const; + + /** + * @brief check if the user is an admin + * + * @return true + * @return false + */ + bool isAdmin() const; + + /** + * @brief Get the url of the avatar image of the user + * + * @return std::string + */ + std::string getAvatarURL() const; + + /** + * @brief convert the user object to a string + * + * @return std::string + */ + std::string to_string() const; + + /** + * @brief return the user id + * + * @return std::uint32_t + */ + std::uint32_t getId() const; + + /** + * @brief Get the login/account of the user + * + * @return std::string + */ + std::string getLogin() const; + + /** + * @brief Set the First Name of the user object + * + * *Remember:* you are only updating the local object! + * + * To change something on the server to have to call the appropriate API method. + * + * @param firstname - the new firstname of the user + */ + void setFirstName(const std::string &firstname); + + + }; // class User + + +}; // namespace Redmine \ No newline at end of file diff --git a/src/Redmine/Object.cpp b/src/Redmine/Object.cpp new file mode 100644 index 0000000..9a83659 --- /dev/null +++ b/src/Redmine/Object.cpp @@ -0,0 +1,46 @@ +/* +* Copyright (C) 2024 Dominik Meyer +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see . +*/ +#include +#define LOGURU_WITH_STREAMS 1 +#include + +void Redmine::Object::_baseVerify(const nlohmann::json &data, const std::string &objectName) +{ + if (data.empty()) + { + DLOG_S(ERROR) << "provided data is empty"; + throw std::invalid_argument("provided data is empty"); + } + + if (!data.is_object()) + { + DLOG_S(ERROR) << "provided data is not a json object/hash"; + throw std::invalid_argument("provided data is not a json objects/hash"); + } + + if (!data.contains(objectName)) + { + DLOG_S(ERROR) << "provided data does not contain object information (" << objectName << ")"; + throw std::invalid_argument("provided data does not contain object information (" + objectName + ")"); + } + + if (data[objectName].empty()) + { + DLOG_S(ERROR) << "provided object information is empty"; + throw std::invalid_argument("provided object information is empty"); + } +} \ No newline at end of file diff --git a/src/Redmine/User.cpp b/src/Redmine/User.cpp new file mode 100644 index 0000000..a1dad86 --- /dev/null +++ b/src/Redmine/User.cpp @@ -0,0 +1,85 @@ + +#include "nlohmann/json_fwd.hpp" +#include +#include +#include +#include +#define LOGURU_WITH_STREAMS 1 +#include + +void Redmine::User::set(const nlohmann::json &data) +{ + _verify(data); + data_=data; +} + +nlohmann::json Redmine::User::get() const +{ + return data_; +} + +void Redmine::User::_verify(const nlohmann::json &data) +{ + + _baseVerify(data, "user"); + + nlohmann::json attributes = data["user"]; + + +} + +Redmine::User::User(const nlohmann::json &user) : Redmine::Object() +{ + set(user); +} + + +std::string Redmine::User::getFullName() const +{ + return data_["user"]["firstname"].get() + " " + data_["user"]["lastname"].get(); +} + +std::string Redmine::User::getFirstName() const +{ + return data_["user"]["firstname"].get(); +} + +std::string Redmine::User::getLastName() const +{ + return data_["user"]["lastname"].get(); +} + +std::string Redmine::User::getEmail() const +{ + return data_["user"]["mail"].get(); +} + +bool Redmine::User::isAdmin() const +{ + return data_["user"]["admin"].get() == true; +} + +std::string Redmine::User::getAvatarURL() const +{ + return data_["user"]["avatar_url"].get(); +} + +std::string Redmine::User::to_string() const +{ + return data_["user"].dump(2); +} + +std::uint32_t Redmine::User::getId() const +{ + return data_["user"]["id"].get(); +} + +std::string Redmine::User::getLogin() const +{ + return data_["user"]["login"].get(); +} + +void Redmine::User::setFirstName(const std::string &firstname) +{ + data_["user"]["firstname"]=firstname; +} \ No newline at end of file