public async Task <SaveRating.Response> Post([FromBody] SaveRating.Command command) { var newRating = new DailyRating { RatingDate = command.Date, MetricId = command.MetricId, Rating = command.Rating, Notes = command.Notes }; try { await _dbContext.DailyRatings.AddAsync(newRating); _dbContext.SaveChanges(); var newDay = RatingData.GetRatingDay(_dbContext, command.Date); return(new SaveRating.Response { DidPost = true, NewDay = newDay }); } catch (Exception ex) { return(new SaveRating.Response { DidPost = false, NewDay = null }); } }
public List <DailyRating> MergeDailyRatings(int[] sourcePlayerIds, int targetPlayerRatingId) { var mergedDailyRatings = new List <DailyRating>(); if (sourcePlayerIds.Length > 0) { // Get daily ratings for source players _context.Database.SetCommandTimeout(120); var dailys = _context.DailyRatings.Where(d => sourcePlayerIds.Contains(d.PlayerRating.PlayerId)).ToList(); // nothing to merge if (!dailys.Any()) { return(mergedDailyRatings); } // get list of distinct dates var dateList = ( from row in dailys.AsEnumerable() select row.Date ).Distinct(); // for each distinct date, average the dailys and store in target player foreach (var dt in dateList) { // Calculate the new dailys for each date var selectedRows = dailys.Where(d => d.Date == dt).ToList(); var mergedDaily = CalculateMergedDailyRating(selectedRows); var mergedReliability = selectedRows.Max(row => (float)row.Reliability); // Create the new rows TODO: hardcoded alg type var newRow = new DailyRating() { PlayerRatingId = (int)targetPlayerRatingId, Rating = double.IsNaN(mergedDaily) ? 0 : mergedDaily, Reliability = float.IsNaN(mergedReliability) ? 0 : mergedReliability, Algorithm = "V3_Singles", Date = dt }; mergedDailyRatings.Add(newRow); } _context.DailyRatings.AddRange(mergedDailyRatings); // delete the old rows _context.DailyRatings.RemoveRange(dailys); _context.SaveChanges(); } return(mergedDailyRatings); }