-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
128 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
-- This file should undo anything in `up.sql` | ||
DROP TABLE rooms |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
-- Your SQL goes here | ||
CREATE TABLE rooms ( | ||
id VARCHAR NOT NULL PRIMARY KEY, | ||
name VARCHAR NOT NULL, | ||
home_id VARCHAR NOT NULL REFERENCES homes(id) on DELETE CASCADE | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod home; | ||
pub mod room; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use actix_web::web::Json; | ||
use diesel::prelude::*; | ||
use uuid::Uuid; | ||
|
||
use crate::app::models::room; | ||
|
||
type DbError = Box<dyn std::error::Error + Send + Sync>; | ||
|
||
pub fn insert_new_room( | ||
conn: &mut PgConnection, | ||
form: &Json<room::NewRoom>, | ||
) -> Result<room::Room, DbError> { | ||
use crate::schema::rooms::dsl::*; | ||
|
||
match form.validate() { | ||
Ok(_) => { | ||
let new_room = room::Room { | ||
id: Uuid::new_v4().to_string(), | ||
name: form.name.to_owned(), | ||
home_id: form.home_id.to_owned(), | ||
}; | ||
|
||
diesel::insert_into(rooms).values(&new_room).execute(conn)?; | ||
|
||
Ok(new_room) | ||
} | ||
Err(error) => Err(DbError::from(error)), | ||
} | ||
} | ||
|
||
pub fn delete_room(conn: &mut PgConnection, uid: Uuid) -> Result<String, DbError> { | ||
use crate::schema::rooms::dsl::*; | ||
|
||
let result = diesel::delete(rooms.filter(id.eq(uid.to_string()))).execute(conn); | ||
|
||
match result { | ||
Ok(_) => Ok("Success".to_string()), | ||
Err(e) => Err(DbError::from(e)), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod home; | ||
pub mod room; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
use actix_web::{error, post, web, HttpResponse, Responder, Result}; | ||
use diesel::{r2d2, PgConnection}; | ||
|
||
use crate::app::actions; | ||
use crate::app::models; | ||
|
||
type DbPool = r2d2::Pool<r2d2::ConnectionManager<PgConnection>>; | ||
|
||
#[post("/room")] | ||
async fn add_room( | ||
pool: web::Data<DbPool>, | ||
form: web::Json<models::room::NewRoom>, | ||
) -> Result<impl Responder> { | ||
let response = web::block(move || { | ||
let mut conn = pool.get()?; | ||
actions::room::insert_new_room(&mut conn, &form) | ||
}) | ||
.await? | ||
.map_err(error::ErrorBadRequest)?; | ||
|
||
Ok(HttpResponse::Created().json(response)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ pub mod db; | |
|
||
pub mod models { | ||
pub mod home; | ||
pub mod room; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
use diesel::prelude::*; | ||
use diesel::{prelude::Insertable, Queryable}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::schema::rooms; | ||
use crate::app::models::home::Home; | ||
|
||
/// Room details. | ||
#[derive(Queryable, Serialize, Selectable, Identifiable, Associations, Debug, PartialEq, Insertable)] | ||
#[diesel(belongs_to(Home))] | ||
#[diesel(table_name = rooms)] | ||
pub struct Room { | ||
pub id: String, | ||
pub name: String, | ||
pub home_id: String, | ||
} | ||
|
||
/// New room details. | ||
#[derive(Debug, Clone, Serialize, Deserialize)] | ||
pub struct NewRoom { | ||
pub name: String, | ||
pub home_id: String, | ||
} | ||
|
||
// validations | ||
impl NewRoom { | ||
pub fn validate(&self) -> Result<(), String> { | ||
if self.name.trim().is_empty() { | ||
return Err("Name is empty".to_string()); | ||
} | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters