public async Task <string> SaveAsync(Asset asset) { try { await _assetRepository.AddAsync(asset); await _unitOfWork.CompleteAsync(); return(MessageConstants.Saved);; } catch (Exception ex) { // Do some logging stuff return($"{MessageConstants.SavedWarning}: {ex.Message}");; } }
public async Task <SaveAssetResponse> SaveAsync(Asset asset) { try { await _assetRepository.AddAsync(asset); await _unitOfWork.CompleteAsync(); return(new SaveAssetResponse(asset)); } catch (Exception ex) { return(new SaveAssetResponse($"An error occured when saving the asset: {ex.Message}")); } }
public async Task <IActionResult> PostAssetAsync([FromBody] Asset asset, [FromRoute] int id) { Asset created; try { var stock = await _stockRepository.GetAsync(asset.Stock.Symbol); asset.Stock.Id = stock.Id; var portfolio = await _portfolioRepository.GetAsync(id); created = await _assetRepository.AddAsync(asset, portfolio); } catch (ArgumentException e) { _logger.LogInformation(e, "Attempted to add an asset with an already existing Id."); return(BadRequest(e.Message)); } catch (DbUpdateException e) { _logger.LogInformation(e, "Attempted to add an asset that violated database constraints."); return(BadRequest(e.Message)); } _logger.LogInformation("Successfully created asset.", created); return(CreatedAtAction(nameof(AssetsController.GetByIdAsync), "Assets", new { id = created.Id }, created)); }
public async Task <bool> Handle(StoreAssetWithOutFileCommand request, CancellationToken cancellationToken) { if (!request.IsValid()) { await NotifyValidationErrors(request); return(false); } var category = await _assetCategoryRepository.GetByIdAsync(request.AssetCategoryId); if (category == null) { await Bus.RaiseEventAsync(new DomainNotification("系统错误", "未找到相关分类,请联系管理员")); return(false); } var tagNumberPrefix = request.StartTagNumber.Substring(0, 10); var startCountParseResult = int.TryParse(request.StartTagNumber.Substring(10, 5), out var startCount); if (!startCountParseResult) { await Bus.RaiseEventAsync(new DomainNotification("操作错误", "录入的起始标签号有误,请重新填写")); return(false); } var endCountParseResult = int.TryParse(request.EndTagNumber.Substring(10, 5), out var endCount); if (!endCountParseResult) { await Bus.RaiseEventAsync(new DomainNotification("操作错误", "录入的结束标签号有误,请重新填写")); return(false); } for (var operationCount = startCount; operationCount <= endCount; operationCount++) { var tagNumber = new StringBuilder(); var tagNumberSuffix = new StringBuilder(operationCount.ToString()); while (tagNumberSuffix.Length != 5) { tagNumberSuffix.Insert(0, '0'); } tagNumber.Append(tagNumberPrefix); tagNumber.Append(tagNumberSuffix); var asset = new Asset { AssetStatus = AssetStatus.在库, Id = Guid.NewGuid(), AssetLocation = request.AssetLocation, AssetCategoryId = category.Id, AssetName = request.AssetName, Brand = request.Brand, AssetDescription = request.AssetDescription, AssetType = request.AssetType, AssetTagNumber = tagNumber.ToString(), InStoreDateTime = DateTime.Now, LastModifyDateTime = DateTime.Now, LatestDeployRecord = request.Message, CreateDateTime = request.CreateDateTime, OrganizationBelongedId = _user.OrgId, StoredOrgIdentifier = _user.OrgIdentifier, StoredOrgName = _user.OrgNam }; await _assetRepository.AddAsync(asset); } if (await CommitAsync()) { return(true); } return(false); }