public RecipeDisplayViewModel EntityToViewModel(RecipeEntity entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException(
                    "entity",
                    "Cannot convert an entity to a view model when no entity is passed.");
            }

            bool canEdit = false;

            if (HttpContext.Current != null
                && HttpContext.Current.Request.IsAuthenticated)
            {
                canEdit = WebSecurity.CurrentUserId == entity.Contributor.UserId;
            }

            return new RecipeDisplayViewModel
            {
                RecipeId = entity.RecipeId,
                Name = entity.Name,
                Category = entity.Style.Category.ToString(),
                Style = entity.Style.Name,
                OriginalGravity = entity.OriginalGravity,
                FinalGravity = entity.FinalGravity,
                PercentAlcoholByVolume = entity.PercentAlcoholByVolume,
                GrainBill = entity.GrainBill,
                Instructions = entity.Instructions,
                Slug = entity.Slug,
                StyleSlug = entity.Style.Slug,
                ContributedBy = entity.Contributor.UserName,
                CanEdit = canEdit
            };
        }
        public RecipeEditViewModel EntityToViewModel(RecipeEntity entity)
        {
            var editModel = new RecipeEditViewModel();

            var styles = this._styleRepository.GetStyles();

            editModel.StyleList = new SelectList(
                styles,
                "StyleId",
                "Name",
                0);

            if (entity != null)
            {
                editModel.FinalGravity = entity.FinalGravity;
                editModel.GrainBill = entity.GrainBill;
                editModel.Instructions = entity.Instructions;
                editModel.Name = entity.Name;
                editModel.OriginalGravity = entity.OriginalGravity;
                editModel.RecipeId = entity.RecipeId;
                editModel.StyleId = entity.Style.StyleId;
                editModel.Slug = entity.Slug;
            }

            return editModel;
        }
        /// <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();
        }
 /// <summary>
 /// Assigns the values of a Recipe entity's 
 /// properties to the properties of a Recipe
 /// data model.
 /// </summary>
 /// <param name="recipe">The Recipe entity
 /// containing the property values to assign.
 /// </param>
 /// <param name="dbRecipe">The Recipe data
 /// model to receive the property values.
 /// </param>
 private void AssignEntityToModel(RecipeEntity recipe, Recipe dbRecipe)
 {
     dbRecipe.FinalGravity = recipe.FinalGravity;
     dbRecipe.GrainBill = recipe.GrainBill;
     dbRecipe.Instructions = recipe.Instructions;
     dbRecipe.Name = recipe.Name;
     dbRecipe.OriginalGravity = recipe.OriginalGravity;
     dbRecipe.RecipeId = recipe.RecipeId;
 }
        private bool CanEdit(RecipeEntity entity)
        {
            if (Request.IsAuthenticated)
            {
                return WebSecurity.CurrentUserId == entity.Contributor.UserId;
            }

            return false;
        }