Ejemplo n.º 1
0
        public OperationResult Create(SellingViewModel input)
        {
            var result = new OperationResult();

            try
            {
                BizModel context = new BizModel();
                using (var transaction = context.Database.BeginTransaction())//Rollback
                {
                    var     sellCount         = input.Quantity;
                    var     procurementRepo   = new BizRepository <Procurement>(context);
                    var     sellingSourceRepo = new BizRepository <SellingSource>(context);
                    var     sellingRepo       = new BizRepository <Selling>(context);
                    Selling entity            = new Selling()
                    {
                        PartNo         = input.PartNo,
                        Quantity       = input.Quantity,
                        SalesJobNumber = input.SalesJobNumber,
                        SellingDay     = input.SellingDay,
                        UnitPrice      = input.UnitPrice
                    };
                    sellingRepo.Create(entity);
                    context.SaveChanges();
                    var products = procurementRepo.GetAll()
                                   .Where((x) => x.PartNo == input.PartNo).OrderBy((x) => x.PurchasingDay);
                    foreach (var p in products)
                    {
                        if (sellCount <= 0)
                        {
                            break;
                        }
                        else
                        {
                            if (p.InvetoryQuantity >= sellCount)
                            {
                                p.InvetoryQuantity = p.InvetoryQuantity - sellCount;
                                CreateSellingSource(sellingSourceRepo, entity.SellingId, p.ProcurementId, sellCount);
                                sellCount = 0;
                            }
                            else
                            {
                                sellCount = sellCount - p.InvetoryQuantity;
                                CreateSellingSource(sellingSourceRepo, entity.SellingId, p.ProcurementId, p.InvetoryQuantity);
                                p.InvetoryQuantity = 0;
                            }
                        }
                    }

                    context.SaveChanges();
                    result.IsSuccessful = true;
                    transaction.Commit();
                }
            }
            catch (Exception ex)
            {
                result.IsSuccessful = false;
                result.exception    = ex;
            }
            return(result);
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            BizModel model        = new BizModel();
            var      allEmployees = model.Employees;

            foreach (Employee e1 in allEmployees)
            {
                Console.WriteLine(e1.Name);
            }

            Console.ReadLine();

            var triangleEmployees = model.Employees.Where(e2 => e2.Department.Name == "Aspit Trekanten").ToList();

            foreach (Employee e2 in triangleEmployees)
            {
                Console.WriteLine(e2.Name);
            }

            Console.ReadLine();

            // INSERT
            //Department storKbh = model.Departments.SingleOrDefault(d => d.Name == "Aspit Storkøbenhavn");
            //Employee employee = new Employee() { Name = "STLA", Department = storKbh };
            //model.Employees.Add(employee);
            //model.SaveChanges();

            foreach (Employee e in model.Employees)
            {
                Console.WriteLine(e.Name + "\t" + e.Department.Name);
            }
            Console.ReadLine();

            //Delete
            var steen = model.Employees.Where(e => e.Name == "STLA").FirstOrDefault();

            model.Employees.Remove(steen);
            model.SaveChanges();

            foreach (Employee e in model.Employees)
            {
                Console.WriteLine(e.Name + "\t" + e.Department.Name);
            }
            Console.ReadLine();

            var klement = model.Employees.SingleOrDefault(e => e.Name == "KLJO");

            klement.Name = "KLJOOOO";
            model.SaveChanges();

            foreach (Employee e in model.Employees)
            {
                Console.WriteLine(e.Name + "\t" + e.Department.Name);
            }
            Console.ReadLine();
        }
Ejemplo n.º 3
0
        public OperationResult Create(ProcurementViewModel input)
        {
            var result = new OperationResult();

            try
            {
                BizModel    context    = new BizModel();
                var         repository = new BizRepository <Procurement>(context);
                Procurement entity     = new Procurement()
                {
                    PartNo           = input.PartNo,
                    PurchasingDay    = input.PurchasingDay,
                    Quantity         = input.Quantity,
                    InvetoryQuantity = input.InvetoryQuantity,
                    UintPrice        = input.UnitPrice
                };
                repository.Create(entity);
                context.SaveChanges();
                result.IsSuccessful = true;
            }
            catch (Exception ex)
            {
                result.IsSuccessful = false;
                result.exception    = ex;
            }
            return(result);
        }
Ejemplo n.º 4
0
        public OperationResult Create(ProductViewModel input)
        {
            var result = new OperationResult();

            try
            {
                BizModel context = new BizModel();
                // context 橋梁 "承上啟下" 資料庫的上下文
                BizRepository <Product> repository
                    = new BizRepository <Product>(context);
                // viewModel 轉成 Model 再給資料庫
                Product entity = new Product()
                {
                    PartNo = input.PartNo, PartName = input.PartName
                };
                repository.Create(entity);
                context.SaveChanges();
                result.IsSuccessful = true;
            }
            catch (Exception ex)
            {
                result.IsSuccessful = false;
                result.exception    = ex;
            }
            return(result);
        }
Ejemplo n.º 5
0
        public OperationResult Create(ProductViewModel input)
        {
            var result = new OperationResult();

            try
            {
                BizModel context = new BizModel();
                BizRepository <Product> repository
                    = new BizRepository <Product>(context);
                Product entity = new Product()
                {
                    PartNo = input.PartNo, PartName = input.PartName
                };
                repository.Create(entity);
                context.SaveChanges();
                result.IsSuccessful = true;

                /*if (FakeProducts.Any((x) => x.PartNo == product.PartNo))
                 * {
                 *  throw new ArgumentException($"PartNo {product.PartNo} exsists");
                 * }
                 * else
                 * {
                 *  FakeProducts.Add(product);
                 *  result.IsSuccessful = true;
                 * }*/
            }
            catch (Exception ex)
            {
                result.IsSuccessful = false;
                result.exception    = ex;
            }
            return(result);
        }
Ejemplo n.º 6
0
        public void Create(Product entity)
        {
            BizModel context = new BizModel();
            BizRepository <Product> repository = new BizRepository <Product>(context);

            repository.Create(entity);
            context.SaveChanges();
        }
Ejemplo n.º 7
0
        public OperationResult Create(SalesManViewModel input)
        {
            var result = new OperationResult();

            try
            {
                BizModel context = new BizModel();
                BizRepository <SalesMan> repository = new BizRepository <SalesMan>(context);
                SalesMan entity = new SalesMan()
                {
                    Name = input.Name
                };
                repository.Create(entity);
                context.SaveChanges();
                result.IsSuccessful = true;
            }
            catch (Exception ex) {
                result.IsSuccessful = false;
                result.exception    = ex;
            }
            return(result);
        }
Ejemplo n.º 8
0
        //假的行為,到目前為止還不知道資料庫的真實內容,測試用
        //public static List<ProductViewModel> FakeProducts = new List<ProductViewModel>();

        public OperationResult Create(ProductViewModel input)
        {
            var result = new OperationResult();

            try
            {
                //if (FakeProducts.Any((x) => x.ParNo == product.PartNo) != null)
                //{
                //    throw new ArgumentException($"PartNo {product.PartNo} is not exsist");
                //}
                //else
                //{
                //    FakeProduct.Add(product);
                //    result.IsSuccessful = true;
                //}

                BizModel context = new BizModel();
                BizRepository <Product> repository = new BizRepository <Product>(context);
                //context 橋梁、承上啟下,接通兩個不同的端點(EX:資料庫、程式)
                Product entity = new Product()
                {
                    PartNo   = input.PartNo,
                    PartName = input.PartName
                };
                repository.Create(entity);
                context.SaveChanges();
                result.IsSuccessful = true;
            }
            catch (Exception ex)
            {
                result.IsSuccessful = false;
                result.exception    = ex;
            }

            return(result);
        }