예제 #1
0
 partial void DeleteReview(Review instance);
예제 #2
0
        /// <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;
        }
예제 #3
0
 partial void UpdateReview(Review instance);
예제 #4
0
 partial void InsertReview(Review instance);
예제 #5
0
 private void detach_Reviews(Review entity)
 {
     this.SendPropertyChanging();
     entity.User_account = null;
 }
예제 #6
0
 private void detach_Reviews(Review entity)
 {
     this.SendPropertyChanging();
     entity.Media = null;
 }
예제 #7
0
 private void attach_Reviews(Review entity)
 {
     this.SendPropertyChanging();
     entity.Media = this;
 }