ADD: added a redmine user representation

This commit is contained in:
Dominik Meyer 2024-02-18 13:12:53 +01:00
parent 0e6fb6c84f
commit 703d952ad3
Signed by: byterazor
GPG Key ID: EABDA0FD5981BC97
4 changed files with 372 additions and 0 deletions

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
#include "nlohmann/json_fwd.hpp"
#include <nlohmann/json.hpp>
/**
* @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

154
include/Redmine/User.hpp Normal file
View File

@ -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 <https://www.gnu.org/licenses/>.
*/
#include "nlohmann/json_fwd.hpp"
#include <cstdint>
#include <nlohmann/json.hpp>
#include <Redmine/Object.hpp>
#include <string>
/**
* @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

46
src/Redmine/Object.cpp Normal file
View File

@ -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 <https://www.gnu.org/licenses/>.
*/
#include <Redmine/Object.hpp>
#define LOGURU_WITH_STREAMS 1
#include <loguru.hpp>
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");
}
}

85
src/Redmine/User.cpp Normal file
View File

@ -0,0 +1,85 @@
#include "nlohmann/json_fwd.hpp"
#include <Redmine/User.hpp>
#include <cstdint>
#include <stdexcept>
#include <string>
#define LOGURU_WITH_STREAMS 1
#include <loguru.hpp>
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<std::string>() + " " + data_["user"]["lastname"].get<std::string>();
}
std::string Redmine::User::getFirstName() const
{
return data_["user"]["firstname"].get<std::string>();
}
std::string Redmine::User::getLastName() const
{
return data_["user"]["lastname"].get<std::string>();
}
std::string Redmine::User::getEmail() const
{
return data_["user"]["mail"].get<std::string>();
}
bool Redmine::User::isAdmin() const
{
return data_["user"]["admin"].get<bool>() == true;
}
std::string Redmine::User::getAvatarURL() const
{
return data_["user"]["avatar_url"].get<std::string>();
}
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::uint32_t>();
}
std::string Redmine::User::getLogin() const
{
return data_["user"]["login"].get<std::string>();
}
void Redmine::User::setFirstName(const std::string &firstname)
{
data_["user"]["firstname"]=firstname;
}