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)); }
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); }
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()); }
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)); }