Exemple #1
0
        public async Task <IHttpActionResult> GetCarts([FromUri] QueryPageURI queryPageURI)
        {
            if (HttpContext.Current.User == null)
            {
                // 无权
                System.Diagnostics.Debug.WriteLine("Get Carts Null");
                return(ResponseMessage(Request.CreateResponse((HttpStatusCode)403)));
            }
            User requestUser = (User)HttpContext.Current.User;

            System.Diagnostics.Debug.WriteLine("GetCarts has:" + requestUser.NickName);

            var cart = await db.Carts.FindAsync(requestUser.UserID);

            // Load data
            await db.Entry(cart).Collection(c => c.CartRecords).LoadAsync();

            // "ExceptionMessage": "The property 'GoodEntity' on type 'SaleEntity' is not a navigation property. The Reference and Collection methods can only be used with navigation properties. Use the Property or ComplexProperty method."
            foreach (var rec in cart.CartRecords)
            {
                await db.Entry(rec).Reference(record => record.SaleEntity).LoadAsync();

                var goodentity = await db.GoodEntities.FirstAsync(s => s.GoodEntityID == rec.SaleEntity.GoodEntityID);

                rec.SaleEntity.GoodEntity = goodentity;
            }
            // await db.Entry(requestUser).Reference(u => u.Cart).LoadAsync();
            return(Ok(new {
                Cart = cart,
                TotalPrice = await cart.TotalPrice(db)
            }));
        }
        public async Task <IHttpActionResult> ViewOrderform([FromUri] QueryPageURI queryData)
        {
            //if (!ModelState.IsValid)
            //{
            //    return BadRequest();
            //}
            bool usePn = true;

            if (queryData == null || queryData.UsePag == false)
            {
                usePn = false;
            }

            var usr      = (User)HttpContext.Current.User;
            var entities = db.Orderforms.Where(odf => odf.UserID == usr.UserID).OrderBy(odf => odf.TransacDateTime);

            foreach (var odf in entities)
            {
                await db.Entry(odf).Collection(of => of.SERecords).LoadAsync();

                foreach (var serec in odf.SERecords)
                {
                    await db.Entry(serec).Reference(ser => ser.SaleEntity).LoadAsync();
                }
            }
            if (usePn)
            {
                int pn      = queryData.Pn ?? 1;
                var results = await entities.Skip((pn - 1) *SinglePageQuery).Take(SinglePageQuery).ToListAsync();

                return(Ok(new
                {
                    Orderforms = results,
                    ResultNums = results.Count(),
                    Pn = pn
                }));
            }
            else
            {
                return(Ok(new
                {
                    Orderforms = entities,
                    ResultNums = entities.Count()
                }));
            }
            // await goodEntities.Skip((pn - 1) * PageDataNumber).Take(PageDataNumber).ToListAsync();
        }