Example #1
0
        public async static Task SaveProduct(Product itemPara, Product oldItem)
        {
            using (var db = new SalesContext())
            {   //to avoid save duplicately Category, change CategoryID instead Category
                //var par = Categories.Where(p => p.Id == itemPara.CategoryID).Single();
                //var chil = par.Products.Where(p => p.Id == itemPara.Id).Single();
                //par.Products.Remove(chil);

                //itemPara.CategoryID = itemPara.Category.Id;
                //Categories.Where(p => p.Id == itemPara.CategoryID).Single().Products.Add(itemPara);

                //var query = db.Products.Find(itemPara.Id);
                //var temp = query.Category;
                //CopyProperties(typeof(Product), query, itemPara);
                //query.Category = temp;
                //db.Entry(query).State = EntityState.Modified;
                //await UpdateDatabase(db);


                db.Entry(oldItem).State = EntityState.Unchanged; //NOT DELETE - IMPORTANT
                db.Categories.Attach(itemPara.Category);         //NOT DELETE - IMPORTANT

                var query = db.Products.Find(itemPara.Id);
                CopyProperties(typeof(Product), query, itemPara);
                query.CategoryID      = itemPara.Category.Id;
                db.Entry(query).State = EntityState.Modified;
                await UpdateDatabase(db);
            }
        }
Example #2
0
        public async static Task SaveCustomerRank(CustomerRank itemPara, CustomerRank oldItem)
        {
            //using (var db = new SalesContext())
            //{
            //    foreach(var item in Customers)  //change customerRank of relative Customer in customers collection to new customerRank
            //    {
            //        if (item.CustomerRankID == itemPara.Id)
            //            item.CustomerRank = itemPara;
            //    }

            //    var query = db.CustomerRanks.Find(itemPara.Id);
            //    var x = query.Customers;
            //    CopyProperties(typeof(CustomerRank), query, itemPara);
            //    query.Customers = x;
            //    db.Entry(query).State = EntityState.Modified;
            //    await UpdateDatabase(db);
            //}

            using (var db = new SalesContext())
            {
                db.Entry(oldItem).State = EntityState.Unchanged;
                var query = db.CustomerRanks.Find(itemPara.Id);
                CopyProperties(typeof(CustomerRank), query, itemPara);
                db.Entry(query).State = EntityState.Modified;
                await UpdateDatabase(db);
            }
        }
Example #3
0
 public void Add(params TDTO[] models)
 {
     foreach (var model in models)
     {
         var entity = DTOtoEntity(model);
         DbSet.Add(entity);
         _context.Entry(entity).State = EntityState.Added;
     }
 }
Example #4
0
 public async static Task DeleteAsset(Asset itemPara)
 {
     using (var db = new SalesContext())
     {
         db.Entry(itemPara).State = EntityState.Unchanged;   //NOT DELETE - IMPORTANT
         Assets.Remove(itemPara);
         db.Entry(itemPara).State = EntityState.Deleted;     // must be executed after deleted it in Assets
         await UpdateDatabase(db);
     }
 }
        public TDto Add(TDto model)
        {
            var entity = DtoToEntity(model);

            var result = DbSet.Add(entity);

            Context.Entry(entity).State = EntityState.Added;

            return(Mapper.Map <TDto>(result));
        }
Example #6
0
 public async static Task DeleteCategory(Category itemPara)
 {
     using (var db = new SalesContext())
     {
         db.Entry(itemPara).State = EntityState.Unchanged;                       //NOT DELETE - IMPORTANT
         Categories.Remove(itemPara);
         db.Entry(itemPara).State = EntityState.Deleted;
         await UpdateDatabase(db);
     }
 }
Example #7
0
 public async static Task SaveInstallationLocation(InstallationLocation itemPara, InstallationLocation oldItem)
 {
     using (var db = new SalesContext())
     {
         db.Entry(oldItem).State = EntityState.Unchanged;
         var query = db.InstallationLocations.Find(itemPara.Id);
         CopyProperties(typeof(InstallationLocation), query, itemPara);
         db.Entry(query).State = EntityState.Modified;
         await UpdateDatabase(db);
     }
 }
Example #8
0
 public void Delete(TEntity entity)
 {
     if (entity != null)
     {
         if (salesContext.Entry(entity).State == EntityState.Detached)
         {
             entitySet.Attach(entity);
         }
         entitySet.Remove(entity);
     }
 }
Example #9
0
 public async static Task SaveCategory(Category itemPara, Category oldItem)
 {
     using (var db = new SalesContext())
     {
         db.Entry(oldItem).State = EntityState.Unchanged;
         var query = db.Categories.Find(itemPara.Id);
         CopyProperties(typeof(Category), query, itemPara);
         db.Entry(query).State = EntityState.Modified;
         await UpdateDatabase(db);
     }
 }
Example #10
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public T Update(T entity)
        {
            var dbEntityEntry = _dbContext.Entry(entity);

            if (dbEntityEntry.State == EntityState.Detached)
            {
                Dbset.Attach(entity);
            }

            dbEntityEntry.State = EntityState.Modified;
            return(entity);
        }
Example #11
0
 public async static Task DeleteCustomer(Customer itemPara)
 {
     using (var db = new SalesContext())
     {
         //if (itemPara.CustomerRank != null)                                   //NOT DELETE - IMPORTANT
         //    db.CustomerRanks.Attach(itemPara.CustomerRank);
         db.Entry(itemPara).State = EntityState.Unchanged;   //NOT DELETE - IMPORTANT
         Customers.Remove(itemPara);
         db.Entry(itemPara).State = EntityState.Deleted;     // must be executed after deleted it in Customers
         await UpdateDatabase(db);
     }
 }
Example #12
0
 public async static Task DeleteGoodsReceipt(GoodsReceipt itemPara)
 {
     using (var db = new SalesContext())
     {
         //if (itemPara.Store != null)                                   //NOT DELETE - IMPORTANT
         //    db.Stores.Attach(itemPara.Store);
         //if (itemPara.Product != null)                                   //NOT DELETE - IMPORTANT
         //    db.Products.Attach(itemPara.Product);
         db.Entry(itemPara).State = EntityState.Unchanged;                       //NOT DELETE - IMPORTANT
         GoodsReceipts.Remove(itemPara);
         db.Entry(itemPara).State = EntityState.Deleted;
         await UpdateDatabase(db);
     }
 }
Example #13
0
 public async Task <IHttpActionResult> Put([FromODataUri] string key, ProductSales update)
 {
     if (!ModelState.IsValid)
     {
         return(BadRequest(ModelState));
     }
     if (key != update.Id)
     {
         return(BadRequest());
     }
     db.Entry(update).State = EntityState.Modified;
     try
     {
         await db.SaveChangesAsync();
     }
     catch (DbUpdateConcurrencyException)
     {
         if (!ProductExists(key))
         {
             return(NotFound());
         }
         else
         {
             throw;
         }
     }
     return(Updated(update));
 }
Example #14
0
        public async static Task DeleteProduct(Product itemPara)
        {
            using (var db = new SalesContext())
            {
                //db.Products.Attach(itemPara);
                //Categories.Where(p => p.Id == itemPara.CategoryID).Single().Products.Remove(itemPara);
                //Products.Remove(itemPara);
                //db.Entry(itemPara).State = EntityState.Deleted; // must be executed after deleted it in Products
                //await UpdateDatabase(db);

                db.Entry(itemPara).State = EntityState.Unchanged;                       //NOT DELETE - IMPORTANT
                Products.Remove(itemPara);
                db.Entry(itemPara).State = EntityState.Deleted;
                await UpdateDatabase(db);
            }
        }
Example #15
0
        public IHttpActionResult PutCustomer(int id, Customer customer)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != customer.Id)
            {
                return(BadRequest());
            }

            db.Entry(customer).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CustomerExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(Ok(customer));
            //return StatusCode(HttpStatusCode.NoContent);
        }
        public async Task <IActionResult> PutOrderline(int id, Orderline orderline)
        {
            if (id != orderline.Id)
            {
                return(BadRequest());
            }

            _context.Entry(orderline).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!OrderlineExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <IActionResult> PutProduct(int id, Product product)
        {
            if (id != product.Id)
            {
                return(BadRequest());
            }

            _context.Entry(product).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ProductExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Example #18
0
        public async Task <IActionResult> PutCustomer(int id, Customer customer)
        {
            if (id != customer.Id)
            {
                return(BadRequest());
            }

            _context.Entry(customer).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CustomerExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Example #19
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (MissingRequiredField(txtFirstName, "First Name") ||
                MissingRequiredField(txtLastName, "Last Name") ||
                TooManyCharacters(txtFirstName, 40, "First Name") ||
                TooManyCharacters(txtLastName, 40, "Last Name") ||
                TooManyCharacters(txtCity, 40, "City") ||
                TooManyCharacters(txtCountry, 40, "Country") ||
                TooManyCharacters(txtPhone, 20, "Phone"))
            {
                return;
            }

            if (btnSave.Text == "&Save")
            {
                ctx.Customers.Add(customer);
                ctx.SaveChanges();
            }
            else
            {
                Customer updatedCustomer = ctx.Customers.Where(x => x.Id == customer.Id).First();

                updatedCustomer.FirstName = customer.FirstName;
                updatedCustomer.LastName  = customer.LastName;
                updatedCustomer.City      = customer.City;
                updatedCustomer.Country   = customer.Country;
                updatedCustomer.Phone     = customer.Phone;

                ctx.Entry(updatedCustomer).CurrentValues.SetValues(updatedCustomer);

                ctx.SaveChanges();
            }
            this.Close();
        }
Example #20
0
        public async static Task SaveCustomer(Customer itemPara, Customer oldItem)
        {
            using (var db = new SalesContext())
            {
                //if (oldItem.CustomerRank != null && oldItem.CustomerRank.Id != itemPara.CustomerRank.Id) //NOT DELETE - IMPORTANT
                //    db.CustomerRanks.Attach(oldItem.CustomerRank);

                db.Entry(oldItem).State = EntityState.Unchanged;        //NOT DELETE - IMPORTANT
                db.CustomerRanks.Attach(itemPara.CustomerRank);         //NOT DELETE - IMPORTANT

                var query = db.Customers.Find(itemPara.Id);
                CopyProperties(typeof(Customer), query, itemPara);
                query.CustomerRankID  = itemPara.CustomerRank.Id;
                db.Entry(query).State = EntityState.Modified;
                await UpdateDatabase(db);
            }
        }
Example #21
0
 public async static Task SaveProductPrice(ProductPrice itemPara, ProductPrice oldItem)
 {
     using (var db = new SalesContext())
     {
         db.Entry(itemPara).State = EntityState.Modified;
         await UpdateDatabase(db);
     }
 }
Example #22
0
 public async static Task SaveGoodsReceipt(GoodsReceipt itemPara, GoodsReceipt oldItem)
 {
     using (var db = new SalesContext())
     {
         db.Entry(itemPara).State = EntityState.Modified;
         await UpdateDatabase(db);
     }
 }
Example #23
0
        public async static Task SaveAsset(Asset itemPara, Asset oldItem)
        {
            using (var db = new SalesContext())
            {
                db.Entry(oldItem).State = EntityState.Unchanged;        //NOT DELETE - IMPORTANT
                db.AssetCategories.Attach(itemPara.AssetCategory);      //NOT DELETE - IMPORTANT
                db.Departments.Attach(itemPara.Department);
                db.InstallationLocations.Attach(itemPara.InstallationLocation);

                var query = db.Assets.Find(itemPara.Id);
                CopyProperties(typeof(Asset), query, itemPara);
                query.AssetCategoryID        = itemPara.AssetCategory.Id;
                query.DepartmentID           = itemPara.Department.Id;
                query.InstallationLocationID = itemPara.InstallationLocation.Id;
                db.Entry(query).State        = EntityState.Modified;
                await UpdateDatabase(db);
            }
        }
Example #24
0
        public async Task <Order> GetDraftOrderByCustomerId(Guid customerId)
        {
            var order = await _context.Orders.FirstOrDefaultAsync(o => o.CustomerId == customerId && o.OrderStatus == OrderStatus.Draft);

            if (order == null)
            {
                return(null);
            }

            await _context.Entry(order).Collection(x => x.OrderItems).LoadAsync();

            if (order.VoucherId != null)
            {
                await _context.Entry(order).Reference(x => x.Voucher).LoadAsync();
            }

            return(order);
        }
Example #25
0
 public async static Task DeleteManyInstallationLocations(IList itemParas)
 {
     using (var db = new SalesContext())
     {
         List <InstallationLocation> list = new List <InstallationLocation>();
         foreach (object item in itemParas)
         {
             list.Add((InstallationLocation)item);
         }
         foreach (InstallationLocation item in list)
         {
             db.Entry(item).State = EntityState.Unchanged;
             InstallationLocations.Remove(item);
             db.Entry(item).State = EntityState.Deleted;
         }
         await UpdateDatabase(db);
     }
 }
Example #26
0
 public async static Task DeleteManyAssets(IList itemParas)
 {
     using (var db = new SalesContext())
     {
         List <Asset> list = new List <Asset>();
         foreach (object item in itemParas)
         {
             list.Add((Asset)item);
         }
         foreach (Asset item in list)
         {
             db.Entry(item).State = EntityState.Unchanged;
             Assets.Remove(item);
             db.Entry(item).State = EntityState.Deleted;
         }
         await UpdateDatabase(db);
     }
 }
Example #27
0
 public ActionResult Edit([Bind(Include = "ID,ProductName,ProductDescription,Price,SaleID,ImageName")] Product product)
 {
     if (ModelState.IsValid)
     {
         db.Entry(product).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(product));
 }
 public ActionResult Edit([Bind(Include = "SalesOrderId,CustomerName,PoNumber,OrderDate")] SalesOrder salesOrder)
 {
     if (ModelState.IsValid)
     {
         db.Entry(salesOrder).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(salesOrder));
 }
Example #29
0
 public ActionResult Edit([Bind(Include = "Id,Name,Birthdate,Address,ContactNo")] Customer customer)
 {
     if (ModelState.IsValid)
     {
         db.Entry(customer).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(customer));
 }
Example #30
0
        public async Task <Order> GetDraftOrderByClientId(Guid clientId)
        {
            var order = await _context.Orders.FirstOrDefaultAsync(p => p.ClientId == clientId && p.OrderStatus == OrderStatus.Draft);

            if (order == null)
            {
                return(null);
            }

            await _context.Entry(order)
            .Collection(i => i.OrderItem).LoadAsync();

            if (order.VoucherId != null)
            {
                await _context.Entry(order)
                .Reference(i => i.Voucher).LoadAsync();
            }
            return(order);
        }