public static List<VoteLog> GetVoteLogsFromServer(string request, Method method) { var votes = new List<VoteLog>(); var results = Request(request, method); try { JArray ja = JsonConvert.DeserializeObject<JArray>(results); foreach (var r in ja) { var c = (VoteLogApi)JsonConvert.DeserializeObject(r.ToString(), typeof(VoteLogApi)); var p = new VoteLog(c); votes.Add(p); } } catch (Exception ex1) { Console.WriteLine(ex1.ToString()); try { JObject r = JsonConvert.DeserializeObject<JObject>(results); var c = (VoteLogApi)JsonConvert.DeserializeObject(r.ToString(), typeof(VoteLogApi)); var p = new VoteLog(c); votes.Add(p); } catch (Exception ex2) { Console.WriteLine(ex2.ToString()); } } return votes; }
public bool SendRating(int sectionId, string username, int autoId, int thisVote) { var poster = _db.Users.Where(sc => sc.Id == autoId).FirstOrDefault(); var voteCount = _db.VoteLogs.Where(vc => vc.VoteForId == autoId).Count(); if (poster != null) { int curvote = 0; if (voteCount > 0) curvote = (curvote*voteCount + thisVote)/(voteCount + 1); else if (voteCount == 0) curvote = thisVote; poster.Rating = curvote; _db.SaveChanges(); VoteLog vm = new VoteLog() { Active = true, SectionId = sectionId, UserName = username, Vote = thisVote, VoteForId = autoId }; _db.VoteLogs.Add(vm); _db.SaveChanges(); return true; } return false; }