~aleteoryx/muditaos

ref: c8c4f82080c1fd95c54c4fc28d64b9eed2255f92 muditaos/module-db/Interface/NotesRecord.cpp -rw-r--r-- 5.1 KiB
c8c4f820 — Piotr Tanski [EGD-4487] Application notes implemented. (#1094) 5 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
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "NotesRecord.hpp"

#include "queries/notes/QueryNotesGet.hpp"
#include "queries/notes/QueryNoteStore.hpp"
#include "queries/notes/QueryNoteRemove.hpp"

#include <cassert>

namespace
{
    NotesTableFields toNotesTableFields(NotesRecordField field)
    {
        switch (field) {
        case NotesRecordField::Date:
            return NotesTableFields::Date;
        case NotesRecordField::Snippet:
            return NotesTableFields::Snippet;
        }
        throw std::invalid_argument{"Invalid notes record field passed."};
    }
} // namespace

NotesRecordInterface::NotesRecordInterface(NotesDB *notesDb) : notesDB(notesDb)
{}

bool NotesRecordInterface::Add(const NotesRecord &rec)
{
    return notesDB->notes.add(NotesTableRow{.date = rec.date, .snippet = rec.snippet});
}

std::uint32_t NotesRecordInterface::GetCount()
{
    return notesDB->notes.count();
}

std::unique_ptr<std::vector<NotesRecord>> NotesRecordInterface::GetLimitOffsetByField(std::uint32_t offset,
                                                                                      std::uint32_t limit,
                                                                                      NotesRecordField field,
                                                                                      const char *str)
{
    auto records     = std::make_unique<std::vector<NotesRecord>>();
    const auto notes = notesDB->notes.getLimitOffsetByField(offset, limit, toNotesTableFields(field), str);
    for (const auto &w : notes) {
        NotesRecord record{w.ID, w.date, w.snippet};
        records->push_back(std::move(record));
    }
    return records;
}

std::unique_ptr<std::vector<NotesRecord>> NotesRecordInterface::GetLimitOffset(std::uint32_t offset,
                                                                               std::uint32_t limit)
{
    return std::make_unique<std::vector<NotesRecord>>(getNotes(offset, limit));
}

bool NotesRecordInterface::Update(const NotesRecord &rec)
{
    auto note = notesDB->notes.getById(rec.ID);
    if (note.ID == DB_ID_NONE) {
        return false;
    }

    notesDB->notes.update(NotesTableRow{.ID = rec.ID, .date = rec.date, .snippet = rec.snippet});
    return true;
}

bool NotesRecordInterface::RemoveAll()
{
    return notesDB->notes.removeAll();
}

bool NotesRecordInterface::RemoveByID(std::uint32_t id)
{
    return notesDB->notes.removeById(id);
}

bool NotesRecordInterface::RemoveByField(NotesRecordField field, const char *str)
{
    switch (field) {
    case NotesRecordField::Date:
        return notesDB->notes.removeByField(NotesTableFields::Date, str);
    default:
        return false;
    }
}

NotesRecord NotesRecordInterface::GetByID(std::uint32_t id)
{
    auto note = notesDB->notes.getById(id);
    return NotesRecord{note.ID, note.date, note.snippet};
}

std::vector<NotesRecord> NotesRecordInterface::getNotes(std::uint32_t offset, std::uint32_t limit) const
{
    std::vector<NotesRecord> records;
    const auto notes = notesDB->notes.getLimitOffset(offset, limit);
    for (const auto &w : notes) {
        NotesRecord record{w.ID, w.date, w.snippet};
        records.push_back(std::move(record));
    }
    return records;
}

std::unique_ptr<db::QueryResult> NotesRecordInterface::runQuery(std::shared_ptr<db::Query> query)
{
    if (typeid(*query) == typeid(db::query::QueryNotesGet)) {
        return getQuery(query);
    }
    if (typeid(*query) == typeid(db::query::QueryNoteStore)) {
        return storeQuery(query);
    }
    if (typeid(*query) == typeid(db::query::QueryNoteRemove)) {
        return removeQuery(query);
    }
    return nullptr;
}

std::unique_ptr<db::QueryResult> NotesRecordInterface::getQuery(const std::shared_ptr<db::Query> &query)
{
    const auto localQuery = static_cast<db::query::QueryNotesGet *>(query.get());
    const auto &records   = getNotes(localQuery->getOffset(), localQuery->getLimit());
    auto response         = std::make_unique<db::query::NotesGetResult>(records, GetCount());
    response->setRequestQuery(query);
    return response;
}

std::unique_ptr<db::QueryResult> NotesRecordInterface::storeQuery(const std::shared_ptr<db::Query> &query)
{
    const auto localQuery = static_cast<db::query::QueryNoteStore *>(query.get());
    const auto &record    = localQuery->getRecord();
    bool isSuccess        = false;
    if (const auto exists = notesDB->notes.getById(record.ID).ID != DB_ID_NONE; exists) {
        isSuccess = Update(record);
    }
    else {
        isSuccess = Add(record);
    }

    auto response = std::make_unique<db::query::NoteStoreResult>(isSuccess);
    response->setRequestQuery(query);
    return response;
}

std::unique_ptr<db::QueryResult> NotesRecordInterface::removeQuery(const std::shared_ptr<db::Query> &query)
{
    const auto localQuery = static_cast<db::query::QueryNoteRemove *>(query.get());
    const auto isSuccess  = RemoveByID(localQuery->getRecordId());
    auto response         = std::make_unique<db::query::NoteRemoveResult>(isSuccess);
    response->setRequestQuery(query);
    return response;
}