~aleteoryx/muditaos

muditaos/module-apps/application-notes/model/NotesRepository.cpp -rw-r--r-- 3.5 KiB
a405cad6Aleteoryx trim readme 6 days 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
// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md

#include "NotesRepository.hpp"

#include <module-db/queries/notes/QueryNotesGet.hpp>
#include <module-db/queries/notes/QueryNotesGetByText.hpp>
#include <module-db/queries/notes/QueryNoteStore.hpp>
#include <module-db/queries/notes/QueryNoteRemove.hpp>

#include <service-db/DBServiceAPI.hpp>

namespace app::notes
{
    NotesDBRepository::NotesDBRepository(ApplicationCommon *application)
        : app::AsyncCallbackReceiver{application}, application{application}
    {}

    void NotesDBRepository::get(std::uint32_t offset, std::uint32_t limit, const OnGetCallback &callback)
    {
        auto query = std::make_unique<db::query::QueryNotesGet>(offset, limit);
        auto task  = app::AsyncQuery::createFromQuery(std::move(query), db::Interface::Name::Notes);
        task->setCallback([callback](auto response) {
            auto result = dynamic_cast<db::query::NotesGetResult *>(response);
            if (result == nullptr) {
                return false;
            }
            if (callback) {
                callback(result->getRecords(), result->getCount());
            }
            return true;
        });
        task->execute(application, this);
    }

    void NotesDBRepository::getByText(const std::string &text,
                                      std::uint32_t offset,
                                      std::uint32_t limit,
                                      const OnGetCallback &callback)
    {
        auto query = std::make_unique<db::query::QueryNotesGetByText>(text, offset, limit);
        auto task  = app::AsyncQuery::createFromQuery(std::move(query), db::Interface::Name::Notes);
        task->setCallback([callback](auto response) {
            auto result = dynamic_cast<db::query::NotesGetByTextResult *>(response);
            if (result == nullptr) {
                return false;
            }
            if (callback) {
                callback(result->getRecords(), result->getCount());
            }
            return true;
        });
        task->execute(application, this);
    }

    void NotesDBRepository::save(const NotesRecord &note, const OnSaveCallback &callback)
    {
        auto query = std::make_unique<db::query::QueryNoteStore>(note);
        auto task  = app::AsyncQuery::createFromQuery(std::move(query), db::Interface::Name::Notes);
        task->setCallback([callback](auto response) {
            auto result = dynamic_cast<db::query::NoteStoreResult *>(response);
            if (result == nullptr) {
                return false;
            }
            if (callback) {
                callback(result->succeed(), result->getNoteId());
            }
            return true;
        });
        task->execute(application, this);
    }

    void NotesDBRepository::remove(const NotesRecord &note, const OnRemoveCallback &callback)
    {
        auto query = std::make_unique<db::query::QueryNoteRemove>(note.ID);
        auto task  = app::AsyncQuery::createFromQuery(std::move(query), db::Interface::Name::Notes);
        task->setCallback([callback](auto response) {
            auto result = dynamic_cast<db::query::NoteRemoveResult *>(response);
            if (result == nullptr) {
                return false;
            }
            if (callback) {
                callback(result->succeed());
            }
            return true;
        });
        task->execute(application, this);
    }
} // namespace app::notes