Skip to content

Commit

Permalink
Finished rework
Browse files Browse the repository at this point in the history
  • Loading branch information
Paradyx committed Mar 2, 2022
1 parent c1d6bbd commit 327768f
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 97 deletions.
1 change: 0 additions & 1 deletion migrations/2022-01-22-200313_create_guilds/up.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
create table if not exists guilds (
guild_id int auto_increment primary key,
external_id varchar(255) not null unique,
name varchar(255) not null unique,
address text not null,
contact_by_account_id int not null,
Expand Down
106 changes: 64 additions & 42 deletions src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub fn create_rpg_system(
Ok(matching)
}

pub fn find_rpg_system(conn: &MysqlConnection, search_id: i32) -> Result<RpgSystem, UE> {
pub fn find_rpg_system(conn: &MysqlConnection, search_id: Id) -> Result<RpgSystem, UE> {
use crate::schema::rpg_systems::dsl::*;
rpg_systems
.find(search_id)
Expand All @@ -67,15 +67,16 @@ pub fn find_rpg_system(conn: &MysqlConnection, search_id: i32) -> Result<RpgSyst

pub fn update_rpg_system(
conn: &MysqlConnection,
write_to_id: i32,
write_to_id: Id,
new_info: NewRpgSystem,
) -> Result<RpgSystem, UE> {
use crate::schema::rpg_systems::dsl::*;

let affected = diesel::update(rpg_systems.find(write_to_id))
.set(new_info.clone())
.set(new_info)
.execute(conn)
.map_err(handle_db_errors)?;
.map_err(handle_db_errors)?
.assert_row_existed()?;
assert_eq!(
affected, 1,
"update rpg system must affect only a single row."
Expand All @@ -84,11 +85,12 @@ pub fn update_rpg_system(
find_rpg_system(conn, write_to_id)
}

pub fn delete_rpgsystem(conn: &MysqlConnection, delete_id: i32) -> Result<(), UE> {
pub fn delete_rpgsystem(conn: &MysqlConnection, delete_id: Id) -> Result<(), UE> {
use crate::schema::rpg_systems::dsl::*;
let affected = diesel::delete(rpg_systems.find(delete_id))
.execute(conn)
.map_err(handle_db_errors)?;
.map_err(handle_db_errors)?
.assert_row_existed()?;
assert_eq!(
affected, 1,
"delete rpg_system must affect only a single row."
Expand Down Expand Up @@ -117,32 +119,34 @@ pub fn create_title(conn: &MysqlConnection, new_title: NewTitle) -> Result<Title
Ok(matching)
}

pub fn find_title(conn: &MysqlConnection, search_id: i32) -> Result<Title, UE> {
pub fn find_title(conn: &MysqlConnection, search_id: Id) -> Result<Title, UE> {
use crate::schema::titles::dsl::*;
titles.find(search_id).first(conn).map_err(handle_db_errors)
}

pub fn update_title(
conn: &MysqlConnection,
write_to_id: i32,
write_to_id: Id,
new_info: NewTitle,
) -> Result<Title, UE> {
use crate::schema::titles::dsl::*;

let affected = diesel::update(titles.find(write_to_id))
.set(new_info)
.execute(conn)
.map_err(handle_db_errors)?;
.map_err(handle_db_errors)?
.assert_row_existed()?;
assert_eq!(affected, 1, "update title must affect only a single row.");

find_title(conn, write_to_id)
}

pub fn delete_title(conn: &MysqlConnection, delete_id: i32) -> Result<(), UE> {
pub fn delete_title(conn: &MysqlConnection, delete_id: Id) -> Result<(), UE> {
use crate::schema::titles::dsl::*;
let affected = diesel::delete(titles.find(delete_id))
.execute(conn)
.map_err(handle_db_errors)?;
.map_err(handle_db_errors)?
.assert_row_existed()?;
assert_eq!(affected, 1, "delete titles must affect only a single row.");
Ok(())
}
Expand All @@ -157,7 +161,8 @@ pub fn create_account(conn: &MysqlConnection, new_account: NewAccount) -> Result
let affected = diesel::insert_into(accounts)
.values(new_account.clone())
.execute(conn)
.map_err(handle_db_errors)?;
.map_err(handle_db_errors)?
.assert_row_existed()?;
assert_eq!(affected, 1, "crate account must affect only a single row.");

let matching = accounts
Expand All @@ -168,7 +173,7 @@ pub fn create_account(conn: &MysqlConnection, new_account: NewAccount) -> Result
Ok(matching)
}

pub fn find_account(conn: &MysqlConnection, search_id: i32) -> Result<Account, UE> {
pub fn find_account(conn: &MysqlConnection, search_id: Id) -> Result<Account, UE> {
use crate::schema::accounts::dsl::*;
accounts
.find(search_id)
Expand All @@ -178,14 +183,15 @@ pub fn find_account(conn: &MysqlConnection, search_id: i32) -> Result<Account, U

pub fn update_account(
conn: &MysqlConnection,
write_to_id: i32,
write_to_id: Id,
new_info: NewAccount,
) -> Result<Account, UE> {
use crate::schema::accounts::dsl::*;
let affected = diesel::update(accounts.find(write_to_id))
.set(new_info)
.execute(conn)
.map_err(handle_db_errors)?;
.map_err(handle_db_errors)?
.assert_row_existed()?;
assert_eq!(affected, 1, "update account must affect only a single row.");

find_account(conn, write_to_id)
Expand All @@ -197,7 +203,7 @@ pub fn find_current_registered_account(
) -> Result<Option<Account>, UE> {
use crate::schema::accounts::dsl::*;
accounts
.filter(external_id.eq(search_external_id).and(active.eq(true)))
.filter(external_id.eq(search_external_id))
.first(conn)
.optional()
.map_err(handle_db_errors)
Expand All @@ -221,7 +227,8 @@ pub fn delete_account(conn: &MysqlConnection, account: &Account) -> Result<(), U
use crate::schema::accounts::dsl::*;
let affected = diesel::delete(accounts.find(account.account_id))
.execute(conn)
.map_err(handle_db_errors)?;
.map_err(handle_db_errors)?
.assert_row_existed()?;
assert_eq!(
affected, 1,
"delete rpg_system must affect only a single row."
Expand All @@ -243,28 +250,29 @@ pub fn create_guild(conn: &MysqlConnection, new_guild: NewGuild) -> Result<Guild
assert_eq!(affected, 1, "create guilds must affect only a single row.");

let matching = guilds
.filter(external_id.eq(new_guild.external_id))
.filter(name.eq(new_guild.name))
.first::<Guild>(conn)
.map_err(handle_db_errors)?;

Ok(matching)
}

pub fn find_guild(conn: &MysqlConnection, search_id: i32) -> Result<Guild, UE> {
pub fn find_guild(conn: &MysqlConnection, search_id: Id) -> Result<Guild, UE> {
use crate::schema::guilds::dsl::*;
guilds.find(search_id).first(conn).map_err(handle_db_errors)
}

pub fn update_guild(
conn: &MysqlConnection,
write_to_id: i32,
write_to_id: Id,
new_info: NewGuild,
) -> Result<Guild, UE> {
use crate::schema::guilds::dsl::*;
let affected = diesel::update(guilds.find(write_to_id))
.set(new_info)
.execute(conn)
.map_err(handle_db_errors)?;
.map_err(handle_db_errors)?
.assert_row_existed()?;
assert_eq!(affected, 1, "update guilds must affect only a single row.");

find_guild(conn, write_to_id)
Expand Down Expand Up @@ -299,9 +307,9 @@ pub fn create_book(conn: &MysqlConnection, new_book: NewBook) -> Result<Book, UE

/// Little helper function which creates an sql query searching for an inventory key.
fn with_inventory_key(
ext_inventory_id: i32,
member_id: Option<i32>,
guild_id: Option<i32>,
ext_inventory_id: Id,
member_id: Option<Id>,
guild_id: Option<Id>,
) -> Box<dyn BoxableExpression<crate::schema::books::table, Mysql, SqlType = Bool>> {
use crate::schema::books::dsl::*;
let matches_member_id: Box<
Expand All @@ -328,37 +336,35 @@ fn with_inventory_key(
)
}

pub fn find_book(conn: &MysqlConnection, search_id: i32) -> Result<Book, UE> {
pub fn find_book(conn: &MysqlConnection, search_id: Id) -> Result<Book, UE> {
use crate::schema::books::dsl::*;
books.find(search_id).first(conn).map_err(handle_db_errors)
}

pub fn update_book(
conn: &MysqlConnection,
write_to_id: i32,
new_info: NewBook,
) -> Result<Book, UE> {
pub fn update_book(conn: &MysqlConnection, write_to_id: Id, new_info: NewBook) -> Result<Book, UE> {
use crate::schema::books::dsl::*;
let affected = diesel::update(books.find(write_to_id))
.set(new_info)
.execute(conn)
.map_err(handle_db_errors)?;
.map_err(handle_db_errors)?
.assert_row_existed()?;
assert_eq!(affected, 1, "update books must affect only a single row.");

find_book(conn, write_to_id)
}

pub fn delete_book(conn: &MysqlConnection, delete_id: i32) -> Result<(), UE> {
pub fn delete_book(conn: &MysqlConnection, delete_id: Id) -> Result<(), UE> {
use crate::schema::books::dsl::*;
let affected = diesel::delete(books.find(delete_id))
.execute(conn)
.map_err(handle_db_errors)?;
.map_err(handle_db_errors)?
.assert_row_existed()?;
assert_eq!(affected, 1, "delete books must affect only a single row.");
Ok(())
}

// Member collection
pub fn create_book_owned_by_member(
pub fn create_book_owned_by_account(
conn: &MysqlConnection,
account: Account,
partial_book: PostOwnedBook,
Expand All @@ -369,10 +375,10 @@ pub fn create_book_owned_by_member(
create_book(&conn, new_book)
}

pub fn find_book_owned_by_member(
pub fn find_book_owned_by_account(
conn: &MysqlConnection,
account: Account,
search_id: i32,
search_id: Id,
) -> Result<Book, UE> {
use crate::schema::books::dsl::*;
books
Expand All @@ -382,7 +388,7 @@ pub fn find_book_owned_by_member(
.map_err(handle_db_errors)
}

pub fn list_books_owned_by_member(
pub fn list_books_owned_by_account(
conn: &MysqlConnection,
account: Account,
) -> Result<Vec<Book>, UE> {
Expand All @@ -393,10 +399,10 @@ pub fn list_books_owned_by_member(
.map_err(handle_db_errors)
}

pub fn delete_book_owned_by_member(
pub fn delete_book_owned_by_account(
conn: &MysqlConnection,
account: &Account,
delete_id: i32,
delete_id: Id,
) -> Result<(), UE> {
use crate::schema::books::dsl::*;
let affected = diesel::delete(
Expand All @@ -405,7 +411,8 @@ pub fn delete_book_owned_by_member(
.find(delete_id),
)
.execute(conn)
.map_err(handle_db_errors)?;
.map_err(handle_db_errors)?
.assert_row_existed()?;
assert_eq!(affected, 1, "delete books must affect only a single row.");
Ok(())
}
Expand Down Expand Up @@ -434,7 +441,7 @@ pub fn create_book_owned_by_guild(
pub fn find_book_owned_by_guild(
conn: &MysqlConnection,
guild: &Guild,
search_id: i32,
search_id: Id,
) -> Result<Book, UE> {
use crate::schema::books::dsl::*;
books
Expand Down Expand Up @@ -465,7 +472,8 @@ pub fn delete_book_owned_by_guild(
.find(delete_id),
)
.execute(conn)
.map_err(handle_db_errors)?;
.map_err(handle_db_errors)?
.assert_row_existed()?;
assert_eq!(affected, 1, "delete books must affect only a single row.");
Ok(())
}
Expand Down Expand Up @@ -524,3 +532,17 @@ impl AccountAssertions for Option<Account> {
self.ok_or(UserFacingError::NotRegistered)
}
}

pub trait RowsAffectedAssertions {
fn assert_row_existed(self) -> Result<usize, UE>;
}

impl RowsAffectedAssertions for usize {
fn assert_row_existed(self) -> Result<usize, UE> {
if self == 0 {
Err(UE::NotFound)
} else {
Ok(self)
}
}
}
26 changes: 13 additions & 13 deletions src/api/me.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ pub async fn put(

pub mod collection {
use crate::actions;
use crate::actions::{AccountAssertions, delete_book_owned_by_member, find_book_owned_by_member};
use crate::actions::{find_book_owned_by_account, AccountAssertions};
use crate::api::MyResponder;
use crate::app::AppState;
use crate::authentication::scopes::{COLLECTION_MODIFY, COLLECTION_READ};
Expand All @@ -88,9 +88,9 @@ pub mod collection {
claims.require_scope(COLLECTION_READ)?;
let external_id = claims.external_account_id()?;
let conn = app.open_database_connection()?;
let account = actions::find_current_registered_account(&conn, external_id)?
.assert_active()?;
let books = actions::list_books_owned_by_member(&conn, account)?;
let account =
actions::find_current_registered_account(&conn, external_id)?.assert_active()?;
let books = actions::list_books_owned_by_account(&conn, account)?;
Ok(HttpResponse::Ok().json(books))
}

Expand All @@ -102,10 +102,10 @@ pub mod collection {
claims.require_scope(COLLECTION_MODIFY)?;
let external_id = claims.external_account_id()?;
let conn = app.open_database_connection()?;
let account = actions::find_current_registered_account(&conn, external_id)?
.assert_active()?;
let account =
actions::find_current_registered_account(&conn, external_id)?.assert_active()?;
let created_book =
actions::create_book_owned_by_member(&conn, account, posted_book.into_inner())?;
actions::create_book_owned_by_account(&conn, account, posted_book.into_inner())?;
Ok(HttpResponse::Created().json(created_book))
}

Expand All @@ -117,9 +117,9 @@ pub mod collection {
claims.require_scope(COLLECTION_READ)?;
let external_id = claims.external_account_id()?;
let conn = app.open_database_connection()?;
let account = actions::find_current_registered_account(&conn, external_id)?
.assert_active()?;
let book = find_book_owned_by_member(&conn, account, *search_id)?;
let account =
actions::find_current_registered_account(&conn, external_id)?.assert_active()?;
let book = find_book_owned_by_account(&conn, account, *search_id)?;
Ok(HttpResponse::Created().json(book))
}

Expand All @@ -131,9 +131,9 @@ pub mod collection {
claims.require_scope(COLLECTION_MODIFY)?;
let external_id = claims.external_account_id()?;
let conn = app.open_database_connection()?;
let account = actions::find_current_registered_account(&conn, external_id)?
.assert_active()?;
delete_book_owned_by_member(&conn, &account, *delete_id)?;
let account =
actions::find_current_registered_account(&conn, external_id)?.assert_active()?;
actions::delete_book_owned_by_account(&conn, &account, *delete_id)?;
Ok(HttpResponse::Ok().finish())
}
}
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ async fn main() -> Result<(), InternalError> {
let matches = command!()
.propagate_version(true)
.subcommand_required(true)
.arg(arg!(-c --config <CONFIG> "Define the config file"))
.arg(arg!(-c --config <CONFIG> "set the config file"))
.subcommand(Command::new("serve").about("start the liberation service"))
.subcommand(Command::new("test").about("run whatever was programed"))
.get_matches();
Expand Down
Loading

0 comments on commit 327768f

Please sign in to comment.