public async Task SubmitCounterCommentRating(CounterComment comment, int score) { IsPending = true; ErrorMessage = null; // Check if the user already rated the counter comment var existingRating = comment.CounterCommentRatings.Where(x => x.UniqueUser == GetDeviceId()).FirstOrDefault(); // If already rated, update to the new score if (existingRating != null) { // Case for pressing the opposite vote button -- change rating to it if (existingRating.Score != score) { existingRating.Score = score; } // Case for pressing the same vote button -- simply a score of 0 else { existingRating.Score = 0; } // Update the rating in database try { await counterCommentRatingTable.UpdateAsync(existingRating); } catch (Exception e) { ErrorMessage = e.Message; } } // Create a new rating otherwise else { var newRating = new CounterCommentRating() { Score = score, UniqueUser = GetDeviceId(), Id = Guid.NewGuid().ToString(), CounterCommentId = comment.Id, }; // Update the counter comment comment.CounterCommentRatings.Add(newRating); // Insert rating into database try { await counterCommentRatingTable.InsertAsync(newRating); } catch (Exception e) { ErrorMessage = e.Message; } } try { var updatedCounterComment = await counterCommentTable.LookupAsync(comment.Id); comment.Score = updatedCounterComment.Score; comment.CounterCommentRatings = updatedCounterComment.CounterCommentRatings; } catch (MobileServicePreconditionFailedException ex) { ErrorMessage = ex.Message; } // Server conflict catch (MobileServiceInvalidOperationException ex1) { ErrorMessage = ex1.Message; } catch (HttpRequestException ex2) { ErrorMessage = ex2.Message; } finally { IsPending = false; } }
public async Task<CounterComment> SubmitCounterCommentAsync(String text, String name, Counter counter) { IsPending = true; ErrorMessage = null; // Create the new comment var comment = new CounterComment() { Score = 0, Text = text, User = name, ChampionFeedbackName = counter.ChampionFeedbackName, CounterId = counter.Id, Id = Guid.NewGuid().ToString(), CounterName = counter.Name }; try { counter.CounterComments.Add(comment); await counterCommentTable.InsertAsync(comment); return comment; } catch (MobileServiceInvalidOperationException ex) { ErrorMessage = ex.Message; return null; } catch (HttpRequestException ex2) { ErrorMessage = ex2.Message; return null; } finally { IsPending = false; } }