Skip to content

Commit

Permalink
Minimize public interface of VTN lib
Browse files Browse the repository at this point in the history
Closes #12

Signed-off-by: Maximilian Pohl <[email protected]>
  • Loading branch information
pohlm01 committed Oct 18, 2024
1 parent b84371c commit 6861d72
Show file tree
Hide file tree
Showing 13 changed files with 163 additions and 131 deletions.
12 changes: 6 additions & 6 deletions openadr-vtn/src/api/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use crate::{
pub async fn get_all(
State(event_source): State<Arc<dyn EventCrud>>,
ValidatedQuery(query_params): ValidatedQuery<QueryParams>,
User(user): User,
user: User,
) -> AppResponse<Vec<Event>> {
trace!(?query_params);

Expand All @@ -39,7 +39,7 @@ pub async fn get_all(
pub async fn get(
State(event_source): State<Arc<dyn EventCrud>>,
Path(id): Path<EventId>,
User(user): User,
user: User,
) -> AppResponse<Event> {
let event = event_source.retrieve(&id, &user).await?;
trace!(%event.id, event.event_name=event.content.event_name, "retrieved event");
Expand All @@ -52,7 +52,7 @@ pub async fn add(
BusinessUser(user): BusinessUser,
ValidatedJson(new_event): ValidatedJson<EventContent>,
) -> Result<(StatusCode, Json<Event>), AppError> {
let event = event_source.create(new_event, &user).await?;
let event = event_source.create(new_event, &User(user)).await?;

info!(%event.id, event_name=event.content.event_name, "event created");

Expand All @@ -65,7 +65,7 @@ pub async fn edit(
BusinessUser(user): BusinessUser,
ValidatedJson(content): ValidatedJson<EventContent>,
) -> AppResponse<Event> {
let event = event_source.update(&id, content, &user).await?;
let event = event_source.update(&id, content, &User(user)).await?;

info!(%event.id, event_name=event.content.event_name, "event updated");

Expand All @@ -77,7 +77,7 @@ pub async fn delete(
Path(id): Path<EventId>,
BusinessUser(user): BusinessUser,
) -> AppResponse<Event> {
let event = event_source.delete(&id, &user).await?;
let event = event_source.delete(&id, &User(user)).await?;
info!(%event.id, event.event_name=event.content.event_name, "deleted event");
Ok(Json(event))
}
Expand Down Expand Up @@ -173,7 +173,7 @@ mod test {
events.push(
store
.events()
.create(event.clone(), &Claims::any_business_user())
.create(event.clone(), &User(Claims::any_business_user()))
.await
.unwrap(),
);
Expand Down
22 changes: 11 additions & 11 deletions openadr-vtn/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,24 @@ use axum_extra::extract::{Query, QueryRejection};
use serde::de::DeserializeOwned;
use validator::Validate;

pub mod auth;
pub mod event;
pub mod program;
pub mod report;
pub mod resource;
pub mod user;
pub mod ven;
pub(crate) mod auth;
pub(crate) mod event;
pub(crate) mod program;
pub(crate) mod report;
pub(crate) mod resource;
pub(crate) mod user;
pub(crate) mod ven;

pub type AppResponse<T> = Result<Json<T>, AppError>;
pub(crate) type AppResponse<T> = Result<Json<T>, AppError>;

#[derive(Debug, Clone)]
pub struct ValidatedForm<T>(T);
pub(crate) struct ValidatedForm<T>(T);

#[derive(Debug, Clone)]
pub struct ValidatedQuery<T>(pub T);
pub(crate) struct ValidatedQuery<T>(pub T);

#[derive(Debug, Clone)]
pub struct ValidatedJson<T>(pub T);
pub(crate) struct ValidatedJson<T>(pub T);

#[async_trait]
impl<T, S> FromRequest<S> for ValidatedJson<T>
Expand Down
12 changes: 6 additions & 6 deletions openadr-vtn/src/api/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::{
pub async fn get_all(
State(program_source): State<Arc<dyn ProgramCrud>>,
ValidatedQuery(query_params): ValidatedQuery<QueryParams>,
User(user): User,
user: User,
) -> AppResponse<Vec<Program>> {
trace!(?query_params);

Expand All @@ -36,7 +36,7 @@ pub async fn get_all(
pub async fn get(
State(program_source): State<Arc<dyn ProgramCrud>>,
Path(id): Path<ProgramId>,
User(user): User,
user: User,
) -> AppResponse<Program> {
let program = program_source.retrieve(&id, &user).await?;
Ok(Json(program))
Expand All @@ -47,7 +47,7 @@ pub async fn add(
BusinessUser(user): BusinessUser,
ValidatedJson(new_program): ValidatedJson<ProgramContent>,
) -> Result<(StatusCode, Json<Program>), AppError> {
let program = program_source.create(new_program, &user).await?;
let program = program_source.create(new_program, &User(user)).await?;

Ok((StatusCode::CREATED, Json(program)))
}
Expand All @@ -58,7 +58,7 @@ pub async fn edit(
BusinessUser(user): BusinessUser,
ValidatedJson(content): ValidatedJson<ProgramContent>,
) -> AppResponse<Program> {
let program = program_source.update(&id, content, &user).await?;
let program = program_source.update(&id, content, &User(user)).await?;

info!(%program.id, program.program_name=program.content.program_name, "program updated");

Expand All @@ -70,7 +70,7 @@ pub async fn delete(
Path(id): Path<ProgramId>,
BusinessUser(user): BusinessUser,
) -> AppResponse<Program> {
let program = program_source.delete(&id, &user).await?;
let program = program_source.delete(&id, &User(user)).await?;
info!(%id, "deleted program");
Ok(Json(program))
}
Expand Down Expand Up @@ -175,7 +175,7 @@ mod test {
for program in new_programs {
let p = store
.programs()
.create(program.clone(), &Claims::any_business_user())
.create(program.clone(), &User(Claims::any_business_user()))
.await
.unwrap();
assert_eq!(p.content, program);
Expand Down
10 changes: 5 additions & 5 deletions openadr-vtn/src/api/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use crate::{
pub async fn get_all(
State(report_source): State<Arc<dyn ReportCrud>>,
ValidatedQuery(query_params): ValidatedQuery<QueryParams>,
User(user): User,
user: User,
) -> AppResponse<Vec<Report>> {
let reports = report_source.retrieve_all(&query_params, &user).await?;

Expand All @@ -38,7 +38,7 @@ pub async fn get_all(
pub async fn get(
State(report_source): State<Arc<dyn ReportCrud>>,
Path(id): Path<ReportId>,
User(user): User,
user: User,
) -> AppResponse<Report> {
let report: Report = report_source.retrieve(&id, &user).await?;
Ok(Json(report))
Expand All @@ -50,7 +50,7 @@ pub async fn add(
VENUser(user): VENUser,
ValidatedJson(new_report): ValidatedJson<ReportContent>,
) -> Result<(StatusCode, Json<Report>), AppError> {
let report = report_source.create(new_report, &user).await?;
let report = report_source.create(new_report, &User(user)).await?;

info!(%report.id, report_name=?report.content.report_name, "report created");

Expand All @@ -64,7 +64,7 @@ pub async fn edit(
VENUser(user): VENUser,
ValidatedJson(content): ValidatedJson<ReportContent>,
) -> AppResponse<Report> {
let report = report_source.update(&id, content, &user).await?;
let report = report_source.update(&id, content, &User(user)).await?;

info!(%report.id, report_name=?report.content.report_name, "report updated");

Expand All @@ -78,7 +78,7 @@ pub async fn delete(
BusinessUser(user): BusinessUser,
Path(id): Path<ReportId>,
) -> AppResponse<Report> {
let report = report_source.delete(&id, &user).await?;
let report = report_source.delete(&id, &User(user)).await?;
info!(%id, "deleted report");
Ok(Json(report))
}
Expand Down
18 changes: 9 additions & 9 deletions openadr-vtn/src/api/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ use crate::{
api::{AppResponse, ValidatedJson, ValidatedQuery},
data_source::ResourceCrud,
error::AppError,
jwt::{Claims, User},
jwt::User,
};

fn has_write_permission(user_claims: &Claims, ven_id: &VenId) -> Result<(), AppError> {
if user_claims.is_ven_manager() {
fn has_write_permission(User(claims): &User, ven_id: &VenId) -> Result<(), AppError> {
if claims.is_ven_manager() {
return Ok(());
}

if user_claims.is_ven() && user_claims.ven_ids().contains(ven_id) {
if claims.is_ven() && claims.ven_ids().contains(ven_id) {
return Ok(());
}

Expand All @@ -40,7 +40,7 @@ pub async fn get_all(
State(resource_source): State<Arc<dyn ResourceCrud>>,
Path(ven_id): Path<VenId>,
ValidatedQuery(query_params): ValidatedQuery<QueryParams>,
User(user): User,
user: User,
) -> AppResponse<Vec<Resource>> {
has_write_permission(&user, &ven_id)?;
trace!(?query_params);
Expand All @@ -55,7 +55,7 @@ pub async fn get_all(
pub async fn get(
State(resource_source): State<Arc<dyn ResourceCrud>>,
Path((ven_id, id)): Path<(VenId, ResourceId)>,
User(user): User,
user: User,
) -> AppResponse<Resource> {
has_write_permission(&user, &ven_id)?;
let ven = resource_source.retrieve(&id, ven_id, &user).await?;
Expand All @@ -65,7 +65,7 @@ pub async fn get(

pub async fn add(
State(resource_source): State<Arc<dyn ResourceCrud>>,
User(user): User,
user: User,
Path(ven_id): Path<VenId>,
ValidatedJson(new_resource): ValidatedJson<ResourceContent>,
) -> Result<(StatusCode, Json<Resource>), AppError> {
Expand All @@ -78,7 +78,7 @@ pub async fn add(
pub async fn edit(
State(resource_source): State<Arc<dyn ResourceCrud>>,
Path((ven_id, id)): Path<(VenId, ResourceId)>,
User(user): User,
user: User,
ValidatedJson(content): ValidatedJson<ResourceContent>,
) -> AppResponse<Resource> {
has_write_permission(&user, &ven_id)?;
Expand All @@ -92,7 +92,7 @@ pub async fn edit(
pub async fn delete(
State(resource_source): State<Arc<dyn ResourceCrud>>,
Path((ven_id, id)): Path<(VenId, ResourceId)>,
User(user): User,
user: User,
) -> AppResponse<Resource> {
has_write_permission(&user, &ven_id)?;
let resource = resource_source.delete(&id, ven_id, &user).await?;
Expand Down
14 changes: 7 additions & 7 deletions openadr-vtn/src/data_source/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::sync::Arc;

use crate::{
error::AppError,
jwt::{AuthRole, Claims},
jwt::{AuthRole, Claims, User},
};

#[async_trait]
Expand Down Expand Up @@ -107,7 +107,7 @@ pub trait ProgramCrud:
NewType = ProgramContent,
Error = AppError,
Filter = crate::api::program::QueryParams,
PermissionFilter = Claims,
PermissionFilter = User,
>
{
}
Expand All @@ -118,7 +118,7 @@ pub trait ReportCrud:
NewType = ReportContent,
Error = AppError,
Filter = crate::api::report::QueryParams,
PermissionFilter = Claims,
PermissionFilter = User,
>
{
}
Expand All @@ -129,7 +129,7 @@ pub trait EventCrud:
NewType = EventContent,
Error = AppError,
Filter = crate::api::event::QueryParams,
PermissionFilter = Claims,
PermissionFilter = User,
>
{
}
Expand Down Expand Up @@ -185,7 +185,7 @@ pub trait ResourceCrud:
NewType = ResourceContent,
Error = AppError,
Filter = crate::api::resource::QueryParams,
PermissionFilter = Claims,
PermissionFilter = User,
>
{
}
Expand Down Expand Up @@ -252,6 +252,6 @@ pub trait DataSource: Send + Sync + 'static {

#[derive(Debug, Clone)]
pub struct AuthInfo {
pub client_id: String,
pub roles: Vec<AuthRole>,
pub(crate) client_id: String,
pub(crate) roles: Vec<AuthRole>,
}
Loading

0 comments on commit 6861d72

Please sign in to comment.