Exemplo n.º 1
0
        public async Task <IActionResult> Create([FromBody] SupplierResource supplierResource)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var supplier = _mapper.Map <SupplierResource, Supplier>(supplierResource);

            await _unitOfWork.SupplierRepository.AddAsync(supplier);

            // if something happens and the new item can not be saved, return the error
            if (!await _unitOfWork.CompleteAsync())
            {
                _logger.LogMessage(LoggingEvents.SavedFail, ApplicationConstants.ControllerName.Supplier, supplier.Id);
                return(FailedToSave(supplier.Id));
            }

            supplier = await _unitOfWork.SupplierRepository.GetSupplier(supplier.Id);

            var result = _mapper.Map <Supplier, SupplierResource>(supplier);

            _logger.LogMessage(LoggingEvents.Created, ApplicationConstants.ControllerName.Supplier, supplier.Id);
            return(CreatedAtRoute(ApplicationConstants.ControllerName.Supplier, new { id = supplier.Id }, result));
        }
Exemplo n.º 2
0
        public async Task <ActionResult <SupplierResource> > GetSupplier(
            Guid supplierIdentity,
            [FromServices] SupplierQuery supplierQuery)
        {
            SupplierResource supplier = await supplierQuery
                                        .GetSupplierAsync(supplierIdentity);

            if (supplier is null)
            {
                return(NotFound());
            }

            return(supplier);
        }
Exemplo n.º 3
0
        public async Task <ActionResult> Update(int id, SupplierResource supplierResource)
        {
            if (id != supplierResource.Id)
            {
                return(BadRequest());
            }
            var supplier = mapper.Map <SupplierResource, Supplier>(supplierResource);
            await service.Update(supplier);

            if (supplier == null)
            {
                return(NotFound());
            }
            return(NoContent());
        }
Exemplo n.º 4
0
        public async Task <IActionResult> Update(string id, [FromBody] SupplierResource supplierResource)
        {
            bool isValid = Guid.TryParse(id, out var supplierId);

            if (!isValid)
            {
                return(InvalidId(id));
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var supplier = await _unitOfWork.SupplierRepository.GetSupplier(supplierId);

            if (supplier == null)
            {
                return(NotFound(supplierId));
            }


            _mapper.Map <SupplierResource, Supplier>(supplierResource, supplier);

            if (!await _unitOfWork.CompleteAsync())
            {
                _logger.LogMessage(LoggingEvents.SavedFail, ApplicationConstants.ControllerName.Supplier, supplier.Id);
                return(FailedToSave(supplier.Id));
            }

            supplier = await _unitOfWork.SupplierRepository.GetSupplier(supplier.Id);

            var result = _mapper.Map <Supplier, SupplierResource>(supplier);

            _logger.LogMessage(LoggingEvents.Updated, ApplicationConstants.ControllerName.Supplier, supplier.Id);
            return(Ok(result));
        }