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); }
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); } }
public async Task <IActionResult> Edit(AccountVM account, IFormFile ImageFile) { var message = new Message(); if (!ModelState.IsValid) { message.Text = "Please check your form details"; message.MessageType = MessageType.Warning; return(BadRequest()); } else { var x = _db.Accounts.Include(a => a.AccountPermissions).FirstOrDefault(a => a.AccountId == account.AccountId); var entity = _db.Accounts.Find(account.AccountId); var hasher = new PasswordHasher(); if (entity == null) { entity = _mapper.Map <Account>(account); //update file url var fileName = await _azureBlobService.UploadSingleAsync(account.UserImage, "assets"); entity.UserImageUrl = fileName; if (!string.IsNullOrEmpty(account.Password)) { var hashedPassword = hasher.HashPassword(account.Password); entity.Password = hashedPassword; } entity.CreatedAt = DateTime.Now; _db.Add(entity); _db.SaveChanges(); account.AccountId = entity.AccountId; account.PermissionIds.ForEach(p => { var permissionId = int.Parse(p); var accountPermissionEntity = new AccountPermission(); accountPermissionEntity.PermissionId = permissionId; accountPermissionEntity.AccountId = entity.AccountId; _db.AccountPermissions.Add(accountPermissionEntity); }); message.Text = "Account has been created."; message.MessageType = MessageType.Success; } else { _db.AccountPermissions.RemoveRange(entity.AccountPermissions); entity.FirstName = account.FirstName; entity.LastName = account.LastName; entity.Email = account.Email; //update file url var fileName = await _azureBlobService.UploadSingleAsync(account.UserImage, "assets"); entity.UserImageUrl = fileName; //entity = _mapper.Map<Account>(account); if (!string.IsNullOrEmpty(account.Password)) { var hashedPassword = hasher.HashPassword(account.Password); entity.Password = hashedPassword; var verifyPassword = hasher.VerifyHashedPassword(hashedPassword, account.Password); } account.PermissionIds.ForEach(p => { var permissionId = int.Parse(p); var accountPermissionEntity = new AccountPermission(); accountPermissionEntity.PermissionId = permissionId; accountPermissionEntity.AccountId = entity.AccountId; _db.AccountPermissions.Add(accountPermissionEntity); _db.SaveChanges(); }); _db.Update(entity); _db.SaveChanges(); message.Text = "Account has been updated."; message.MessageType = MessageType.Success; } _db.SaveChanges(); } account.SelectList = GenerateSelectLists(); return(RedirectToAction("Edit", new { id = account.AccountId, message = message.Text, messageType = message.MessageType.ToString() })); }