Example #1
0
 public void Update(Catalog entity)
 {
     try
     {
         var catalog = db.Catalogs.Find(entity.id);
         CatalogMapper.Mapper(catalog, entity);
         db.SaveChanges();
     }
     catch (Exception)
     {
         throw new Exception("Lỗi cập nhật sản phẩm");
     }
 }
Example #2
0
        public bool SaveLaptop(int id, LaptopDTO catalogDTO)
        {
            DbContextTransaction transaction = _context.Database.BeginTransaction();
            var check = SaveCatalog(id, transaction, catalogDTO);

            if (!check)
            {
                return(false);
            }
            if (id == 0)
            {
                var laptop = new SpecificationsLaptop();
                laptop = CatalogMapper.toSpecificationsLaptop(catalogDTO, laptop);
                try
                {
                    _context.SpecificationsLaptops.Add(laptop);
                    _context.SaveChanges();
                    transaction.Commit();
                    return(true);
                }
                catch (Exception)
                {
                    transaction.Rollback();
                    return(false);
                }
            }
            else
            {
                var laptop = _context.SpecificationsLaptops.SingleOrDefault(x => x.catalogid == catalogDTO.id);
                if (laptop == null)
                {
                    transaction.Rollback();
                    return(false);
                }
                laptop = CatalogMapper.toSpecificationsLaptop(catalogDTO, laptop);
                try
                {
                    _context.SaveChanges();
                    transaction.Commit();
                    return(true);
                }
                catch (Exception)
                {
                    transaction.Rollback();
                    return(false);
                }
            }
        }
Example #3
0
        public bool SaveMobile(int id, MobileDTO catalogDTO)
        {
            DbContextTransaction transaction = _context.Database.BeginTransaction();
            var check = SaveCatalog(id, transaction, catalogDTO);

            if (!check)
            {
                return(false);
            }
            if (id == 0)
            {
                var mobile = new SpecificationsMobile();
                mobile = CatalogMapper.toSpecificationsMobile(catalogDTO, mobile);
                try
                {
                    _context.SpecificationsMobiles.Add(mobile);
                    _context.SaveChanges();
                    transaction.Commit();
                }
                catch (Exception)
                {
                    transaction.Rollback();
                    return(false);
                }
            }
            else
            {
                var mobile = _context.SpecificationsMobiles.SingleOrDefault(x => x.catalogid == catalogDTO.id);
                if (mobile == null)
                {
                    transaction.Rollback();
                    return(false);
                }
                mobile = CatalogMapper.toSpecificationsMobile(catalogDTO, mobile);
                try
                {
                    _context.SaveChanges();
                    transaction.Commit();
                }
                catch (Exception)
                {
                    transaction.Rollback();
                    return(false);
                }
            }
            return(true);
        }
 public CatalogDataAccessObject(DataModel.CatalogContext context, CatalogMapper mapperAccessor) : base(context, mapperAccessor.Mapper)
 {
 }
Example #5
0
 private bool SaveCatalog(int id, DbContextTransaction transaction, CatalogDTO catalogDTO)
 {
     if (id == 0)
     {
         var count = _context.Catalogs.Count(x => x.name == catalogDTO.name);
         if (count > 0)
         {
             return(false);
         }
         var catalog = new Catalog();
         catalog = CatalogMapper.toCatalog(catalogDTO, catalog);
         try
         {
             _context.Catalogs.Add(catalog);
             _context.SaveChanges();
         }
         catch (Exception)
         {
             transaction.Rollback();
             return(false);
         }
         catalogDTO.id = _context.Catalogs.SingleOrDefault(x => x.name == catalogDTO.name).id;
         var history = new History()
         {
             catalogid  = catalogDTO.id,
             unit       = catalogDTO.quantity,
             inputprice = catalogDTO.price,
             createdAt  = DateTime.Now,
             updatedAt  = DateTime.Now
         };
         var specification = new Specification();
         specification = CatalogMapper.toSpecification(catalogDTO, specification);
         try
         {
             _context.Histories.Add(history);
             _context.Specifications.Add(specification);
             _context.SaveChanges();
             return(true);
         }
         catch (Exception)
         {
             transaction.Rollback();
             return(false);
         }
     }
     else
     {
         var catalog = _context.Catalogs.Find(catalogDTO.id);
         var history = _context.Histories.FirstOrDefault(x => x.catalogid == catalog.id);
         if (history != null)
         {
             if (catalogDTO.quantity > catalog.quantity)
             {
                 int unit       = catalogDTO.quantity - catalog.quantity;
                 var newHistory = new History()
                 {
                     catalogid  = history.catalogid,
                     inputprice = history.inputprice,
                     unit       = unit,
                     createdAt  = DateTime.Now,
                     updatedAt  = DateTime.Now
                 };
                 _context.Histories.Add(newHistory);
             }
         }
         else
         {
             if (catalogDTO.quantity > catalog.quantity)
             {
                 int unit       = catalogDTO.quantity - catalog.quantity;
                 var newHistory = new History()
                 {
                     catalogid  = catalog.id,
                     inputprice = catalog.price - Convert.ToDecimal(0.05 * Convert.ToDouble(catalog.price)),
                     unit       = unit,
                     createdAt  = DateTime.Now,
                     updatedAt  = DateTime.Now
                 };
                 _context.Histories.Add(newHistory);
             }
         }
         var specification = _context.Specifications.SingleOrDefault(x => x.catalogid == catalogDTO.id);
         catalog       = CatalogMapper.toCatalog(catalogDTO, catalog);
         specification = CatalogMapper.toSpecification(catalogDTO, specification);
         try
         {
             _context.SaveChanges();
             return(true);
         }
         catch (Exception)
         {
             transaction.Rollback();
             return(false);
         }
     }
 }
Example #6
0
 public MovieDataAccessObject(CatalogContext context, CatalogMapper mapperAccessor) : base(context, mapperAccessor)
 {
 }