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));
        }
        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));
        }