~aleteoryx/muditaos

ref: 6533aba24ef55cde97a89ef8040b9da21425d571 muditaos/module-db/Interface/NotesRecord.cpp -rw-r--r-- 2.6 KiB
6533aba2 — KacperLewandowski [EGD-4073] Alarms database rework (#1006) 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
// 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 "../Tables/NotesTable.hpp"

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

NotesRecordInterface::~NotesRecordInterface()
{}

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

    // TODO: error check

    return true;
}

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

std::unique_ptr<std::vector<NotesRecord>> NotesRecordInterface::GetLimitOffsetByField(uint32_t offset,
                                                                                      uint32_t limit,
                                                                                      NotesRecordField field,
                                                                                      const char *str)
{
    return GetLimitOffset(offset, limit);
}

std::unique_ptr<std::vector<NotesRecord>> NotesRecordInterface::GetLimitOffset(uint32_t offset, uint32_t limit)
{
    auto notes = notesDB->notes.getLimitOffset(offset, limit);

    auto records = std::make_unique<std::vector<NotesRecord>>();
    //
    NotesRecordInterface notesInterface(notesDB);
    for (const auto &w : notes) {

        records->push_back({
            .ID      = w.ID,
            .date    = w.date,
            .snippet = w.snippet,
            .path    = w.path,
        });
    }

    return records;
}

bool NotesRecordInterface::Update(const NotesRecord &rec)
{

    auto note = notesDB->notes.getById(rec.ID);
    if (note.ID == 0) {
        return false;
    }

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

    return true;
}

bool NotesRecordInterface::RemoveByID(uint32_t id)
{

    auto note = notesDB->notes.getById(id);
    if (note.ID == 0) {
        return false;
    }

    // Remove SMS
    if (notesDB->notes.removeById(id) == false) {
        return false;
    }

    return true;
}

bool NotesRecordInterface::RemoveByField(NotesRecordField field, const char *str)
{

    switch (field) {
    case NotesRecordField::Data:
        return notesDB->notes.removeByField(NotesTableFields::Date, str);
    default:
        return false;
    }
}

NotesRecord NotesRecordInterface::GetByID(uint32_t id)
{
    auto note = notesDB->notes.getById(id);

    return NotesRecord{.ID = note.ID, .date = note.date, .snippet = note.snippet, .path = note.path};
}