feat: ctx resolver
This commit is contained in:
parent
a38438a700
commit
8843134aa6
5 changed files with 71 additions and 36 deletions
11
src/model.rs
11
src/model.rs
|
|
@ -1,7 +1,7 @@
|
|||
//! Simplistic model layer
|
||||
//! (with mock-store layer)
|
||||
|
||||
use crate::{Error, Result};
|
||||
use crate::{Error, Result, ctx::Ctx};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
|
|
@ -9,6 +9,7 @@ use std::sync::{Arc, Mutex};
|
|||
#[derive(Clone, Debug, Serialize)]
|
||||
pub struct Property {
|
||||
pub id: u64,
|
||||
pub creator_id: u64,
|
||||
pub address: String,
|
||||
pub contact: String,
|
||||
}
|
||||
|
|
@ -37,21 +38,21 @@ impl ModelController {
|
|||
|
||||
// CRUD implementation
|
||||
impl ModelController {
|
||||
pub async fn create_property(&self, property: PropertyForCreate) -> Result<Property> {
|
||||
pub async fn create_property(&self, ctx: Ctx, property: PropertyForCreate) -> Result<Property> {
|
||||
let mut store = self.property_store.lock().unwrap();
|
||||
let id = store.len() as u64;
|
||||
let property = Property { id, address: property.address, contact: property.contact };
|
||||
let property = Property { id, creator_id: ctx.user_id(), address: property.address, contact: property.contact };
|
||||
store.push(Some(property.clone()));
|
||||
Ok(property)
|
||||
}
|
||||
|
||||
pub async fn list_properties(&self) -> Result<Vec<Property>> {
|
||||
pub async fn list_properties(&self, ctx: Ctx) -> Result<Vec<Property>> {
|
||||
let store = self.property_store.lock().unwrap();
|
||||
let properties = store.iter().filter_map(|p| p.clone()).collect();
|
||||
Ok(properties)
|
||||
}
|
||||
|
||||
pub async fn delete_property(&self, id: u64) -> Result<Property> {
|
||||
pub async fn delete_property(&self, ctx: Ctx, id: u64) -> Result<Property> {
|
||||
let mut store = self.property_store.lock().unwrap();
|
||||
let property = store.get_mut(id as usize).and_then(|p| p.take());
|
||||
property.ok_or(Error::PropertyDeleteFailIdNotFound { id })
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue