public OperationStatus GetTimeEntryList(object token, TimeEntryFilter filter) { try { var validation = filter.Validate(); if (!(bool)validation) { return(validation); } using (IDatabase db = this.CurrentDatabase) { var existingUser = db.SingleOrDefault <User>("where accesstoken=@0", token); if (existingUser == null) { return(OperationStatus.Failed("Token is invalid")); } //TODO var entries = filter.HasFilter ? db.Fetch <TimeEntry>("where userId=@0 and entryDate>=@1 and entryDate<=@2", existingUser.Id, DateTimeSQLite(filter.StartDate.Value), DateTimeSQLite(filter.EndDate.Value)) : db.Fetch <TimeEntry>("where userId=@0", existingUser.Id); return(OperationStatus.Passed("Time entries fetched", entries)); } } catch (Exception exc) { Logger.LogException(exc); return(OperationStatus.Failed("Error fetching time entries")); } }
public Task <OperationStatus> GetWeeklyReports() { var tcs = new TaskCompletionSource <OperationStatus>(); var client = new RestClient(BaseUrl); var request = new RestRequest("api/reports/weeklyReport", Method.POST); request.RequestFormat = DataFormat.Json; request.AddHeader("auth-token", AppConfiguration.Token); client.ExecuteAsync(request, response => { if (response.StatusCode == System.Net.HttpStatusCode.OK) { var data = JsonConvert.DeserializeObject <OperationStatus>(response.Content); if (data != null && (bool)data) { var list = JsonConvert.DeserializeObject <List <WeeklyReport> >(data.Data.ToString()); data.Data = list; } tcs.SetResult(data); } else { tcs.SetResult(OperationStatus.Failed("Error fetching reports!")); } }); return(tcs.Task); }
public Task <OperationStatus> CreateUser(string username, string password) { var tcs = new TaskCompletionSource <OperationStatus>(); var client = new RestClient(BaseUrl); var request = new RestRequest("api/users/createAccount", Method.POST); request.RequestFormat = DataFormat.Json; request.AddBody(new User() { Username = username, Password = password }); //request.AddHeader("header", "value"); client.ExecuteAsync(request, response => { if (response.StatusCode == System.Net.HttpStatusCode.OK) { var data = JsonConvert.DeserializeObject <OperationStatus>(response.Content); tcs.SetResult(data); } else { tcs.SetResult(OperationStatus.Failed("Error login in!")); } }); return(tcs.Task); }
public OperationStatus InsertTimeEntry(TimeEntry timeEntry, object token) { try { using (IDatabase db = this.CurrentDatabase) { var existingUser = db.SingleOrDefault <User>("where accesstoken=@0", token); if (existingUser == null) { return(OperationStatus.Failed("Token is invalid")); } timeEntry.UserId = existingUser.Id; db.Insert(timeEntry); return(OperationStatus.Passed("Time entry added")); } } catch (Exception exc) { Logger.LogException(exc); return(OperationStatus.Failed("Error adding time entry")); } }
public Task <OperationStatus> AddTimeEntry(TimeEntry timeEntry) { var tcs = new TaskCompletionSource <OperationStatus>(); var client = new RestClient(BaseUrl); var request = new RestRequest("api/time/add", Method.POST); request.RequestFormat = DataFormat.Json; request.AddHeader("auth-token", AppConfiguration.Token); request.AddBody(timeEntry); client.ExecuteAsync(request, response => { if (response.StatusCode == System.Net.HttpStatusCode.OK) { var data = JsonConvert.DeserializeObject <OperationStatus>(response.Content); tcs.SetResult(data); } else { tcs.SetResult(OperationStatus.Failed("Error adding record!")); } }); return(tcs.Task); }
public OperationStatus GetWeeklyReports(object token) { try { using (IDatabase db = this.CurrentDatabase) { var existingUser = db.SingleOrDefault <User>("where accesstoken=@0", token); if (existingUser == null) { return(OperationStatus.Failed("Token is invalid")); } var entries = db.Fetch <TimeEntry>("where userId=@0", existingUser.Id); var report = this._reportGenerator.GenerateWeeklyReport(entries); return(OperationStatus.Passed("Report generated", report)); } } catch (Exception exc) { Logger.LogException(exc); return(OperationStatus.Failed("Error generating report")); } }
public OperationStatus CreateUser(string username, string password) { if (string.IsNullOrWhiteSpace(username)) { return(OperationStatus.Failed("Please provide username")); } if (string.IsNullOrWhiteSpace(password)) { return(OperationStatus.Failed("Please provide password")); } using (IDatabase db = this.CurrentDatabase) { var existingUser = db.SingleOrDefault <User>("where username=@0", username); if (existingUser != null) { return(OperationStatus.Failed("User already exists")); } User newUser = new User() { Username = username, Password = password }; db.Insert(newUser); return(OperationStatus.Passed("User was created sucessfully")); } }
public OperationStatus PostGetWeeklyReport() { string token = ServerHelpers.GetAccessTokenFromRequest(this.Request); if ((bool)_usersService.Authorize(token)) { return(this._reportService.GetWeeklyReports(token)); } else { return(OperationStatus.Failed("Token is invalid")); } }
public OperationStatus TryRunWithCachedCredentials() { User cachedUser = this.GetUserFromCache(); if (cachedUser != null && !string.IsNullOrEmpty(cachedUser.AccessToken)) { return(OperationStatus.Passed("Token exists", cachedUser.AccessToken)); } else { return(OperationStatus.Failed("Token does not exist")); } }
public OperationStatus PostGetTimeEntries(TimeEntryFilter filter) { string token = ServerHelpers.GetAccessTokenFromRequest(this.Request); if ((bool)_usersService.Authorize(token)) { return(_timeService.GetTimeEntryList(token, filter)); } else { return(OperationStatus.Failed("Token is invalid")); } }
public OperationStatus PostAddTimeEntry(TimeEntry timeEntry) { string token = ServerHelpers.GetAccessTokenFromRequest(this.Request); if ((bool)_usersService.Authorize(token)) { return(_timeService.InsertTimeEntry(timeEntry, token)); } else { return(OperationStatus.Failed("Token is invalid")); } }
public OperationStatus Authorize(string accessToken) { using (IDatabase db = this.CurrentDatabase) { var existingUser = db.SingleOrDefault <User>("where accessToken=@0", accessToken); if (existingUser == null) { return(OperationStatus.Failed("Token is invalid", false)); } return(OperationStatus.Passed("Token is valid", true)); } }
public Task <OperationStatus> LoginUser(string username, string password) { var user = new User() { Username = username, Password = password }; var tcs = new TaskCompletionSource <OperationStatus>(); var client = new RestClient(BaseUrl); var request = new RestRequest("api/users/login", Method.POST); request.RequestFormat = DataFormat.Json; request.AddBody(user); client.ExecuteAsync(request, response => { if (response.StatusCode == System.Net.HttpStatusCode.OK) { var data = JsonConvert.DeserializeObject <OperationStatus>(response.Content); if (data != null && (bool)data) { SaveUserToCache(new User() { AccessToken = data.Data.ToString() }); AppConfiguration.Token = data.Data.ToString(); } tcs.SetResult(data); } else { tcs.SetResult(OperationStatus.Failed("Error login in!")); } }); return(tcs.Task); }
public OperationStatus LoginUser(string username, string password) { if (string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(password)) { return(OperationStatus.Failed("Provide valid username and password")); } using (IDatabase db = this.CurrentDatabase) { var existingUser = db.SingleOrDefault <User>("where username=@0 and password=@1", username, password); if (existingUser == null) { return(OperationStatus.Failed("Provide valid username and password")); } existingUser.AccessToken = tokenProvider.GenerateToken(username, password); int rowUpdated = db.Update(existingUser); return(OperationStatus.Passed("User logged in", existingUser.AccessToken)); } }