/// <summary> /// Searches the database for any open timespent entitys of the user. /// </summary> /// <param name="userId">The id of that user which spenttime entity should be searched for</param> /// <returns>The last spent time entity that hasn't been closed; if not found null</returns> public TimeSpentEntity GetOpenTimeSpentFromUser(int userId) { this.EnsureOpenConnection(); // Creates the query var query = new MySqlCommand("SELECT * FROM `timespent` WHERE `userid`=@val1 AND `stop` IS NULL LIMIT 1", this.connection); // Inserts the values query.Parameters.AddWithValue("@val1", userId); query.Prepare(); // Gets the result var reader = query.ExecuteReader(); // Checks if a query got found if (!reader.Read()) { reader.Close(); return(null); } // Creates the entity var timespent = new TimeSpentEntity() { Id = reader.GetInt32("id"), Enddisconnect = reader.GetBoolean("enddisconnect"), UserId = reader.GetInt32("userid"), Start = reader.GetDateTime("start") }; // Closes the reader reader.Close(); return(timespent); }
/// <summary> /// Log out the user with the timespent entity /// </summary> /// <param name="spent">The spent time entity</param> public void LogoutUser(TimeSpentEntity spent) { // Creates the query var query = new MySqlCommand("UPDATE `timespent` SET `stop`=@stopped WHERE `id`=@id", this.connection); // Appends all values query.Parameters.AddWithValue("@stopped", spent.Stop); query.Parameters.AddWithValue("@id", spent.Id); query.Prepare(); // Executes the query query.ExecuteNonQuery(); }
/// <summary> /// Logs in the user with the timespent entity /// </summary> /// <param name="user">The user to log in</param> /// <param name="spent">The spent time entity</param> public void LoginUser(UserEntity user, TimeSpentEntity spent) { this.EnsureOpenConnection(); // Creates the query var query = new MySqlCommand("INSERT INTO `timespent` (`id`, `start`, `stop`, `enddisconnect`, `userid`) VALUES (NULL, @start, NULL, '0', @userid)", this.connection); // Appends all values query.Parameters.AddWithValue("@start", spent.Start); query.Parameters.AddWithValue("@userid", user.Id); query.Prepare(); // Executes the query query.ExecuteNonQuery(); }
/// <summary> /// Searches a user by it's rfid code /// </summary> /// <param name="rfid">The rfid code that should be used to search the user</param> /// <returns>Null if no user got found; else the found user</returns> public Tuple <UserEntity, TimeSpentEntity> GetUserByRFIDCode(string rfid) { this.EnsureOpenConnection(); // Gets the user UserEntity user; { // Create the query to get the user var query = new MySqlCommand("SELECT `firstname`,`lastname`,`id` FROM `user` WHERE `rfidcode`=@rfidcode", this.connection); query.Parameters.AddWithValue("@rfidcode", rfid); query.Prepare(); // Gets the result var reader = query.ExecuteReader(); // Checks if no user got found if (!reader.Read()) { // Exits and returns no user reader.Close(); return(Tuple.Create <UserEntity, TimeSpentEntity>(null, null)); } // Creates the user user = new UserEntity { Id = reader.GetInt32("id"), Firstname = reader.GetString("firstname"), Lastname = reader.GetString("lastname") }; // Closes the query reader.Close(); } // Gets the spenttimeentity TimeSpentEntity entity = this.GetOpenTimeSpentFromUser(user.Id); return(Tuple.Create(user, entity)); }