public DTO.Wholesaler CreateWholesaler(Wholesaler wholesaler) { return new DTO.Wholesaler() { Id = wholesaler.Id, Created = wholesaler.Created, Modified = wholesaler.Modified, Address = wholesaler.Address, Name = wholesaler.Name }; }
public RepositoryActionResult<Wholesaler> AddWholesaler(Wholesaler newWholesaler) { try { //Adds new Wholesaler _ctx.Wholesalers.Add(newWholesaler); //Saves Changes var result = _ctx.SaveChanges(); //Returns Wholesaler & Status if (result > 0) { return new RepositoryActionResult<Wholesaler>(newWholesaler, RepositoryActionStatus.Created); } else { return new RepositoryActionResult<Wholesaler>(newWholesaler, RepositoryActionStatus.NothingModified, null); } } catch (Exception ex) { return new RepositoryActionResult<Wholesaler>(null, RepositoryActionStatus.Error, ex); } }
public RepositoryActionResult<Wholesaler> UpdateWholesaler(Wholesaler updatedWholesaler) { try { // Only update when Wholesaler already exists var existingWholesaler = _ctx.Wholesalers.FirstOrDefault(b => b.Id == updatedWholesaler.Id); if (existingWholesaler == null) { return new RepositoryActionResult<Wholesaler>(updatedWholesaler, RepositoryActionStatus.NotFound); } // Change the original entity status to detached; otherwise, we get an error on attach // as the entity is already in the dbSet // set original entity state to detached _ctx.Entry(existingWholesaler).State = EntityState.Detached; // attach & save _ctx.Wholesalers.Attach(updatedWholesaler); // set the updated entity state to modified, so it gets updated. _ctx.Entry(updatedWholesaler).State = EntityState.Modified; // Save Changes var result = _ctx.SaveChanges(); //Returns Book & Status if (result > 0) { return new RepositoryActionResult<Wholesaler>(updatedWholesaler, RepositoryActionStatus.Updated); } else { return new RepositoryActionResult<Wholesaler>(updatedWholesaler, RepositoryActionStatus.NothingModified, null); } } catch (Exception ex) { return new RepositoryActionResult<Wholesaler>(null, RepositoryActionStatus.Error, ex); } }