コード例 #1
0
        public async Task <ActionResult <IEnumerable <ResponseProductList> > > GetProducts()
        {
            var email         = User.Identity.Name;
            var currentWorker = await _context.Workers.SingleOrDefaultAsync(x => x.Email == email);

            var products = await _context.Products
                           .Include(x => x.FavoriteProductsWorkers)
                           .ToListAsync();

            return(Ok(products.Select(x =>
            {
                //var fullName = Constans.ApiUrl + Constans.FileDownloadPart +
                var image = _context.Files.SingleOrDefault(y => x.ImageUrl.Contains(y.Name));
                var isFavorite = x.FavoriteProductsWorkers.Any(y => y.WorkerId == currentWorker.Id && y.FavoriteProductId == x.Id);

                return ResponseProductList.FromApiProduct(x, image: image, isFavorite: isFavorite, isCanBuy: currentWorker.Balance >= x.Price);
            }).ToList()));
        }
コード例 #2
0
        public async Task <ActionResult <ResponseProductList> > AddProduct([FromBody] CreateProduct createProduct)
        {
            var email       = User.Identity.Name;
            var currentUser = await _context.Workers.SingleAsync(x => x.Email == email);

            var file = await _context.Files.SingleOrDefaultAsync(x => x.Id == createProduct.FileId);

            if (file == null)
            {
                return(NotFound(new ExceptionInfo
                {
                    Message = ProductErrorsMessages.FileNotFound,
                    Description = "Не найден файл"
                }));
            }

            var fileUrl = Constans.ApiUrl + Constans.FileDownloadPart + file.Name;

            var tempProduct = new Product
            {
                Name        = createProduct.Name,
                Descriptiom = createProduct.Description,
                Price       = createProduct.Price,
                ImageUrl    = fileUrl
            };

            _context.Products.Add(tempProduct);
            await _context.SaveChangesAsync();

            var addedProduct = await _context.Products
                               .Include(x => x.FavoriteProductsWorkers)
                               .SingleAsync(x => x.Id == tempProduct.Id);

            var isFavorite = addedProduct.FavoriteProductsWorkers.Any(y => y.WorkerId == currentUser.Id && y.FavoriteProductId == addedProduct.Id);

            return(Ok(ResponseProductList.FromApiProduct(tempProduct, image: file, isFavorite: isFavorite, isCanBuy: currentUser.Balance >= tempProduct.Price)));
        }