Skip to content

Commit 036fde1

Browse files
authored
Check user login status before fetching problems (#93)
1 parent 31035c9 commit 036fde1

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

cache/src/kvstore.rs

+5
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ impl KvStore {
118118
Ok(None)
119119
}
120120

121+
/// Check if key exists in the cache
122+
pub fn has_key(&self, key: String) -> bool {
123+
self.index.contains_key(&key)
124+
}
125+
121126
/// Removes the given key.
122127
pub fn remove(&mut self, key: String) -> Result<()> {
123128
// check if key exist in index and delete if from the log file

src/service/leetcode.rs

+12
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@ impl<'a> ServiceProvider<'a> for Leetcode<'a> {
8585
}
8686

8787
async fn list_problems(&mut self, list: List) -> Result<()> {
88+
if !self.is_user_logged_in() {
89+
print!(
90+
"{}",
91+
Color::Red("You need to login to list problems").make()
92+
);
93+
return Ok(());
94+
}
95+
8896
let problems_res = self.fetch_all_problems().await?;
8997
let mut probs: ProblemInfoSeq = vec![];
9098

@@ -306,6 +314,10 @@ impl<'a> Leetcode<'a> {
306314
})
307315
}
308316

317+
fn is_user_logged_in(&self) -> bool {
318+
self.cache.has_key(CacheKey::Session.into())
319+
}
320+
309321
fn cache_session(&mut self, session: Session) -> Result<()> {
310322
let session_str = serde_json::to_string(&session)?;
311323
self.cache.set(CacheKey::Session.into(), session_str)?;

0 commit comments

Comments
 (0)