12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- using System.Text.Json;
- using System;
- using System.Collections.Generic;
- namespace gameapi.Library
- {
- public class SessionRetriever
- {
- public SessionRetriever()
- {
- this.jsonFileReader = new JsonFileReader();
- }
- private JsonFileReader jsonFileReader;
- public dynamic GetValid(string token)
- {
- var sessionData = this.jsonFileReader.ReadGameData($"sessions/{token}.json");
- var dateProp = sessionData.GetProperty("validUntil").ToString();
- var date = DateTime.Parse(dateProp.ToString());
- if(DateTime.Now > date) {
- throw new Exception("Session expired.");
- }
- return sessionData;
- }
- private List<User> GetUsers()
- {
- JsonElement usersProp = this.jsonFileReader.ReadGameData("users.json");
- List<User> users = new List<User>();
- foreach (JsonElement userElem in usersProp.EnumerateArray())
- {
- var userToAdd = User.Parse(userElem);
- users.Add(userToAdd);
- }
- return users;
- }
- public User GetUserByToken(string token) {
- var sessionData = this.GetValid(token);
- List<User> users = this.GetUsers();
- User existingUser = users.Find(x => x.email == sessionData.GetProperty("email").ToString());
- if(existingUser == null) {
- throw new Exception("User not found.");
- }
- return existingUser;
- }
- }
- }
|