Example #1
0
        public int EditCategory(CategoryVM model)
        {
            var entity = _db.Categories.FirstOrDefault(c => c.CategoryId == model.CategoryId);

            if (entity != null)
            {
                _mapper.Map(model, entity);

                // trim file to convert to base64
                if (!string.IsNullOrEmpty(model.ImageBase64))
                {
                    var    base64     = model.ImageBase64.Substring(model.ImageBase64.LastIndexOf(',') + 1);
                    byte[] imageBytes = Convert.FromBase64String(base64);

                    using (var stream = new MemoryStream(imageBytes))
                    {
                        IFormFile file         = new FormFile(stream, 0, imageBytes.Length, "", "");
                        var       fileNameTask = _azureBlobService.UploadSingleAsync(file, imageContainerName);
                        entity.ImageUrl = fileNameTask.Result;
                    }
                    _db.Categories.Update(entity);
                    _db.SaveChanges();
                }
            }
            return(entity.CategoryId);
        }
Example #2
0
        public Account Create(Account account, string password)
        {
            if (String.IsNullOrEmpty(password))
            {
                throw new ApplicationException("Password is Required");
            }
            if (_db.Accounts.Any(a => a.Email == account.Email))
            {
                throw new ApplicationException($"Username {account.Email} is already taken");
            }

            var entity = new Account();

            entity.Email = account.Email;

            //create password hash
            byte[] passwordHash, passwordSalt;
            CreatePasswordHash(password, out passwordHash, out passwordSalt);
            entity.PasswordHash = passwordHash;
            entity.PasswordSalt = passwordSalt;

            _db.Accounts.Add(entity);
            _db.SaveChanges();

            return(entity);
        }
Example #3
0
        public int CreateNewRecipe(RecipeVM recipe)
        {
            try
            {
                var entity = new Recipe();
                entity.CategoryId           = recipe.CategoryId;
                entity.Name                 = recipe.Name;
                entity.DescriptionPrimary   = recipe.DescriptionPrimary;
                entity.DescriptionSecondary = recipe.DescriptionSecondary;
                entity.Ingredients          = new List <Ingredient>();
                for (int i = 0; i < recipe.Ingredients.Count; i++)
                {
                    var currentIngredient = recipe.Ingredients[i];
                    if (!string.IsNullOrEmpty(currentIngredient.Name))
                    {
                        var ingredient = new Ingredient();
                        _mapper.Map(currentIngredient, ingredient);
                        ingredient.PositionNo = i + 1;
                        entity.Ingredients.Add(ingredient);
                    }
                }
                entity.MethodItems = new List <MethodItem>();
                for (int i = 0; i < recipe.MethodItems.Count; i++)
                {
                    var currentMethodItem = recipe.MethodItems[i];
                    if (!string.IsNullOrEmpty(currentMethodItem.Text))
                    {
                        var methodItem = new MethodItem();
                        _mapper.Map(currentMethodItem, methodItem);
                        methodItem.StepNo = i + 1;
                        entity.MethodItems.Add(methodItem);
                    }
                }

                // trim file to convert to base64
                if (!string.IsNullOrEmpty(recipe.ImageFile))
                {
                    var    base64     = recipe.ImageFile.Substring(recipe.ImageFile.LastIndexOf(',') + 1);
                    byte[] imageBytes = Convert.FromBase64String(base64);

                    using (var stream = new MemoryStream(imageBytes))
                    {
                        IFormFile file         = new FormFile(stream, 0, imageBytes.Length, "", "");
                        var       fileNameTask = _azureBlobService.UploadSingleAsync(file, imageContainerName);
                        entity.ImageUrl = fileNameTask.Result;
                    }
                }

                //add image
                // will be receiving a base64 byte array from front end
                //var file = recipe.Image;
                //_azureBlobService.UploadSingleAsync()
                _db.Recipes.Add(entity);
                _db.SaveChanges();
                return(entity.RecipeId);
            }
            catch (Exception e)
            {
                var xx = e;
                return(0);
            }
        }