Skip to content

Commit

Permalink
feat: include user in responses when creating and editing bots
Browse files Browse the repository at this point in the history
  • Loading branch information
insertish committed Jun 26, 2024
1 parent ce20e68 commit d6bcb84
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 17 deletions.
11 changes: 8 additions & 3 deletions crates/core/database/src/models/bots/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>>,
{
Expand All @@ -86,7 +91,7 @@ impl Bot {

let id = Ulid::new().to_string();

User::create(
let user = User::create(
db,
username,
Some(id.to_string()),
Expand All @@ -111,7 +116,7 @@ impl Bot {
}

db.insert_bot(&bot).await?;
Ok(bot)
Ok((bot, user))
}

/// Remove a field from this object
Expand Down
7 changes: 7 additions & 0 deletions crates/core/models/src/v0/bots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
);
9 changes: 6 additions & 3 deletions crates/delta/src/routes/bots/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,19 @@ 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 {
error: error.to_string()
})
})?;

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)]
Expand Down
2 changes: 1 addition & 1 deletion crates/delta/src/routes/bots/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`");

Expand Down
16 changes: 11 additions & 5 deletions crates/delta/src/routes/bots/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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?;
}

Expand All @@ -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 {
Expand Down Expand Up @@ -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)]
Expand All @@ -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`");

Expand Down
2 changes: 1 addition & 1 deletion crates/delta/src/routes/bots/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`");

Expand Down
2 changes: 1 addition & 1 deletion crates/delta/src/routes/bots/fetch_owned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`");

Expand Down
2 changes: 1 addition & 1 deletion crates/delta/src/routes/bots/fetch_public.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`");

Expand Down
4 changes: 2 additions & 2 deletions crates/delta/src/routes/bots/invite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`");

Expand Down Expand Up @@ -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`");

Expand Down

0 comments on commit d6bcb84

Please sign in to comment.