Beispiel #1
0
        public async Task <IHttpActionResult> AddGoodEntity([FromBody] GoodEntitySchema goodEntitySchema)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            System.Diagnostics.Debug.WriteLine("Good Model.");
            Seller seller = (Seller)HttpContext.Current.User;

            System.Diagnostics.Debug.WriteLine("Seller phone number " + seller.PhoneNumber);
            GoodEntity goodEntity = new GoodEntity()
            {
                SellerID        = seller.SellerID,
                GoodName        = goodEntitySchema.GoodName,
                BrandID         = goodEntitySchema.BrandID,
                Stock           = goodEntitySchema.Stock ?? 0,
                Brief           = goodEntitySchema.Brief,
                DetailImages    = goodEntitySchema.DetailImages,
                FavoriteNum     = 0,
                GoodEntityState = 0,
                SellProvince    = "上海",
            };

            System.Diagnostics.Debug.WriteLine(seller.SellerID + " " + goodEntitySchema.BrandID + " " +
                                               goodEntitySchema.Stock + " " + goodEntitySchema.Brief + " " + goodEntitySchema.DetailImages);
            db.GoodEntities.Add(goodEntity);
            await db.SaveChangesAsync();

            var resp = Request.CreateResponse(HttpStatusCode.NoContent);

            resp.Headers.Add("Location", "api/GoodEntities/" + goodEntity.GoodEntityID);
            return(ResponseMessage(resp));
        }
Beispiel #2
0
        public async Task <IHttpActionResult> GetSaleEntity(int GoodID, int SaleID)
        {
            SaleEntity saleEntity = await db.SaleEntities.FindAsync(SaleID);

            GoodEntity goodEntity = await db.GoodEntities.FindAsync(GoodID);

            Dictionary <string, object> dict = new Dictionary <string, object>();
            HttpResponseMessage         resp;

            if (saleEntity == null || goodEntity == null)
            {
                if (saleEntity == null)
                {
                    dict.Add("error", "SaleEntity " + SaleID + " not exists");
                }
                else
                {
                    dict.Add("error", "GoodEntity " + GoodID + " not exists");
                }
                resp = Request.CreateResponse(HttpStatusCode.NotFound, dict);
            }
            else
            {
                dict.Add("SaleEntity", saleEntity);
                resp = Request.CreateResponse(HttpStatusCode.OK, dict);
            }

            return(ResponseMessage(resp));
        }
        public HttpResponseMessage PostQuestion([FromBody] JObject obj)
        {
            int      Goodid         = int.Parse(obj["GoodID"].ToString());
            string   QuestionDetail = obj["QuestionDetail"].ToString();
            Question question       = new Question();

            question.Detail = QuestionDetail;

            HttpResponseMessage response;

            //找到这个商品的东东
            GoodEntity goodEntity = db.GoodEntities.Find(Goodid);

            question.GoodEntityID = Goodid;


            if (goodEntity == null)
            {
                response = Request.CreateResponse(HttpStatusCode.NotFound, "the GoodEntity not exists");
            }
            else
            {
                db.Questions.Add(question);
                response = Request.CreateResponse(HttpStatusCode.OK, "Created");
            }
            db.SaveChanges();
            return(response);
        }
Beispiel #4
0
        public async Task <ApiMessage> UpdateGood(GoodEntity goodEntity)
        {
            var resultMsg = new ApiMessage();

            try
            {
                var goodInfo = await DataGoodsAccessor.Update <GoodEntity>(goodEntity);

                resultMsg.Data = goodInfo;
            }
            catch (Exception exc)
            {
                resultMsg.Success = false;
                resultMsg.Message = exc.Message;
                return(resultMsg);
            }
            return(resultMsg);
        }
        public async Task <IHttpActionResult> GetGoodEntity(int id)
        {
            GoodEntity entity = db.GoodEntities.Find(id);

            if (entity == null)
            {
                return(NotFound());
            }
            // 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();
            }
            return(Ok(entity));
        }
Beispiel #6
0
        public async Task <ApiMessage> AddGood(GoodEntity goodEntity)
        {
            var resultMsg = new ApiMessage();

            try
            {
                var category = await DataGoodsAccessor.OneAsync <GoodCategory>(x => x.Id == goodEntity.Category.Id);

                goodEntity.Category = category;
                await DataGoodsAccessor.Add(goodEntity);
            }
            catch (Exception exc)
            {
                resultMsg.Success = false;
                resultMsg.Message = exc.Message;
                return(resultMsg);
            }
            return(resultMsg);
        }
 public bool create(Good good)
 {
     using (GoodsDBEntities entity = new GoodsDBEntities())
     {
         try
         {
             GoodEntity g = new GoodEntity();
             g.good_id = good.id;
             g.good_name = good.name;
             g.good_price = good.price;
             g.good_quantity = good.quantity;
             //entity.Good.Add(g);
             entity.Good.Add(g);
             entity.SaveChanges();
             return true;
         }
         catch
         {
             return false;
         }
     };
 }
        public HttpResponseMessage PostFavorite([FromUri] int GoodID)
        {
            if (HttpContext.Current.User == null)
            {
                // 无权
                System.Diagnostics.Debug.WriteLine("Get Favorites Null");
                return(Request.CreateResponse((HttpStatusCode)403));
            }
            User requestUser = (User)HttpContext.Current.User;
            int  user_id     = requestUser.UserID;
            int  good_id     = GoodID;

            HttpResponseMessage response;

            User       user = db.Users.Find(user_id);
            GoodEntity good = db.GoodEntities.Find(good_id);

            if (user == null)
            {
                response = Request.CreateResponse(HttpStatusCode.NotFound, "the user not exists");
            }
            else if (good == null)
            {
                response = Request.CreateResponse(HttpStatusCode.NotFound, "the GoodEntity not exists");
            }
            else
            {
                Favorite favorite = new Favorite();
                favorite.GoodEntity   = good;
                favorite.GoodEntityID = good_id;
                favorite.User         = user;
                favorite.UserID       = user_id;
                db.Favorites.Add(favorite);
                response = Request.CreateResponse(HttpStatusCode.OK, "Created");
            }
            db.SaveChanges();
            return(response);
        }
        public async Task <IHttpActionResult> AddGoodEntity([FromBody] GoodEntitySchema goodEntitySchema)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            System.Diagnostics.Debug.WriteLine("Good Model.");
            Seller seller = (Seller)HttpContext.Current.User;

            System.Diagnostics.Debug.WriteLine("Seller phone number " + seller.PhoneNumber);

            System.Diagnostics.Debug.WriteLine(seller.SellerID + " " + goodEntitySchema.BrandID + " " +
                                               goodEntitySchema.Stock + " " + goodEntitySchema.Brief + " " + goodEntitySchema.DetailImages
                                               + " " + goodEntitySchema.ShownImage + " " + goodEntitySchema.GoodName);

            System.Diagnostics.Debug.WriteLine("Create");

            var Brand = await db.Brands.FindAsync(goodEntitySchema.BrandID);

            if (Brand == null)
            {
                return(NotFound());
            }

            GoodEntity goodEntity = new GoodEntity()
            {
                Seller          = seller,
                SellerID        = seller.SellerID,
                Brand           = Brand,
                GoodName        = goodEntitySchema.GoodName,
                BrandID         = Brand.BrandID,
                Stock           = goodEntitySchema.Stock ?? 1,
                Brief           = goodEntitySchema.Brief,
                DetailImages    = goodEntitySchema.DetailImages,
                FavoriteNum     = 0,
                GoodEntityState = 0,
                SellProvince    = goodEntitySchema.SellProvince,
                Detail          = goodEntitySchema.ShownImage,
            };

            System.Diagnostics.Debug.WriteLine(JsonConvert.SerializeObject(goodEntity));

            db.GoodEntities.Add(goodEntity);
            // 保存单纯的 GoodEntity
            db.Configuration.AutoDetectChangesEnabled = true;
            await db.SaveChangesAsync();

            System.Diagnostics.Debug.WriteLine("Save GoodEntity");
            // 保存 GoodEntity 的属性

            List <List <Option> > innerList = new List <List <Option> >();

            foreach (var attr in goodEntitySchema.GAttributes)
            {
                List <Option> currentList = new List <Option>();

                GAttribute gAttribute = new GAttribute();
                gAttribute.GAttributeName = attr.GAttributeName;
                db.GAttributes.Add(gAttribute);
                // TODO: find out when to save these changes
                await db.SaveChangesAsync();

                foreach (var attrSubName in attr.Describes)
                {
                    Option curOption = new Option()
                    {
                        GAttribute   = gAttribute,
                        GAttributeID = gAttribute.GAttributeID,
                        Describe     = attrSubName
                    };
                    currentList.Add(curOption);
                    db.Options.Add(curOption);
                }
                await db.SaveChangesAsync();

                innerList.Add(currentList);
            }
            System.Diagnostics.Debug.WriteLine("Save options and gttrs");

            // 创建保存 SaleEntity , 递归开始创建
            AddSaleEntity(goodEntity.GoodEntityID, innerList, Price: goodEntitySchema.Price);

            System.Diagnostics.Debug.WriteLine("Save SaleEntities");

            var resp = Request.CreateResponse(HttpStatusCode.NoContent);

            resp.Headers.Add("Location", "api/GoodEntities/" + goodEntity.GoodEntityID);
            return(ResponseMessage(resp));
        }
Beispiel #10
0
        public async Task <IActionResult> UpdateGood(GoodEntity goodEntity)
        {
            var result = await goodsBs.UpdateGood(goodEntity);

            return(Json(result));
        }
Beispiel #11
0
        public async Task <IActionResult> AddGoods(GoodEntity goodModel)
        {
            var result = await goodsBs.AddGood(goodModel);

            return(Json(result));
        }
Beispiel #12
0
 internal Good(GoodEntity goodEntity)
 {
     Name  = goodEntity.Name;
     Price = goodEntity.Price;
     Type  = new GoodType(goodEntity.Type);
 }
Beispiel #13
0
        public IHttpActionResult GetDiscount()
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (HttpContext.Current.User.Identity != null)
            {
                // 无权
                System.Diagnostics.Debug.WriteLine("Not Login");
                return(BadRequest());
            }

            User requestUser = (User)HttpContext.Current.User;
            int  user_id     = requestUser.UserID;

            var     cart         = db.Carts.Find(user_id);
            var     user         = db.Users.Find(user_id);
            decimal max_discount = 0;

            if (user.Coupons.Count == 0)
            {
                return(Ok(new {
                    Discount = max_discount
                }));
            }

            // 初始化优惠券的所有类别的ID,无重复
            List <int> coupon_categories_ID = new List <int>();

            foreach (var c in user.Coupons)
            {
                if (getIndex(c.CategoryID, coupon_categories_ID) == -1)
                {
                    coupon_categories_ID.Add(c.CategoryID);
                }
            }

            // 初始化每一类的总价List与可降价List,均为0
            List <decimal> prices_by_category   = new List <decimal>();
            List <decimal> discount_by_category = new List <decimal>();
            List <int>     indexes_coupon       = new List <int>();
            List <Coupons> max_coupons          = new List <Coupons>();

            for (int i = 0; i < coupon_categories_ID.Count; i++)
            {
                prices_by_category.Add(0);
                discount_by_category.Add(0);
                indexes_coupon.Add(0);
            }

            // 得到所有类别的总价
            // var SErecord = orderform.SERecords;
            var cartRecord = cart.CartRecords;

            if (cartRecord == null)
            {
                // 无权
                System.Diagnostics.Debug.WriteLine("No cartRecord");
                return(BadRequest());
            }

            foreach (var record in cartRecord)
            {
                //得到该商品的所有类别
                GoodEntity good            = db.GoodEntities.Find(record.SaleEntity.GoodEntityID);
                var        good_categories = good.Categories;
                foreach (var cate in good_categories)
                {
                    //更新总价List
                    int index = getIndex(cate.CategoryID, coupon_categories_ID);
                    if (index != -1)
                    {
                        prices_by_category[index] += record.SaleEntity.Price;
                    }
                }
            }

            // 得到所有类别的最高降价
            for (int i = 0; i < coupon_categories_ID.Count; i++)
            {
                var     total_price = prices_by_category[i];
                var     cate_id     = coupon_categories_ID[i];
                Coupons max_coupon  = user.Coupons.First();
                discount_by_category[i] = get_max_discount_by_category(total_price, cate_id, user.Coupons, ref max_coupon);
                max_coupons.Add(max_coupon);
            }

            Coupons valid_coupon = max_coupons[0];

            max_discount = get_max_discount(discount_by_category, max_coupons, ref valid_coupon);
            user.Coupons.Remove(valid_coupon);
            db.SaveChanges();
            return(Ok(new
            {
                Discount = max_discount
            }));
        }