public async Task <long> AddResource(ResourceBaseModel resource) { IDbBase dbBase = null; try { if (string.IsNullOrWhiteSpace(resource.Name)) { throw new OutputException( new Exception(RESOURCE_NAME_IS_MANDATORY), StatusCodes.Status400BadRequest, MnemoxStatusCodes.INVALID_MODEL); } dbBase = _dbFactory.GetDbBase(); var parameters = new List <TimescaleParameter> { new TimescaleParameter { NpgsqlValue = resource.Name, ParameterName = RESOURCE_NAME_PARAMETER_NAME, NpgsqlDbType = NpgsqlDbType.Varchar }, new TimescaleParameter { NpgsqlValue = resource.Description, ParameterName = RESOURCE_DESCRIPTION_PARAMETER_NAME, NpgsqlDbType = NpgsqlDbType.Varchar }, new TimescaleParameter { NpgsqlValue = resource.Type, ParameterName = RESOURCE_TYPE_PARAMETER_NAME, NpgsqlDbType = NpgsqlDbType.Smallint } }; await dbBase.ConnectAsync(); var resourceId = (long)await dbBase.ExecuteScalarAsync(ADD_RESOURCE_FUNCTION_NAME, parameters); return(resourceId); } catch (Exception ex) { await _logsManager.ErrorAsync(new ErrorLogStructure(ex).WithErrorSource()); throw new HandledException(ex); } finally { if (dbBase != null) { await dbBase.DisconnectAsync(); } } }
public async Task <long> SetSignedInUserIntoStorage(User user, string signInToken, DateTime tokenValidUntilUtc) { IDbBase dbBase = null; try { var parameters = new List <TimescaleParameter> { new TimescaleParameter { NpgsqlValue = signInToken, ParameterName = TOKEN_PARAMETER_NAME, NpgsqlDbType = NpgsqlDbType.Varchar }, new TimescaleParameter { NpgsqlValue = user.UserId, ParameterName = OWNER_ID_PARAMETER_NAME, NpgsqlDbType = NpgsqlDbType.Bigint }, new TimescaleParameter { NpgsqlValue = (int)MnemoxAccessObjectsTypesEnum.USER, ParameterName = OWNER_TYPE_ID_PARAMETER_NAME, NpgsqlDbType = NpgsqlDbType.Integer }, new TimescaleParameter { NpgsqlValue = tokenValidUntilUtc, ParameterName = VALID_UNTIL_PARAMETER_NAME, NpgsqlDbType = NpgsqlDbType.Timestamp } }; dbBase = _dbFactory.GetDbBase(); await dbBase.ConnectAsync(); var tokenId = (long)await dbBase.ExecuteScalarAsync(STORE_TOKEN_FUNCTION_NAME, parameters); return(tokenId); } catch (Exception ex) { await _logsManager.ErrorAsync(new ErrorLogStructure(ex).WithErrorSource()); throw new HandledException(ex); } finally { if (dbBase != null) { await dbBase.DisconnectAsync(); } } }
public async Task <int> Add(ServerModel serverModel) { IDbBase db = null; try { var parameters = new List <TimescaleParameter> { new TimescaleParameter { NpgsqlValue = serverModel.ServerName, ParameterName = SERVER_NAME_PARAMTER_NAME, NpgsqlDbType = NpgsqlDbType.Varchar }, new TimescaleParameter { NpgsqlValue = (short)serverModel.ServerState, ParameterName = STATE_ID_PARAMETER_NAME, NpgsqlDbType = NpgsqlDbType.Smallint } }; db = _dbFactory.GetDbBase(); await db.ConnectAsync(); var serverId = (int)await db.ExecuteScalarAsync(ADD_SERVER_FNC_NAME, parameters); return(serverId); } catch (Exception ex) { await _logsManager.ErrorAsync(new ErrorLogStructure(ex).WithErrorSource()); throw new HandledException(ex); } finally { await db?.DisconnectAsync(); } }
public async Task <InitializationStatesEnums> InitializationState(IDbBase dbBase) { try { using (var reader = await dbBase.ExecuteReaderQueryAsync(SELECT_SERVER_SCHEMA_INFO)) { if (!reader.HasRows) { return(InitializationStatesEnums.DATABASE_NOT_INITIALIZED); } } using (var reader = await dbBase.ExecuteReaderQueryAsync(SELECT_DATABASE_STATE_TABLE_INFO)) { if (!reader.HasRows) { return(InitializationStatesEnums.DATABASE_NOT_INITIALIZED); } } var state = await dbBase.ExecuteScalarAsync(GET_INITIALIZATION_STATE_FNC_NAME); if (state is DBNull) { return(InitializationStatesEnums.DATABASE_NOT_INITIALIZED); } return((InitializationStatesEnums)(short)state); } catch (Exception ex) { await _logsManager.ErrorAsync(new ErrorLogStructure(ex).WithErrorSource()); throw new OutputException( ex, StatusCodes.Status500InternalServerError, MnemoxStatusCodes.CANNOT_CREATE_EXTENSION); } }