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

Commit

Permalink
add config
Browse files Browse the repository at this point in the history
  • Loading branch information
ImSoZRious committed Aug 3, 2023
1 parent 57f474d commit 9550982
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
2 changes: 2 additions & 0 deletions config/config.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ debug = true
max_file_size = 10
phase = "register"
event_day = 1
e_stamp_count = 4
redeem_full = false

[service]
backend = "localhost:3001"
Expand Down
2 changes: 2 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ pub struct AppConfig {
pub max_file_size: i32,
pub event_day: i32,
pub phase: String,
pub e_stamp_count: usize,
pub redeem_full: bool,
}

#[derive(serde::Deserialize, Clone)]
Expand Down
15 changes: 13 additions & 2 deletions src/service/estamp.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use axum::http::StatusCode;
use rpkm66_rust_proto::rpkm66::{
checkin::event::v1::event_service_client::EventServiceClient,
checkin::user::v1::{user_service_client::UserServiceClient, GetUserEventByEventIdRequest},
Expand All @@ -8,22 +9,25 @@ use rpkm66_rust_proto::rpkm66::{
};
use tonic::{transport::Channel, Code};

use crate::{error::Error, handler::user, Result};
use crate::{error::Error, Result};

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

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

Expand Down Expand Up @@ -72,12 +76,19 @@ impl Service {
}

pub async fn redeem_item(&self, user_id: String) -> Result<bool> {
if self.config.redeem_full {
return Err(Error::WithMessage(
StatusCode::TOO_MANY_REQUESTS,
"Item run out".to_string(),
));
}

let has_redeem = self.has_redeem_item(user_id.clone()).await?;

if !has_redeem {
let stamps = self.get_user_estamp(user_id.clone()).await?;

if stamps.len() != 4 {
if stamps.len() != self.config.e_stamp_count {
return Err(Error::Forbidden);
}

Expand Down

0 comments on commit 9550982

Please sign in to comment.