コード例 #1
0
        public Respone AddProduct(ProductCreateViewModel product, string username)
        {
            User current = UserRepository.GetItemByID(username);

            if (current == null)
            {
                return new Respone {
                           IsDone = false, Message = "Only signed users can add products"
                }
            }
            ;
            else
            {
                try
                {
                    product.Product.PublicationDate = DateTime.Now;
                    product.Product.UploadedByUser  = current;
                    if (Validator.TryValidateObject(product.Files, new ValidationContext(product.Files), null))
                    {
                        ProductRepository.InsertItem(product.Product);
                        PictureRepository.InsertItem(new Picture()
                        {
                            IsMainPicture = true, ProductID = product.Product, Image = PicturesSerializationAdapter.Serialize(product.Files.MainImage)
                        });
                        PictureRepository.InsertItem(new Picture()
                        {
                            ProductID = product.Product, Image = PicturesSerializationAdapter.Serialize(product.Files.Image2)
                        });
                        PictureRepository.InsertItem(new Picture()
                        {
                            ProductID = product.Product, Image = PicturesSerializationAdapter.Serialize(product.Files.Image3)
                        });
                        Save();
                        return(new Respone {
                            IsDone = true
                        });
                    }
                    else
                    {
                        throw new Exception("Invalid files");
                    }
                }
                catch (Exception e)
                {
                    Rollback();
                    return(new Respone {
                        IsDone = false, Message = e.Message
                    });
                }
            }
        }