private async Task <OperationStatus <StudentDescription[]> > GetMembersInternal(Guid groupId) { await using var conn = new NpgsqlConnection(ConnectionString); return(OperationStatus <StudentDescription[]> .Success( (await conn.QueryAsync <StudentDescription>( $@"select gm.user_id, pi.fullname from {PgSchema.GroupMembership} gm left join {PgSchema.ProfileIndex} pi on gm.user_id = pi.user_id where group_id = @GroupId" , new { groupId }).ConfigureAwait(false)).ToArray())); }
public async Task <OperationStatus <Guid> > SignUp(RegistrationData registrationData) { // TODO var profile = new Profile { Surname = registrationData.Surname, FirstName = registrationData.FirstName, SecondName = registrationData.SecondName, Group = registrationData.Group }; var authData = new AuthData { Email = registrationData.Email, Password = registrationData.Password }; try { profile.InitAsFresh(); await using var conn = new NpgsqlConnection(ConnectionString); await conn.OpenAsync().ConfigureAwait(false); await using var transaction = await conn.BeginTransactionAsync().ConfigureAwait(false); await conn.ExecuteAsync( @"insert into auth_data (email, password_hash, user_id) values (@Email, digest(@Password, 'sha256'), @UserId)", new { authData.Email, authData.Password, UserId = profile.Id }).ConfigureAwait(false); await conn.ExecuteAsync( @"insert into profile (id, deleted, version, data) values (@Id, false, 0, @Profile::jsonb)", new { profile.Id, profile }).ConfigureAwait(false); await transaction.CommitAsync().ConfigureAwait(false); return(OperationStatus <Guid> .Success(profile.Id)); } catch (PostgresException e) when(e.SqlState == "23505") { return(OperationStatus <Guid> .Fail(OperationStatusCode.Conflict, "User with such email already exists")); } catch (Exception e) { Console.WriteLine(e); // TODO logging return(OperationStatus <Guid> .Fail(OperationStatusCode.InternalServerError)); } }
public async Task<OperationStatus<TEntity>> Read(Guid id) { try { await using var conn = new NpgsqlConnection(ConnectionString); var data = await conn.QuerySingleOrDefaultAsync<TEntity>( $@"select data from {relationName} where id = @Id and not deleted limit 1", new {id}).ConfigureAwait(false); return data != null ? OperationStatus<TEntity>.Success(data) : OperationStatus<TEntity>.Fail(OperationStatusCode.NotFound); } catch (Exception e) { Console.WriteLine(e); // TODO logging return OperationStatus<TEntity>.Fail(OperationStatusCode.InternalServerError); } }
public async Task<OperationStatus<Guid>> Create(TEntity data) { try { data.InitAsFresh(); await using var conn = new NpgsqlConnection(ConnectionString); await conn.ExecuteAsync( $@"insert into {relationName} (id, deleted, version, data) values (@Id, false, 0, @Data::jsonb)", new {data.Id, data}).ConfigureAwait(false); return OperationStatus<Guid>.Success(data.Id); } catch (PostgresException e) when (e.SqlState == "23505") { return OperationStatus<Guid>.Fail(OperationStatusCode.Conflict, "Entity with such ID already exists"); } catch (Exception e) { Console.WriteLine(e); // TODO logging return OperationStatus<Guid>.Fail(OperationStatusCode.InternalServerError); } }