~aleteoryx/muditaos

ref: sign_test muditaos/module-db/Interface/NotificationsRecord.hpp -rw-r--r-- 3.9 KiB
a217eeb3 — Dawid Wojtas [BH-2024] Fix lack of alarm directory after updating software 1 year, 5 months 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
// Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once

#include "Common/Common.hpp"
#include "module-db/Tables/NotificationsTable.hpp"
#include "Record.hpp"
#include "Interface/ContactRecord.hpp"

#include <utf8/UTF8.hpp>
#include <cstdint>
#include <vector>

// fw declarations
class ContactRecordInterface;
class NotificationsDB;

namespace db::query::notifications
{
    class Get;
    class GetResult;
    class Increment;
    class IncrementResult;
    class Decrement;
    class DecrementResult;
    class MultipleIncrement;
    class MultipleIncrementResult;
    class Clear;
    class ClearResult;
    class GetAll;
    class GetAllResult;
} // namespace db::query::notifications

struct NotificationsRecord : public Record
{
    enum class Key
    {
        NotValidKey = 0,
        Calls,
        Sms,
        NumberOfKeys // do not use directly
    };

    Key key        = Key::NotValidKey;
    uint32_t value = 0;
    std::optional<ContactRecord> contactRecord;

    friend std::ostream &operator<<(std::ostream &out, const NotificationsRecord &point);

    NotificationsRecord() = default;
    explicit NotificationsRecord(const NotificationsTableRow &tableRow,
                                 std::optional<ContactRecord> record = std::nullopt);

    static bool isValidKey(Key key);
};

enum class NotificationsRecordField
{
    key
};

class NotificationsRecordInterface : public RecordInterface<NotificationsRecord, NotificationsRecordField>
{
  public:
    NotificationsRecordInterface(NotificationsDB *notificationsDb, ContactRecordInterface *contactsDb);

    bool Add(const NotificationsRecord &rec) override final;
    bool RemoveByID(uint32_t id) override final;
    bool RemoveByField(NotificationsRecordField field, const char *str) override final;
    bool Update(const NotificationsRecord &rec) override final;
    NotificationsRecord GetByID(uint32_t id) override final;
    uint32_t GetCount() override final;
    std::unique_ptr<std::vector<NotificationsRecord>> GetLimitOffset(uint32_t offset, uint32_t limit) override final;
    std::unique_ptr<std::vector<NotificationsRecord>> GetLimitOffsetByField(uint32_t offset,
                                                                            uint32_t limit,
                                                                            NotificationsRecordField field,
                                                                            const char *str) override final;

    NotificationsRecord GetByKey(NotificationsRecord::Key key);

    std::unique_ptr<db::QueryResult> runQuery(std::shared_ptr<db::Query> query) override;

  private:
    NotificationsDB *notificationsDb   = nullptr;
    ContactRecordInterface *contactsDb = nullptr;

    std::optional<ContactRecord> getContactRecord(uint32_t id) const;
    std::unique_ptr<db::query::notifications::GetResult> runQueryImpl(const db::query::notifications::Get *query);
    std::unique_ptr<db::query::notifications::IncrementResult> runQueryImpl(
        const db::query::notifications::Increment *query);
    std::unique_ptr<db::query::notifications::DecrementResult> runQueryImpl(
        const db::query::notifications::Decrement *query);
    std::unique_ptr<db::query::notifications::MultipleIncrementResult> runQueryImpl(
        const db::query::notifications::MultipleIncrement *query);
    std::unique_ptr<db::query::notifications::ClearResult> runQueryImpl(const db::query::notifications::Clear *query);
    std::unique_ptr<db::query::notifications::GetAllResult> runQueryImpl(const db::query::notifications::GetAll *query);
    [[nodiscard]] bool processIncrement(NotificationsRecord::Key key,
                                        std::optional<utils::PhoneNumber::View> &&number,
                                        size_t size);
    [[nodiscard]] bool processDecrement(NotificationsRecord::Key key, size_t size);
};