account apply

This commit is contained in:
ntr
2019-09-22 18:36:03 +10:00
parent 8151c37f96
commit c4a291e890
5 changed files with 69 additions and 2 deletions
+17
View File
@@ -158,6 +158,23 @@ pub fn new_token(tx: &mut Transaction, id: Uuid) -> Result<String, MnmlHttpError
Ok(token)
}
pub fn new_img(tx: &mut Transaction, id: Uuid) -> Result<Account, Error> {
let query = "
UPDATE accounts
SET img = $1, updated_at = now()
WHERE id = $2
RETURNING id, password, name, balance, subscribed, img
";
let result = tx
.query(query, &[&Uuid::new_v4(), &id])?;
let row = result.iter().next()
.ok_or(format_err!("account not updated {:?}", id))?;
Account::try_from(row)
}
pub fn set_password(tx: &mut Transaction, id: Uuid, current: &String, password: &String) -> Result<String, MnmlHttpError> {
if password.len() < PASSWORD_MIN_LEN {
return Err(MnmlHttpError::PasswordUnacceptable);
+20
View File
@@ -167,6 +167,26 @@ pub fn apply(tx: &mut Transaction, account: &Account, variant: MtxVariant, const
account::team(tx, account)
}
pub fn account_apply(tx: &mut Transaction, account: &Account, variant: MtxVariant) -> Result<Account, Error> {
let mtx = select(tx, variant, account.id)?;
let cost = match mtx.variant {
MtxVariant::Rename => return Err(err_msg("rename not usable on account")),
_ => NEW_IMAGE_COST,
};
account::debit(tx, account.id, cost)?;
let account = account::new_img(tx, account.id)?;
match mtx.variant {
MtxVariant::Invader => img::invader_write(account.img)?,
MtxVariant::Molecular => img::molecular_write(account.img)?,
MtxVariant::Shapes => img::shapes_write(account.img)?,
_ => account.img,
};
Ok(account)
}
pub fn select(tx: &mut Transaction, variant: MtxVariant, account: Uuid) -> Result<Mtx, Error> {
let query = "
SELECT id, account, variant
+3
View File
@@ -230,6 +230,9 @@ impl Connection {
RpcRequest::MtxConstructApply { mtx, construct_id, name } =>
Ok(RpcMessage::AccountTeam(mtx::apply(&mut tx, account, mtx, construct_id, name)?)),
RpcRequest::MtxAccountApply { mtx } =>
Ok(RpcMessage::AccountState(mtx::account_apply(&mut tx, account, mtx)?)),
RpcRequest::MtxBuy { mtx } =>
Ok(RpcMessage::AccountShop(mtx::buy(&mut tx, account, mtx)?)),