public bool CreateAsset(CreateAssetEvent asset) { try { var owningCustomerUID = asset.OwningCustomerUID.HasValue ? asset.OwningCustomerUID.Value : new Guid(); DateTime actionUTC = DateTime.UtcNow; //Db Object var assetObject = new DbModel.AssetPayload { AssetUID = new Guid(asset.AssetUID.ToString()), OwningCustomerUID = owningCustomerUID, AssetName = string.IsNullOrWhiteSpace(asset.AssetName) ? null : asset.AssetName, LegacyAssetID = asset.LegacyAssetID == -9999999 ? 0 : asset.LegacyAssetID, SerialNumber = asset.SerialNumber, MakeCode = asset.MakeCode, Model = string.IsNullOrWhiteSpace(asset.Model) ? null : asset.Model, AssetTypeName = string.IsNullOrWhiteSpace(asset.AssetType) ? null : asset.AssetType, IconKey = asset.IconKey == -9999999 ? null : asset.IconKey, EquipmentVIN = string.IsNullOrWhiteSpace(asset.EquipmentVIN) ? null : asset.EquipmentVIN, ModelYear = asset.ModelYear == -9999999 ? null : asset.ModelYear, InsertUTC = actionUTC, UpdateUTC = actionUTC, StatusInd = true, ObjectType = asset.ObjectType, Category = asset.Category, ProjectStatus = asset.ProjectStatus, SortField = asset.SortField, Source = asset.Source, UserEnteredRuntimeHours = asset.UserEnteredRuntimeHours, Classification = asset.Classification, PlanningGroup = asset.PlanningGroup }; var actions = new List <Action>() { () => _transaction.Upsert(assetObject), () => _transaction.Publish(GetGetAssetSourceSnapshotTopublish(assetObject, asset.ReceivedUTC.Value, false)) }; return(_transaction.Execute(actions)); } catch (MySqlException ex) { _logger.LogError("error while creating asset in db: ", ex); throw ex; } finally { } }
public int CreateAsset(string _connectionString, CreateAssetEvent asset) { int rowsAffected = 0; using (var connection = new MySqlConnection(_connectionString)) { connection.Open(); try { rowsAffected = connection.Execute( string.Format("insert into Asset ( AssetUID, AssetName, LegacyAssetID, SerialNumber, MakeCode, Model, AssetTypeName,IconKey,EquipmentVIN,ModelYear,InsertUTC,UpdateUTC,StatusInd) values ({0}, @AssetName, @LegacyAssetID, @SerialNumber, @MakeCode, @Model, @AssetTypeName,@IconKey,@EquipmentVIN,@ModelYear,@InsertUTC,@UpdateUTC,@StatusInd);" , asset.AssetUID.ToStringWithoutHyphens().WrapWithUnhex() ), new { AssetName = asset.AssetName == "$#$#$" || String.IsNullOrWhiteSpace(asset.AssetName) ? null : asset.AssetName, LegacyAssetID = asset.LegacyAssetID == -9999999 ? 0 : asset.LegacyAssetID, SerialNumber = asset.SerialNumber, MakeCode = asset.MakeCode, Model = asset.Model == "$#$#$" || String.IsNullOrWhiteSpace(asset.Model) ? null : asset.Model, AssetTypeName = asset.AssetType == "$#$#$" || String.IsNullOrWhiteSpace(asset.AssetType) ? null : asset.AssetType, IconKey = asset.IconKey == -9999999 ? null : asset.IconKey, EquipmentVIN = asset.EquipmentVIN == "$#$#$" || String.IsNullOrWhiteSpace(asset.EquipmentVIN) ? null : asset.EquipmentVIN, ModelYear = asset.ModelYear == -9999999 ? null : asset.ModelYear, InsertUTC = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss"), UpdateUTC = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss"), StatusInd = 1 }); connection.Close(); } catch (MySqlException ex) { if (!ex.Message.Contains("Duplicate")) { throw; } } finally { connection.Close(); } } return(rowsAffected); }
public void SetDefaultValidValuesToAssets() { CreateAsset = new CreateAssetEvent { AssetName = "ASSET" + Convert.ToString(DateTime.Now), LegacyAssetID = 18000265498, SerialNumber = "SN" + Convert.ToString(DateTime.Now), MakeCode = "MK001" + Convert.ToString(DateTime.Now), Model = "MODEL001", AssetType = "SKID STEER LOADERS", IconKey = 109, EquipmentVIN = "3AS6411", ModelYear = 2017, AssetUID = Guid.NewGuid(), OwningCustomerUID = CustomerUID, ActionUTC = DateTime.Now }; }
public void SetDefaultValues() { createAssetEvent = new CreateAssetEvent { AssetName = "Asset" + DateTime.UtcNow.ToString("yyyyMMddhhmmss"), LegacyAssetID = RandomNumber(), SerialNumber = "SerialNumber" + DateTime.UtcNow.ToString("yyyyMMddhhmmss"), MakeCode = "MakeCode" + DateTime.UtcNow.ToString("yyyyMMddhhmmss"), Model = "Model" + DateTime.UtcNow.ToString("yyyyMMddhhmmss"), AssetType = "AssetType" + DateTime.UtcNow.ToString("yyyyMMddhhmmss"), IconKey = RandomNumber(), EquipmentVIN = "EquipmentVIN" + DateTime.UtcNow.ToString("yyyyMMddhhmmss"), ModelYear = 2017, OwningCustomerUID = Guid.Parse(CustomerUID), AssetUID = Guid.NewGuid(), ActionUTC = DateTime.UtcNow, ReceivedUTC = DateTime.UtcNow }; }
public int CreateAssetSummary(string _connectionString, CreateAssetEvent asset) { int rowsAffected = 0; using (var connection = new MySqlConnection(_connectionString)) { connection.Open(); try { rowsAffected = connection.Execute( string.Format("insert into AssetSummary ( AssetUID, AssetName, SerialNumber, MakeCode, Model, Family,ModelYear,IconKey,LastAssetSummaryUTC) values ({0}, @AssetName, @SerialNumber, @MakeCode, @Model, @Family,@ModelYear,@IconKey,@LastAssetSummaryUTC);" , asset.AssetUID.ToStringWithoutHyphens().WrapWithUnhex() ), new { AssetName = asset.AssetName == "$#$#$" || String.IsNullOrWhiteSpace(asset.AssetName) ? null : asset.AssetName, SerialNumber = asset.SerialNumber, MakeCode = asset.MakeCode, Model = asset.Model == "$#$#$" || String.IsNullOrWhiteSpace(asset.Model) ? null : asset.Model, Family = asset.AssetType == "$#$#$" || String.IsNullOrWhiteSpace(asset.AssetType) ? null : asset.AssetType, ModelYear = asset.ModelYear == -9999999 ? null : asset.ModelYear, IconKey = asset.IconKey == -9999999 ? null : asset.IconKey, LastAssetSummaryUTC = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss"), }); connection.Close(); } catch (MySqlException ex) { if (!ex.Message.Contains("Duplicate")) { throw; } } finally { connection.Close(); } } return(rowsAffected); }
public void Create_Asset() { //Arrange for Create var assetObject = new CreateAssetEvent() { AssetName = "TestAssetName", AssetType = "loader", SerialNumber = "TestSerialNumber", AssetUID = Guid.NewGuid(), MakeCode = "TestMake", Model = "model", EquipmentVIN = "equipmentVIN", IconKey = 1, LegacyAssetID = 1, ActionUTC = DateTime.UtcNow, ModelYear = 2016, ReceivedUTC = DateTime.UtcNow, OwningCustomerUID = Guid.NewGuid(), ObjectType = "test", Category = "test", ProjectStatus = "test", SortField = "test", Source = "test", Classification = "test", PlanningGroup = "test", UserEnteredRuntimeHours = "1234" }; _transaction.Execute(Arg.Any <List <Action> >()).Returns(x => { foreach (var action in x.Arg <List <Action> >()) { action(); } return(true); }); //Act Assert.True(_assetServices.CreateAsset(assetObject)); }
public ActionResult CreateAsset([FromBody] CreateAssetEvent asset) { try { //var jwt = GetSampleJwt(); //use this for testing locally if (!Request.Headers.TryGetValue("X-JWT-Assertion", out StringValues headerValues)) { return(BadRequest("Could not validate X-VisionLink-UserUid or X-JWT-Assertion Headers in Request")); } TPaaSJWT jwt; try { jwt = new TPaaSJWT(headerValues); } catch (Exception ex) { jwt = null; } if (jwt != null) { _logger.LogInformation("Creating asset for user: {0}", jwt.UserUid); } else { return(BadRequest("no jwt token")); } asset.ReceivedUTC = DateTime.UtcNow; _logger.LogDebug($"CreateAsset - Calling Application Name: {jwt.ApplicationName}, UserType: {jwt.UserType}"); bool isCGIntegrator = jwt.UserType == "APPLICATION" && jwt.ApplicationName == _configuration["CGIntegratorAppName"]; if (isCGIntegrator) { if (asset.AssetUID == null || asset.AssetUID.Equals(Guid.Empty) || !Guid.TryParse(asset.AssetUID.ToString(), out Guid g)) { return(BadRequest("AssetUID must be given for VisionLink integrator")); } if (asset.OwningCustomerUID == null) { asset.OwningCustomerUID = new Guid(); } } else if (!string.IsNullOrEmpty(jwt.ApplicationName)) { asset.AssetUID = Guid.NewGuid(); var customers = _assetService.GetCustomersForApplication(jwt.ApplicationName); if (customers?.Count > 0) { if (asset.OwningCustomerUID == null) { asset.OwningCustomerUID = customers.First(); } } else { return(BadRequest("Application does not have any customers mapped. Please contact your API administrator.")); } Guid?existingAssetGuid = _assetService.GetAssetUid(asset.AssetUID.Value, asset.MakeCode, asset.SerialNumber); if (existingAssetGuid.HasValue) { return(StatusCode((int)HttpStatusCode.Conflict, (existingAssetGuid.Value.ToString()))); } } else { return(BadRequest("jwt application name is empty")); } Guid?existingAsset = _assetService.GetAssetUid(asset.AssetUID.Value, asset.MakeCode, asset.SerialNumber); if (existingAsset.HasValue) { if (isCGIntegrator) { var updatePayload = new UpdateAssetEvent { AssetUID = existingAsset.Value, OwningCustomerUID = asset.OwningCustomerUID, LegacyAssetID = asset.LegacyAssetID, AssetName = asset.AssetName, Model = asset.Model, AssetType = asset.AssetType, IconKey = asset.IconKey, EquipmentVIN = asset.EquipmentVIN, ModelYear = asset.ModelYear, ActionUTC = DateTime.UtcNow, ObjectType = asset.ObjectType, Category = asset.Category, ProjectStatus = asset.ProjectStatus, SortField = asset.SortField, Source = asset.Source, UserEnteredRuntimeHours = asset.UserEnteredRuntimeHours, Classification = asset.Classification, PlanningGroup = asset.PlanningGroup }; var updateResult = UpdateAssetInfo(updatePayload); return(updateResult.GetType().Name == "OkObjectResult" ? StatusCode((int)HttpStatusCode.Conflict, (existingAsset.Value.ToString())) : updateResult); } return(Conflict(new { message = $"Asset already exists" })); } if (!_assetService.IsValidMakeCode(asset.MakeCode.ToUpper())) { return(BadRequest($"Asset make code '{asset.MakeCode.ToUpper()}' is not valid.")); } if (_assetService.CreateAsset(asset)) { return(Ok(asset.AssetUID.ToString())); } return(BadRequest("Unable to save to db. Make sure request is not duplicated and all keys exist")); } catch (Exception ex) { _logger.LogError("Create Asset Exception: " + ex.ToString()); return(StatusCode((int)HttpStatusCode.InternalServerError, ex.Message)); } }
public void GetAssetDeviceList(string searchType = null, string pageNo = null, string pageSize = null) { createAssetEvent = AssetServiceSteps.assetServiceSupport.CreateAssetModel; string searchString = string.Empty; string requestType = string.Empty; if (searchType == "AssetSN") { searchString = createAssetEvent.SerialNumber; } else if (searchType == "AssetName") { searchString = createAssetEvent.AssetName; } else if (searchType == "DeviceSN") { searchString = CreateDeviceModel.DeviceSerialNumber; } else if (searchType == "AssetSNSorting" || searchType == "Valid") { searchString = validSearchString; } else if (searchType == "Wrong") { searchString = wrongSearchString; } else { searchString = searchType; } if (pageNo == "Valid") { pageNo = validPageNo; } else if (pageNo == "Invalid") { pageNo = invalidPageNo; requestType = "Invalid"; } else if (pageNo == "Wrong") { pageNo = wrongPageNo; } else if (pageNo == "0" || pageNo == "-1") { requestType = "Invalid"; } if (pageSize == "Valid") { pageSize = validPageSize; } else if (pageSize == "Invalid") { pageSize = invalidPageSize; requestType = "Invalid"; } else if (pageSize == "Wrong") { pageSize = wrongPageSize; } else if (pageSize == "0" || pageSize == "-1") { requestType = "Invalid"; } string parameters = string.Empty; if (searchString != null && pageNo == null && pageSize == null) { parameters = "?" + AssetServiceConfig.SearchString + "=" + searchString; } else if (searchString == null && pageNo != null && pageSize == null) { parameters = "?" + AssetServiceConfig.PageNo + "=" + pageNo; } else if (searchString == null && pageNo == null && pageSize != null) { parameters = "?" + AssetServiceConfig.PageSize + "=" + pageSize; } else if (searchString != null && pageNo != null && pageSize == null) { parameters = "?" + AssetServiceConfig.SearchString + "=" + searchString + "&" + AssetServiceConfig.PageNo + "=" + pageNo; } else if (searchString != null && pageNo == null && pageSize != null) { parameters = "?" + AssetServiceConfig.SearchString + "=" + searchString + "&" + AssetServiceConfig.PageSize + "=" + pageSize; } else if (searchString == null && pageNo != null && pageSize != null) { parameters = "?" + AssetServiceConfig.PageNo + "=" + pageNo + "&" + AssetServiceConfig.PageSize + "=" + pageSize; } else if (searchString != null && pageNo != null && pageSize != null) { parameters = "?" + AssetServiceConfig.SearchString + "=" + searchString + "&" + AssetServiceConfig.PageNo + "=" + pageNo + "&" + AssetServiceConfig.PageSize + "=" + pageSize; } else if (searchString == null && pageNo == null && pageSize == null) { parameters = ""; } try { string accessToken = AssetServiceConfig.GetValidUserAccessToken(); LogResult.Report(Log, "log_ForInfo", "Getting the AssetDetail Values:"); if (requestType == "Invalid") { ResponseString = RestClientUtil.DoInvalidHttpRequest(AssetServiceConfig.AssetSearchEndpoint + parameters, HeaderSettings.GetMethod, accessToken, HeaderSettings.JsonMediaType, null, HttpStatusCode.BadRequest, HeaderSettings.BearerType, HeaderSettings.JsonMediaType); } else { ResponseString = RestClientUtil.DoHttpRequest(AssetServiceConfig.AssetSearchEndpoint + parameters, HeaderSettings.GetMethod, accessToken, HeaderSettings.JsonMediaType, null, HttpStatusCode.OK, HeaderSettings.BearerType, HeaderSettings.JsonMediaType); } } catch (Exception e) { LogResult.Report(Log, "log_ForError", "Got Error While Getting Data from Asset Device Search Service", e); throw new Exception(e + " Got Error While Getting Data from Asset Device Search Service"); } }