Пример #1
0
        public async Task UpdateRecipe(CreateRecipeViewModel model, IPrincipal user)
        {
            var unitOfWork = OwinContext.Get <UnitOfWork>();

            // retrieve model from database
            var recipe = await unitOfWork.Recipes.GetById(model.Id);

            if (recipe == null)
            {
                throw new ObjectNotFoundException("The entry you are looking for doesn't exist in our database.");
            }

            // update model
            recipe.Title       = model.Title;
            recipe.Description = model.Description;
            if (model.PictureFile != null)
            {
                var fileStorageService = new LocalFileStorageService();
                var picturePath        = fileStorageService.StoreFile(model.PictureFile);
                model.Picture = picturePath;
            }

            // save changes of modified model
            await unitOfWork.SaveChangesAsync();
        }
Пример #2
0
        public async Task CreateNewRecipe(CreateRecipeViewModel model, IPrincipal user)
        {
            var fileStorageService = new LocalFileStorageService();
            var picturePath        = fileStorageService.StoreFile(model.PictureFile);

            // create model
            var recipe = new Recipe()
            {
                Title       = model.Title,
                Description = model.Description,
                Picture     = picturePath,
                AuthorId    = user.Identity.GetUserId()
            };

            // insert model in the database
            await UnitOfWork.Recipes.Add(recipe);

            // save changes
            await UnitOfWork.SaveChangesAsync();
        }
Пример #3
0
        public async Task UpdateRecipe(CreateRecipeViewModel model, IPrincipal user)
        {
            // retrieve model from database
            var recipe = await UnitOfWork.Recipes.GetById(model.Id);

            if (recipe == null)
            {
                throw new EntityNotFoundException <Recipe, int>(model.Id, typeof(Recipe));
            }

            // update model
            recipe.Title       = model.Title;
            recipe.Description = model.Description;
            if (model.PictureFile != null)
            {
                var fileStorageService = new LocalFileStorageService();
                var picturePath        = fileStorageService.StoreFile(model.PictureFile);
                recipe.Picture = picturePath;
            }

            // save changes of modified model
            await UnitOfWork.SaveChangesAsync();
        }