public async Task SubmitUserRating(Comment comment, int score) { IsPending = true; ErrorMessage = null; // Check if the user already rated the comment var existingRating = comment.UserRatings.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; } try { await userTable.UpdateAsync(existingRating); } catch (Exception e) { ErrorMessage = e.Message; } } // Create a new rating otherwise else{ var newRating = new UserRating() { Score = score, UniqueUser = GetDeviceId(), Id = Guid.NewGuid().ToString(), CommentId = comment.Id, }; // Update the comment comment.UserRatings.Add(newRating); try { await userTable.InsertAsync(newRating); } catch (Exception e) { ErrorMessage = e.Message; } } try { var updatedComment = await commentTable.LookupAsync(comment.Id); comment.Score = updatedComment.Score; comment.UserRatings = updatedComment.UserRatings; } 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<Comment> SubmitCommentAsync(String text, String name, PageEnum.CommentPage type) { IsPending = true; ErrorMessage = null; // Create the new comment var comment = new Comment() { Score = 0, Text = text, User = name, Page = type, ChampionFeedbackName = ChampionFeedback.Name, ChampionFeedbackId = ChampionFeedback.Id, Id = Guid.NewGuid().ToString(), }; try { this.ChampionFeedback.Comments.Add(comment); await commentTable.InsertAsync(comment); return comment; } catch (MobileServiceInvalidOperationException ex) { ErrorMessage = ex.Message; return null; } catch (HttpRequestException ex2) { ErrorMessage = ex2.Message; return null; } finally { IsPending = false; } }