public async Task<IHttpActionResult> Update(GameScore score) { //Cleanup. This should be a background job. var badRange = AppDatabase.Scores.Where(o => o.CreatedOn < DateTime.UtcNow.Subtract(new TimeSpan(30, 0, 0, 0))); AppDatabase.Scores.RemoveRange(badRange); await AppDatabase.SaveChangesAsync(); // Select old var model = await AppDatabase.Scores.OrderByDescending(o => o.Score).FirstOrDefaultAsync(o => o.UserId == UserId); //Update ? if (model != null ) { //Skip if less than if (model.Score > score.Score) { return Ok(); } model.Score = score.Score; } else { //Create AppDatabase.Scores.Add(score); } await AppDatabase.SaveChangesAsync(); return Ok(); }
/// <summary> /// Posts the score to the server /// </summary> /// <typeparam name="T"></typeparam> /// <param name="entity"></param> /// <returns></returns> public UnityTask Update(GameScore entity) { if (!IsAuthenticated) return UnityTask.FailedTask<GameScore>("Not authenticated"); return HttpPostAsync("Update", entity); }
/// <summary> /// Posts the score to the server /// </summary> /// <typeparam name="T"></typeparam> /// <param name="entity"></param> /// <returns></returns> public void Update(GameScore entity, Action<Response> callback) { if (!IsAuthenticated) { callback(new Response(new Exception("Not authenticated"))); return; } HttpPostAsync("Update", entity, callback); }