/// <summary> /// WORK IN PROGRESS /// UNUSED FOR NOW /// </summary> /// <param name="tag"></param> /// <returns></returns> public static bool UpdateTenant(string tenantTag, string location) { try { using var db = new Database.DatabaseFilePath(); var thisBool = false; while (!thisBool) { var checkInfo = location; var checkAnswer = db.Tenants .Any(e => e.TenantTag == checkInfo); if (!checkAnswer) { var aptNr = db.DoorLocations .SingleOrDefault(e => e.Name == location); var exist = db.Tenants .Count(e => e.TenantTag == checkInfo); db.Tenants .Update(new Models.Tenant { TenantTag = checkInfo, Location = aptNr } ); db.SaveChanges(); thisBool = true; } } return(true); } catch (Exception) { return(false); } }
/// <summary> /// Create log info and send to database /// </summary> /// <param name="dateTime">Date and time of log entry</param> /// <param name="code">Door event code</param> /// <param name="doorName">Where door is located</param> /// <param name="tenantTag">Tag number of tenant performing action</param> /// <returns></returns> public static bool Log(string dateTime, string code, string doorName, string tenantTag) { try { using var db = new Database.DatabaseFilePath(); var eventID = db.DoorEvents .FirstOrDefault(e => e.Code == code.ToUpper()); var door = db.DoorNames .FirstOrDefault(e => e.Name == doorName.ToUpper()); var tenant = db.Tenants .FirstOrDefault(e => e.TenantTag == tenantTag.ToUpper()); db.EventLogs .Add( new Models.EventLog { DateTime = dateTime, DoorEvent = eventID, DoorName = door, Tenant = tenant } ); db.SaveChanges(); return(true); } catch (Exception) { return(false); } }
/// <summary> /// List tenants at requested apartment /// </summary> /// <param name="aptNr">Apartment number</param> /// <returns></returns> public static List <string> ListTenantsAt(string aptNr) { using var db = new Database.DatabaseFilePath(); var tName = db.DoorLocations .FirstOrDefault(e => e.Name == aptNr); return(db.Tenants .Where(e => e.Location == tName) .Select(e => e.FirstName) .ToList()); }
/// <summary> /// Find entries made by a tenant /// </summary> /// <param name="name">Name of tenant</param> /// <returns></returns> public static List <Models.EventLog> FindEntriesByTenant(string name) { using var db = new Database.DatabaseFilePath(); var tenant = db.Tenants .FirstOrDefault(e => e.FirstName == name); return(db.EventLogs .Include("DoorName") .Include("Tenant") .Include("DoorEvent") .Where(e => e.Tenant == tenant) .OrderByDescending(e => e.DateTime) .ToList()); }
/// <summary> /// Find entries from a location /// </summary> /// <param name="location">Where the door is located</param> /// <returns></returns> public static List <Models.EventLog> FindEntriesByLocation(string location) { using var db = new Database.DatabaseFilePath(); var door = db.DoorNames .FirstOrDefault(e => e.Name == location); return(db.EventLogs .Include("DoorName") .Include("Tenant") .Include("DoorEvent") .Where(e => e.DoorName == door) .Take(MaxEntries) .OrderByDescending(e => e.DateTime) .ToList()); }
/// <summary> /// Move tenant from apartment /// </summary> /// <param name="tenant">Tenant name</param> /// <param name="location">Where tenant are listed</param> /// <returns></returns> public static bool MoveTenant(string tenant, string location = null) { try { using var db = new Database.DatabaseFilePath(); var tenantInfo = db.Tenants.Include("DoorLocation") .FirstOrDefault(e => e.TenantTag == tenant || e.FirstName + " " + e.LastName == tenant); if (String.IsNullOrEmpty(location)) { tenantInfo.Location = null; tenantInfo.TenantTag = null; db.SaveChanges(); } else { const string letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZÅÄÖ"; var letterArray = letter .ToCharArray(); var i = 0; var thisBool = false; while ((i < letterArray.Length) && (!thisBool)) { var checkInfo = location + letterArray[i]; var checkAnswer = db.Tenants .Any(e => e.TenantTag == checkInfo); if (!checkAnswer) { var aptNr = db.DoorLocations .FirstOrDefault(e => e.Name == location); tenantInfo.TenantTag = checkInfo; tenantInfo.Location = aptNr; thisBool = true; } i++; } } return(true); } catch (Exception) { return(false); } }
/// <summary> /// Check if database contain information about tenants /// Add preset data if database is empty /// Add info if database is empty /// </summary> public static void CheckDbPopulation() { using var db = new Database.DatabaseFilePath(); var presetData = db.Tenants .Take(2) .ToList(); var presetLogData = db.EventLogs .Take(2) .ToList(); if (presetData.Count == 0) { PresetData(); } if (presetLogData.Count == 0) { PresetLogData(); } }
/// <summary> /// Add a new tenant to building /// </summary> /// <param name="name">Tenant name</param> /// <param name="location">Apartment tenant is added to</param> /// <returns></returns> public static bool AddTenant(string name, string location) { try { using var db = new Database.DatabaseFilePath(); var split = name .Split(" "); const string letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZÅÄÖ"; var letterArray = letter .ToCharArray(); var i = 0; var thisBool = false; while ((i < letterArray.Length) && (!thisBool)) { var checkInfo = location + letterArray[i]; var checkAnswer = db.Tenants .Any(e => e.FirstName == checkInfo); if (!checkAnswer) { var aptNr = db.DoorLocations .SingleOrDefault(e => e.Name == location); var exist = db.Tenants .Count(e => e.TenantTag == checkInfo); db.Tenants .Add(new Models.Tenant { FirstName = split[0], LastName = split[1], TenantTag = checkInfo, Location = aptNr } ); db.SaveChanges(); thisBool = true; } i++; } return(true); } catch (Exception) { return(false); } }
/// <summary> /// Adds tables with standard data about the building /// Fills tables with standard data /// </summary> public static void PresetData() { using var db = new Database.DatabaseFilePath(); var doorEvent = new[] { "DÖIN", "DÖUT", "FDIN", "FDUT" }; var doorLocation = new[] { "0101", "0102", "0103", "0201", "0202", "0301", "0302", "VAKT", "SOPRUM", "TVÄTT" }; var doorName = new[] { "LGH0101", "LGH0102", "LGH0103", "LGH0201", "LGH0202", "LGH0301", "LGH0302", "VAKT", "SOPRUM", "TVÄTT", "UT" }; foreach (var Event in doorEvent) { db.DoorEvents .Add(new Models.DoorEvent { Code = Event }); } db.SaveChanges(); foreach (var location in doorLocation) { db.DoorLocations .Add(new Models.DoorLocation { Name = location }); } db.SaveChanges(); foreach (var name in doorName) { db.DoorNames .Add(new Models.DoorName { Name = name }); } db.SaveChanges(); var tenants = new[, ] { { "Liam", "Jönsson", "0101A", "1" }, { "Elias", "Petterson", "0102A", "2" }, { "Wilma", "Johansson", "0102B", "2" }, { "Alicia", "Sanchez", "0103A", "3" }, { "Aaron", "Sanchez", "0103B", "3" }, { "Olivia", "Erlander", "0201A", "4" }, { "William", "Erlander", "0201B", "4" }, { "Alexander", "Erlander", "0201C", "4" }, { "Astrid", "Erlander", "0201D", "4" }, { "Lucas", "Adolfsson", "0202A", "5" }, { "Ebba", "Adolfsson", "0202B", "5" }, { "Lilly", "Adolfsson", "0202C", "5" }, { "Ella", "Ahlström", "0301A", "6" }, { "Alma", "Alfredsson", "0301B", "6" }, { "Elsa", "Ahlström", "0301C", "6" }, { "Maja", "Ahlström", "0301D", "6" }, { "Noah", "Almgren", "0302A", "7" }, { "Adam", "Andersen", "0302B", "7" }, { "Kattis", "Backman", "0302C", "7" }, { "Oscar", "Chen", "0302D", "7" }, { "Vaktmästare", "", "VAKT01", "8" } }; for (int i = 0; i < tenants.GetLongLength(0); i++) { db.Tenants .Add( new Models.Tenant { FirstName = tenants[i, 0], LastName = tenants[i, 1], TenantTag = tenants[i, 2], Location = db.DoorLocations .FirstOrDefault(l => l.ID == int.Parse(tenants[i, 3])) }); } db.SaveChanges(); }