summaryrefslogtreecommitdiff
path: root/src/channel/app.rs
diff options
context:
space:
mode:
authorOwen Jacobson <owen@grimoire.ca>2024-09-18 22:49:38 -0400
committerOwen Jacobson <owen@grimoire.ca>2024-09-20 16:42:44 -0400
commit22348bfa35f009e62abe2f30863e0434079a1fe2 (patch)
treec5b5b5e660a1ee2a05785f4669102c1023b6e7b0 /src/channel/app.rs
parentaafdeb9ffaf9a993ca4462b3422667e04469b2e3 (diff)
Remove the HTML client, and expose a JSON API.
This API structure fell out of a conversation with Kit. Described loosely: kit: ok kit: Here's what I'm picturing in a client kit: list channels, make-new-channel, zero to one active channels, post-to-active. kit: login/sign-up, logout owen: you will likely also want "am I logged in" here kit: sure, whoami
Diffstat (limited to 'src/channel/app.rs')
-rw-r--r--src/channel/app.rs32
1 files changed, 28 insertions, 4 deletions
diff --git a/src/channel/app.rs b/src/channel/app.rs
index 2f37878..8ae0c3c 100644
--- a/src/channel/app.rs
+++ b/src/channel/app.rs
@@ -31,13 +31,17 @@ impl<'a> Channels<'a> {
Self { db, broadcaster }
}
- pub async fn create(&self, name: &str) -> Result<(), InternalError> {
+ pub async fn create(&self, name: &str) -> Result<Channel, CreateError> {
let mut tx = self.db.begin().await?;
- let channel = tx.channels().create(name).await?;
- self.broadcaster.register_channel(&channel);
+ let channel = tx
+ .channels()
+ .create(name)
+ .await
+ .map_err(|err| CreateError::from_duplicate_name(err, name))?;
+ self.broadcaster.register_channel(&channel.id);
tx.commit().await?;
- Ok(())
+ Ok(channel)
}
pub async fn all(&self) -> Result<Vec<Channel>, InternalError> {
@@ -122,6 +126,26 @@ impl<'a> Channels<'a> {
}
#[derive(Debug, thiserror::Error)]
+pub enum CreateError {
+ #[error("channel named {0} already exists")]
+ DuplicateName(String),
+ #[error(transparent)]
+ DatabaseError(#[from] sqlx::Error),
+}
+
+impl CreateError {
+ fn from_duplicate_name(error: sqlx::Error, name: &str) -> Self {
+ if let Some(error) = error.as_database_error() {
+ if error.is_unique_violation() {
+ return Self::DuplicateName(name.into());
+ }
+ }
+
+ Self::from(error)
+ }
+}
+
+#[derive(Debug, thiserror::Error)]
pub enum InternalError {
#[error(transparent)]
DatabaseError(#[from] sqlx::Error),