private static Message VoteToBan(InteractiveMessage slackMessage) { var vote = Vote.Create(slackMessage); var voted = Repository.Get <Vote>().FirstOrDefault(v => v.Voteban.Id == vote.Voteban.Id && v.UserId == vote.UserId); if (voted != null) { voted.Update(vote); voted.Save(); } else { vote.Save(); } return(VotebanController.FormatMessage(vote.Voteban)); }
public static async Task LoadData(bool resetDb = false, bool reloadFiles = false) { DownloadFiles(reloadFiles); using (var dbContext = new VoteDbContext()) { if (resetDb) { await dbContext.Database.EnsureDeletedAsync(); } await dbContext.Database.EnsureCreatedAsync(); Console.WriteLine("Loading data"); foreach (var filePath in Directory.GetFiles($"{AppDomain.CurrentDomain.BaseDirectory}\\nyt-data")) { var state = filePath.Substring(filePath.LastIndexOf('\\') + 1).Replace(".json", string.Empty); state = state[0].ToString().ToUpper() + state.Substring(1); if (!await dbContext.States.AnyAsync(s => s.StateName == state)) { dbContext.States.Add(State.Create(state, Swingers.Contains(state), DateTime.UtcNow)); } if (!await dbContext.Votes.AnyAsync(s => s.StateName == state)) { Console.WriteLine($" Loading {state}..."); var votes = JObject.Parse(await File.ReadAllTextAsync(filePath)); var timeseries = votes?["data"]?["races"]?.FirstOrDefault()?["timeseries"]; if (timeseries == null) { Console.WriteLine($"WARNING: {state} voting data is null!"); continue; } var typedSeries = timeseries.Select(s => (VoteTimeSeries)s.ToObject(typeof(VoteTimeSeries))).OrderBy(s => s.Timestamp).ToArray(); Console.WriteLine($" There are {typedSeries.Length} items for {state}"); typedSeries = typedSeries.Select((ts, i) => i > 0 ? ts.SetPrevious(typedSeries[i - 1]) : ts).ToArray(); foreach (var ts in typedSeries) { dbContext.Votes.Add(Vote.Create(state, ts)); } } await dbContext.SaveChangesAsync(); } } }
public JObject Add(JObject data) { try { DBServer dbserver = new SqlServer(); Vote model = new Vote(dbserver); data.Remove("ID"); data.Add(new JProperty("ID", GUID.NewGuid())); string nvcElectiveNum = data["nvcElectiveNum"].ToString().Replace("\"", "").Trim(); string nvcStuNo = data["nvcStuNo"].ToString().Replace("\"", "").Trim(); string cmdText = string.Format("select Count(*) from V_Evaluate_Vote where nvcElectiveNum='{0}' and nvcStuNo='{1}'", nvcElectiveNum, nvcStuNo); DBServer server = new SqlServer(); object o = dbserver.ExecuteScalar(cmdText); if (Convert.ToInt32(o) != 0) { return(ShowExecuteError("你已经为该老师投过票!")); } else { long count = GetVotedCount(nvcStuNo); if (model.Create(data)) { string msg = string.Format("投票成功!(总共3票,还有{0}票)", 2 - count); return(ShowSuccess(msg)); } else { return(ShowExecuteError("保存失败.")); } } } catch (Exception ex) { return(ShowExecuteError(ex.Message)); } }