-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
71 additions
and
9 deletions.
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
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,7 +1,12 @@ | ||
pub mod reset; | ||
pub mod state; | ||
|
||
use actix_web::Scope; | ||
pub use reset::*; | ||
pub use state::*; | ||
|
||
pub fn services(api_scope: Scope) -> Scope { | ||
api_scope.service(reset::reset_quest_instance) | ||
api_scope | ||
.service(reset_quest_instance) | ||
.service(get_quest_instance_state) | ||
} |
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,54 @@ | ||
use crate::{api::middlewares::RequiredAuthUser, domain::quests::QuestError}; | ||
use actix_web::{get, web, HttpResponse}; | ||
use quests_db::{ | ||
core::definitions::{Event, QuestsDatabase}, | ||
Database, | ||
}; | ||
use quests_protocol::definitions::QuestState; | ||
use quests_system::get_instance_state; | ||
use serde::{Deserialize, Serialize}; | ||
use utoipa::ToSchema; | ||
|
||
#[derive(Serialize, Deserialize, ToSchema)] | ||
pub struct GetInstanceStateResponse { | ||
pub state: QuestState, | ||
pub events: Vec<Event>, | ||
} | ||
|
||
/// Get Quest Instance's state. Allowed for the Quest Creator | ||
#[utoipa::path( | ||
params( | ||
("quest_instance" = String, description = "Quest Instance UUID") | ||
), | ||
responses( | ||
(status = 200, description = "Quest Instance state", body = GetInstanceStateResponse), | ||
(status = 401, description = "Unauthorized"), | ||
(status = 403, description = "Forbidden"), | ||
(status = 404, description = "Quest Instance not found"), | ||
(status = 500, description = "Internal Server Error") | ||
) | ||
)] | ||
#[get("/instances/{quest_instance}/state")] | ||
pub async fn get_quest_instance_state( | ||
data: web::Data<Database>, | ||
quest_instance: web::Path<String>, | ||
auth_user: RequiredAuthUser, | ||
) -> HttpResponse { | ||
let db = data.into_inner(); | ||
|
||
let RequiredAuthUser { address } = auth_user; | ||
|
||
match db.get_quest_instance(&quest_instance).await { | ||
Ok(instance) => match db.is_quest_creator(&instance.quest_id, &address).await { | ||
Ok(is_creator) if !is_creator => HttpResponse::from_error(QuestError::NotQuestCreator), | ||
Ok(_) => match get_instance_state(db.clone(), &instance.quest_id, &instance.id).await { | ||
Ok((_, state, events)) => { | ||
HttpResponse::Ok().json(GetInstanceStateResponse { state, events }) | ||
} | ||
Err(err) => HttpResponse::from_error(QuestError::from(err)), | ||
}, | ||
Err(err) => HttpResponse::from_error(QuestError::from(err)), | ||
}, | ||
Err(err) => HttpResponse::from_error(QuestError::from(err)), | ||
} | ||
} |
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
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
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