using System.Linq; using LinqToDB; using LinqToDB.Data; using LinqToDB.DataProvider; using SeniorAssistant.Models; using SeniorAssistant.Models.Data; using SeniorAssistant.Models.Users; namespace SeniorAssistant.Data { public class SeniorDataContext : DataConnection { public SeniorDataContext(IDataProvider dataProvider, string connectionString) : base(dataProvider, connectionString) { } public ITable Users => GetTable(); public ITable Heartbeats => GetTable(); public ITable Sleeps => GetTable(); public ITable Steps => GetTable(); public ITable Doctors => GetTable(); public ITable Patients => GetTable(); public ITable Notifications => GetTable(); public ITable Messages => GetTable(); public ITable Forgot => GetTable(); public ITable MenuPatients => GetTable(); public T[] GetLastMessages(ITable table, string receiver, ref int numNotSeen, int max = 10) where T : IHasMessage { var notSeen = (from t in table where t.Receiver.Equals(receiver) && t.Seen == default orderby t.Time descending select t).Take(max).ToArray(); var messages = new T[max]; numNotSeen = notSeen.Length; int i; for (i = 0; i < numNotSeen; i++) { messages[i] = notSeen[i]; } if (numNotSeen < max) { var messSeen = (from t in table where t.Receiver.Equals(receiver) && t.Seen != default orderby t.Time descending select t).Take(max - numNotSeen).ToArray(); foreach (var m in messSeen) { messages[i] = m; i++; } } return messages; } } }