/// <inheritdoc/>
        public async Task <StoreItem> CreateStoreItemAsync(UpdateStoreItemRequest createRequest)
        {
            var dbProduct = _productContext.Products.Where(p => p.Id == createRequest.ProductId).FirstOrDefault();

            if (dbProduct == null)
            {
                throw new RequestedResourceHasConflictException("Product");
            }

            var dbAllowedSectionCatagegories = _categoryContext.Categories.Where(c => c.StoreHiveSectionId == createRequest.HiveSectionId && c.ProductCategoryId == dbProduct.Category.Id).FirstOrDefault();

            if (dbAllowedSectionCatagegories == null)
            {
                throw new RequestedResourceHasConflictException("Unallowable product category for this hive section.");
            }

            var dbStoreItem = Mapper.Map <UpdateStoreItemRequest, DbStoreItem>(createRequest);

            dbStoreItem.CreatedDate = DateTime.UtcNow;
            _storeItemContext.Items.Add(dbStoreItem);

            await _storeItemContext.SaveChangesAsync();

            return(Mapper.Map <StoreItem>(dbStoreItem));
        }
        /// <inheritdoc/>
        public async Task AddHiveSectionCategoryProductAsync(UpdateHiveSectionCategoryProduct createRequest)
        {
            StoreItem newItem = new StoreItem();

            newItem.Id            = _context.Items.Max(i => i.Id) + 1;
            newItem.ProductId     = createRequest.Id;
            newItem.Quantity      = createRequest.Quantity;
            newItem.HiveSectionId = createRequest.HiveSectionId;

            _context.Items.Add(newItem);

            await _context.SaveChangesAsync();
        }
Пример #3
0
        /// <inheritdoc/>
        public async Task <ProductStoreItemRequest> CreateRequestAsync(UpdateRequestRequest createRequest)
        {
            var dbProduct = await _productService.GetProductAsync(createRequest.ProductId);

            var dbHiveSection = await _hiveSectionService.GetHiveSectionAsync(createRequest.HiveSectionId);

            var dbRequest = Mapper.Map <UpdateRequestRequest, DbProductStoreItemRequest>(createRequest);

            _context.Requests.Add(dbRequest);

            await _context.SaveChangesAsync();

            return(Mapper.Map <ProductStoreItemRequest>(dbRequest));
        }
        public async Task ConfirmRequestAsync(int requestId)
        {
            var dbRequests = await _requestContext.Requests.Where(r => r.Id == requestId).ToArrayAsync();

            var dbRequest = dbRequests.FirstOrDefault();

            if (dbRequest == null)
            {
                throw new RequestedResourceNotFoundException();
            }

            var dbProducts = await _productContext.Items.Where(p => p.ProductId == dbRequest.ProductId && p.HiveSectionId == dbRequest.HiveSectionId).ToArrayAsync();

            if (dbProducts.Length == 0)
            {
                throw new RequestedResourceNotFoundException();
            }

            dbProducts[0].Quantity += dbRequests[0].Quantity;
            dbRequests[0].Status    = true;

            await _requestContext.SaveChangesAsync();

            await _productContext.SaveChangesAsync();
        }
Пример #5
0
        /// <inheritdoc/>
        public async Task <ProductStoreItem> CreateProductStoreItemAsync(UpdateProductStoreItemRequest createRequest)
        {
            var dbItems = await _context.Items.Where(i => i.ProductId == createRequest.ProductId && i.HiveSectionId == createRequest.HiveSectionId).ToArrayAsync();

            if (dbItems.Length > 0)
            {
                throw new RequestedResourceHasConflictException("Product Id and/or Hive section Id");
            }

            var dbItem = Mapper.Map <UpdateProductStoreItemRequest, DbProductStoreItem>(createRequest);

            _context.Items.Add(dbItem);

            await _context.SaveChangesAsync();

            return(Mapper.Map <ProductStoreItem>(dbItem));
        }
Пример #6
0
        public async Task <HiveSectionProduct> CreateHiveSectionProductAsync(UpdateHiveSectionProductRequest createRequest)
        {
            var dbStoreItems = await _context.Items.Where(p => p.ProductId == createRequest.ProductId && p.HiveSectionId == createRequest.HiveSectionId).ToArrayAsync();

            if (dbStoreItems.Length > 0)
            {
                throw new RequestedResourceHasConflictException("ProductId");
            }

            var dbStoreItem = Mapper.Map <UpdateHiveSectionProductRequest, DbItem>(createRequest);

            _context.Items.Add(dbStoreItem);

            await _context.SaveChangesAsync();

            return(Mapper.Map <HiveSectionProduct>(dbStoreItem));
        }
Пример #7
0
        /// <inheritdoc/>
        public async Task ApproveRequestAsync(int requestId)
        {
            var dbRequest = _productStoreRequestContext.Requests.Where(r => r.Id == requestId).FirstOrDefault();

            if (dbRequest == null)
            {
                throw new RequestedResourceNotFoundException();
            }

            var dbStoreItem = _productStoreContext.Items.Where(i => i.Id == dbRequest.ProductStoreId).FirstOrDefault();

            if (dbStoreItem == null)
            {
                throw new RequestedResourceNotFoundException();
            }

            dbStoreItem.Quantity += dbRequest.Quantity;
            dbRequest.IsProcessed = true;

            await _productStoreRequestContext.SaveChangesAsync();

            await _productStoreContext.SaveChangesAsync();
        }