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 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 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 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 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 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)); } }