From: APTX Date: Mon, 15 Apr 2013 20:57:27 +0000 (+0200) Subject: RequestHandler can now set/update votes. X-Git-Url: https://gitweb.aptx.org/?a=commitdiff_plain;h=81e97884e985295039306c38497d1c5381e0f3f3;p=localmylist.git RequestHandler can now set/update votes. --- diff --git a/localmylist/database.cpp b/localmylist/database.cpp index 83e7b51..2c2b651 100644 --- a/localmylist/database.cpp +++ b/localmylist/database.cpp @@ -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; diff --git a/localmylist/database.h b/localmylist/database.h index 194a6da..58959d0 100644 --- a/localmylist/database.h +++ b/localmylist/database.h @@ -65,6 +65,7 @@ public slots: LocalMyList::Anime getAnime(int aid); QList 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); diff --git a/localmylist/requesthandler.cpp b/localmylist/requesthandler.cpp index 27fac44..0a25244 100644 --- a/localmylist/requesthandler.cpp +++ b/localmylist/requesthandler.cpp @@ -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(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 diff --git a/localmylist/requesthandler.h b/localmylist/requesthandler.h index d8e2574..540118c 100644 --- a/localmylist/requesthandler.h +++ b/localmylist/requesthandler.h @@ -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