Skip to content
This repository has been archived by the owner on Aug 7, 2023. It is now read-only.

Commit

Permalink
feat: checkin (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
ImSoZRious authored Jul 30, 2023
1 parent 4833c24 commit 34ca5f9
Show file tree
Hide file tree
Showing 10 changed files with 170 additions and 3 deletions.
1 change: 1 addition & 0 deletions config/config.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ port = 3000
debug = true
max_file_size = 10
phase = "register"
event_day = 1

[service]
backend = "localhost:3001"
Expand Down
7 changes: 4 additions & 3 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
#[derive(serde::Deserialize)]
#[derive(serde::Deserialize, Clone)]
pub struct Config {
pub app: AppConfig,
pub service: ServiceConfig,
}

#[derive(serde::Deserialize)]
#[derive(serde::Deserialize, Clone)]
pub struct AppConfig {
pub port: i32,
pub debug: bool,
pub max_file_size: i32,
pub event_day: i32,
pub phase: String,
}

#[derive(serde::Deserialize)]
#[derive(serde::Deserialize, Clone)]
pub struct ServiceConfig {
pub backend: String,
pub auth: String,
Expand Down
5 changes: 5 additions & 0 deletions src/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ use utoipa::{
crate::handler::estamp::get_all_estamps,
crate::handler::estamp::get_user_estamps,
crate::handler::estamp::claim_estamp,
crate::handler::checkin::has_checkin,
crate::handler::checkin::checkin,
),
components(schemas(
crate::dto::Validate,
Expand All @@ -51,6 +53,8 @@ use utoipa::{
crate::dto::GetUserEstampsResponse,
crate::dto::EstampEvent,
crate::dto::UserEstampEvent,
crate::dto::HasCheckinResponse,
crate::dto::CheckinResponse,
)),
info(
title = "RPKM66",
Expand All @@ -73,6 +77,7 @@ use utoipa::{
(name = "Staff"),
(name = "Freshy Night"),
(name = "Estamp"),
(name = "Check in")
)
)]
pub struct ApiDoc;
Expand Down
43 changes: 43 additions & 0 deletions src/dto/checkin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use utoipa::ToSchema;

use super::IntoDto;

#[derive(serde::Serialize, ToSchema)]
pub struct HasCheckinResponse {
has_checkin: bool,
}

impl From<bool> for HasCheckinResponse {
fn from(value: bool) -> Self {
Self { has_checkin: value }
}
}

impl IntoDto for HasCheckinResponse {
type Target = HasCheckinResponse;

fn into_dto(self) -> Self::Target {
self
}
}

#[derive(serde::Serialize, ToSchema)]
pub struct CheckinResponse {
success: bool,
}

impl From<bool> for CheckinResponse {
fn from(value: bool) -> Self {
Self { success: value }
}
}

impl IntoDto for CheckinResponse {
type Target = CheckinResponse;

fn into_dto(self) -> Self::Target {
self
}
}


2 changes: 2 additions & 0 deletions src/dto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ pub trait IntoDto: Sized {

mod auth;
mod baan;
mod checkin;
mod ci_user;
mod estamp;
mod file;
Expand All @@ -68,6 +69,7 @@ mod user;

pub use auth::*;
pub use baan::*;
pub use checkin::*;
pub use ci_user::*;
pub use estamp::*;
pub use file::*;
Expand Down
58 changes: 58 additions & 0 deletions src/handler/checkin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use axum::{extract::State, response::IntoResponse};

use crate::{dto::IntoDto, middleware::auth::Cred};

#[derive(Clone)]
pub struct Handler {
service: crate::service::checkin::Service,
}

impl Handler {
pub fn new(service: crate::service::checkin::Service) -> Self {
Self { service }
}
}

/// Has checkin
///
/// Check whether user has checkin or not
#[utoipa::path(
get,
path = "/checkin",
tag = "Check in",
responses(
(status = 200, description = "Success", body = HasCheckinResponse),
(status = 401, description = "Unauthorized"),
),
security(
("api_key" = []),
),
)]
pub async fn has_checkin(State(hdr): State<Handler>, cred: Cred) -> impl IntoResponse {
hdr.service
.has_checkin(cred.user_id)
.await
.map(crate::dto::HasCheckinResponse::from)
.map(IntoDto::into_response)
}

#[utoipa::path(
post,
path = "/checkin",
tag = "Check in",
responses(
(status = 200, description = "Success", body = CheckinResponse),
(status = 401, description = "Unauthorized"),
(status = 409, description = "Duplicated"),
),
security(
("api_key" = []),
),
)]
pub async fn checkin(State(hdr): State<Handler>, cred: Cred) -> impl IntoResponse {
hdr.service
.checkin(cred.user_id)
.await
.map(crate::dto::CheckinResponse::from)
.map(IntoDto::into_response)
}
1 change: 1 addition & 0 deletions src/handler/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod auth;
pub mod baan;
pub mod checkin;
pub mod ci_user;
pub mod estamp;
pub mod file;
Expand Down
6 changes: 6 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub struct AppState {
pub ci_staff_hdr: handler::staff::Handler,
pub ci_user_hdr: handler::ci_user::Handler,
pub estamp_hdr: handler::estamp::Handler,
pub checkin_hdr: handler::checkin::Handler,
pub auth_svc: service::auth::Service,
}

Expand Down Expand Up @@ -115,6 +116,7 @@ async fn main() {
let ci_staff_svc = service::staff::Service::new(ci_staff_client.clone());
let ci_user_svc = service::ci_user::Service::new(ci_user_client.clone());
let estamp_svc = service::estamp::Service::new(event_client.clone(), ci_user_client.clone());
let checkin_svc = service::checkin::Service::new(ci_user_client.clone(), config.app.clone());

let auth_hdr = handler::auth::Handler::new(auth_svc.clone(), user_svc.clone());
let baan_hdr = handler::baan::Handler::new(baan_svc.clone(), user_svc.clone());
Expand All @@ -124,6 +126,7 @@ async fn main() {
let ci_staff_hdr = handler::staff::Handler::new(ci_staff_svc.clone());
let ci_user_hdr = handler::ci_user::Handler::new(ci_user_svc.clone());
let estamp_hdr = handler::estamp::Handler::new(estamp_svc.clone());
let checkin_hdr = handler::checkin::Handler::new(checkin_svc.clone());

let state = AppState {
auth_hdr: auth_hdr.clone(),
Expand All @@ -135,6 +138,7 @@ async fn main() {
ci_user_hdr: ci_user_hdr.clone(),
estamp_hdr: estamp_hdr.clone(),
group_hdr: group_hdr.clone(),
checkin_hdr: checkin_hdr.clone(),
};

let mut non_state_app: Router<AppState, Body> = Router::new();
Expand Down Expand Up @@ -175,6 +179,8 @@ async fn main() {
.route("/estamp", get(handler::estamp::get_all_estamps))
.route("/estamp/my", get(handler::estamp::get_user_estamps))
.route("/estamp/:token", post(handler::estamp::claim_estamp))
.route("/checkin", get(handler::checkin::has_checkin))
.route("/checkin", post(handler::checkin::checkin))
.layer(body_limit_layer)
.layer(trace)
.layer(cors);
Expand Down
49 changes: 49 additions & 0 deletions src/service/checkin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use crate::Result;
use rpkm66_rust_proto::rpkm66::checkin::user::v1::{
user_service_client::UserServiceClient, AddEventRequest, GetUserEventByEventIdRequest,
};
use tonic::transport::Channel;

#[derive(Clone)]
pub struct Service {
client: UserServiceClient<Channel>,
config: crate::config::AppConfig,
}

impl Service {
pub fn new(client: UserServiceClient<Channel>, config: crate::config::AppConfig) -> Self {
Service { client, config }
}

pub async fn has_checkin(&self, user_id: String) -> Result<bool> {
Ok(self
.client
.clone()
.get_user_event_by_event_id(GetUserEventByEventIdRequest {
user_id,
event_id: self.get_checkin_event_id(),
})
.await?
.into_inner()
.user_event
.is_some())
}

pub async fn checkin(&self, user_id: String) -> Result<bool> {
Ok(self
.client
.clone()
.add_event(AddEventRequest {
user_id,
token: self.get_checkin_event_id(),
})
.await?
.into_inner()
.event
.is_some())
}

fn get_checkin_event_id(&self) -> String {
format!("checkin-day-{}", self.config.event_day)
}
}
1 change: 1 addition & 0 deletions src/service/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod auth;
pub mod baan;
pub mod checkin;
pub mod ci_user;
pub mod estamp;
pub mod file;
Expand Down

0 comments on commit 34ca5f9

Please sign in to comment.