]> Some of my projects - localmylist.git/commitdiff
RequestHandler can now set/update votes.
authorAPTX <marek321@gmail.com>
Mon, 15 Apr 2013 20:57:27 +0000 (22:57 +0200)
committerAPTX <marek321@gmail.com>
Mon, 15 Apr 2013 20:57:27 +0000 (22:57 +0200)
localmylist/database.cpp
localmylist/database.h
localmylist/requesthandler.cpp
localmylist/requesthandler.h

index 83e7b515e59c0aa4927ea9283b5c4cf2665bdc18..2c2b651cc4ec5b813de5bb8394d42c1f04bdd407 100644 (file)
@@ -421,6 +421,34 @@ Episode Database::getEpisode(int eid)
        return e;
 }
 
+Episode Database::getEpisode(int aid, int epno, const QString &type)
+{
+       Episode e;
+
+       QSqlQuery &q = prepare(
+       "SELECT eid, aid, entry_added, anidb_update, entry_update, my_update, epno, "
+       "               title_english, title_romaji, title_kanji, length, airdate, state, "
+       "               type, recap, rating, votes, my_vote, my_vote_date "
+       "       FROM episode "
+       "       WHERE aid = :aid "
+       "               AND epno = :epno "
+       "               AND type = :type ");
+
+       q.bindValue(":aid", aid);
+       q.bindValue(":epno", epno);
+       q.bindValue(":type", type.isNull() ? "" : type);
+
+       if (!exec(q))
+               return e;
+
+       if (q.next())
+               e = readEpisode(q);
+
+       q.finish();
+
+       return e;
+}
+
 File Database::getFile(int fid)
 {
        File f;
index 194a6da6ba798f9ce296a7f662c7015330c36cdc..58959d02901b8f6184c1f6c272dc57fa961de364 100644 (file)
@@ -65,6 +65,7 @@ public slots:
        LocalMyList::Anime getAnime(int aid);
        QList<LocalMyList::Episode> getEpisodes(int aid);
        LocalMyList::Episode getEpisode(int eid);
+       LocalMyList::Episode getEpisode(int aid, int epno, const QString &type = "");
        LocalMyList::File getFile(int fid);
        LocalMyList::File getFileByPath(const QString &path);
        LocalMyList::File getFileByTitle(const QString &title, int epno = 1);
index 27fac4432386f8489a095d71bad674e8f0e7b7a5..0a2524434fc81e49c4c73988ac72743f67c8f0eb 100644 (file)
@@ -138,6 +138,21 @@ void RequestHandler::handleMyListUpdates()
 
        foreach (const PendingMyListUpdate &request, requests)
        {
+               if (request.setVote)
+               {
+                       VoteCommand cmd;
+
+                       cmd.setVoteType(VoteCommand::AnimeVote);
+                       cmd.setId(request.aid);
+                       cmd.setEpno(request.epno);
+                       cmd.setValue(int(request.vote * 100));
+
+                       VoteReply *reply = Client::instance()->send(cmd);
+                       connect(reply, SIGNAL(replyReady(bool)), this, SLOT(myListUpdateVoteReplyRecieved(bool)));
+                       myListUpdateVoteIdMap.insert(reply, request.updateId);
+                       continue;
+               }
+
                MyListAddCommand cmd;
 
                cmd.setEdit(true);
@@ -597,4 +612,48 @@ void RequestHandler::myListEditReplyRecieved(bool success)
        t.commit();
 }
 
+void RequestHandler::myListUpdateVoteReplyRecieved(bool success)
+{
+       using namespace ::AniDBUdpClient;
+
+       VoteReply *reply = qobject_cast<VoteReply *>(sender());
+
+       Q_ASSERT(reply);
+       Q_ASSERT(myListUpdateVoteIdMap.contains(reply));
+       reply->deleteLater();
+
+       qint64 id = myListUpdateVoteIdMap.take(reply);
+
+       if (!success)
+               return;
+
+       RaiiTransaction t(db);
+
+       PendingMyListUpdate request = db->getPendingMyListUpdate(id);
+
+       if (!request.updateId)
+       {
+               qWarning("PendingMyListUpdate not in DB");
+               return;
+       }
+
+       if (request.epno == 0)
+       {
+               Anime anime = db->getAnime(request.aid);
+
+               anime.myVote = request.vote;
+               anime.myVoteDate = QDateTime::currentDateTime();
+
+               db->setAnime(anime);
+               return;
+       }
+
+       Episode episode = db->getEpisode(request.aid, request.epno);
+
+       episode.myVote = request.vote;
+       episode.myVoteDate = QDateTime::currentDateTime();
+
+       db->setEpisode(episode);
+}
+
 } // namespace LocalMyList
index d8e257477a45ff012ef71912646f72db6ebbe773..540118c40dfe2742b7d376ca4945fc9c864472f9 100644 (file)
@@ -40,12 +40,13 @@ private slots:
        void voteRequestComplete(bool);
        void myListAddReplyRecieved(bool success);
        void myListEditReplyRecieved(bool success);
-
+       void myListUpdateVoteReplyRecieved(bool success);
 private:
        Database *db;
 
        QMap< ::AniDBUdpClient::VoteReply *, int> idMap;
        QMap< ::AniDBUdpClient::MyListAddReply *, qint64> myListUpdateIdMap;
+       QMap< ::AniDBUdpClient::VoteReply *, qint64> myListUpdateVoteIdMap;
 };
 
 } // namespace LocalMyList