public IHttpActionResult UpdateUser([FromUri] int UserId, [FromBody] User User) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (UserId != User.Id) { return(BadRequest()); } SQLContext.Entry(User).State = EntityState.Modified; try { SQLContext.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!UserExists(UserId)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public void Delete(int id) { TEntity entityToDelete = _collection.Find(id); if (_context.Entry(entityToDelete).State == EntityState.Detached) { _collection.Attach(entityToDelete); } _collection.Remove(entityToDelete); }
private Media TreatMediaCreation(string Name, string FileExtension, int UserId, bool IsUserMedia) { var MediaType = SQLContext.TypeMedias.Where(x => x.Name == FileExtension).FirstOrDefault(); if (MediaType == default) { MediaType = new TypeMedia { Name = FileExtension }; SQLContext.TypeMedias.Add(MediaType); SQLContext.SaveChanges(); } var NewMedia = new Media { URL = Url.Content(Path.Combine("~/Media_Data", Name)) }; SQLContext.Medias.Add(NewMedia); SQLContext.SaveChanges(); if (IsUserMedia) { var User = SQLContext.Users.Find(UserId); User.MediaId = NewMedia.Id; SQLContext.Entry(User).State = System.Data.Entity.EntityState.Modified; } SQLContext.SaveChanges(); return(NewMedia); }
/// <summary> /// Método padrão para Atualizar entidade no BD /// </summary> /// <param name="obj"></param> /// <returns></returns> public virtual async Task <TEntity> Update(TEntity obj) { sqlContext.Entry(obj).State = Microsoft.EntityFrameworkCore.EntityState.Modified; await SaveChanges(); return(obj); }
public void Update(T obj) { using (var data = new SQLContext(_OptionsBuilder)) { data.Entry(obj).State = Microsoft.EntityFrameworkCore.EntityState.Modified; data.SaveChanges(); } }
public virtual T Atualizar(T obj) { var entry = Db.Entry(obj); dbSet.Attach(obj); entry.State = EntityState.Modified; this.Salvar(); return(dbSet.Find(obj.Id)); }
public virtual void Update(TEntity obj) { try { _context.Entry(obj).State = EntityState.Modified; _context.SaveChanges(); } catch (Exception ex) { throw ex; } }
public EstadoCivil Update(EstadoCivil estadoCivil) { if (!Exist(estadoCivil.Id)) { return(new EstadoCivil()); } var result = _context.estadoCivil.SingleOrDefault(p => p.Id.Equals(estadoCivil.Id)); try { _context.Entry(estadoCivil).CurrentValues.SetValues(estadoCivil); _context.SaveChanges(); } catch (Exception ex) { throw ex; } return(estadoCivil); }
public Person update(Person person) { //se não existir a pessoa, ele cria if (!Exist(person.Id)) { return(new Person()); } var result = _context.Persons.SingleOrDefault(p => p.Id.Equals(person.Id)); try { _context.Entry(result).CurrentValues.SetValues(person); _context.SaveChanges(); } catch (Exception ex) { throw ex; } return(person); }
public Person Update(Person person) { var dbPerson = FindById(person.Id.GetValueOrDefault()); if (dbPerson.IsNull()) { return(null); } try { _context.Entry(dbPerson).CurrentValues.SetValues(person); _context.SaveChanges(); return(person); } catch (Exception ex) { throw ex; } }
public T Update(T item) { var dbitem = FindById(item.Id.GetValueOrDefault()); if (dbitem.IsNull()) { return(null); } try { _context.Entry(dbitem).CurrentValues.SetValues(item); _context.SaveChanges(); return(item); } catch (Exception ex) { throw ex; } }
public T Update(T item) { var result = _dataSet.SingleOrDefault(x => x.Id.Equals(item.Id)); if (result != null) { try { _context.Entry(result).CurrentValues.SetValues(item); _context.SaveChanges(); return(result); } catch (Exception) { throw; } } else { return(null); } }
public User RefreshUserInfo(User user) { if (!_context.Users.Any(u => u.Id.Equals(user.Id))) { return(null); } var result = _context.Users.SingleOrDefault(x => x.Id.Equals(user.Id)); if (result != null) { try { _context.Entry(result).CurrentValues.SetValues(user); _context.SaveChanges(); return(result); } catch (Exception) { throw; } } return(result); }
public IHttpActionResult ApplyDiscountToProduct([FromUri] string AccessToken, [FromUri] int ProductId, [FromUri] float Discount) { var UserId = TokenValidator.VerifyToken(AccessToken); var User = SQLContext.Users.Find(UserId); var Product = SQLContext.Products.Find(ProductId); if (!User.IsPro || Product.ProId != UserId) { return(Content(HttpStatusCode.Forbidden, new { Message = "Usuário não é Pro" })); } if (Discount > Product.MaxDiscount) { return(Content(HttpStatusCode.Forbidden, new { Message = "Desconto maior que o máximo." })); } Product.CurrentDiscount = Discount; SQLContext.Entry(Product).State = EntityState.Modified; SQLContext.SaveChanges(); return(Ok()); }
public Usuario AtualizarInformacoesUsuario(Usuario usuario) { if (!Existe(usuario.Id)) { return(null); } var result = _context.Produtos.SingleOrDefault(p => p.Id.Equals(usuario.Id)); if (result != null) { try { _context.Entry(result).CurrentValues.SetValues(usuario); _context.SaveChanges(); } catch (Exception) { throw; } } return(usuario); }
public Produto Atualizar(Produto produto) { if (!Existe(produto.Id)) { return(null); } var result = _context.Produtos.SingleOrDefault(p => p.Id.Equals(produto.Id)); if (result != null) { try { _context.Entry(result).CurrentValues.SetValues(produto); _context.SaveChanges(); } catch (Exception) { throw; } } return(produto); }
public void Atualizar(T item) { context.Entry(item).State = EntityState.Modified; }
public T Update(T obj) { context.Entry(obj).State = Microsoft.EntityFrameworkCore.EntityState.Modified; context.SaveChanges(); return(obj); }
public IHttpActionResult AddProductToCart([FromUri] string AccessToken, [FromBody] ProductInfo ProductInfo) { var UserId = TokenValidator.VerifyToken(AccessToken); var Cart = UserAuxiliar.StoreCart(UserId, ProductInfo.ProId, SQLContext); if (Cart == default || Cart.Finished) { var NewCart = new Cart() { ProId = ProductInfo.ProId, UserId = UserId, Finished = false }; SQLContext.Carts.Add(NewCart); SQLContext.SaveChanges(); try { var ProductCart = new ProductCart() { ProductId = ProductInfo.ProductId, CartId = NewCart.Id, Amount = ProductInfo.Amount }; SQLContext.ProductCarts.Add(ProductCart); SQLContext.SaveChanges(); }catch(DbUpdateException) { return Content(HttpStatusCode.Forbidden, new { Message = "Estoque insuficente!" }); } var Product = SQLContext.Products.Find(ProductInfo.ProductId); return Ok(new { Cart = NewCart, Product = FilterProduct(Product) }); } var AlreadyExists = SQLContext.ProductCarts.FirstOrDefault(x => x.CartId == Cart.Id && x.ProductId == ProductInfo.ProductId && !x.Cart.Finished); if(AlreadyExists != default) { try { AlreadyExists.Amount += ProductInfo.Amount; SQLContext.Entry(AlreadyExists).State = EntityState.Modified; SQLContext.SaveChanges(); return Ok(new { Cart, Product = FilterProduct(AlreadyExists.Product) }); }catch(DbUpdateException) { return Content(HttpStatusCode.Forbidden, new { Message = "Estoque insuficente!" }); } } try { var NewProductCart = new ProductCart() { ProductId = ProductInfo.ProductId, CartId = Cart.Id, Amount = ProductInfo.Amount }; SQLContext.ProductCarts.Add(NewProductCart); SQLContext.SaveChanges(); return Ok(new { Cart, Product = FilterProduct(NewProductCart.Product)}); } catch (DbUpdateException) { return Content(HttpStatusCode.Forbidden, new { Message = "Estoque insuficente!" }); } }