Skip to content

Commit c3c6411

Browse files
committed
some changes
1 parent 391ff7a commit c3c6411

1 file changed

Lines changed: 2 additions & 160 deletions

File tree

src/data.rs

Lines changed: 2 additions & 160 deletions
Original file line numberDiff line numberDiff line change
@@ -1,166 +1,8 @@
1-
use lazy_static::lazy_static;
2-
use serde::{Deserialize, Serialize};
3-
use std::collections::HashMap;
4-
use std::path::Path;
51
use std::time::{SystemTime, UNIX_EPOCH};
6-
use tokio::sync::Mutex;
72

8-
const FILE_PATH: &str = "users.json";
9-
10-
pub type BotResult<T> = Result<T, Box<dyn std::error::Error + Send + Sync>>;
11-
12-
lazy_static! {
13-
static ref DATA_MUTEX: Mutex<()> = Mutex::new(());
14-
}
15-
16-
#[derive(Serialize, Deserialize, Debug, Clone)]
17-
pub struct User {
18-
pub id: u64,
19-
pub chat_id: Option<i64>,
20-
pub name: String,
21-
pub requests_amount: u64,
22-
pub timestamp: u64,
23-
pub register_timestamp: u64,
24-
}
25-
26-
pub enum UserColumn {
27-
Id,
28-
Name,
29-
RequestsAmount,
30-
Timestamp,
31-
ChatId,
32-
RegisterTimestamp
33-
}
34-
35-
async fn read_users() -> BotResult<HashMap<u64, User>> {
36-
let path = Path::new(FILE_PATH);
37-
if !path.exists() {
38-
return Ok(HashMap::new());
39-
}
40-
41-
let content = tokio::fs::read_to_string(path).await?;
42-
let users = serde_json::from_str(&content)?;
43-
Ok(users)
44-
}
45-
46-
async fn write_users(users: &HashMap<u64, User>) -> BotResult<()> {
47-
let path = Path::new(FILE_PATH);
48-
let content = serde_json::to_string_pretty(users)?;
49-
tokio::fs::write(path, content).await?;
50-
Ok(())
51-
}
52-
53-
pub fn current_timestamp() -> u64 {
3+
pub fn current_timestamp() -> i64 {
544
SystemTime::now()
555
.duration_since(UNIX_EPOCH)
566
.unwrap()
57-
.as_millis() as u64
58-
}
59-
60-
#[allow(dead_code)]
61-
pub async fn set_data(
62-
user_id: u64,
63-
column: UserColumn,
64-
value: &str,
65-
) -> BotResult<()> {
66-
let _lock = DATA_MUTEX.lock().await;
67-
let mut users = read_users().await?;
68-
69-
let user = users.get_mut(&user_id)
70-
.ok_or("User not found")?;
71-
72-
match column {
73-
UserColumn::Name => user.name = value.to_string(),
74-
UserColumn::RequestsAmount => user.requests_amount = value.parse()?,
75-
UserColumn::Timestamp => user.timestamp = value.parse()?,
76-
UserColumn::Id => return Err("Cannot modify user ID".into()),
77-
UserColumn::RegisterTimestamp => return Err("Cannot modify user register timestamp".into()),
78-
UserColumn::ChatId => user.chat_id = value.parse().ok(),
79-
}
80-
81-
write_users(&users).await?;
82-
Ok(())
83-
}
84-
85-
pub async fn has_user(id: u64) -> BotResult<bool> {
86-
let _lock = DATA_MUTEX.lock().await;
87-
let users = read_users().await?;
88-
Ok(users.contains_key(&id))
89-
}
90-
91-
pub async fn add_user(user: User) -> BotResult<()> {
92-
let _lock = DATA_MUTEX.lock().await;
93-
let mut users = read_users().await?;
94-
95-
if users.contains_key(&user.id) {
96-
return Err("User already exists".into());
97-
}
98-
99-
users.insert(user.id, user);
100-
write_users(&users).await?;
101-
Ok(())
102-
}
103-
104-
#[allow(dead_code)]
105-
pub async fn update_user(user: User) -> BotResult<()> {
106-
let _lock = DATA_MUTEX.lock().await;
107-
let mut users = read_users().await?;
108-
109-
if !users.contains_key(&user.id) {
110-
return Err("User not found".into());
111-
}
112-
113-
users.insert(user.id, user);
114-
write_users(&users).await?;
115-
Ok(())
116-
}
117-
118-
#[allow(dead_code)]
119-
pub async fn remove_user(id: u64) -> BotResult<()> {
120-
let _lock = DATA_MUTEX.lock().await;
121-
let mut users = read_users().await?;
122-
123-
if users.remove(&id).is_none() {
124-
return Err("User not found".into());
125-
}
126-
127-
write_users(&users).await?;
128-
Ok(())
129-
}
130-
131-
#[allow(dead_code)]
132-
pub async fn get_data(user_id: u64, column: UserColumn) -> BotResult<Option<String>> {
133-
let _lock = DATA_MUTEX.lock().await;
134-
let users = read_users().await?;
135-
136-
Ok(users.get(&user_id).and_then(|user| {
137-
match column {
138-
UserColumn::Id => Some(user.id.to_string()),
139-
UserColumn::Name => Some(user.name.clone()),
140-
UserColumn::RequestsAmount => Some(user.requests_amount.to_string()),
141-
UserColumn::Timestamp => Some(user.timestamp.to_string()),
142-
UserColumn::ChatId => user.chat_id.map(|id| id.to_string()),
143-
UserColumn::RegisterTimestamp => Some(user.register_timestamp.to_string()),
144-
}
145-
}))
146-
}
147-
148-
pub async fn get_user(id: u64) -> BotResult<Option<User>> {
149-
let _lock = DATA_MUTEX.lock().await;
150-
let users = read_users().await?;
151-
Ok(users.get(&id).cloned())
152-
}
153-
154-
pub async fn increment_requests(id: u64) -> BotResult<()> {
155-
let _lock = DATA_MUTEX.lock().await;
156-
let mut users = read_users().await?;
157-
158-
if let Some(user) = users.get_mut(&id) {
159-
user.requests_amount += 1;
160-
user.timestamp = current_timestamp();
161-
write_users(&users).await?;
162-
Ok(())
163-
} else {
164-
Err("User not found".into())
165-
}
7+
.as_millis() as i64
1668
}

0 commit comments

Comments
 (0)