Add update shop & owner endpoints
This commit is contained in:
@@ -59,7 +59,7 @@ impl Model for InteriorRefList {
|
||||
}
|
||||
|
||||
#[instrument(level = "debug", skip(self, db))]
|
||||
async fn save(self, db: &PgPool) -> Result<Self> {
|
||||
async fn create(self, db: &PgPool) -> Result<Self> {
|
||||
// TODO:
|
||||
// * Decide if I'll need to make the same changes to merchandise and transactions
|
||||
// - answer depends on how many rows of each I expect to insert in one go
|
||||
|
||||
@@ -55,7 +55,7 @@ impl Model for MerchandiseList {
|
||||
}
|
||||
|
||||
#[instrument(level = "debug", skip(self, db))]
|
||||
async fn save(self, db: &PgPool) -> Result<Self> {
|
||||
async fn create(self, db: &PgPool) -> Result<Self> {
|
||||
Ok(sqlx::query_as_unchecked!(
|
||||
Self,
|
||||
"INSERT INTO merchandise_lists
|
||||
|
||||
@@ -9,7 +9,7 @@ pub mod owner;
|
||||
pub mod shop;
|
||||
|
||||
pub use interior_ref_list::InteriorRefList;
|
||||
pub use model::Model;
|
||||
pub use model::{Model, UpdateableModel};
|
||||
pub use owner::Owner;
|
||||
pub use shop::Shop;
|
||||
pub use merchandise_list::MerchandiseList;
|
||||
|
||||
@@ -23,7 +23,15 @@ where
|
||||
}
|
||||
}
|
||||
async fn get(db: &PgPool, id: i32) -> Result<Self>;
|
||||
async fn save(self, db: &PgPool) -> Result<Self>;
|
||||
async fn create(self, db: &PgPool) -> Result<Self>;
|
||||
async fn delete(db: &PgPool, owner_id: i32, id: i32) -> Result<u64>;
|
||||
async fn list(db: &PgPool, list_params: &ListParams) -> Result<Vec<Self>>;
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
pub trait UpdateableModel
|
||||
where
|
||||
Self: std::marker::Sized,
|
||||
{
|
||||
async fn update(self, db: &PgPool, owner_id: i32, id: i32) -> Result<Self>;
|
||||
}
|
||||
@@ -8,7 +8,7 @@ use tracing::instrument;
|
||||
use uuid::Uuid;
|
||||
|
||||
use super::ListParams;
|
||||
use super::Model;
|
||||
use super::{Model, UpdateableModel};
|
||||
use crate::problem::forbidden_permission;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
@@ -43,7 +43,7 @@ impl Model for Owner {
|
||||
}
|
||||
|
||||
#[instrument(level = "debug", skip(self, db))]
|
||||
async fn save(self, db: &PgPool) -> Result<Self> {
|
||||
async fn create(self, db: &PgPool) -> Result<Self> {
|
||||
Ok(sqlx::query_as!(
|
||||
Self,
|
||||
"INSERT INTO owners
|
||||
@@ -103,3 +103,31 @@ impl Model for Owner {
|
||||
Ok(result)
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl UpdateableModel for Owner {
|
||||
#[instrument(level = "debug", skip(self, db))]
|
||||
async fn update(self, db: &PgPool, owner_id: i32, id: i32) -> Result<Self> {
|
||||
let owner = sqlx::query!("SELECT id FROM owners WHERE id = $1", id)
|
||||
.fetch_one(db)
|
||||
.await?;
|
||||
if owner.id == owner_id {
|
||||
Ok(sqlx::query_as!(
|
||||
Self,
|
||||
"UPDATE owners SET
|
||||
name = $2,
|
||||
mod_version = $3,
|
||||
updated_at = now()
|
||||
WHERE id = $1
|
||||
RETURNING *",
|
||||
id,
|
||||
self.name,
|
||||
self.mod_version,
|
||||
)
|
||||
.fetch_one(db)
|
||||
.await?)
|
||||
} else {
|
||||
return Err(forbidden_permission());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@ use sqlx::postgres::PgPool;
|
||||
use tracing::instrument;
|
||||
|
||||
use super::ListParams;
|
||||
use super::Model;
|
||||
use super::{Model, UpdateableModel};
|
||||
use crate::problem::forbidden_permission;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
@@ -43,7 +43,7 @@ impl Model for Shop {
|
||||
}
|
||||
|
||||
#[instrument(level = "debug", skip(self, db))]
|
||||
async fn save(self, db: &PgPool) -> Result<Self> {
|
||||
async fn create(self, db: &PgPool) -> Result<Self> {
|
||||
Ok(sqlx::query_as!(
|
||||
Self,
|
||||
"INSERT INTO shops
|
||||
@@ -102,3 +102,34 @@ impl Model for Shop {
|
||||
Ok(result)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[async_trait]
|
||||
impl UpdateableModel for Shop {
|
||||
#[instrument(level = "debug", skip(self, db))]
|
||||
async fn update(self, db: &PgPool, owner_id: i32, id: i32) -> Result<Self> {
|
||||
let shop = sqlx::query!("SELECT owner_id FROM shops WHERE id = $1", id)
|
||||
.fetch_one(db)
|
||||
.await?;
|
||||
if shop.owner_id == owner_id {
|
||||
Ok(sqlx::query_as!(
|
||||
Self,
|
||||
"UPDATE shops SET
|
||||
name = $2,
|
||||
owner_id = $3,
|
||||
description = $4,
|
||||
updated_at = now()
|
||||
WHERE id = $1
|
||||
RETURNING *",
|
||||
id,
|
||||
self.name,
|
||||
self.owner_id,
|
||||
self.description,
|
||||
)
|
||||
.fetch_one(db)
|
||||
.await?)
|
||||
} else {
|
||||
return Err(forbidden_permission());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user