示例#1
0
        public async Task <ActionResult <ItemResponse> > SaveItem(long id, [FromBody] ItemUpdateRequest request)
        {
            if (request == null)
            {
                return(BadRequest());
            }

            var item = await itemRepository.FindById(id).ConfigureAwait(false);

            item.SellPrice  = request.SellPrice;
            item.StockPrice = request.StockPrice;
            item.Stock      = request.Stock;


            var success = await itemRepository.Update(item).ConfigureAwait(false);

            if (success)
            {
                await cache.Invalidate(item.CategoryId);

                return(Ok(ItemResponse.Of(item)));
            }
            else
            {
                return(Conflict());
            }
        }
        public async Task <ActionResult <ItemResponse> > SaveItem(long id, [FromBody] NewItemRequest request)
        {
            var category = await cache.TryGet(id);

            if (category == null)
            {
                category = await categoryRepository.FindById(id).ConfigureAwait(false);
            }

            if (category == null)
            {
                return(NotFound());
            }

            var newItem = request.ToNew(category);

            var savedItem = await categoryRepository.InsertItem(category.Id, newItem);

            if (savedItem != null)
            {
                await cache.Invalidate(id);

                return(Created(nameof(SaveItem), ItemResponse.Of(savedItem)));
            }
            else
            {
                return(Conflict());
            }
        }