示例#1
0
 public ResultModel Create(GoodsItem goods)
 {
     try
     {
         int id  = _shopService.CreateGoods(goods);
         var res = new ResultModel(true, new { id = id });
         return(res);
     }
     catch (Exception e)
     {
         return(new ResultModel(e));
     }
 }
示例#2
0
        private static void ExportGoodsToDB()
        {
            const string albumName = "Импортированные товары";
            int          albumId;

            if (_albumService.GetAlbums().Any(x => x.Name.Equals(albumName)))
            {
                albumId = _albumService.GetAlbums().First(x => x.Name.Equals(albumName)).AlbumId;
            }
            else
            {
                albumId = _albumService.CreateAlbum(albumName);
            }


            foreach (var goodsItem in _goods)
            {
                var i = new GoodsItem()
                {
                    Article         = goodsItem.Article,
                    HtmlDescription = goodsItem.Description,
                    //Description = RemoveHtml(goodsItem.Description),
                    Categories = new[] { new IdNamePair <int>()
                                         {
                                             ID = goodsItem.CategoryId
                                         } },
                    Name = goodsItem.Name
                };

                i.Description = "";
                throw new NotImplementedException("Description");

                try
                {
                    i.Price = int.Parse(goodsItem.Price);
                }
                catch
                {
                    if (goodsItem.Price.Contains(" за 100 г."))
                    {
                        decimal p = decimal.Parse(goodsItem.Price.Replace(" за 100 г.", ""));
                        i.Price = p;
                        //Пачка
                        i.Packs.Add(new GoodsPack()
                        {
                            PackInfo = new Pack()
                            {
                                PackId = 1
                            }
                        });
                        i.Packs.Add(new GoodsPack()
                        {
                            PackInfo = new Pack()
                            {
                                PackId = 2
                            }
                        });
                        i.Packs.Add(new GoodsPack()
                        {
                            PackInfo = new Pack()
                            {
                                PackId = 3
                            }
                        });
                        i.Packs.Add(new GoodsPack()
                        {
                            PackInfo = new Pack()
                            {
                                PackId = 4
                            }
                        });
                    }
                    if (goodsItem.Price.Contains(" за 250 г."))
                    {
                        decimal p = decimal.Parse(goodsItem.Price.Replace(" за 250 г.", ""));
                        p       = Decimal.Divide(p, 2.5m);
                        i.Price = p;
                        //Пачка
                        i.Packs.Add(new GoodsPack()
                        {
                            PackInfo = new Pack()
                            {
                                PackId = 1
                            }
                        });
                        i.Packs.Add(new GoodsPack()
                        {
                            PackInfo = new Pack()
                            {
                                PackId = 2
                            }
                        });
                        i.Packs.Add(new GoodsPack()
                        {
                            PackInfo = new Pack()
                            {
                                PackId = 3
                            }
                        });
                        i.Packs.Add(new GoodsPack()
                        {
                            PackInfo = new Pack()
                            {
                                PackId = 4
                            }
                        });
                    }
                }

                try
                {
                    var img     = new RoCMS.Web.Contract.Models.Image();
                    var fStream = File.OpenRead(goodsItem.PhotoUrl);
                    img.Size = fStream.Length;
                    byte[] bytes = new byte[fStream.Length];
                    fStream.Read(bytes, 0, (int)fStream.Length);

                    const string MIMETYPE_JPG = "image/jpeg";
                    const string MIMETYPE_PNG = "image/png";

                    string fileType = goodsItem.PhotoUrl.Split('.').Last();
                    img.Content     = bytes;
                    img.ContentType = fileType == "jpg" ? MIMETYPE_JPG : MIMETYPE_PNG;
                    var imageId = _imageService.UploadImage(img, goodsItem.PhotoUrl);
                    _albumService.AddImageToAlbum(albumId, imageId);

                    i.MainImageId = imageId;
                }
                catch
                {
                    Console.WriteLine("Failed to find image for {0}", i.Name);
                }
                try
                {
                    _shopService.CreateGoods(i);
                }
                catch
                {
                    Console.WriteLine("Failed to add {0}", i.Name);
                }
            }
        }