public Owner Get(OwnerId ownerId) { using (var context = new BakeryContext()) { var dto = context .Owners .Find(ownerId.Id); return dto == null ? null : Mapper.ToDomainObject(dto); } }
public void Remove(Product product) { using (var context = new BakeryContext()) { context.Products.Remove( context.Products.Find(product.Id.Id) ); context.SaveChanges(); } }
public Product Get(ProductId productId) { using (var context = new BakeryContext()) { var dto = context .Products .Find(productId.Id); return dto == null ? null : Mapper.ToDomainObject(dto); } }
public void Save(Product product) { using (var context = new BakeryContext()) { var dto = context.Products.Find(product.Id.Id) ?? new Context.Product.Product(); Mapper.MapToDto(product, dto); context.Set<Context.Product.Product>().AddOrUpdate(dto); context.SaveChanges(); } }
public List<Product> GetAll() { using (var context = new BakeryContext()) { var products = context .Products; var list = new List<Product>(); foreach (var product in products) { list.Add(Mapper.ToDomainObject(product)); } return list; } }
public Owner GetByCode(string code) { using (var context = new BakeryContext()) { var dtos = context .Owners .Where(el => el.Code == code); if (dtos.Any()) { return null; } var dto = dtos.First(); return Mapper.ToDomainObject(dto); } }
public List<Owner> GetAll() { using (var context = new BakeryContext()) { var owners = context.Owners; var list = new List<Owner>(); foreach ( var owner in owners .Include("OwnerAddress") .Include("Phones") ) { list.Add(Mapper.ToDomainObject(owner)); } return list; } }
public void Remove(Owner owner) { using (var context = new BakeryContext()) { context.Owners.Remove( context.Owners.Find(owner.Id.Id) ); try { context.SaveChanges(); } catch (DbUpdateException e) { throw new RepositoryException(e.Message, e); } } }
public void Save(Owner owner) { using (var context = new BakeryContext()) { var existingDto = context.Owners .Include("Phones") .Include("OwnerAddress") .FirstOrDefault(o => o.Id == owner.Id.Id) ?? new Context.Shop.Owner(); var newDto = Mapper.ToDto(owner); var id = newDto.Id; if (context.Owners.Any(e => e.Id == id)) { var updatedPhones = newDto.Phones.ToList(); var existingPhones = existingDto.Phones.ToList(); var addedPhones = updatedPhones.Except(existingPhones).ToList(); var deletedPhones = existingPhones.Except(updatedPhones).ToList(); var modifiedPhones = updatedPhones.Except(addedPhones).ToList(); addedPhones.ForEach(phn => context.Entry(phn).State = EntityState.Added); deletedPhones.ForEach(phn => context.Entry(phn).State = EntityState.Deleted); foreach (var phone in modifiedPhones) { var existingPhone = context.OwnerPhones .FirstOrDefault(phn => phn.Equals(phone)); if (existingPhone == null) { continue; } var phoneEntry = context.Entry(existingPhone); phoneEntry.CurrentValues.SetValues(phone); } var ownerEntry = context.Entry(existingDto); ownerEntry.CurrentValues.SetValues(newDto); var ownerAddressEntry = context.Entry(existingDto.OwnerAddress); ownerAddressEntry.CurrentValues.SetValues(newDto.OwnerAddress); } else { context.Owners.Add(newDto); } try { context.SaveChanges(); } catch (DbUpdateException e) { throw new RepositoryException(e.Message, e); } } }