예제 #1
0
        /// <summary>
        /// Updates the time.
        /// </summary>
        /// <param name="td">The td.</param>
        /// <returns>System.Int32.</returns>
        /// <exception cref="MyTimeDatabaseLib.TimeDataItemNotFoundException">Couldn't find the time data with that id.</exception>
        public static bool UpdateTime(ref TimeData td)
        {
            if (td.ItemId < 0)
            {
                return(AddTime(ref td));
            }
            int itemId = td.ItemId;

            using (var db = new TimeDataContext(TimeDataContext.DBConnectionString)) {
                try {
                    TimeDataItem tdi = db.TimeDataItems.Single(s => s.ItemId == itemId);

                    tdi.BibleStudies = td.BibleStudies;
                    tdi.Books        = td.Books;
                    tdi.Brochures    = td.Brochures;
                    tdi.Date         = td.Date;
                    tdi.Magazines    = td.Magazines;
                    tdi.Minutes      = td.Minutes;
                    tdi.Notes        = td.Notes;
                    tdi.ReturnVisits = td.ReturnVisits;
                    tdi.Tracts       = td.Tracts;

                    db.SubmitChanges();
                    return(true);
                } catch (InvalidOperationException) {
                    return(AddTime(ref td));
                } catch {
                    throw new TimeDataItemNotFoundException("Couldn't find the time data with that id.");
                }
            }
        }
예제 #2
0
 /// <summary>
 /// Adds the time.
 /// </summary>
 /// <param name="d">The d.</param>
 public static bool AddTime(ref TimeData d)
 {
     using (var db = new TimeDataContext(TimeDataContext.DBConnectionString)) {
         var newTd = TimeData.Copy(d);
         db.TimeDataItems.InsertOnSubmit(newTd);
         db.SubmitChanges();
         d.ItemId = newTd.ItemId;
         return(newTd.ItemId > 0);
     }
 }
예제 #3
0
 /// <summary>
 /// Gets the time data item.
 /// </summary>
 /// <param name="id">The id.</param>
 /// <returns>TimeData.</returns>
 public static TimeData GetTimeDataItem(int id)
 {
     using (var db = new TimeDataContext(TimeDataContext.DBConnectionString)) {
         try {
             TimeDataItem tdi = db.TimeDataItems.Single(s => s.ItemId == id);
             var          td  = TimeData.Copy(tdi);
             return(td);
         } catch (InvalidOperationException) {
             return(null);
         }
     }
 }
예제 #4
0
        /// <summary>
        /// Deletes the time.
        /// </summary>
        /// <param name="itemId">The item id.</param>
        public static bool DeleteTime(int itemId)
        {
            using (var db = new TimeDataContext(TimeDataContext.DBConnectionString)) {
                try {
                    TimeDataItem t = db.TimeDataItems.Single(s => s.ItemId == itemId);

                    db.TimeDataItems.DeleteOnSubmit(t);
                    db.SubmitChanges();
                    return(true);
                } catch (InvalidOperationException) { return(false); }
            }
        }
예제 #5
0
        /// <summary>
        /// Gets the entries.
        /// </summary>
        /// <param name="from">From.</param>
        /// <param name="to">To.</param>
        /// <param name="so">The so.</param>
        /// <returns>TimeData[][].</returns>
        public static TimeData[] GetEntries(DateTime @from, DateTime to, SortOrder so)
        {
            using (var db = new TimeDataContext(TimeDataContext.DBConnectionString)) {
                @from = new DateTime(@from.Year, @from.Month, @from.Day, 0, 0, 0);
                to    = new DateTime(to.Year, to.Month, to.Day, 0, 0, 0).AddDays(1);
                IOrderedQueryable <TimeDataItem> entries = from x in db.TimeDataItems
                                                           where x.Date >= @from && x.Date < to
                                                           orderby x.Date
                                                           select x;

                if (entries.Any())
                {
                    IEnumerable <TimeDataItem> e = so == SortOrder.DateNewestToOldest ? entries.ToArray().Reverse() : entries.ToArray();
                    return(e.Select(tdi => new TimeData
                    {
                        BibleStudies = tdi.BibleStudies, Books = tdi.Books, Brochures = tdi.Brochures, Date = tdi.Date, ItemId = tdi.ItemId, Magazines = tdi.Magazines, Minutes = tdi.Minutes, Notes = tdi.Notes, ReturnVisits = tdi.ReturnVisits, Tracts = tdi.Tracts ?? 0
                    }).ToArray());
                }
                return(new TimeData[0]);
            }
        }
예제 #6
0
 /// <summary>
 /// Checks the database.
 /// </summary>
 public static void CheckDatabase()
 {
     using (var db = new TimeDataContext(TimeDataContext.DBConnectionString)) {
         if (db.DatabaseExists() == false)
         {
             db.CreateDatabase();
             DatabaseSchemaUpdater dbUpdater = db.CreateDatabaseSchemaUpdater();
             dbUpdater.DatabaseSchemaVersion = APP_VERSION;
             dbUpdater.Execute();
         }
         else
         {
             var dbUpdater = db.CreateDatabaseSchemaUpdater();
             if (dbUpdater.DatabaseSchemaVersion < 2)                      //update from 1.0 to 2.0 db version
             {
                 dbUpdater.AddColumn <TimeDataItem>("Tracts");
                 dbUpdater.DatabaseSchemaVersion = APP_VERSION;
                 dbUpdater.Execute();
             }
         }
     }
 }
예제 #7
0
        public static bool IsDoubleDataEntry(DateTime date, out int id)
        {
            //
            using (var db = new TimeDataContext(TimeDataContext.DBConnectionString)) {
                try {
                    var q = from x in db.TimeDataItems
                            where x.Date.Date == date.Date
                            select x;

                    if (q.Any())
                    {
                        id = q.First().ItemId;
                        return(true);
                    }
                    id = -1;
                    return(false);
                } catch {
                    id = -1;
                    return(false);
                }
            }
        }
예제 #8
0
 public OnTimeService()
 {
     _context = new TimeDataContext();
 }