~aleteoryx/muditaos

ref: a3aa441b0fbb94956cdafaebd92b15b5c93da59f muditaos/module-db/Interface/NotificationsRecord.cpp -rw-r--r-- 5.9 KiB
a3aa441b — Bartosz Cichocki [EGD-6682] Fix BT name change bug 4 years ago
                                                                                
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "NotificationsRecord.hpp"
#include "module-db/queries/notifications/QueryNotificationsGet.hpp"
#include "module-db/queries/notifications/QueryNotificationsIncrement.hpp"
#include "module-db/queries/notifications/QueryNotificationsClear.hpp"
#include "module-db/queries/notifications/QueryNotificationsGetAll.hpp"

#include <log/log.hpp>
#include <Utils.hpp>

#include <cassert>
#include <vector>

NotificationsRecord::NotificationsRecord(const NotificationsTableRow &tableRow)
    : Record{tableRow.ID}, value{tableRow.value}
{
    if (tableRow.key > static_cast<uint32_t>(Key::NotValidKey) &&
        tableRow.key < static_cast<uint32_t>(Key::NumberOfKeys)) {
        key = static_cast<Key>(tableRow.key);
        return;
    }
    LOG_ERROR("Invalid record");
    key = Key::NotValidKey;
    ID  = DB_ID_NONE;
}

bool NotificationsRecord::isValidRecord() const
{
    return isValid() && gotValidKey();
}

bool NotificationsRecord::gotValidKey() const
{
    return isValidKey(key);
}

bool NotificationsRecord::isValidKey(Key key)
{
    return key != Key::NotValidKey && key != Key::NumberOfKeys;
}

std::ostream &operator<<(std::ostream &out, const NotificationsRecord &rec)
{
    out << " <id> " << rec.ID << " <key> " << static_cast<int>(rec.key) << " <value> " << rec.value;

    return out;
}

NotificationsRecordInterface::NotificationsRecordInterface(NotificationsDB *notificationsDb)
    : notificationsDb(notificationsDb)
{}

bool NotificationsRecordInterface::Add(const NotificationsRecord &rec)
{
    assert(0 && "Not implemented");

    return false;
}

std::unique_ptr<std::vector<NotificationsRecord>> NotificationsRecordInterface::GetLimitOffsetByField(
    uint32_t offset, uint32_t limit, NotificationsRecordField field, const char *str)
{
    assert(0 && "Not implemented");

    return GetLimitOffset(offset, limit);
}

std::unique_ptr<std::vector<NotificationsRecord>> NotificationsRecordInterface::GetLimitOffset(uint32_t offset,
                                                                                               uint32_t limit)
{
    if (limit == 0) {
        limit = GetCount();
    }

    auto rows = notificationsDb->notifications.getLimitOffset(offset, limit);

    auto records = std::make_unique<std::vector<NotificationsRecord>>();

    for (auto &r : rows) {
        records->push_back(NotificationsRecord{r});
    }

    return records;
}

bool NotificationsRecordInterface::Update(const NotificationsRecord &rec)
{
    auto entry = notificationsDb->notifications.getById(rec.ID);
    if (!entry.isValid() || entry.key != static_cast<uint32_t>(rec.key)) {
        return false;
    }

    return notificationsDb->notifications.update(
        NotificationsTableRow{{.ID = rec.ID}, .key = static_cast<uint32_t>(rec.key), .value = rec.value});
}

bool NotificationsRecordInterface::RemoveByID(uint32_t id)
{
    assert(0 && "Not implemented");

    return false;
}

bool NotificationsRecordInterface::RemoveByField(NotificationsRecordField field, const char *str)
{
    assert(0 && "Not implemented");

    return false;
}

NotificationsRecord NotificationsRecordInterface::GetByID(uint32_t id)
{
    return NotificationsRecord{notificationsDb->notifications.getById(id)};
}

uint32_t NotificationsRecordInterface::GetCount()
{
    return notificationsDb->notifications.count();
}

NotificationsRecord NotificationsRecordInterface::GetByKey(NotificationsRecord::Key key)
{
    if (!NotificationsRecord::isValidKey(key)) {
        return NotificationsRecord();
    }

    NotificationsTableRow notificationsTableRow = notificationsDb->notifications.GetByKey(static_cast<uint32_t>(key));
    return NotificationsRecord{notificationsTableRow};
}

std::unique_ptr<db::QueryResult> NotificationsRecordInterface::runQuery(std::shared_ptr<db::Query> query)
{
    if (const auto local_query = dynamic_cast<const db::query::notifications::Get *>(query.get())) {
        return runQueryImpl(local_query);
    }
    if (const auto local_query = dynamic_cast<const db::query::notifications::Increment *>(query.get())) {
        return runQueryImpl(local_query);
    }
    if (const auto local_query = dynamic_cast<const db::query::notifications::Clear *>(query.get())) {
        return runQueryImpl(local_query);
    }
    if (const auto local_query = dynamic_cast<const db::query::notifications::GetAll *>(query.get())) {
        return runQueryImpl(local_query);
    }
    return nullptr;
}

std::unique_ptr<db::query::notifications::GetResult> NotificationsRecordInterface::runQueryImpl(
    const db::query::notifications::Get *query)
{
    auto value = GetByKey(query->key);
    return std::make_unique<db::query::notifications::GetResult>(value);
}

std::unique_ptr<db::query::notifications::IncrementResult> NotificationsRecordInterface::runQueryImpl(
    const db::query::notifications::Increment *query)
{
    auto ret = false;

    auto record = GetByKey(query->key);
    if (record.isValid() && record.key == query->key) {
        record.value++;
        ret = Update(record);
    }
    return std::make_unique<db::query::notifications::IncrementResult>(ret);
}

std::unique_ptr<db::query::notifications::ClearResult> NotificationsRecordInterface::runQueryImpl(
    const db::query::notifications::Clear *query)
{
    auto ret = false;

    auto record = GetByKey(query->key);
    if (record.isValid() && record.key == query->key) {
        record.value = 0;
        ret          = Update(record);
    }
    return std::make_unique<db::query::notifications::ClearResult>(ret);
}

std::unique_ptr<db::query::notifications::GetAllResult> NotificationsRecordInterface::runQueryImpl(
    const db::query::notifications::GetAll *query)
{
    auto numberOfNotifications = GetCount();
    auto records               = GetLimitOffset(0, numberOfNotifications);
    return std::make_unique<db::query::notifications::GetAllResult>(std::move(records));
}