-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add events groups * chore: add generate type * feat: start adding infra & endpoints for groups * chore: remove unused files for now * test: add basic infra tests * feat: add api structs + "create" route * feat: add other routes * Update crates/api/src/event_group/update_event_group.rs
- Loading branch information
1 parent
3aab60e
commit e52a183
Showing
5 changed files
with
385 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
use actix_web::{web, HttpRequest, HttpResponse}; | ||
use nittei_api_structs::delete_event_group::*; | ||
use nittei_domain::{event_group::EventGroup, User, ID}; | ||
use nittei_infra::NitteiContext; | ||
|
||
use crate::{ | ||
error::NitteiError, | ||
shared::{ | ||
auth::{account_can_modify_event, account_can_modify_user, protect_account_route}, | ||
usecase::{execute, UseCase}, | ||
}, | ||
}; | ||
|
||
pub async fn delete_event_group_admin_controller( | ||
http_req: HttpRequest, | ||
path_params: web::Path<PathParams>, | ||
ctx: web::Data<NitteiContext>, | ||
) -> Result<HttpResponse, NitteiError> { | ||
let account = protect_account_route(&http_req, &ctx).await?; | ||
let e = account_can_modify_event(&account, &path_params.event_group_id, &ctx).await?; | ||
let user = account_can_modify_user(&account, &e.user_id, &ctx).await?; | ||
|
||
let usecase = DeleteEventGroupUseCase { | ||
user, | ||
event_group_id: e.id, | ||
}; | ||
|
||
execute(usecase, &ctx) | ||
.await | ||
.map(|event| HttpResponse::Ok().json(APIResponse::new(event))) | ||
.map_err(NitteiError::from) | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct DeleteEventGroupUseCase { | ||
pub user: User, | ||
pub event_group_id: ID, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub enum UseCaseError { | ||
NotFound(ID), | ||
StorageError, | ||
} | ||
|
||
impl From<UseCaseError> for NitteiError { | ||
fn from(e: UseCaseError) -> Self { | ||
match e { | ||
UseCaseError::StorageError => Self::InternalError, | ||
UseCaseError::NotFound(event_group_id) => Self::NotFound(format!( | ||
"The event group with id: {}, was not found.", | ||
event_group_id | ||
)), | ||
} | ||
} | ||
} | ||
|
||
#[async_trait::async_trait(?Send)] | ||
impl UseCase for DeleteEventGroupUseCase { | ||
type Response = EventGroup; | ||
|
||
type Error = UseCaseError; | ||
|
||
const NAME: &'static str = "DeleteEvent"; | ||
|
||
// TODO: use only one db call | ||
async fn execute(&mut self, ctx: &NitteiContext) -> Result<Self::Response, Self::Error> { | ||
let event_group = ctx | ||
.repos | ||
.event_groups | ||
.find(&self.event_group_id) | ||
.await | ||
.map_err(|_| UseCaseError::StorageError)?; | ||
let g = match event_group { | ||
Some(g) if g.user_id == self.user.id => g, | ||
_ => return Err(UseCaseError::NotFound(self.event_group_id.clone())), | ||
}; | ||
|
||
ctx.repos | ||
.event_groups | ||
.delete(&g.id) | ||
.await | ||
.map_err(|_| UseCaseError::StorageError)?; | ||
|
||
Ok(g) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
use actix_web::{web, HttpRequest, HttpResponse}; | ||
use nittei_api_structs::get_event_group::*; | ||
use nittei_domain::{event_group::EventGroup, ID}; | ||
use nittei_infra::NitteiContext; | ||
|
||
use crate::{ | ||
error::NitteiError, | ||
shared::{ | ||
auth::{account_can_modify_event, protect_account_route}, | ||
usecase::{execute, UseCase}, | ||
}, | ||
}; | ||
|
||
pub async fn get_event_group_admin_controller( | ||
http_req: HttpRequest, | ||
path_params: web::Path<PathParams>, | ||
ctx: web::Data<NitteiContext>, | ||
) -> Result<HttpResponse, NitteiError> { | ||
let account = protect_account_route(&http_req, &ctx).await?; | ||
let e = account_can_modify_event(&account, &path_params.event_group_id, &ctx).await?; | ||
|
||
let usecase = GetEventGroupUseCase { | ||
user_id: e.user_id, | ||
event_group_id: e.id, | ||
}; | ||
|
||
execute(usecase, &ctx) | ||
.await | ||
.map(|event_group| HttpResponse::Ok().json(APIResponse::new(event_group))) | ||
.map_err(NitteiError::from) | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct GetEventGroupUseCase { | ||
pub event_group_id: ID, | ||
pub user_id: ID, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub enum UseCaseError { | ||
InternalError, | ||
NotFound(ID), | ||
} | ||
|
||
impl From<UseCaseError> for NitteiError { | ||
fn from(e: UseCaseError) -> Self { | ||
match e { | ||
UseCaseError::InternalError => Self::InternalError, | ||
UseCaseError::NotFound(event_id) => Self::NotFound(format!( | ||
"The event group with id: {}, was not found.", | ||
event_id | ||
)), | ||
} | ||
} | ||
} | ||
|
||
#[async_trait::async_trait(?Send)] | ||
impl UseCase for GetEventGroupUseCase { | ||
type Response = EventGroup; | ||
|
||
type Error = UseCaseError; | ||
|
||
const NAME: &'static str = "GetEvent"; | ||
|
||
async fn execute(&mut self, ctx: &NitteiContext) -> Result<Self::Response, Self::Error> { | ||
let e = ctx | ||
.repos | ||
.event_groups | ||
.find(&self.event_group_id) | ||
.await | ||
.map_err(|_| UseCaseError::InternalError)?; | ||
match e { | ||
Some(event_group) if event_group.user_id == self.user_id => Ok(event_group), | ||
_ => Err(UseCaseError::NotFound(self.event_group_id.clone())), | ||
} | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
crates/api/src/event_group/get_event_group_by_external_id.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
use actix_web::{web, HttpRequest, HttpResponse}; | ||
use nittei_api_structs::get_event_group_by_external_id::*; | ||
use nittei_domain::{event_group::EventGroup, ID}; | ||
use nittei_infra::NitteiContext; | ||
|
||
use crate::{ | ||
error::NitteiError, | ||
shared::{ | ||
auth::protect_account_route, | ||
usecase::{execute, UseCase}, | ||
}, | ||
}; | ||
|
||
pub async fn get_event_group_by_external_id_admin_controller( | ||
http_req: HttpRequest, | ||
path_params: web::Path<PathParams>, | ||
ctx: web::Data<NitteiContext>, | ||
) -> Result<HttpResponse, NitteiError> { | ||
let account = protect_account_route(&http_req, &ctx).await?; | ||
|
||
let usecase = GetEventGroupByExternalIdUseCase { | ||
account_id: account.id, | ||
external_id: path_params.external_id.clone(), | ||
}; | ||
|
||
execute(usecase, &ctx) | ||
.await | ||
.map(|event| HttpResponse::Ok().json(APIResponse::new(event))) | ||
.map_err(NitteiError::from) | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct GetEventGroupByExternalIdUseCase { | ||
pub external_id: String, | ||
pub account_id: ID, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub enum UseCaseError { | ||
InternalError, | ||
NotFound(String), | ||
} | ||
|
||
impl From<UseCaseError> for NitteiError { | ||
fn from(e: UseCaseError) -> Self { | ||
match e { | ||
UseCaseError::InternalError => Self::InternalError, | ||
UseCaseError::NotFound(external_id) => Self::NotFound(format!( | ||
"The event group with external_id: {}, was not found.", | ||
external_id | ||
)), | ||
} | ||
} | ||
} | ||
|
||
#[async_trait::async_trait(?Send)] | ||
impl UseCase for GetEventGroupByExternalIdUseCase { | ||
type Response = EventGroup; | ||
|
||
type Error = UseCaseError; | ||
|
||
const NAME: &'static str = "GetEvent"; | ||
|
||
async fn execute(&mut self, ctx: &NitteiContext) -> Result<Self::Response, Self::Error> { | ||
let g = ctx | ||
.repos | ||
.event_groups | ||
.get_by_external_id(&self.external_id) | ||
.await | ||
.map_err(|_| UseCaseError::InternalError)?; | ||
match g { | ||
Some(event_group) if event_group.account_id == self.account_id => Ok(event_group), | ||
_ => Err(UseCaseError::NotFound(self.external_id.clone())), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,45 @@ | ||
mod create_event_group; | ||
mod delete_event_group; | ||
mod get_event_group; | ||
mod get_event_group_by_external_id; | ||
mod update_event_group; | ||
|
||
use actix_web::web; | ||
use create_event_group::create_event_group_admin_controller; | ||
use delete_event_group::delete_event_group_admin_controller; | ||
use get_event_group::get_event_group_admin_controller; | ||
use get_event_group_by_external_id::get_event_group_by_external_id_admin_controller; | ||
use update_event_group::update_event_group_admin_controller; | ||
|
||
// Configure the routes for the event_group module | ||
pub fn configure_routes(cfg: &mut web::ServiceConfig) { | ||
// Create an event for a user (admin route) | ||
// Create an event group for a user (admin route) | ||
cfg.route( | ||
"/user/{user_id}/event_group", | ||
web::post().to(create_event_group_admin_controller), | ||
); | ||
|
||
// Get a specific event group by external id | ||
cfg.route( | ||
"/user/event_groups/external_id/{external_id}", | ||
web::get().to(get_event_group_by_external_id_admin_controller), | ||
); | ||
|
||
// Get a specific event group by uid (admin route) | ||
cfg.route( | ||
"/user/event_groups/{event_group_id}", | ||
web::get().to(get_event_group_admin_controller), | ||
); | ||
|
||
// Update an event group by uid (admin route) | ||
cfg.route( | ||
"/user/event_grups/{event_group_id}", | ||
web::put().to(update_event_group_admin_controller), | ||
); | ||
|
||
// Delete an event group by uid (admin route) | ||
cfg.route( | ||
"/user/event_groups/{event_group_id}", | ||
web::delete().to(delete_event_group_admin_controller), | ||
); | ||
} |
Oops, something went wrong.