示例#1
0
 private void AssignEntityToModel(UserProfileEntity userProfile, UserProfile dbUserProfile)
 {
     dbUserProfile.UserId = userProfile.UserId;
     dbUserProfile.UserName = userProfile.UserName;
 }
示例#2
0
        /// <summary>
        /// Saves the state of a recipe to persistent
        /// storage.
        /// </summary>
        /// <param name="recipeEntity">The recipe to persist
        /// in storage.</param>
        public void Save(RecipeEntity recipeEntity)
        {
            if (recipeEntity == null)
            {
                throw new ArgumentNullException("recipe",
                    "You must specify a recipe to save.");
            }

            if (recipeEntity.Style == null)
            {
                throw new InvalidOperationException(
                    "Recipes require a style.");
            }

            if (recipeEntity.Contributor == null)
            {
                throw new InvalidOperationException(
                    "Recipes require a contributor.");
            }

            // Load the style assigned to the recipe.  We load this because
            // it may change from whatthe current db model is.
            var existingStyleModel = new Style();
            AssignEntityToModel(recipeEntity.Style, existingStyleModel);

            this
                .Context
                .Styles
                .Attach(existingStyleModel);

            // If it's a new recipe we have to do a decent bit of work.
            if (recipeEntity.RecipeId == 0)
            {
                // Create the recipe.
                var newRecipeModel = new Recipe();
                // Add the recipe to the context for change tracking.
                this.Context.Recipes.Add(newRecipeModel);

                // Assumes the user already exists and the domain has validated
                // this is the user that created the model
                var existingUserModel = new UserProfile();
                AssignEntityToModel(recipeEntity.Contributor, existingUserModel);

                this
                    .Context
                    .UserProfiles
                    .Attach(existingUserModel);

                // Assign the properties that can only be assigned on creation.
                newRecipeModel.Style = existingStyleModel;
                newRecipeModel.Slug = recipeEntity.Slug;
                newRecipeModel.Contributor = existingUserModel;
                AssignEntityToModel(recipeEntity, newRecipeModel);
                this.Context.SaveChanges();

                recipeEntity.RecipeId = newRecipeModel.RecipeId;

                return;
            }

            var recipeModel = this
                .Context
                .Recipes
                .Include("Style")
                .Include("Contributor")
                .FirstOrDefault(r => r.RecipeId == recipeEntity.RecipeId);

            if (recipeModel == null)
            {
                throw new InvalidOperationException(
                    "The recipe being modified does not exist.");
            }

            AssignEntityToModel(recipeEntity, recipeModel);
            recipeModel.Style = existingStyleModel;

            this.Context.SaveChanges();
        }
示例#3
0
        public void SaveReview(ReviewEntity review)
        {
            // It's not a new review.  Update the value.
            var recipe = Context
                .Recipes
                .FirstOrDefault(r => r.RecipeId == review.RecipeId);

            if (recipe == null)
            {
                throw new InvalidOperationException(
                    "Cannot save review without a recipe.");
            }

            // It's a new review.
            if (review.ReviewId == 0)
            {
                var modelReview = new Review();
                AssignEntityToModel(review, modelReview);

                var userProfile = new UserProfile();
                userProfile.UserId = review.Reviewer.UserId;
                userProfile.UserName = review.Reviewer.UserName;

                this
                    .Context
                    .UserProfiles
                    .Attach(userProfile);

                modelReview.Reviewer = userProfile;
                recipe.Reviews.Add(modelReview);
                Context.SaveChanges();

                review.ReviewId = modelReview.ReviewId;

                return;
            }

            // Add the review to the recipe.
            var oldReviewModel = recipe
                .Reviews
                .FirstOrDefault(r => r.ReviewId == review.ReviewId);

            if (oldReviewModel == null)
            {
                throw new ArgumentOutOfRangeException("review",
                    "Cannot edit the review as it cannot be located.");
            }

            AssignEntityToModel(review, oldReviewModel);

            Context.SaveChanges();
        }