public Entity.BaseResponse <Entity.Entity> Manage([FromForm] Entity.EntityModel request) { Entity.BaseResponse <Entity.Entity> response = new Entity.BaseResponse <Entity.Entity>(false); try { var status = _service.Manage(request); if (status.Success) { response.IsSuccess = status.Success; response.Message = status.Message; response.Data = status.Data; } else { response.IsSuccess = status.Success; response.Message = status.Message; } } catch (Exception ex) { base.LogException(ex); return(new Entity.BaseResponse <Entity.Entity>(false, ex.Message)); } return(response); }
public Entity.ActionStatus Manage(Entity.EntityModel request) { Entity.ActionStatus actionStatus = new Entity.ActionStatus(true); try { if (request.Guid == null || request.Guid == Guid.Empty) { var checkExisting = _entityRepository.FindBy(x => x.Name.Equals(request.Name) && x.CompanyGuid.Equals(SolutionConfiguration.CompanyId) && x.IsActive == true && !x.IsDeleted).FirstOrDefault(); if (checkExisting == null) { Entity.Entity ghEntity = Mapper.Configuration.Mapper.Map <Entity.EntityModel, Entity.Entity>(request); var addEntityResult = AsyncHelpers.RunSync <IOT.DataResponse <IOT.AddEntityResult> >(() => _iotConnectClient.Entity.Add(Mapper.Configuration.Mapper.Map <IOT.AddEntityModel>(ghEntity))); if (addEntityResult != null && addEntityResult.status && addEntityResult.data != null) { request.Guid = Guid.Parse(addEntityResult.data.EntityGuid.ToUpper()); var dbEntity = Mapper.Configuration.Mapper.Map <Entity.EntityModel, Model.Entity>(request); if (request.ImageFile != null) { // upload image dbEntity.Image = SaveEntityImage(request.Guid, request.ImageFile); } dbEntity.Guid = request.Guid; dbEntity.CompanyGuid = SolutionConfiguration.CompanyId; dbEntity.CreatedDate = DateTime.Now; dbEntity.CreatedBy = SolutionConfiguration.CurrentUserId; if (request.ParentEntityGuid == SolutionConfiguration.EntityGuid) { dbEntity.ParentEntityGuid = null; } actionStatus = _entityRepository.Manage(dbEntity); actionStatus.Data = Mapper.Configuration.Mapper.Map <Model.Entity, Entity.Entity>(actionStatus.Data); if (!actionStatus.Success) { _logger.Error($"Location is not added in solution database, Error: {actionStatus.Message}"); var deleteEntityResult = _iotConnectClient.Entity.Delete(request.Guid.ToString()).Result; if (deleteEntityResult != null && deleteEntityResult.status) { _logger.Error($"Location is not deleted from iotconnect, Error: {deleteEntityResult.message}"); actionStatus.Success = false; actionStatus.Message = new UtilityHelper().IOTResultMessage(deleteEntityResult.errorMessages); } } } else { _logger.Error($"Location is not added in iotconnect, Error: {addEntityResult.message}"); actionStatus.Success = false; actionStatus.Message = new UtilityHelper().IOTResultMessage(addEntityResult.errorMessages); } } else { _logger.Error($"Location Already Exist !!"); actionStatus.Success = false; actionStatus.Message = "Name Already Exists"; } } else { var olddbEntity = _entityRepository.FindBy(x => x.Guid.Equals(request.Guid)).FirstOrDefault(); if (olddbEntity == null) { throw new NotFoundCustomException($"{CommonException.Name.NoRecordsFound} : Location"); } var updateEntityResult = _iotConnectClient.Entity.Update(request.Guid.ToString(), Mapper.Configuration.Mapper.Map <IOT.UpdateEntityModel>(request)).Result; if (updateEntityResult != null && updateEntityResult.status && updateEntityResult.data != null) { string existingImage = olddbEntity.Image; var dbEntity = Mapper.Configuration.Mapper.Map(request, olddbEntity); if (request.ImageFile != null) { if (File.Exists(SolutionConfiguration.UploadBasePath + dbEntity.Image) && request.ImageFile.Length > 0) { //if already exists image then delete old image from server File.Delete(SolutionConfiguration.UploadBasePath + dbEntity.Image); } if (request.ImageFile.Length > 0) { // upload new image dbEntity.Image = SaveEntityImage(request.Guid, request.ImageFile); } } else { dbEntity.Image = existingImage; } dbEntity.UpdatedDate = DateTime.Now; dbEntity.UpdatedBy = SolutionConfiguration.CurrentUserId; dbEntity.CompanyGuid = SolutionConfiguration.CompanyId; if (request.ParentEntityGuid == SolutionConfiguration.EntityGuid) { dbEntity.ParentEntityGuid = null; } actionStatus = _entityRepository.Manage(dbEntity); actionStatus.Data = Mapper.Configuration.Mapper.Map <Model.Entity, Entity.Entity>(dbEntity); if (!actionStatus.Success) { _logger.Error($"Location is not updated in solution database, Error: {actionStatus.Message}"); actionStatus.Success = false; actionStatus.Message = "Something Went Wrong!"; } } else { _logger.Error($"Location is not added in iotconnect, Error: {updateEntityResult.message}"); actionStatus.Success = false; actionStatus.Message = new UtilityHelper().IOTResultMessage(updateEntityResult.errorMessages); } } } catch (Exception ex) { _logger.Error(Constants.ACTION_EXCEPTION, "EntityService.Manage " + ex); actionStatus.Success = false; actionStatus.Message = ex.Message; } return(actionStatus); }