public static void AddSeriesVote(HttpContext context, SVR_AnimeSeries ser, int userID, Vote vote) { int voteType = (vote.Type?.ToLowerInvariant() ?? "") switch { "temporary" => (int)AniDBVoteType.AnimeTemp, "permanent" => (int)AniDBVoteType.Anime, _ => ser.GetAnime()?.GetFinishedAiring() ?? false ? (int)AniDBVoteType.Anime : (int)AniDBVoteType.AnimeTemp, }; AniDB_Vote dbVote = RepoFactory.AniDB_Vote.GetByEntityAndType(ser.AniDB_ID, AniDBVoteType.AnimeTemp) ?? RepoFactory.AniDB_Vote.GetByEntityAndType(ser.AniDB_ID, AniDBVoteType.Anime); if (dbVote == null) { dbVote = new AniDB_Vote { EntityID = ser.AniDB_ID, }; } dbVote.VoteValue = (int)Math.Floor(vote.GetRating(1000)); dbVote.VoteType = voteType; RepoFactory.AniDB_Vote.Save(dbVote); var cmdVote = new CommandRequest_VoteAnime(ser.AniDB_ID, voteType, vote.GetRating()); cmdVote.Save(); }
public System.IO.Stream VoteAnime(string userid, string objectid, string votevalue, string votetype) { Respond rsp = new Respond(); rsp.code = 500; int objid = 0; int usid = 0; int vt = 0; double vvalue = 0; if (!int.TryParse(objectid, out objid)) { return(KodiHelper.GetStreamFromXmlObject(rsp)); } if (!int.TryParse(userid, out usid)) { return(KodiHelper.GetStreamFromXmlObject(rsp)); } if (!int.TryParse(votetype, out vt)) { return(KodiHelper.GetStreamFromXmlObject(rsp)); } if (!double.TryParse(votevalue, NumberStyles.Any, CultureInfo.InvariantCulture, out vvalue)) { return(KodiHelper.GetStreamFromXmlObject(rsp)); } using (var session = JMMService.SessionFactory.OpenSession()) { if (vt == (int)enAniDBVoteType.Episode) { AnimeEpisodeRepository repEpisodes = new AnimeEpisodeRepository(); AnimeEpisode ep = repEpisodes.GetByID(session, objid); AniDB_Anime anime = ep.GetAnimeSeries().GetAnime(); if (anime == null) { rsp.code = 404; return(KodiHelper.GetStreamFromXmlObject(rsp)); } string msg = string.Format("Voting for anime episode: {0} - Value: {1}", ep.AnimeEpisodeID, vvalue); logger.Info(msg); // lets save to the database and assume it will work AniDB_VoteRepository repVotes = new AniDB_VoteRepository(); List <AniDB_Vote> dbVotes = repVotes.GetByEntity(ep.AnimeEpisodeID); AniDB_Vote thisVote = null; foreach (AniDB_Vote dbVote in dbVotes) { if (dbVote.VoteType == (int)enAniDBVoteType.Episode) { thisVote = dbVote; } } if (thisVote == null) { thisVote = new AniDB_Vote(); thisVote.EntityID = ep.AnimeEpisodeID; } thisVote.VoteType = vt; int iVoteValue = 0; if (vvalue > 0) { iVoteValue = (int)(vvalue * 100); } else { iVoteValue = (int)vvalue; } msg = string.Format("Voting for anime episode Formatted: {0} - Value: {1}", ep.AnimeEpisodeID, iVoteValue); logger.Info(msg); thisVote.VoteValue = iVoteValue; repVotes.Save(thisVote); CommandRequest_VoteAnime cmdVote = new CommandRequest_VoteAnime(anime.AnimeID, vt, Convert.ToDecimal(vvalue)); cmdVote.Save(); } if (vt == (int)enAniDBVoteType.Anime) { AnimeSeriesRepository repSeries = new AnimeSeriesRepository(); AnimeSeries ser = repSeries.GetByID(session, objid); AniDB_Anime anime = ser.GetAnime(); if (anime == null) { rsp.code = 404; return(KodiHelper.GetStreamFromXmlObject(rsp)); } string msg = string.Format("Voting for anime: {0} - Value: {1}", anime.AnimeID, vvalue); logger.Info(msg); // lets save to the database and assume it will work AniDB_VoteRepository repVotes = new AniDB_VoteRepository(); List <AniDB_Vote> dbVotes = repVotes.GetByEntity(anime.AnimeID); AniDB_Vote thisVote = null; foreach (AniDB_Vote dbVote in dbVotes) { // we can only have anime permanent or anime temp but not both if (vt == (int)enAniDBVoteType.Anime || vt == (int)enAniDBVoteType.AnimeTemp) { if (dbVote.VoteType == (int)enAniDBVoteType.Anime || dbVote.VoteType == (int)enAniDBVoteType.AnimeTemp) { thisVote = dbVote; } } else { thisVote = dbVote; } } if (thisVote == null) { thisVote = new AniDB_Vote(); thisVote.EntityID = anime.AnimeID; } thisVote.VoteType = vt; int iVoteValue = 0; if (vvalue > 0) { iVoteValue = (int)(vvalue * 100); } else { iVoteValue = (int)vvalue; } msg = string.Format("Voting for anime Formatted: {0} - Value: {1}", anime.AnimeID, iVoteValue); logger.Info(msg); thisVote.VoteValue = iVoteValue; repVotes.Save(thisVote); CommandRequest_VoteAnime cmdVote = new CommandRequest_VoteAnime(anime.AnimeID, vt, Convert.ToDecimal(vvalue)); cmdVote.Save(); } rsp.code = 200; return(KodiHelper.GetStreamFromXmlObject(rsp)); } }