Commit aebca940 authored by camilo higuita's avatar camilo higuita

now updating notes works both locally and remotely

parent 76a94884
......@@ -31,10 +31,10 @@ Notes::Notes(QObject *parent) : MauiList(parent),
void Notes::sortList()
{
emit this->preListChanged();
// this->notes = this->db->getDBData(QString("select * from notes ORDER BY %1 %2").arg(
// FMH::MODEL_NAME[static_cast<FMH::MODEL_KEY>(this->sort)],
// this->order == ORDER::ASC ? "asc" : "desc"));
emit this->preListChanged();
// this->notes = this->db->getDBData(QString("select * from notes ORDER BY %1 %2").arg(
// FMH::MODEL_NAME[static_cast<FMH::MODEL_KEY>(this->sort)],
// this->order == ORDER::ASC ? "asc" : "desc"));
emit this->postListChanged();
}
......@@ -87,22 +87,6 @@ bool Notes::insert(const QVariantMap &note)
return true;
}
bool Notes::update(const int &index, const QVariant &value, const int &role)
{
if(index < 0 || index >= notes.size())
return false;
const auto oldValue = this->notes[index][static_cast<FMH::MODEL_KEY>(role)];
if(oldValue == value.toString())
return false;
this->notes[index].insert(static_cast<FMH::MODEL_KEY>(role), value.toString());
this->update(this->notes[index]);
return true;
}
bool Notes::update(const QVariantMap &data, const int &index)
{
......@@ -112,50 +96,19 @@ bool Notes::update(const QVariantMap &data, const int &index)
auto newData = this->notes[index];
QVector<int> roles;
for(auto key : data.keys())
for(const auto &key : data.keys())
if(newData[FMH::MODEL_NAME_KEY[key]] != data[key].toString())
{
newData.insert(FMH::MODEL_NAME_KEY[key], data[key].toString());
newData[FMH::MODEL_NAME_KEY[key]] = data[key].toString();
roles << FMH::MODEL_NAME_KEY[key];
}
this->notes[index] = newData;
if(this->update(newData))
{
emit this->updateModel(index, roles);
return true;
}
return false;
}
this->syncer->updateNote(newData[FMH::MODEL_KEY::ID], newData);
bool Notes::update(const FMH::MODEL &note)
{
auto id = note[FMH::MODEL_KEY::ID];
auto title = note[FMH::MODEL_KEY::TITLE];
auto content = note[FMH::MODEL_KEY::CONTENT];
auto color = note[FMH::MODEL_KEY::COLOR];
auto pin = note[FMH::MODEL_KEY::PIN].toInt();
auto favorite = note[FMH::MODEL_KEY::FAVORITE].toInt();
auto tags = note[FMH::MODEL_KEY::TAG].split(",", QString::SkipEmptyParts);
auto modified =note[FMH::MODEL_KEY::MODIFIED];
QVariantMap note_map =
{
{FMH::MODEL_NAME[FMH::MODEL_KEY::TITLE], title},
{FMH::MODEL_NAME[FMH::MODEL_KEY::CONTENT], content},
{FMH::MODEL_NAME[FMH::MODEL_KEY::COLOR], color},
{FMH::MODEL_NAME[FMH::MODEL_KEY::PIN], pin},
{FMH::MODEL_NAME[FMH::MODEL_KEY::FAVORITE], favorite},
{FMH::MODEL_NAME[FMH::MODEL_KEY::MODIFIED], modified}
};
// for(auto tg : tags)
// this->tag->tagAbstract(tg, OWL::TABLEMAP[OWL::TABLE::NOTES], id, color);
// return this->db->update(OWL::TABLEMAP[OWL::TABLE::NOTES], note_map, {{FMH::MODEL_NAME[FMH::MODEL_KEY::ID], id}} );
return false;
emit this->updateModel(index, roles);
return true;
}
bool Notes::remove(const int &index)
......@@ -164,12 +117,12 @@ bool Notes::remove(const int &index)
auto id = this->notes.at(index)[FMH::MODEL_KEY::ID];
QVariantMap note = {{FMH::MODEL_NAME[FMH::MODEL_KEY::ID], id}};
// if(this->db->remove(OWL::TABLEMAP[OWL::TABLE::NOTES], note))
// {
// this->notes.removeAt(index);
// emit this->postItemRemoved();
// return true;
// }
// if(this->db->remove(OWL::TABLEMAP[OWL::TABLE::NOTES], note))
// {
// this->notes.removeAt(index);
// emit this->postItemRemoved();
// return true;
// }
return false;
}
......@@ -188,11 +141,11 @@ QVariantMap Notes::getAccount() const
QVariantList Notes::getTags(const int &index)
{
// if(index < 0 || index >= this->notes.size())
// return QVariantList();
// if(index < 0 || index >= this->notes.size())
// return QVariantList();
// auto id = this->notes.at(index)[FMH::MODEL_KEY::ID];
// return this->tag->getAbstractTags(OWL::TABLEMAP[OWL::TABLE::NOTES], id);
// auto id = this->notes.at(index)[FMH::MODEL_KEY::ID];
// return this->tag->getAbstractTags(OWL::TABLEMAP[OWL::TABLE::NOTES], id);
return QVariantList();
}
......
......@@ -72,9 +72,7 @@ public slots:
QVariantMap get(const int &index) const;
bool insert(const QVariantMap &note);
bool update(const int &index, const QVariant &value, const int &role); //deprecrated
bool update(const QVariantMap &data, const int &index);
bool update(const FMH::MODEL &note);
bool remove(const int &index);
};
......
......@@ -11,7 +11,30 @@
#include <MauiKit/fm.h>
#endif
const QString NextNote::API = "https://PROVIDER/index.php/apps/notes/api/v0.2/";
const QString NextNote::API = QStringLiteral("https://PROVIDER/index.php/apps/notes/api/v0.2/");
static const inline QNetworkRequest formRequest(const QUrl &url, const QString &user, const QString &password)
{
if(!url.isValid() && !user.isEmpty() && !password.isEmpty())
return QNetworkRequest();
const QString concatenated = user + ":" + password;
const QByteArray data = concatenated.toLocal8Bit().toBase64();
const QString headerData = "Basic " + data;
// QVariantMap headers
// {
// {"Authorization", headerData.toLocal8Bit()},
// {QString::number(QNetworkRequest::ContentTypeHeader),"application/json"}
// };
QNetworkRequest request;
request.setUrl(QUrl(url));
request.setHeader(QNetworkRequest::ContentTypeHeader,"application/json");
request.setRawHeader(QString("Authorization").toLocal8Bit(), headerData.toLocal8Bit());
return request;
}
NextNote::NextNote(QObject *parent) : AbstractNotesProvider(parent)
{
......@@ -24,7 +47,7 @@ NextNote::~NextNote()
void NextNote::getNote(const QString &id)
{
auto url = QString(NextNote::API+"%1, %2").replace("PROVIDER", this->m_provider).arg("notes/", id);
auto url = QString(NextNote::API+"%1%2").replace("PROVIDER", this->m_provider).arg("notes/", id);
QString concatenated = this->m_user + ":" + this->m_password;
QByteArray data = concatenated.toLocal8Bit().toBase64();
......@@ -50,7 +73,6 @@ void NextNote::sendNotes(QByteArray array)
void NextNote::getNotes()
{
//https://milo.h@aol.com:Corazon1corazon@free01.thegood.cloud/index.php/apps/notes/api/v0.2/notes
auto url = NextNote::formatUrl(this->m_user, this->m_password, this->m_provider)+"notes";
QString concatenated = this->m_user + ":" + this->m_password;
......@@ -59,7 +81,7 @@ void NextNote::getNotes()
QMap<QString, QString> header {{"Authorization", headerData.toLocal8Bit()}};
auto downloader = new FMH::Downloader;
const auto downloader = new FMH::Downloader;
connect(downloader, &FMH::Downloader::dataReady, [&, downloader = std::move(downloader)](QByteArray array)
{
emit this->notesReady(this->parseNotes(array));
......@@ -76,26 +98,10 @@ void NextNote::insertNote(const FMH::MODEL &note)
const auto url = QString(NextNote::API+"%1").replace("PROVIDER", this->m_provider).arg("notes");
QString concatenated = this->m_user + ":" + this->m_password;
QByteArray data = concatenated.toLocal8Bit().toBase64();
QString headerData = "Basic " + data;
QVariantMap headers
{
{"Authorization", headerData.toLocal8Bit()},
{QString::number(QNetworkRequest::ContentTypeHeader),"application/json"}
};
QNetworkRequest request;
request.setUrl(QUrl(url));
request.setHeader(QNetworkRequest::ContentTypeHeader,"application/json");
request.setRawHeader(QString("Authorization").toLocal8Bit(), headerData.toLocal8Bit());
const auto request = formRequest(url, this->m_user, this->m_password);
auto restclient = new QNetworkAccessManager; //constructor
QNetworkReply *reply = restclient->post(request,payload);
qDebug() << "AUTH >> " << concatenated << headerData;
connect(reply, &QNetworkReply::finished, [=, __note = note]()
{
qDebug() << "Note insertyion finished?";
......@@ -119,13 +125,52 @@ void NextNote::insertNote(const FMH::MODEL &note)
void NextNote::updateNote(const QString &id, const FMH::MODEL &note)
{
if(id.isEmpty() || note.isEmpty())
{
qWarning()<< "The id or note are empty. Can not proceed. NextNote::update";
return;
}
QByteArray payload = QJsonDocument::fromVariant(FM::toMap(FMH::filterModel(note, {FMH::MODEL_KEY::CONTENT,
FMH::MODEL_KEY::FAVORITE,
FMH::MODEL_KEY::MODIFIED,
FMH::MODEL_KEY::CATEGORY}))).toJson();
qDebug() << "UPDATING NOTE" << QVariant(payload).toString();
const auto url = QString(NextNote::API+"%1%2").replace("PROVIDER", this->m_provider).arg("notes/", id);
qDebug()<< "tryiong to update note" << url;
const auto request = formRequest(url, this->m_user, this->m_password);
auto restclient = new QNetworkAccessManager; //constructor
QNetworkReply *reply = restclient->put(request, payload);
connect(reply, &QNetworkReply::finished, [=, __note = note]()
{
qDebug() << "Note update finished?" << reply->errorString();
const auto notes = this->parseNotes(reply->readAll());
emit this->noteUpdated([&]() -> FMH::MODEL {
FMH::MODEL note;
if(notes.isEmpty())
return note;
note = notes.first();
note[FMH::MODEL_KEY::STAMP] = note[FMH::MODEL_KEY::ID]; //adds the id of the local note as a stamp
note[FMH::MODEL_KEY::ID] = __note[FMH::MODEL_KEY::ID]; //adds the id of the local note as a stamp
note[FMH::MODEL_KEY::SERVER] = this->m_provider; //adds the provider server address
note[FMH::MODEL_KEY::USER] = this->m_user; //adds the user name
return note;
}());
restclient->deleteLater();
});
}
void NextNote::removeNote(const QString &id)
{
}
QString NextNote::formatUrl(const QString &user, const QString &password, const QString &provider)
const QString NextNote::formatUrl(const QString &user, const QString &password, const QString &provider)
{
auto url = NextNote::API;
url.replace("USER", user);
......@@ -134,7 +179,7 @@ QString NextNote::formatUrl(const QString &user, const QString &password, const
return url;
}
FMH::MODEL_LIST NextNote::parseNotes(const QByteArray &array)
const FMH::MODEL_LIST NextNote::parseNotes(const QByteArray &array)
{
FMH::MODEL_LIST res;
qDebug()<< "trying to parse notes" << array;
......
......@@ -24,8 +24,8 @@ public:
private:
const static QString API;
static QString formatUrl(const QString &user, const QString &password, const QString &provider);
static FMH::MODEL_LIST parseNotes(const QByteArray &array);
static const QString formatUrl(const QString &user, const QString &password, const QString &provider);
static const FMH::MODEL_LIST parseNotes(const QByteArray &array);
// template<typename T>
// void request(const QString &url, const QMap<QString, QString> &header, T cb);
......
This diff is collapsed.
......@@ -42,7 +42,6 @@ class Syncer: public QObject
{
Q_OBJECT
public:
explicit Syncer(QObject *parent = nullptr);
......@@ -154,14 +153,7 @@ private:
*/
static void stampNote(FMH::MODEL &note);
/**
* @brief packNote
* packs the note to what is expected by the internal database,
* by only using the needed fields
* @param note
* @return
*/
static FMH::MODEL filterNote(const FMH::MODEL &note, const QVector<FMH::MODEL_KEY> &keys);
static const QString idFromStamp(DB *db, const QString &provider, const QString &stamp) ;
protected:
/**
......@@ -183,7 +175,7 @@ protected:
void insertRemote(FMH::MODEL &note);
void updateLocal(const QString &id, const FMH::MODEL &note);
bool updateLocal(const QString &id, const FMH::MODEL &note);
void updateRemote(const QString &id, const FMH::MODEL &note);
signals:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment