Ejemplo n.º 1
0
        /// <summary>
        /// Inserts or updates a FireBrigade in the cache.
        /// </summary>
        /// <param name="fireBrigade">FireBrigade that should be stored in the cache.</param>
        public static void UpsertFireBrigade(FireBrigade fireBrigade)
        {
            if (fireBrigade != null)
            {
                List <FireBrigade> allFireBrigades = GetAllFireBrigades().ToList();
                FireBrigade        old             = null;

                foreach (FireBrigade fb in allFireBrigades)
                {
                    if (fb.Id == fireBrigade.Id)
                    {
                        old = fb;
                        break;
                    }
                }

                if (old != null)
                {
                    allFireBrigades.Remove(old);
                    allFireBrigades.Add(fireBrigade);
                }
                else
                {
                    allFireBrigades.Add(fireBrigade);
                }

                GlobalCachingProvider.Instance.RemoveItem(fireBrigadesString);
                GlobalCachingProvider.Instance.AddItem(fireBrigadesString, allFireBrigades);
            }
        }
Ejemplo n.º 2
0
 public bool UploadFireBrigade([FromBody] FireBrigade fb)
 {
     try {
         User user;
         Authentication.Token.CheckAccess(Request.Headers, out user);
         if (user != null)
         {
             if (user.UserType == UserTypes.admin)
             {
                 return(DatabaseOperations.FireBrigades.Upsert(fb, user));
             }
             else
             {
                 // User is not an admin.
                 throw new InvalidOperationException();
             }
         }
         else
         {
             // Notify user that the login was not successful.
             throw new NullReferenceException();
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
         return(false);
     }
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Deletes the FireBrigade from the Database and Cache.
        /// The assoziations with the Users and FireAlarmSystems are also deleted.
        /// </summary>
        /// <param name="id">The id of the FireBrigade you want to delete.</param>
        /// <returns>Returns true if FireBrigade was deleted from DB.</returns>
        public static bool Delete(int id, User user)
        {
            FireBrigade old = GetById(id);

            // Delete from database
            bool ok = DatabaseOperations.DbDeletes.DeleteFireBrigade(id);

            if (ok)
            {
                // Write log message.
                Logging.Logger.Log("delete", user.GetUserDescription(), old);

                // Delete from authorizedObjectIds of Users in the cache.
                foreach (User u in DatabaseOperations.Users.GetAll())
                {
                    if (u.UserType == UserTypes.fireFighter && u.AuthorizedObjectIds.Contains(id))
                    {
                        u.AuthorizedObjectIds.Remove(id);
                        DatabaseOperations.Users.Upsert(u, user);
                    }
                }

                // Delete from List of FireBrigades of FireAlarmSystems in the cache.
                foreach (FireAlarmSystem fas in DatabaseOperations.FireAlarmSystems.GetAll())
                {
                    if (fas.FireBrigades.Contains(id))
                    {
                        fas.FireBrigades.Remove(id);
                        DatabaseOperations.FireAlarmSystems.Upsert(fas, user);
                    }
                }
            }
            return(ok);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Upserts a FireBrigade into the database.
        /// </summary>
        /// <param name="fb">the FireBrigade you want to upsert.</param>
        /// <returns>Returns true if the FireBrigade was upserted.</returns>
        public static bool UpsertFireBrigade(FireBrigade fb)
        {
            try
            {
                // Insert into local database.
                //LocalDatabase.UpsertFireBrigade(fb);

                // Insert into remote database.
                return(DatabaseOperations.LiteDB.LiteDbUpserts.UpsertFireBrigade(fb));
            }
            catch (Exception)
            {
                return(false);
            }
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Inserts a FireBrigade into the database or updates it if it already exists.
 /// </summary>
 /// <param name="fb">The FireBrigade you want to upsert.</param>
 /// <returns>Returns true if the insert was successful.</returns>
 public static bool Upsert(FireBrigade fb, User user)
 {
     if (fb != null && fb.Id != 0)
     {
         bool ok = DatabaseOperations.DbUpserts.UpsertFireBrigade(fb);
         if (ok)
         {
             Logging.Logger.Log("upsert", user.GetUserDescription(), fb);
         }
         return(ok);
     }
     else
     {
         return(false);
     }
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Upserts a FireBrigade into the LiteDB.
 /// </summary>
 /// <param name="fb">the FireBrigade you want to upsert.</param>
 /// <returns>Returns true if the FireBrigade was upserted.</returns>
 public static bool UpsertFireBrigade(FireBrigade fb)
 {
     if (fb != null)
     {
         using (var db = AppData.FireBrigadeDB())
         {
             var table = db.FireBrigadeTable();
             table.Upsert(fb);
             return(true);
         }
     }
     else
     {
         return(false);
     }
 }
Ejemplo n.º 7
0
        /// <summary>
        /// Deletes a FireBrigade from the cache and from the lists of the FireAlarmSystems.
        /// </summary>
        /// <param name="id">Id of the FireBrigade you want to delete.</param>
        public static void DeleteFireBrigade(int id)
        {
            List <FireBrigade> allFireBrigades = GetAllFireBrigades().ToList();
            FireBrigade        old             = null;

            foreach (FireBrigade fb in allFireBrigades)
            {
                if (fb.Id == id)
                {
                    old = fb;
                    break;
                }
            }

            if (old != null)
            {
                allFireBrigades.Remove(old);
                GlobalCachingProvider.Instance.RemoveItem(fireBrigadesString);
                if (allFireBrigades != null)
                {
                    GlobalCachingProvider.Instance.AddItem(fireBrigadesString, allFireBrigades);
                }
            }
        }