예제 #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, cancellationToken);

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

            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 <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, cancellationToken);

            if (newItem.Id != 0)
            {
                //We disabled the upload functionality and added a default/placeholder image to this sample due to a potential security risk
                //  pointed out by the community. More info in this issue: https://github.com/dotnet-architecture/eShopOnWeb/issues/537
                //  In production, we recommend uploading to a blob storage and deliver the image via CDN after a verification process.

                newItem.UpdatePictureUri("eCatalog-item-default.png");
                await _itemRepository.UpdateAsync(newItem, cancellationToken);
            }

            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);
        }
예제 #3
0
    public async Task <IResult> HandleAsync(CreateCatalogItemRequest request)
    {
        var response = new CreateCatalogItemResponse(request.CorrelationId());

        var catalogItemNameSpecification = new CatalogItemNameSpecification(request.Name);
        var existingCataloogItem         = await _itemRepository.CountAsync(catalogItemNameSpecification);

        if (existingCataloogItem > 0)
        {
            throw new DuplicateException($"A catalogItem with name {request.Name} already exists");
        }

        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)
        {
            //We disabled the upload functionality and added a default/placeholder image to this sample due to a potential security risk
            //  pointed out by the community. More info in this issue: https://github.com/dotnet-architecture/eShopOnWeb/issues/537
            //  In production, we recommend uploading to a blob storage and deliver the image via CDN after a verification process.

            newItem.UpdatePictureUri("eCatalog-item-default.png");
            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(Results.Created($"api/catalog-items/{dto.Id}", response));
    }