diff options
| author | ilotterytea <iltsu@alright.party> | 2024-04-30 21:14:27 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2024-04-30 21:14:27 +0500 |
| commit | 6d74bb52954156d6324d27455712e0d008ab938a (patch) | |
| tree | bf5312512aee2a7d3ce29e0e981fc8d56f9e4ac4 /src | |
| parent | 184fcd5cb5bacd11271028c1031a7bfef4db48a2 (diff) | |
feat: Channel model
Diffstat (limited to 'src')
| -rw-r--r-- | src/schemas/channel.hpp | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/src/schemas/channel.hpp b/src/schemas/channel.hpp new file mode 100644 index 0000000..2e92a5e --- /dev/null +++ b/src/schemas/channel.hpp @@ -0,0 +1,66 @@ +#pragma once + +#include <chrono> +#include <ctime> +#include <iomanip> +#include <optional> +#include <pqxx/pqxx> +#include <sstream> +#include <stdexcept> +#include <string> + +namespace bot::schemas { + class Channel { + public: + Channel(const pqxx::row &row) { + this->id = row[0].as<int>(); + this->alias_id = row[1].as<int>(); + this->alias_name = row[2].as<std::string>(); + + std::tm tm = {}; + std::stringstream ss(row[3].as<std::string>()); + + ss >> std::get_time(&tm, "%Y-%m-%d %H:%M:%S"); + + if (ss.fail()) { + throw std::invalid_argument("Invalid time format"); + } + + this->joined_at = + std::chrono::system_clock::from_time_t(std::mktime(&tm)); + + if (!row[4].is_null()) { + tm = {}; + ss = std::stringstream(row[4].as<std::string>()); + + ss >> std::get_time(&tm, "%Y-%m-%d %H:%M:%S"); + + if (ss.fail()) { + throw std::invalid_argument("Invalid time format"); + } + + this->opted_out_at = + std::chrono::system_clock::from_time_t(std::mktime(&tm)); + } + } + + ~Channel() = default; + + const int &get_id() const { return this->id; } + const int &get_alias_id() const { return this->alias_id; } + const std::string &get_alias_name() const { return this->alias_name; } + const std::chrono::system_clock::time_point &get_joined_at() const { + return this->joined_at; + } + const std::optional<std::chrono::system_clock::time_point> & + get_opted_out_at() const { + return this->opted_out_at; + } + + private: + int id, alias_id; + std::string alias_name; + std::chrono::system_clock::time_point joined_at; + std::optional<std::chrono::system_clock::time_point> opted_out_at; + }; +} |
