private async Task <bool> UpdateAsync() { var valor = false; byte[] imageByte = null; var strategy = _context.Database.CreateExecutionStrategy(); await strategy.ExecuteAsync(async() => { using (var transaction = _context.Database.BeginTransaction()) { try { var postData = _post.getTPost(Input.ID); if (postData.Count.Equals(0) || postData[0].ID.Equals(_dataPost2.ID)) { if (Input.AvatarImage == null) { imageByte = _dataPost2.Image; } else { imageByte = await _uploadimage.ByteAvatarImageAsync(Input.AvatarImage, _environment, ""); } var post = new TPosts { ID = _dataPost2.ID, Titulo = Input.Titulo, Categoria = Input.Categoria, FechaCreacion = DateTime.Now, Contenido = Input.Contenido, Image = imageByte, PostActivo = 1, }; _context.Update(post); _context.SaveChanges(); transaction.Commit(); valor = true; } else { _dataInput.ErrorMessage = $"El {Input.ID} ya esta registrado"; valor = false; } } catch (Exception ex) { _dataInput.ErrorMessage = ex.Message; transaction.Rollback(); valor = false; } } }); return(valor); }
private async Task <bool> SaveAsync() { _dataInput = Input; var valor = false; if (ModelState.IsValid) { var strategy = _context.Database.CreateExecutionStrategy(); await strategy.ExecuteAsync(async() => { using (var transaction = _context.Database.BeginTransaction()) { try { var imageByte = await _uploadimage.ByteAvatarImageAsync( Input.AvatarImage, _environment, "images/images/default.png"); var post = new TPosts { Titulo = Input.Titulo, Contenido = Input.Contenido, Image = imageByte, Categoria = Input.Categoria, FechaCreacion = DateTime.Now, PostActivo = 1, }; await _context.AddAsync(post); _context.SaveChanges(); transaction.Commit(); _dataInput = null; valor = true; } catch (Exception ex) { _dataInput.ErrorMessage = ex.Message; transaction.Rollback(); valor = false; } } }); } else { foreach (var modelState in ModelState.Values) { foreach (var error in modelState.Errors) { _dataInput.ErrorMessage += error.ErrorMessage; } } valor = false; } return(valor); }