make bot owner nullable

This commit is contained in:
Ilion Beyst 2022-02-26 23:07:13 +01:00
parent 29bd21e489
commit 6ef6a872fe
4 changed files with 6 additions and 6 deletions

View file

@ -1,6 +1,6 @@
CREATE TABLE bots ( CREATE TABLE bots (
id serial PRIMARY KEY, id serial PRIMARY KEY,
owner_id integer REFERENCES users(id) NOT NULL, owner_id integer REFERENCES users(id),
name text NOT NULL name text NOT NULL
); );

View file

@ -7,14 +7,14 @@ use chrono;
#[derive(Insertable)] #[derive(Insertable)]
#[table_name = "bots"] #[table_name = "bots"]
pub struct NewBot<'a> { pub struct NewBot<'a> {
pub owner_id: i32, pub owner_id: Option<i32>,
pub name: &'a str, pub name: &'a str,
} }
#[derive(Queryable, Debug, PartialEq, Serialize, Deserialize)] #[derive(Queryable, Debug, PartialEq, Serialize, Deserialize)]
pub struct Bot { pub struct Bot {
pub id: i32, pub id: i32,
pub owner_id: i32, pub owner_id: Option<i32>,
pub name: String, pub name: String,
} }

View file

@ -24,7 +24,7 @@ pub async fn create_bot(
params: Json<BotParams>, params: Json<BotParams>,
) -> (StatusCode, Json<Bot>) { ) -> (StatusCode, Json<Bot>) {
let bot_params = bots::NewBot { let bot_params = bots::NewBot {
owner_id: user.id, owner_id: Some(user.id),
name: &params.name, name: &params.name,
}; };
let bot = bots::create_bot(&bot_params, &conn).unwrap(); let bot = bots::create_bot(&bot_params, &conn).unwrap();
@ -71,7 +71,7 @@ pub async fn upload_code_multipart(
let bot = bots::find_bot(bot_id, &conn).map_err(|_| StatusCode::NOT_FOUND)?; let bot = bots::find_bot(bot_id, &conn).map_err(|_| StatusCode::NOT_FOUND)?;
if user.id != bot.owner_id { if Some(user.id) != bot.owner_id {
return Err(StatusCode::FORBIDDEN); return Err(StatusCode::FORBIDDEN);
} }

View file

@ -7,7 +7,7 @@ table! {
bots (id) { bots (id) {
id -> Int4, id -> Int4,
owner_id -> Int4, owner_id -> Nullable<Int4>,
name -> Text, name -> Text,
} }
} }