-
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
13 changed files
with
186 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -15,6 +15,10 @@ Run Migration | |
|
||
``diesel migration run`` | ||
|
||
Undo Migration | ||
|
||
``diesel migration redo`` | ||
|
||
Run Cargo | ||
|
||
``cargo run`` |
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 items |
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,11 @@ | ||
-- Your SQL goes here | ||
CREATE TABLE items ( | ||
id VARCHAR NOT NULL PRIMARY KEY, | ||
room_id VARCHAR NOT NULL REFERENCES rooms(id) on DELETE CASCADE, | ||
name VARCHAR NOT NULL, | ||
description VARCHAR, | ||
category VARCHAR NOT NULL, | ||
purchase_date Timestamp NOT NULL, | ||
expiry_date Timestamp, | ||
value DOUBLE PRECISION NOT NULL | ||
) |
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,53 @@ | ||
use actix_web::web::Json; | ||
use diesel::prelude::*; | ||
use uuid::Uuid; | ||
|
||
use crate::app::models::item; | ||
|
||
type DbError = Box<dyn std::error::Error + Send + Sync>; | ||
|
||
pub fn list_items(conn: &mut PgConnection, uid: Uuid) -> Result<Vec<item::Item>, DbError> { | ||
use crate::schema::items::dsl::*; | ||
|
||
let item_list = items.filter(room_id.eq(uid.to_string())).load(conn)?; | ||
|
||
Ok(item_list) | ||
} | ||
|
||
pub fn insert_new_item( | ||
conn: &mut PgConnection, | ||
form: &Json<item::NewItem>, | ||
) -> Result<item::Item, DbError> { | ||
use crate::schema::items::dsl::*; | ||
|
||
match form.validate() { | ||
Ok(_) => { | ||
let new_item = item::Item { | ||
id: Uuid::new_v4().to_string(), | ||
room_id: form.room_id.to_owned(), | ||
name: form.name.to_owned(), | ||
description: form.description.to_owned(), | ||
category: form.category.to_owned(), | ||
purchase_date: form.purchase_date.to_owned(), | ||
expiry_date: form.expiry_date.to_owned(), | ||
value: form.value.to_owned(), | ||
}; | ||
|
||
diesel::insert_into(items).values(&new_item).execute(conn)?; | ||
|
||
Ok(new_item) | ||
} | ||
Err(error) => Err(DbError::from(error)), | ||
} | ||
} | ||
|
||
pub fn delete_item(conn: &mut PgConnection, uid: Uuid) -> Result<String, DbError> { | ||
use crate::schema::items::dsl::*; | ||
|
||
let result = diesel::delete(items.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,2 +1,3 @@ | ||
pub mod home; | ||
pub mod room; | ||
pub mod item; |
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,39 @@ | ||
use actix_web::{error, get, 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("/item")] | ||
async fn add_item( | ||
pool: web::Data<DbPool>, | ||
form: web::Json<models::item::NewItem>, | ||
) -> Result<impl Responder> { | ||
let response = web::block(move || { | ||
let mut conn = pool.get()?; | ||
actions::item::insert_new_item(&mut conn, &form) | ||
}) | ||
.await? | ||
.map_err(error::ErrorBadRequest)?; | ||
|
||
Ok(HttpResponse::Created().json(response)) | ||
} | ||
|
||
#[get("/item")] | ||
async fn get_items( | ||
pool: web::Data<DbPool>, | ||
query: web::Query<models::item::ItemQuery>, | ||
) -> Result<impl Responder> { | ||
let room_uid = query.room_id; | ||
|
||
let response = web::block(move || { | ||
let mut conn = pool.get()?; | ||
actions::item::list_items(&mut conn, room_uid) | ||
}) | ||
.await? | ||
.map_err(error::ErrorBadRequest)?; | ||
|
||
Ok(HttpResponse::Ok().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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod home; | ||
pub mod room; | ||
pub mod item; |
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 |
---|---|---|
|
@@ -5,4 +5,5 @@ pub mod db; | |
pub mod models { | ||
pub mod home; | ||
pub mod room; | ||
pub mod item; | ||
} |
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,49 @@ | ||
use diesel::prelude::*; | ||
use diesel::{prelude::Insertable, Queryable}; | ||
use serde::{Deserialize, Serialize}; | ||
use uuid::Uuid; | ||
|
||
use crate::schema::items; | ||
use crate::app::models::room::Room; | ||
|
||
/// Item details. | ||
#[derive(Queryable, Serialize, Selectable, Identifiable, Associations, Debug, PartialEq, Insertable)] | ||
#[diesel(belongs_to(Room))] | ||
#[diesel(table_name = items)] | ||
pub struct Item { | ||
pub id: String, | ||
pub name: String, | ||
pub room_id: String, | ||
pub description: Option<String>, | ||
pub category: String, | ||
pub purchase_date: String, | ||
pub expiry_date: Option<String>, | ||
pub value: f64 | ||
} | ||
|
||
/// New Item. | ||
#[derive(Debug, Clone, Serialize, Deserialize)] | ||
pub struct NewItem { | ||
pub name: String, | ||
pub room_id: String, | ||
pub description: Option<String>, | ||
pub category: String, | ||
pub purchase_date: String, | ||
pub expiry_date: Option<String>, | ||
pub value: f64 | ||
} | ||
|
||
#[derive(Deserialize)] | ||
pub struct ItemQuery { | ||
pub room_id: Uuid, | ||
} | ||
|
||
// validations | ||
impl NewItem { | ||
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