feat: include user in responses when creating and editing bots

This commit is contained in:
Paul Makles
2024-06-26 19:49:26 +01:00
parent ce20e689cc
commit d6bcb844db
9 changed files with 38 additions and 17 deletions

View File

@@ -72,7 +72,12 @@ impl Default for Bot {
#[allow(clippy::disallowed_methods)]
impl Bot {
/// Create a new bot
pub async fn create<D>(db: &Database, username: String, owner: &User, data: D) -> Result<Bot>
pub async fn create<D>(
db: &Database,
username: String,
owner: &User,
data: D,
) -> Result<(Bot, User)>
where
D: Into<Option<PartialBot>>,
{
@@ -86,7 +91,7 @@ impl Bot {
let id = Ulid::new().to_string();
User::create(
let user = User::create(
db,
username,
Some(id.to_string()),
@@ -111,7 +116,7 @@ impl Bot {
}
db.insert_bot(&bot).await?;
Ok(bot)
Ok((bot, user))
}
/// Remove a field from this object

View File

@@ -162,4 +162,11 @@ auto_derived!(
/// User objects
pub users: Vec<User>,
}
/// Bot with user response
pub struct BotWithUserResponse {
#[serde(flatten)]
pub bot: Bot,
pub user: User,
}
);

View File

@@ -14,7 +14,7 @@ pub async fn create_bot(
db: &State<Database>,
user: User,
info: Json<v0::DataCreateBot>,
) -> Result<Json<v0::Bot>> {
) -> Result<Json<v0::BotWithUserResponse>> {
let info = info.into_inner();
info.validate().map_err(|error| {
create_error!(FailedValidation {
@@ -22,8 +22,11 @@ pub async fn create_bot(
})
})?;
let bot = Bot::create(db, info.name, &user, None).await?;
Ok(Json(bot.into()))
let (bot, user) = Bot::create(db, info.name, &user, None).await?;
Ok(Json(v0::BotWithUserResponse {
bot: bot.into(),
user: user.into_self(false).await,
}))
}
#[cfg(test)]

View File

@@ -32,7 +32,7 @@ mod test {
let mut harness = TestHarness::new().await;
let (_, session, user) = harness.new_user().await;
let bot = Bot::create(&harness.db, TestHarness::rand_string(), &user, None)
let (bot, _) = Bot::create(&harness.db, TestHarness::rand_string(), &user, None)
.await
.expect("`Bot`");

View File

@@ -16,7 +16,7 @@ pub async fn edit_bot(
user: User,
target: Reference,
data: Json<DataEditBot>,
) -> Result<Json<v0::Bot>> {
) -> Result<Json<v0::BotWithUserResponse>> {
let data = data.into_inner();
data.validate().map_err(|error| {
create_error!(FailedValidation {
@@ -29,8 +29,8 @@ pub async fn edit_bot(
return Err(create_error!(NotFound));
}
let mut user = db.fetch_user(&bot.id).await?;
if let Some(name) = data.name {
let mut user = db.fetch_user(&bot.id).await?;
user.update_username(db, name).await?;
}
@@ -39,7 +39,10 @@ pub async fn edit_bot(
&& data.interactions_url.is_none()
&& data.remove.is_none()
{
return Ok(Json(bot.into()));
return Ok(Json(v0::BotWithUserResponse {
bot: bot.into(),
user: user.into_self(false).await,
}));
}
let DataEditBot {
@@ -68,7 +71,10 @@ pub async fn edit_bot(
)
.await?;
Ok(Json(bot.into()))
Ok(Json(v0::BotWithUserResponse {
bot: bot.into(),
user: user.into_self(false).await,
}))
}
#[cfg(test)]
@@ -83,7 +89,7 @@ mod test {
let harness = TestHarness::new().await;
let (_, session, user) = harness.new_user().await;
let bot = Bot::create(&harness.db, TestHarness::rand_string(), &user, None)
let (bot, _) = Bot::create(&harness.db, TestHarness::rand_string(), &user, None)
.await
.expect("`Bot`");

View File

@@ -40,7 +40,7 @@ mod test {
let harness = TestHarness::new().await;
let (_, session, user) = harness.new_user().await;
let bot = Bot::create(&harness.db, TestHarness::rand_string(), &user, None)
let (bot, _) = Bot::create(&harness.db, TestHarness::rand_string(), &user, None)
.await
.expect("`Bot`");

View File

@@ -41,7 +41,7 @@ mod test {
let harness = TestHarness::new().await;
let (_, session, user) = harness.new_user().await;
let bot = Bot::create(&harness.db, TestHarness::rand_string(), &user, None)
let (bot, _) = Bot::create(&harness.db, TestHarness::rand_string(), &user, None)
.await
.expect("`Bot`");

View File

@@ -35,7 +35,7 @@ mod test {
let harness = TestHarness::new().await;
let (_, _, user) = harness.new_user().await;
let mut bot = Bot::create(&harness.db, TestHarness::rand_string(), &user, None)
let (mut bot, _) = Bot::create(&harness.db, TestHarness::rand_string(), &user, None)
.await
.expect("`Bot`");

View File

@@ -74,7 +74,7 @@ mod test {
let mut harness = TestHarness::new().await;
let (_, session, user) = harness.new_user().await;
let bot = Bot::create(&harness.db, TestHarness::rand_string(), &user, None)
let (bot, _) = Bot::create(&harness.db, TestHarness::rand_string(), &user, None)
.await
.expect("`Bot`");
@@ -121,7 +121,7 @@ mod test {
let mut harness = TestHarness::new().await;
let (_, session, user) = harness.new_user().await;
let bot = Bot::create(&harness.db, TestHarness::rand_string(), &user, None)
let (bot, _) = Bot::create(&harness.db, TestHarness::rand_string(), &user, None)
.await
.expect("`Bot`");