public Entity.BaseResponse <Entity.Role> Manage([FromBody] Entity.Role request)
 {
     Entity.BaseResponse <Entity.Role> response = new Entity.BaseResponse <Entity.Role>(true);
     try
     {
         var status = _service.Manage(request);
         response.IsSuccess = status.Success;
         response.Message   = status.Message;
         response.Data      = status.Data;
     }
     catch (Exception ex)
     {
         return(new Entity.BaseResponse <Entity.Role>(false, ex.Message));
     }
     return(response);
 }
        public Entity.ActionStatus Manage(Entity.Role request)
        {
            Entity.ActionStatus actionStatus = new Entity.ActionStatus(true);
            try
            {
                if (request.Guid == null || request.Guid == Guid.Empty)
                {
                    List <string> solutionkeys = new List <string>();
                    solutionkeys.Add(SolutionConfiguration.SolutionId.ToString());
                    var addModel = new IOT.AddRoleModel()
                    {
                        name = request.Name, description = request.Description, solutions = solutionkeys
                    };
                    var addRoleResult = AsyncHelpers.RunSync <IOT.DataResponse <IOT.AddRoleResult> >(() => _iotConnectClient.Role.AddRole(addModel));

                    if (addRoleResult != null && addRoleResult.status)
                    {
                        request.Guid = Guid.Parse(addRoleResult.data.newid.ToUpper());
                        var dbRole = Mapper.Configuration.Mapper.Map <Entity.Role, Model.Role>(request);
                        dbRole.Guid        = request.Guid;
                        dbRole.CompanyGuid = SolutionConfiguration.CompanyId;
                        dbRole.CreatedDate = DateTime.Now;
                        dbRole.CreatedBy   = SolutionConfiguration.CurrentUserId;
                        actionStatus       = _roleRepository.Manage(dbRole);
                        actionStatus.Data  = Mapper.Configuration.Mapper.Map <Model.Role, Entity.Role>(dbRole);
                        if (!actionStatus.Success)
                        {
                            _logger.Error($"Role is not added in solution database, Error: {actionStatus.Message}");
                            //var deleteEntityResult = _iotConnectClient.Role.Delete(request.Guid.ToString()).Result;
                            //if (deleteEntityResult != null && deleteEntityResult.status != (int)HttpStatusCode.OK)
                            //{
                            //_logger.Error($"Role is not deleted from iotconnect, Error: {deleteEntityResult.message}");
                            _logger.Error($"Role is not deleted from iotconnect");
                            actionStatus.Success = false;
                            actionStatus.Message = "Something Went Wrong!";
                            //}
                        }
                    }
                    else
                    {
                        _logger.Error($"Role is not added in iotconnect, Error: {addRoleResult.message}");
                        actionStatus.Success = false;
                        actionStatus.Message = new UtilityHelper().IOTResultMessage(addRoleResult.errorMessages);
                    }
                }
                else
                {
                    var olddbRole = _roleRepository.FindBy(x => x.Guid.Equals(request.Guid)).FirstOrDefault();
                    if (olddbRole == null)
                    {
                        throw new NotFoundCustomException($"{CommonException.Name.NoRecordsFound} : Role");
                    }
                    List <string> solutionkeys = new List <string>();
                    solutionkeys.Add(SolutionConfiguration.SolutionId.ToString());
                    var updateModel = new IOT.UpdateRoleModel()
                    {
                        name = request.Name, description = request.Description, solutions = solutionkeys
                    };
                    var updateEntityResult = AsyncHelpers.RunSync <IOT.DataResponse <IOT.UpdateRoleResult> >(() =>
                                                                                                             _iotConnectClient.Role.UpdateRole(request.Guid.ToString(), Mapper.Configuration.Mapper.Map <IOT.UpdateRoleModel>(updateModel)));

                    if (updateEntityResult != null && updateEntityResult.status)
                    {
                        var dbRole = Mapper.Configuration.Mapper.Map(request, olddbRole);
                        dbRole.CreatedBy   = olddbRole.CreatedBy;
                        dbRole.CreatedDate = olddbRole.CreatedDate;
                        dbRole.UpdatedDate = DateTime.Now;
                        dbRole.CompanyGuid = SolutionConfiguration.CompanyId;
                        dbRole.UpdatedBy   = SolutionConfiguration.CurrentUserId;
                        actionStatus       = _roleRepository.Manage(dbRole);
                        actionStatus.Data  = Mapper.Configuration.Mapper.Map <Model.Role, Entity.Role>(dbRole);
                        if (!actionStatus.Success)
                        {
                            _logger.Error($"Role is not updated in solution database, Error: {actionStatus.Message}");
                            actionStatus.Success = false;
                            actionStatus.Message = "Something Went Wrong!";
                        }
                    }
                    else
                    {
                        _logger.Error($"User is not updated in iotconnect, Error: {updateEntityResult.message}");
                        actionStatus.Success = false;
                        actionStatus.Message = new UtilityHelper().IOTResultMessage(updateEntityResult.errorMessages);
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.Error(Constants.ACTION_EXCEPTION, "RoleManager.Delete " + ex);
                actionStatus.Success = false;
                actionStatus.Message = ex.Message;
            }
            return(actionStatus);
        }