feat: patch account
This commit is contained in:
@ -56,7 +56,16 @@ impl LNVpsDb for LNVpsDbMysql {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn update_user(&self, user: &User) -> Result<()> {
|
async fn update_user(&self, user: &User) -> Result<()> {
|
||||||
todo!()
|
sqlx::query(
|
||||||
|
"update users set email = ?, contact_nip17 = ?, contact_email = ? where id = ?",
|
||||||
|
)
|
||||||
|
.bind(&user.email)
|
||||||
|
.bind(user.contact_nip17)
|
||||||
|
.bind(user.contact_email)
|
||||||
|
.bind(user.id)
|
||||||
|
.fetch_one(&self.db)
|
||||||
|
.await?;
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn delete_user(&self, id: u64) -> Result<()> {
|
async fn delete_user(&self, id: u64) -> Result<()> {
|
||||||
@ -248,16 +257,18 @@ impl LNVpsDb for LNVpsDbMysql {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn update_vm(&self, vm: &Vm) -> Result<()> {
|
async fn update_vm(&self, vm: &Vm) -> Result<()> {
|
||||||
sqlx::query("update vm set image_id=?,template_id=?,ssh_key_id=?,expires=?,disk_id=? where id=?")
|
sqlx::query(
|
||||||
.bind(vm.image_id)
|
"update vm set image_id=?,template_id=?,ssh_key_id=?,expires=?,disk_id=? where id=?",
|
||||||
.bind(vm.template_id)
|
)
|
||||||
.bind(vm.ssh_key_id)
|
.bind(vm.image_id)
|
||||||
.bind(vm.expires)
|
.bind(vm.template_id)
|
||||||
.bind(vm.disk_id)
|
.bind(vm.ssh_key_id)
|
||||||
.bind(vm.id)
|
.bind(vm.expires)
|
||||||
.execute(&self.db)
|
.bind(vm.disk_id)
|
||||||
.await
|
.bind(vm.id)
|
||||||
.map_err(Error::new)?;
|
.execute(&self.db)
|
||||||
|
.await
|
||||||
|
.map_err(Error::new)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
26
src/api.rs
26
src/api.rs
@ -18,6 +18,7 @@ use ws::Message;
|
|||||||
|
|
||||||
pub fn routes() -> Vec<Route> {
|
pub fn routes() -> Vec<Route> {
|
||||||
routes![
|
routes![
|
||||||
|
v1_patch_account,
|
||||||
v1_list_vms,
|
v1_list_vms,
|
||||||
v1_get_vm,
|
v1_get_vm,
|
||||||
v1_list_vm_templates,
|
v1_list_vm_templates,
|
||||||
@ -77,6 +78,31 @@ struct VMPatchRequest {
|
|||||||
pub ssh_key_id: Option<u64>,
|
pub ssh_key_id: Option<u64>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
struct AccountPatchRequest {
|
||||||
|
pub email: Option<String>,
|
||||||
|
pub contact_nip17: bool,
|
||||||
|
pub contact_email: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[patch("/api/v1/account", format = "json", data = "<req>")]
|
||||||
|
async fn v1_patch_account(
|
||||||
|
auth: Nip98Auth,
|
||||||
|
db: &State<Box<dyn LNVpsDb>>,
|
||||||
|
req: Json<AccountPatchRequest>,
|
||||||
|
) -> ApiResult<()> {
|
||||||
|
let pubkey = auth.event.pubkey.to_bytes();
|
||||||
|
let uid = db.upsert_user(&pubkey).await?;
|
||||||
|
let mut user = db.get_user(uid).await?;
|
||||||
|
|
||||||
|
user.email = req.email.clone();
|
||||||
|
user.contact_nip17 = req.contact_nip17;
|
||||||
|
user.contact_email = req.contact_email;
|
||||||
|
|
||||||
|
db.update_user(&user).await?;
|
||||||
|
ApiData::ok(())
|
||||||
|
}
|
||||||
|
|
||||||
#[get("/api/v1/vm")]
|
#[get("/api/v1/vm")]
|
||||||
async fn v1_list_vms(
|
async fn v1_list_vms(
|
||||||
auth: Nip98Auth,
|
auth: Nip98Auth,
|
||||||
|
Reference in New Issue
Block a user