~aleteoryx/muditaos

ref: sign_test muditaos/module-db/Tables/ContactsGroups.hpp -rw-r--r-- 3.1 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
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once

#include <Database/Database.hpp>
#include "Record.hpp"
#include "Table.hpp"

#include <set>
#include <string>

struct ContactsGroupsTableRow : public Record
{
    ContactsGroupsTableRow() = default;
    ContactsGroupsTableRow(std::string name);
    ContactsGroupsTableRow(uint32_t id, std::string name);
    ContactsGroupsTableRow(uint32_t id);
    bool operator==(const ContactsGroupsTableRow &other) const
    {
        return this->ID == other.ID;
    }
    bool operator!=(const ContactsGroupsTableRow &other) const
    {
        return !this->operator==(other);
    }
    bool operator<(const ContactsGroupsTableRow &other) const
    {
        return this->ID < other.ID;
    }
    bool operator>(const ContactsGroupsTableRow &other) const
    {
        return this->ID > other.ID;
    }
    std::string name;
};

enum class ContactsGroupsTableFields
{
    GroupID,
    Name
};

struct ContactsMatchGroupsTableRow : public Record
{
    uint32_t groupID   = DB_ID_NONE;
    uint32_t contactID = DB_ID_NONE;
};

enum class ContacsMatchGroupsTableFields
{
    MatchID,
    GroupID,
    ContactID,
};

class ContactsGroupsTable : public Table<ContactsGroupsTableRow, ContactsGroupsTableFields>
{
  public:
    ContactsGroupsTable(Database *db);
    virtual ~ContactsGroupsTable() override = default;

    bool create() override final;

    /** Adds new group.
     * ID field have to be 0
     */
    bool add(ContactsGroupsTableRow entry) override final;
    bool removeById(uint32_t id) override final;
    bool update(ContactsGroupsTableRow entry) override final;
    void updateGroups(uint32_t contactId, std::set<ContactsGroupsTableRow> newGroups);
    ContactsGroupsTableRow getById(uint32_t id) override final;
    std::vector<ContactsGroupsTableRow> getAllRows();
    std::vector<ContactsGroupsTableRow> getLimitOffset(uint32_t offset, uint32_t limit) override;
    std::vector<ContactsGroupsTableRow> getLimitOffsetByField(uint32_t offset,
                                                              uint32_t limit,
                                                              ContactsGroupsTableFields field,
                                                              const char *str) override;
    bool removeByField(ContactsGroupsTableFields field, const char *str) override;
    uint32_t count() override;
    uint32_t countByFieldId(const char *field, uint32_t id) override;
    [[nodiscard]] uint32_t favouritesId() const;
    [[nodiscard]] uint32_t iceId() const;
    [[nodiscard]] uint32_t blockedId() const;
    [[nodiscard]] uint32_t temporaryId() const;
    /** Returns id of the grup
     */
    uint32_t getId(const std::string &name);

    std::set<ContactsGroupsTableRow> getGroupsForContact(uint32_t contactId);
    bool addContactToGroup(uint32_t contactId, uint32_t groupId);
    bool removeContactFromGroup(uint32_t contactId, uint32_t groupId);

    std::set<uint32_t> getContactsForGroup(uint32_t groupId);

  private:
    uint32_t getIdOrCount(const char *queryString) const;
};