Пример #1
0
        public async Task <ActionResult <VendorDto> > CreateVendorAsync(VendorForCreationDto vendorForCreationDto)
        {
            // TODO: Setup "REQUIRED" attributes on your entities to replace this code.
            // Maybe implement some custom "Validation Attributes" just for practice.
            //if (string.IsNullOrEmpty(vendorForCreationDto.Name))
            //{
            //    var msg = "No Vendor name found in request.";
            //    _logger.LogError(msg);
            //    return UnprocessableEntity(new { status = "Error:", message = msg });

            //}

            var vendorEntity = _mapper.Map <Vendor>(vendorForCreationDto);

            // Get the new "vendor added" that contains the vendor Id from the datastore
            var vendorAdded = await _service.CreateVendorAsync(vendorEntity).ConfigureAwait(false);

            // Map the new vendor to a "presentation vendor" or DTO vendor
            // Yeah, yeah, I could just update the Id on the vendorDto that was passed in and return that
            // but, I'd rather be consistent with the vendor that is presented to the client, always a VendorDto.
            // And besides, we do all the mapping in one place, in the MAPPER.
            var vendorDtoToReturn = _mapper.Map <VendorDto>(vendorAdded);

            vendorDtoToReturn.Links = CreateLinksForVendor(vendorDtoToReturn);

            // Return the DTO vendor
            return(CreatedAtRoute(nameof(GetVendorAsync), new { id = vendorDtoToReturn.Id }, vendorDtoToReturn));
        }