/// <author>Kenneth Søhrmann</author> public bool SubmitReview(MediaReview review, AccountCredentials credentials) { // Validate the credentials. ValidateCredentials(credentials); // Check if the user name of the credentials match the one of the review. if (!credentials.UserName.Equals(review.UserName)) { throw new FaultException<ArgumentException>( new ArgumentException("The user name specified in review does not match that of the credentials.")); } DatabaseDataContext db; try { db = new DatabaseDataContext(); } catch (Exception e) { throw new FaultException<Exception>( new Exception("1. An internal error has occured. This is not related to the input. " + e.Message)); } // Get the media instance reviewed. RentItDatabase.Media media; try { media = (from m in db.Medias where m.id.Equals(review.MediaId) select m).Single(); } catch (Exception) { throw new FaultException<ArgumentException>( new ArgumentException("The specified media id does not exist.")); } // Get the account of the reviewer. RentItDatabase.User_account userAccount; try { userAccount = (from u in db.User_accounts where u.user_name.Equals(review.UserName) select u).Single(); } catch (Exception) { throw new FaultException<ArgumentException>( new ArgumentException("Only user accounts can add reviews. If it fails with a user account, an internal error has occured.")); } try { // Create a new database-entry of the submitted review. var dbReview = new RentItDatabase.Review { Media = media, review1 = review.ReviewText, rating = Util.ValueOfRating(review.Rating), timestamp = review.Timestamp, User_account = userAccount, }; db.Reviews.InsertOnSubmit(dbReview); // Calculate new average rating and rating count. double avgRating = media.Rating.avg_rating; int numOfRatings = media.Rating.ratings_count; media.Rating.avg_rating = ((avgRating * numOfRatings) + Util.ValueOfRating(review.Rating)) / (numOfRatings + 1.0); media.Rating.ratings_count = numOfRatings + 1; db.SubmitChanges(); } catch (Exception e) { throw new FaultException<Exception>( new Exception("2 An internal error has occured. This is not related to the input. " + e.Message)); } return true; }
/// <summary> /// Update the ListView with new data. /// When this method is called, all data previously added to the list /// will be disregarded, and the contents of the ListView will match /// the media items passed in the parameter. /// </summary> /// <param name="reviews"> /// The list of reviews to be added to the list. /// </param> internal void UpdateListContents(MediaReview[] reviews) { this.BeginUpdate(); this.CurrentItems = new List<ListViewItem>(); foreach (var review in reviews) { string reviewText = review.ReviewText.Length > 70 ? review.ReviewText.Substring(0, 70) + " ..." : review.ReviewText; var item = new ListViewItem(review.UserName); item.SubItems.Add(review.Timestamp.Date.ToString("dd/MM/yyyy HH:mm")); item.SubItems.Add(reviewText); item.SubItems.Add(review.Rating.ToString()); item.Tag = review; this.CurrentItems.Add(item); } this.RePopulateList(); this.EndUpdate(); }
public void SubmitReviewExceptionTest() { RentItClient target = new RentItClient(); MediaReview review = new MediaReview { MediaId = 64, Rating = Rating.Three, ReviewText = "Bucket.", Timestamp = DateTime.Now, UserName = "******" }; AccountCredentials credentials = new AccountCredentials { UserName = "******", HashedPassword = "******" }; target.SubmitReview(review, credentials); target.SubmitReview(review, credentials); }
public void SubmitReviewTest() { RentItClient target = new RentItClient(); const string User = "******"; const int MediaId = 79; MediaReview review = new MediaReview { MediaId = MediaId, Rating = Rating.Three, ReviewText = "Bucketboy, yes.", Timestamp = DateTime.Now, UserName = User }; AccountCredentials credentials = new AccountCredentials { UserName = User, HashedPassword = "******" }; bool actual = target.SubmitReview(review, credentials); Assert.IsTrue(actual); this.SubmitReviewCleanup(User, MediaId); }
private void SubmitReviewButtonClick(object sender, EventArgs e) { if (Credentials == null) return; Rating rating; switch (RatingSelector.SelectedIndex) { case 0: rating = RentIt.Rating.One; break; case 1: rating = RentIt.Rating.Two; break; case 2: rating = RentIt.Rating.Three; break; case 3: rating = RentIt.Rating.Four; break; case 4: rating = RentIt.Rating.Five; break; default: rating = RentIt.Rating.Three; break; } var review = new MediaReview { MediaId = Media.Id, Rating = rating, ReviewText = ReviewText.Text, Timestamp = DateTime.Now, UserName = Credentials.UserName }; // submit review Cursor.Current = Cursors.WaitCursor; try { RentItProxy.SubmitReview(review, Credentials); } catch (FaultException) { // if review was already submitted by this user RentItMessageBox.AlreadyReviewedItem(); return; } // reload list ReloadList(); Cursor.Current = Cursors.Default; }