diff --git a/src/entity/gateway/postgres/migrations/V0010__char_create_timestamp.sql b/src/entity/gateway/postgres/migrations/V0010__char_create_timestamp.sql new file mode 100644 index 0000000..5ba1fd0 --- /dev/null +++ b/src/entity/gateway/postgres/migrations/V0010__char_create_timestamp.sql @@ -0,0 +1,2 @@ +alter table player_character + add created_at timestamptz default current_timestamp not null; diff --git a/src/entity/gateway/postgres/postgres.rs b/src/entity/gateway/postgres/postgres.rs index ba77827..0a81bb7 100644 --- a/src/entity/gateway/postgres/postgres.rs +++ b/src/entity/gateway/postgres/postgres.rs @@ -276,17 +276,17 @@ async fn create_character(conn: &mut sqlx::PgConnection, char: NewCharacterEntit async fn get_characters_by_user(conn: &mut sqlx::PgConnection, user: &UserAccountEntity) -> Result<[Option; 4], GatewayError> { - let mut stream = sqlx::query_as::<_, PgCharacter>("select * from player_character where user_account = $1 and slot < 4 order by slot") + let stream = sqlx::query_as::<_, PgCharacter>("select * from player_character where user_account = $1 and slot < 4 order by created_at;") .bind(user.id.0) .fetch(conn); - const NONE: Option = None; - let mut result = [NONE; 4]; - while let Some(character) = stream.try_next().await? { - let index = character.slot as usize; - result[index] = Some(character.into()) - } - Ok(result) + Ok(stream.fold(core::array::from_fn(|_| None), |mut acc, char| { + if let Ok(char) = char { + let slot = char.slot as usize; + acc[slot] = Some(char.into()) + } + acc + }).await) } async fn save_character(conn: &mut sqlx::PgConnection, char: &CharacterEntity) -> Result<(), GatewayError>