summaryrefslogtreecommitdiff
path: root/src/schemas/channel.hpp
blob: d0c8ab30c7228a4fff8d744c44e821bd27ea62f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#pragma once

#include <chrono>
#include <ctime>
#include <iomanip>
#include <optional>
#include <pqxx/pqxx>
#include <sstream>
#include <stdexcept>
#include <string>

#include "../constants.hpp"

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;
  };

  class ChannelPreference {
    public:
      ChannelPreference(const pqxx::row &row) {
        this->id = row[0].as<int>();
        this->channel_id = row[1].as<int>();

        if (!row[2].is_null()) {
          this->prefix = row[2].as<std::string>();
        } else {
          this->prefix = DEFAULT_PREFIX;
        }

        if (!row[3].is_null()) {
          this->locale = row[3].as<std::string>();
        } else {
          this->locale = DEFAULT_LOCALE_ID;
        }
      }

      ~ChannelPreference() = default;

      const int &get_id() const { return this->id; }
      const int &get_channel_id() const { return this->channel_id; }
      const std::string &get_prefix() const { return this->prefix; }
      const std::string &get_locale() const { return this->locale; }

    private:
      int id, channel_id;
      std::string prefix, locale;
  };
}