Skip to content

Commit

Permalink
Check user login status before fetching problems (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
dragfire authored Mar 28, 2024
1 parent 31035c9 commit 036fde1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cache/src/kvstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ impl KvStore {
Ok(None)
}

/// Check if key exists in the cache
pub fn has_key(&self, key: String) -> bool {
self.index.contains_key(&key)
}

/// Removes the given key.
pub fn remove(&mut self, key: String) -> Result<()> {
// check if key exist in index and delete if from the log file
Expand Down
12 changes: 12 additions & 0 deletions src/service/leetcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ impl<'a> ServiceProvider<'a> for Leetcode<'a> {
}

async fn list_problems(&mut self, list: List) -> Result<()> {
if !self.is_user_logged_in() {
print!(
"{}",
Color::Red("You need to login to list problems").make()
);
return Ok(());
}

let problems_res = self.fetch_all_problems().await?;
let mut probs: ProblemInfoSeq = vec![];

Expand Down Expand Up @@ -306,6 +314,10 @@ impl<'a> Leetcode<'a> {
})
}

fn is_user_logged_in(&self) -> bool {
self.cache.has_key(CacheKey::Session.into())
}

fn cache_session(&mut self, session: Session) -> Result<()> {
let session_str = serde_json::to_string(&session)?;
self.cache.set(CacheKey::Session.into(), session_str)?;
Expand Down

0 comments on commit 036fde1

Please sign in to comment.