public override void Add(UnitDTO entity) { if (entity.Id.HasValue) { entity.Id = null; } SiennContext.Unit.Add(entity); SiennContext.SaveChanges(); }
public override void Add(CategoryDTO entity) { if (entity.Id.HasValue) { entity.Id = null; } SiennContext.Category.Add(entity); SiennContext.SaveChanges(); }
public override void Add(ProductDTO entity) { if (entity.Id.HasValue) { throw new Exception("Id should not be set."); } SiennContext.Product.Add(entity); SiennContext.SaveChanges(); }
public override void Update(UnitDTO entity) { var dto = SiennContext.Unit.FirstOrDefault(x => x.Id == entity.Id); if (dto == null) { throw new Exception($"Unit with ID = {entity.Id} not found."); } dto.Code = entity.Code; dto.Description = entity.Description; SiennContext.SaveChanges(); }
public override void Update(ProductDTO entity) { using (var dbContextTransaction = SiennContext.Database.BeginTransaction()) { try { if (!entity.Id.HasValue) { throw new Exception($"Entity does not contain Id."); } var dto = SiennContext.Product.FirstOrDefault(x => x.Id == entity.Id); if (dto == null) { throw new Exception($"Product with ID = {entity.Id} not found."); } SiennContext.ProductCategory.RemoveRange(SiennContext.ProductCategory.Where(x => x.ProductId == entity.Id)); SiennContext.SaveChanges(); dto.Code = entity.Code; dto.Description = entity.Description; dto.DeliveryDate = entity.DeliveryDate; dto.IsAvailable = entity.IsAvailable ?? dto.IsAvailable; dto.Price = entity.Price ?? dto.Price; dto.TypeId = entity.TypeId; dto.UnitId = entity.UnitId; dto.ProductCategories = entity.ProductCategories; SiennContext.SaveChanges(); dbContextTransaction.Commit(); } catch (Exception ex) { dbContextTransaction.Rollback(); throw ex; } } }
public override void Remove(TEntity entity) { SiennContext.Remove(entity); SiennContext.SaveChanges(); }