public bool InsertDevicesAndBackEndBackEnd(DevicesSubmitViewModel param) { _db.Database.BeginTransaction(); try { _logger.LogInformation("Populating Object Device"); var dataDevice = new Device(); dataDevice.Id = Guid.NewGuid().ToString(); dataDevice.IMEI = param.IMEI; dataDevice.Model = param.Model; dataDevice.SimCardNo = param.SimCardNo; dataDevice.Enabled = param.Enabled; dataDevice.CreatedDate = param.CreatedDate; dataDevice.CreatedBy = param.CreatedBy; _db.Devices.Add(dataDevice); _logger.LogInformation("Add Data Device"); if (param.listBackEnd.Any()) { _logger.LogInformation("Start Loop detail Backend"); for (int i = 0; i < param.listBackEnd.Count(); i++) { _logger.LogInformation("Populating Object BackEnd"); var dataBackEnd = new Backend(); dataBackEnd.Id = Guid.NewGuid().ToString(); dataBackEnd.Name = param.listBackEnd[i].Name; dataBackEnd.Address = param.listBackEnd[i].Address; _db.Backends.Add(dataBackEnd); _logger.LogInformation("Add Data BackEnd"); _logger.LogInformation("Populating Object DeviceBackEnd"); var dataDeviceMappingBackEnd = new DeviceBackend(); dataDeviceMappingBackEnd.Id = Guid.NewGuid().ToString(); dataDeviceMappingBackEnd.IdBackEnd = dataBackEnd.Id; dataDeviceMappingBackEnd.IdDevice = dataDevice.Id; dataDeviceMappingBackEnd.MappedTime = DateTime.Now; _db.DeviceBackends.Add(dataDeviceMappingBackEnd); _logger.LogInformation("Add Data DeviceBackEnd"); } } _logger.LogInformation("Start Save Change"); _db.SaveChanges(); _db.Database.CommitTransaction(); return(true); } catch (Exception ex) { _db.Database.RollbackTransaction(); _logger.Log(LogLevel.Critical, ex.Message.ToString()); throw new ArgumentException(ex.Message); } }
public JsonResult UpdateDevicesAndBackEndMapping([FromBody] DevicesSubmitViewModel param) { try { bool returnData = _facade.UpdateDevicesAndBackEndBackEnd(param); return(Json(new ApiResult <bool>() { isSuccessful = true, Payload = true })); } catch (Exception ex) { return(Json(new ApiResult <bool>() { isSuccessful = false, Payload = false, message = ex.Message })); } }
public async Task <JsonResult> DevicesSubmit([FromForm] DevicesSubmitViewModel param) { try { var contentType = "application/json"; ApiResult <bool> resutlApi = new ApiResult <bool>(); _client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(contentType)); var dataParameter = JsonConvert.SerializeObject(param); StringContent contentData = new StringContent(dataParameter, Encoding.UTF8, "application/json"); if (param.Mode == "Create") { resutlApi = await _client.PostAsync <bool>($"{_apiConfig.ApiUrl}{_apiAliasConfig.SaveDevicesAndBackEndMapping}", contentData); if (!resutlApi.isSuccessful) { throw new ArgumentException(resutlApi.message); } } else { resutlApi = await _client.PostAsync <bool>($"{_apiConfig.ApiUrl}{_apiAliasConfig.UpdateDevicesAndBackEndMapping}", contentData); if (!resutlApi.isSuccessful) { throw new ArgumentException(resutlApi.message); } } return(Json(new { isError = false })); } catch (Exception ex) { return(Json(new { isError = true, msg = ex.Message })); } }
public bool UpdateDevicesAndBackEndBackEnd(DevicesSubmitViewModel param) { _db.Database.BeginTransaction(); try { _logger.LogInformation("Start Search Data Device"); var dataDevice = _db.Devices.Where(x => x.Id == param.Id).FirstOrDefault(); if (dataDevice == null) { _logger.Log(LogLevel.Critical, "Data Device NOT FOUND"); throw new ArgumentException("Data Device Not Found"); } _logger.LogInformation("Start Populating Object Device"); dataDevice.IMEI = param.IMEI; dataDevice.Model = param.Model; dataDevice.SimCardNo = param.SimCardNo; _db.Devices.Update(dataDevice); _logger.LogInformation("Update Data Device"); #region Remove Backend And Mapping Back End var listIdBackEndFromUI = new List <string>(); listIdBackEndFromUI = param.listBackEnd.Where(x => (x.Id != null || x.Id != string.Empty)).Select(x => x.Id).ToList(); if (listIdBackEndFromUI.Any()) { _logger.LogInformation("Start Search Data DeviceBackends"); var dataBackEndMapping = _db.DeviceBackends.Where(x => x.IdDevice == param.Id && !listIdBackEndFromUI.Contains(x.IdBackEnd)).ToList(); if (dataBackEndMapping.Any()) { _db.DeviceBackends.RemoveRange(dataBackEndMapping); _logger.LogInformation("Remove Data DeviceBackends"); } } else { _logger.LogInformation("Start Search Data DeviceBackends ALL by device id"); var dataBackEndMapping = _db.DeviceBackends.Where(x => x.IdDevice == param.Id).ToList(); if (dataBackEndMapping.Any()) { _db.DeviceBackends.RemoveRange(dataBackEndMapping); } _logger.LogInformation("Remove Data DeviceBackends ALL by device id"); } #endregion foreach (var item in param.listBackEnd.Where(x => (x.Id == null || x.Id == string.Empty)).ToList()) { _logger.LogInformation("Start Populating Object Backend"); var dataBackEnd = new Backend(); dataBackEnd.Id = Guid.NewGuid().ToString(); dataBackEnd.Name = item.Name; dataBackEnd.Address = item.Address; _db.Backends.Add(dataBackEnd); _logger.LogInformation("Add Data BackEnd"); _logger.LogInformation("Start Populating Object DeviceBackend"); var dataDeviceMappingBackEnd = new DeviceBackend(); dataDeviceMappingBackEnd.Id = Guid.NewGuid().ToString(); dataDeviceMappingBackEnd.IdBackEnd = dataBackEnd.Id; dataDeviceMappingBackEnd.IdDevice = dataDevice.Id; dataDeviceMappingBackEnd.MappedTime = DateTime.Now; _db.DeviceBackends.Add(dataDeviceMappingBackEnd); _logger.LogInformation("Add Data DeviceBackEnd"); } foreach (var item in param.listBackEnd.Where(x => x.Id != null).ToList()) { _logger.LogInformation("Start Search Data Backends"); var dataBackEnd = _db.Backends.Where(x => x.Id == item.Id).FirstOrDefault(); if (dataBackEnd == null) { _logger.Log(LogLevel.Critical, "Data Backends NOT FOUND"); throw new ArgumentException("Data Backend Not Found"); } _logger.LogInformation("Start Populating Object Backend"); dataBackEnd.Name = item.Name; dataBackEnd.Address = item.Address; _db.Backends.Update(dataBackEnd); _logger.LogInformation("Update Data Backends"); } _logger.LogInformation("Start Save Change"); _db.SaveChanges(); _db.Database.CommitTransaction(); return(true); } catch (Exception ex) { _db.Database.RollbackTransaction(); _logger.Log(LogLevel.Critical, ex.Message.ToString()); throw new ArgumentException(ex.Message); } }