/// <summary> /// Removes all pools i database /// </summary> public void DeleteAllPools() { using (var db = new DatabaseContext()) { db.Database.ExecuteSqlCommand("DELETE [PoolSet]"); } }
/// <summary> /// Adds pool to a users poolSet. /// </summary /// <param name="email">The email of the user to recieve the pool</param> /// <param name="name">The pools name</param> /// <param name="volume">the pools volume</param> /// <returns>true on succes, false on fail</returns> public bool AddPool(string email, string name, double volume) { if (name == "") { return false; } if (IsPoolNameAvailable(email, name) == false) { return false; } if (volume < 0) { return false; } Pool newPool = new Pool { Name = name, Volume = volume, UserId = UserAccess.FindUserByEmail(email).Id }; using (var db = new DatabaseContext()) { db.PoolSet.Add(newPool); db.SaveChanges(); } return true; }
public void CreateDataEntry_AddingDataEntry_SearchReveilsCount1ForHumiditySet() { _uut.CreateDataEntry(ownerEmail, poolName, 987, 89, 8, 33); using (var db = new DatabaseContext()) { var searchdata = from data in db.HumiditySet select data; Assert.That(searchdata.Count(), Is.EqualTo(1)); } }
/// <summary> /// Creates a data entry with all types of data /// </summary> /// <param name="ownerEmail"></param> /// <param name="poolName"></param> /// <param name="chlorine"></param> /// <param name="temp"></param> /// <param name="pH"></param> /// <param name="humidity"></param> /// <returns></returns> public bool CreateDataEntry(string ownerEmail, string poolName, double chlorine, double temp, double pH, double humidity) { // make value checks here! if (PoolAccess.IsPoolNameAvailable(ownerEmail, poolName) == true) return false; using (var db = new DatabaseContext()) { // find pool to add mesurements for int userId = PoolAccess.FindSpecificPool(ownerEmail, poolName).UserId; var poolsearch = from pools in db.PoolSet where pools.UserId == userId && pools.Name == poolName select pools; // create 'Data' entity to store measurements in string time = DateTime.UtcNow.ToString(); var newData = new Data() { PoolId = poolsearch.First().Id, Timestamp = time }; db.DataSet.Add(newData); db.SaveChanges(); // the newdata must be saved to db, so that mesurement can find it by PK // get latest dataset from db var datasearch = from data in db.DataSet where data.Timestamp == time select data; // check for errors in datasearch if (datasearch.Count() > 1) return false; // create measurements var newChlorine = new Chlorine() { DataId = datasearch.First().Id, Value = chlorine }; var newTemperature = new Temperature() { DataId = datasearch.First().Id, Value = temp }; var newPH = new pH() { DataId = datasearch.First().Id, Value = pH }; var newHumidity = new Humidity() { DataId = datasearch.First().Id, Value = humidity }; // add mesurements to db db.ChlorineSet.Add(newChlorine); db.TemperatureSet.Add(newTemperature); db.pHSet.Add(newPH); db.HumiditySet.Add(newHumidity); db.SaveChanges(); } return true; }
public bool EditUserEmail(string email, string newEmail) { if (!IsEmailInUse(email) || newEmail.Length == 0) { return false; } using (var db = new DatabaseContext()) { var original = db.UserSet.Find(FindUserByEmail(email).Id); if (original != null) { original.Email = newEmail; db.SaveChanges(); } } return true; }
/// <summary> /// Edits name of a pool. /// </summary> /// <param name="ownerEmail">Email of owner.</param> /// <param name="currentName">Current name of the pool.</param> /// <param name="newName">The new name, which should be set for pool.</param> /// <returns>True on success, false on fail</returns> public bool EditPoolName(string ownerEmail, string currentName, string newName) { if (newName == "") return false; if (!IsPoolNameAvailable(ownerEmail, newName)) return false; using (var db = new DatabaseContext()) { var searchForPool = from pool in db.PoolSet where pool.User.Email == ownerEmail && pool.Name == currentName select pool; if (searchForPool.Any() == false) return false; searchForPool.First().Name = newName; db.SaveChanges(); } return true; }
/// <summary> /// Adds a 'User'-entry to database. /// </summary> /// <param name="fullname">String containing 2 or 3 names for user.</param> /// <param name="email">Email for user, only one user may exist per email.</param> /// <param name="password">Password for user.</param> /// <returns>Returns true is the user could be added to the database, false if the email is used.</returns> public bool AddUser(string fullname, string email, string password) { #region Checking 'email' and creating instance of 'User' if (IsEmailInUse(email)) { return false; } User user; if (!ValidateName(fullname)) { return false; } string[] names = fullname.Split(' '); if (names.Length <= 2) { user = new User() { Firstname = names[0], Lastname = names[1], Email = email, Password = password }; } else { user = new User() { Firstname = names[0], Middelname = names[1], Lastname = names[2], Email = email, Password = password }; } #endregion using (var db = new DatabaseContext()) { db.UserSet.Add(user); db.SaveChanges(); } return true; }
public void AddPool_AddingPoolToExistingUser_UserOnlyAppearOnceInDb() { // clear pools in db _uut.DeleteAllPools(); _userAccess.RemoveUser(_testUser2.Email); // list to store found users in List<User> listOfFoundUsers = new List<User>(); _uut.AddPool(_testUser1.Email, "poolname", 8); using (var db = new DatabaseContext()) { var searchForUsers = from user in db.UserSet select user; foreach (User user in searchForUsers) { listOfFoundUsers.Add(user); } } Assert.That(listOfFoundUsers.Count, Is.EqualTo(1)); }
public void Setup() { _userAccess = new UserAccess(); _uut = new PoolAccess(_userAccess); _testUser1 = new User() { Firstname = "John", Middelname = "Derp", Lastname = "Andersen", Email = "*****@*****.**", Password = "******" }; _testUser2 = new User() { Firstname = "Sire", Middelname = "Herp", Lastname = "Jensenei", Email = "*****@*****.**", Password = "******" }; using (var db = new DatabaseContext()) { db.UserSet.Add(_testUser1); db.UserSet.Add(_testUser2); db.SaveChanges(); } }
/// <summary> /// Removes a specific pool /// </summary> /// <param name="email">Identifies the administrating user</param> /// <param name="name">identifies the name of the pool</param> public bool RemovePool(string email, string name) { if (IsPoolNameAvailable(email, name)) { return false; } using (var db = new DatabaseContext()) { var searchForPool = from pool in db.PoolSet where pool.Name == name select pool; foreach (Pool pool in searchForPool) { if (pool.UserId == UserAccess.FindUserByEmail(email).Id) { db.PoolSet.Remove(pool); } } db.SaveChanges(); } return true; }
/// <summary> /// Checks if a specific pool name is in use on a specific address /// </summary /// <param name="name">Name of the pool</param> /// <param name="email">The email of the user to run check against</param> /// <returns>True if name is available, false if name is in use</returns> public bool IsPoolNameAvailable(string email, string name) { using (var db = new DatabaseContext()) { var searchPoolSet = from pool in db.PoolSet where pool.Name == name select pool; // check for errors in searchPoolSet if (searchPoolSet.Any() == false) return true; foreach (Pool pool in searchPoolSet) { try { if (pool.UserId == UserAccess.FindUserByEmail(email).Id) { return false; } } catch (Exception e) { Console.WriteLine("Herro pree, u got errors: " + e); } } } return true; }
/// <summary> /// Finds a specific pool /// </summary> /// <param name="email"> Email of the user to search upon for pools in</param> /// <param name="name">the name of the pool</param> /// <returns></returns> public Pool FindSpecificPool(string email, string name) { List<Pool> listOfFoundPools = new List<Pool>(); if (IsPoolNameAvailable(email, name) == true) { throw new PoolNotFoundException(); } using (var db = new DatabaseContext()) { var searchPools = from search in db.PoolSet where search.Name.Equals(name) select search; foreach (Pool pool in searchPools) { listOfFoundPools.Add(pool); } } return listOfFoundPools[0]; }
public List<Pool> FindAllPoolsOfUser(string ownerEmail) { if (UserAccess.IsEmailInUse(ownerEmail) == false) throw new UserNotFoundException(); List<Pool> poolList = new List<Pool>(); int userId = UserAccess.FindUserByEmail(ownerEmail).Id; using (var db = new DatabaseContext()) { var searchForPools = from pool in db.PoolSet where pool.UserId == userId select pool; foreach (Pool pool in searchForPools) { poolList.Add(pool); } } return poolList; }
/// <summary> /// Edits volume of pool. /// </summary> /// <param name="ownerEmail">Email of owner.</param> /// <param name="name">Name of pool to change value for.</param> /// <param name="newVolume">New value to be set.</param> /// <returns>True on success, false on fail</returns> public bool EditPoolVolume(string ownerEmail, string name, double newVolume) { if (newVolume <= 0) return false; using (var db = new DatabaseContext()) { var searchForPool = from pool in db.PoolSet where pool.User.Email == ownerEmail && pool.Name == name select pool; if (searchForPool.Any() == false) return false; searchForPool.First().Volume = newVolume; db.SaveChanges(); } return true; }
/// <summary> /// Changes owner of pool. /// </summary> /// <param name="currectOwnerEmail">Email of current owner.</param> /// <param name="name">Name of pool to change value for.</param> /// <param name="newUserEmail">New value to be set.</param> /// <returns>True on success, false on fail</returns> public bool EditPoolUser(string currectOwnerEmail, string name, string newUserEmail) { if (IsPoolNameAvailable(currectOwnerEmail, name) == true) return false; if (UserAccess.IsEmailInUse(newUserEmail) == false) return false; if (IsPoolNameAvailable(newUserEmail, name) == false) return false; using (var db = new DatabaseContext()) { var searchForPool = from pool in db.PoolSet where pool.User.Email == currectOwnerEmail && pool.Name == name select pool; searchForPool.First().UserId = UserAccess.FindUserByEmail(newUserEmail).Id; db.SaveChanges(); } return true; }
/// <summary> /// Directly execute an SQL statemen on the database, deleting all DataSets /// </summary> /// <returns>Allways returning true</returns> public void DeleteAllData() { using (var db = new DatabaseContext()) { db.Database.ExecuteSqlCommand("DELETE [ChlorineSet]"); db.Database.ExecuteSqlCommand("DELETE [pHSet]"); db.Database.ExecuteSqlCommand("DELETE [TemperatureSet]"); db.Database.ExecuteSqlCommand("DELETE [HumiditySet]"); db.Database.ExecuteSqlCommand("DELETE [DataSet]"); } }
public void DeleteAllData_AddedDataToSomePools_NoDataInDatabase() { _uut.CreateDataEntry(ownerEmail, poolName, 78, 89, 8, 33); _uut.CreateDataEntry(ownerEmail, poolName, 78, 89, 8, 33); _uut.CreateDataEntry(ownerEmail, poolName, 78, 89, 8, 33); _uut.DeleteAllData(); int entries = 0; using (var db = new DatabaseContext()) { var search = from data in db.ChlorineSet select data; entries = search.Count(); } Assert.That(entries, Is.EqualTo(0)); }
/// <summary> /// Searches database for a user with email matching argument. /// </summary> /// <param name="email"></param> /// <returns>Returns reference to type of User class. /// If the user could not be found, the return will be null.</returns> public User FindUserByEmail(string email) { User foundUser; using (var db = new DatabaseContext()) { var searchByEmail = from search in db.UserSet where search.Email.Equals(email) select search; if (searchByEmail.Count() > 1) throw new MultipleOccourencesOfEmailWasFoundException(); if (searchByEmail.Count() == 0) throw new UserNotFoundException(); foundUser = searchByEmail.First(); } return foundUser; }
public bool EditUserName(string email, string newName) { if (!IsEmailInUse(email)) { return false; } string[] names = newName.Split(' '); if (names.Length < 2) { return false; } using (var db = new DatabaseContext()) { var original = db.UserSet.Find(FindUserByEmail(email).Id); if (original != null) { if (names.Length == 2) { original.Firstname = names[0]; original.Middelname = null; original.Lastname = names[1]; } if (names.Length == 3) { original.Firstname = names[0]; original.Middelname = names[1]; original.Lastname = names[2]; } db.SaveChanges(); } } return true; }
/// <summary> /// Method to check for existing email. /// </summary> /// <param name="email">Checks for this email.</param> /// <returns>Return true if the email is already used. /// False if no entry was found with that email.</returns> public bool IsEmailInUse(string email) { List<User> listOfFoundUsers = new List<User>(); using (var db = new DatabaseContext()) { var searchByEmail = from search in db.UserSet where search.Email.Equals(email) select search; foreach (User user in searchByEmail) { listOfFoundUsers.Add(user); } } if (listOfFoundUsers.Count == 0) { return false; } return true; }
/// <summary> /// Method will search for a User with argument email and remove /// user from database. /// </summary> /// <param name="email">Email of user to be deleted from database.</param> public void RemoveUser(string email) { using (var db = new DatabaseContext()) { if (!IsEmailInUse(email)) { throw new UserNotFoundException(); } var removeUserByEmail = from user in db.UserSet where user.Email == email select user; foreach (var user in removeUserByEmail) { db.UserSet.Remove(user); } db.SaveChanges(); } }
public void FindUserByEmail_TwoUsersWithSameEmailExistInDB_ThrowsMultipleOccourencesOfEmailWasFoundException() { User user1 = new User() { Firstname = "John", Middelname = "Derp", Lastname = "Herpson", Email = "email", Password = "******" }; User user2 = new User() { Firstname = "Simon", Middelname = "Siggy", Lastname = "Sergson", Email = "email", Password = "******" }; using (var db = new DatabaseContext()) { db.UserSet.Add(user1); db.UserSet.Add(user2); db.SaveChanges(); } Assert.Throws<MultipleOccourencesOfEmailWasFoundException>(() => _uut.FindUserByEmail("email")); }
/// <summary> /// Queries temperature values within a given time range: dd/MM/yyyy HH:mm:ss /// </summary> /// <param name="poolOwnerEmail">The email of the pool owner</param> /// <param name="poolName">The specific pool name</param> /// <param name="daysToGoBack">Specifies how many days ago to start looking at data</param> /// <returns>A list of tuples, where each tuple contains a temperature value and the sensor that measured it</returns> public List<Tuple<SensorTypes, double>> GetTemperatureValues(string poolOwnerEmail, string poolName, int daysToGoBack) { double days = System.Convert.ToDouble(daysToGoBack); string now = DateTime.UtcNow.ToString("G"); string start = DateTime.Parse(now).AddDays(-days).ToString("G"); using (var db = new DatabaseContext()) { #region Convert start and end times to DateTime types DateTime startTime = DateTime.ParseExact(start, "dd/MM/yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture); DateTime endTime = DateTime.ParseExact(now, "dd/MM/yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture); #endregion #region Query for all user-pool specific temperature data var temperatureDataQuery = from temperature in db.TemperatureSet where temperature.Data.Pool.Name == poolName && temperature.Data.Pool.User.Email == poolOwnerEmail select temperature; #endregion #region Check for timestamp matches and add to tuples List<Tuple<SensorTypes, double>> temperatureTuples = new List<Tuple<SensorTypes, double>>(); foreach (var temperature in temperatureDataQuery) { if (DateTime.Parse(temperature.Data.Timestamp).CompareTo(endTime) < 0 || DateTime.Parse(temperature.Data.Timestamp).CompareTo(startTime) > 0) { temperatureTuples.Add(new Tuple<SensorTypes, double>(SensorTypes.Temperature, temperature.Value)); } } #endregion return temperatureTuples; } }