예제 #1
0
        public override async Task <ActionResult <CreateCatalogItemResponse> > HandleAsync(CreateCatalogItemRequest request, CancellationToken cancellationToken)
        {
            var response = new CreateCatalogItemResponse(request.CorrelationId());

            var newItem = new CatalogItem(request.CatalogTypeId, request.CatalogBrandId, request.Description, request.Name, request.Price, request.PictureUri);

            newItem = await _itemRepository.AddAsync(newItem);

            if (newItem.Id != 0)
            {
                var picName = $"{newItem.Id}{Path.GetExtension(request.PictureName)}";
                if (await _webFileSystem.SavePicture(picName, request.PictureBase64))
                {
                    newItem.UpdatePictureUri(picName);
                    await _itemRepository.UpdateAsync(newItem);
                }
            }

            var dto = new CatalogItemDto
            {
                Id             = newItem.Id,
                CatalogBrandId = newItem.CatalogBrandId,
                CatalogTypeId  = newItem.CatalogTypeId,
                Description    = newItem.Description,
                Name           = newItem.Name,
                PictureUri     = _uriComposer.ComposePicUri(newItem.PictureUri),
                Price          = newItem.Price
            };

            response.CatalogItem = dto;
            return(response);
        }
예제 #2
0
        public override async Task <ActionResult <UpdateCatalogItemResponse> > HandleAsync(UpdateCatalogItemRequest request, CancellationToken cancellationToken)
        {
            var response = new UpdateCatalogItemResponse(request.CorrelationId());

            var existingItem = await _itemRepository.GetByIdAsync(request.Id);

            existingItem.UpdateDetails(request.Name, request.Description, request.Price);
            existingItem.UpdateBrand(request.CatalogBrandId);
            existingItem.UpdateType(request.CatalogTypeId);

            if (string.IsNullOrEmpty(request.PictureBase64) && string.IsNullOrEmpty(request.PictureUri))
            {
                existingItem.UpdatePictureUri(string.Empty);
            }
            else
            {
                var picName = $"{existingItem.Id}{Path.GetExtension(request.PictureName)}";
                if (await _webFileSystem.SavePicture($"{picName}", request.PictureBase64))
                {
                    existingItem.UpdatePictureUri(picName);
                }
            }

            await _itemRepository.UpdateAsync(existingItem);

            var dto = new CatalogItemDto
            {
                Id             = existingItem.Id,
                CatalogBrandId = existingItem.CatalogBrandId,
                CatalogTypeId  = existingItem.CatalogTypeId,
                Description    = existingItem.Description,
                Name           = existingItem.Name,
                PictureUri     = _uriComposer.ComposePicUri(existingItem.PictureUri),
                Price          = existingItem.Price
            };

            response.CatalogItem = dto;
            return(response);
        }