コード例 #1
0
        /// <inheritdoc />
        public async Task <Result> AddProduct(ProductDto productDto)
        {
            try
            {
                var picture     = new Picture();
                var listPicture = new List <Picture>();

                if (productDto.Picture != null)
                {
                    picture = await GetFile(productDto.Picture);
                }

                if (productDto.Files != null && productDto.Files.Count > 0)
                {
                    listPicture = productDto.Files.Select(file => GetFile(file).Result).ToList();
                }

                var manufacturer = await _context.Manufacturers.FindAsync(productDto.ManufacturerId);

                var listRecomendedProducts = await _context.Products
                                             .Where(x => productDto.RecomendedProductsId.Contains(x.Id))
                                             .ToListAsync();

                var category = await _context.Categories.FindAsync(productDto.CategoryId);

                var product = GetNewProduct(productDto, picture, manufacturer, category);

                _context.Add(product);
                _context.SaveChanges();

                var productPictures = GetProductPictureList(listPicture, product);

                _context.AddRange(productPictures);
                _context.SaveChanges();

                var listProductProduct = GetProductProductList(listRecomendedProducts, product);

                _context.AddRange(listProductProduct);
                _context.SaveChanges();

                return(Result.Ok());
            }
            catch (Exception e)
            {
                throw new ApplicationException(e.Message);
            }
        }
コード例 #2
0
        /// <inheritdoc />
        public async Task <Result> SaveVideo(SaveLinkVideoDto saveLinkVideoDto)
        {
            await using var transaction = _context.Database.BeginTransaction();
            try
            {
                if (saveLinkVideoDto.ProductId != null && saveLinkVideoDto.Videos.Count > 0)
                {
                    var videos = saveLinkVideoDto.Videos.Select(x => new Video
                    {
                        CategoryId = x.ChanelId, Link = x.Video, PlaylistId = x.PlaylistId, Title = x.Title
                    });

                    _context.AddRange(videos);
                    _context.SaveChanges();

                    var product = await _context.Products.FindAsync(saveLinkVideoDto.ProductId);

                    var videoProduct = videos.Select(video => new VideoProduct {
                        Product = product, ProductId = product.Id, Video = video, VideoId = video.Id
                    });

                    _context.AddRange(videoProduct);
                    _context.SaveChanges();
                    transaction.Commit();
                    return(Result.Ok());
                }

                return(Result.Fail(
                           $"Запись видео не удалась. Отсутствует товар с таким идентификатором: {saveLinkVideoDto.ProductId}"));
            }
            catch (Exception e)
            {
                await transaction.RollbackAsync();

                throw;
            }
        }
コード例 #3
0
        /// <inheritdoc />
        public Result UpdateOrder(OrderDto orderDto)
        {
            using var transaction = _context.Database.BeginTransaction();
            try
            {
                var customer         = _context.Customers.Find(orderDto.CustomerId);
                var productsQueryNew =
                    _context.Products.Where(x => orderDto.ProductsId.Contains(x.Id)).ToList();
                var order = _context.Orders.Include(x => x.Products).FirstOrDefault(x => x.Id == orderDto.Id);

                if (order != null)
                {
                    order.Customer   = customer ?? null;
                    order.Sum        = orderDto.Sum;
                    order.TimeAdd    = orderDto.TimeAdd;
                    order.TimeUpdate = orderDto.TimeUpdate;

                    _context.Update(order);
                    _context.SaveChanges();

                    if (productsQueryNew.Count > 0)
                    {
                        var oldProduct = order.Products;
                        if (oldProduct.Count > 0)
                        {
                            _context.RemoveRange(oldProduct);
                        }

                        var products = productsQueryNew.Select(x => new OrderProduct
                        {
                            OrderId = order.Id, Order = order, Product = x, ProductId = x.Id
                        });

                        _context.AddRange(products);
                        _context.SaveChanges();
                    }

                    transaction.Commit();
                    return(Result.Ok());
                }

                return(Result.Fail("Обновление не увенчалось успехом."));
            }
            catch (Exception e)
            {
                transaction.Rollback();
                throw new ApplicationException(e.InnerException.Message ?? e.Message);
            }
        }