Ejemplo n.º 1
0
        public async Task <IHttpActionResult> GetCategories([FromBody] CategoryQuery data)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            if (data.Kw == null)
            {
                return(BadRequest("No Kw in your URI"));
            }
            int pn = data.Pn ?? 1;
            IQueryable <Category> categories;

            if (data.Kw == null)
            {
                categories = db.Categories;
            }
            else
            {
                categories = db.Categories.Where(u => u.Name.ToLower().Contains(data.Kw.ToLower()));
            }
            // TODO: make it run faster with accerlerate it
            categories = categories.OrderBy(c => c.CategoryID);
            var rs = await categories.Skip((pn - 1) *PageDataNumber).Take(PageDataNumber).ToListAsync();

            return(ResponseMessage(Request.CreateResponse(HttpStatusCode.OK,
                                                          new
            {
                result_num = rs.Count(),
                categories = rs,
                page_num = pn
            })));
        }
Ejemplo n.º 2
0
        public async Task <IHttpActionResult> GetRelatedEntities(int CategoryID, [FromUri] CategoryQuery data)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            int pn = data.Pn ?? 1;
            IQueryable <GoodEntity> fullEntities;
            IQueryable <GoodEntity> goodEntities;
            var cate = await db.Categories.FindAsync(CategoryID);

            await db.Entry(cate).Collection(c => c.GoodEntities).LoadAsync();

            fullEntities = cate.GoodEntities.AsQueryable();
            if (data.Kw == null)
            {
                goodEntities = fullEntities;
            }
            else
            {
                goodEntities = fullEntities.Where(u => u.GoodName.ToLower().Contains(data.Kw.ToLower()));
            }
            int allNum = (await goodEntities.CountAsync()) / PageDataNumber;

            if (allNum % PageDataNumber != 0)
            {
                allNum += 1;
            }

            var rs = await goodEntities.Skip((pn - 1) *PageDataNumber).Take(PageDataNumber).ToListAsync();

            int resultNum = rs.Count();
            List <GoodEntitySchema> resultSchema = new List <GoodEntitySchema>();


            return(ResponseMessage(Request.CreateResponse(HttpStatusCode.OK,
                                                          new
            {
                ResultNum = rs.Count(),
                GoodEntities = rs,
                PageNum = pn,
            })));
        }
Ejemplo n.º 3
0
        public async Task <IHttpActionResult> GetGoodEntities([FromUri] CategoryQuery data)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            int pn = 1;

            if (data != null)
            {
                pn = data.Pn ?? 1;
            }

            IQueryable <GoodEntity> goodEntities;

            if (data != null && data.Kw == null)
            {
                System.Diagnostics.Debug.WriteLine("data is not null.");
                goodEntities = db.GoodEntities;
            }
            else
            {
                System.Diagnostics.Debug.WriteLine("data is null.");
                if (db.GoodEntities == null)
                {
                    System.Diagnostics.Debug.WriteLine("db.GE is null.");
                    goodEntities = null;
                }
                else
                {
                    System.Diagnostics.Debug.WriteLine("db.GE is not null.");

                    goodEntities = db.GoodEntities.Where(u => u.GoodName.ToLower().Contains(data.Kw.ToLower()));
                    System.Diagnostics.Debug.WriteLine("Get search result.");
                    int cnt = await goodEntities.CountAsync();

                    if (cnt == 0)
                    {
                        System.Diagnostics.Debug.WriteLine("Cnt is zero");
                        goodEntities = null;
                    }
                    else
                    {
                        System.Diagnostics.Debug.WriteLine("cnt is " + cnt);
                    }
                }
            }
            List <GoodEntitySchema> resultSchema = new List <GoodEntitySchema>();

            if (goodEntities == null)
            {
                System.Diagnostics.Debug.WriteLine("goodentity is null ");
                return(ResponseMessage(Request.CreateResponse(HttpStatusCode.OK,
                                                              new
                {
                    ResultNum = toPageNum(0),
                    GoodEntities = resultSchema,
                    PageNum = pn,
                    AllNum = 0
                })));
            }
            System.Diagnostics.Debug.WriteLine("ge is not null");
            goodEntities = goodEntities.Include(ge => ge.GAttributes).OrderBy(entity => entity.GoodEntityID);
            System.Diagnostics.Debug.WriteLine("ge is still not null");
            int allNum = await goodEntities.CountAsync();

            System.Diagnostics.Debug.WriteLine("ge get success");

            System.Diagnostics.Debug.WriteLine("ge cnt success");
            if (goodEntities == null)
            {
                System.Diagnostics.Debug.WriteLine("ge is null here, die");
                return(ResponseMessage(Request.CreateResponse(HttpStatusCode.OK,
                                                              new
                {
                    ResultNum = toPageNum(allNum),
                    GoodEntities = resultSchema,
                    PageNum = pn,
                    AllNum = allNum
                })));
            }
            System.Diagnostics.Debug.WriteLine("See rs!");

            try
            {
                var rs = await goodEntities.Skip((pn - 1) *PageDataNumber).Take(PageDataNumber).ToListAsync();

                foreach (var entity in rs)
                {
                    // TODO: only load one image
                    // https://stackoverflow.com/questions/3356541/entity-framework-linq-query-include-multiple-children-entities
                    // await db.Entry(entity).Collection(ge => ge.Images).LoadAsync();
                    await db.Entry(entity).Collection(ge => ge.SaleEntities).LoadAsync();

                    //await db.Entry(entity).Collection(ge => ge.GAttributes).LoadAsync();
                    //foreach (var attr in entity.GAttributes)
                    //{
                    //    await db.Entry(attr).Collection(a => a.Options).LoadAsync();
                    //}
                    string image;
                    try
                    {
                        image = entity.DescribeImages[0];
                    }
                    catch
                    {
                        image = null;
                    }
                    decimal min_price = 0;
                    try
                    {
                        min_price = entity.SaleEntities.Select(se => se.Price).Min();
                    } catch (InvalidOperationException)
                    {
                        min_price = 0;
                    }

                    resultSchema.Add(new GoodEntitySchema()
                    {
                        GoodName     = entity.GoodName,
                        GoodEntityID = entity.GoodEntityID,
                        DetailImages = image,
                        Price        = min_price,
                    });

                    // load attrs
                }



                return(ResponseMessage(Request.CreateResponse(HttpStatusCode.OK,
                                                              new
                {
                    ResultNum = toPageNum(allNum),
                    GoodEntities = resultSchema,
                    PageNum = pn,
                    AllNum = allNum
                })));
            } catch (InvalidOperationException)
            {
                return(ResponseMessage(Request.CreateResponse(HttpStatusCode.OK,
                                                              new
                {
                    ResultNum = toPageNum(allNum),
                    GoodEntities = resultSchema,
                    PageNum = pn,
                    AllNum = allNum
                })));
            }
        }