public async Task NewCatalogItem()
        {
            CreateCatalogItemResponse createCatalogItemResponse =
                await PostJsonAsync <CreateCatalogItemRequest, CreateCatalogItemResponse>
                (
                    CreateCatalogItemRequest.GetRoute(),
                    CreateCatalogItemRequest
                );

            ValidateCreateCatalogItemResponse(createCatalogItemResponse);
        }
Example #2
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));
    }
 private void ValidateCreateCatalogItemResponse(CreateCatalogItemResponse aCreateCatalogItemResponse)
 {
     aCreateCatalogItemResponse.CorrelationId.Should().Be(CreateCatalogItemRequest.CorrelationId);
 }
Example #4
0
        public async Task NewCatalogItem()
        {
            CreateCatalogItemResponse createCatalogItemResponse = await Send(CreateCatalogItemRequest);

            ValidateCreateCatalogItemResponse(createCatalogItemResponse);
        }