Exemple #1
0
        /// <summary>
        /// Remove the Geocache item with the given ID from the database.
        /// </summary>
        /// <param name="id">The id of the item to remove from the database.</param>
        /// <returns>If the item was removed true, otherwise false.</returns>
        internal bool DeleteGeocacheItem(int id)
        {
            //The return value
            bool rcode = false;
            try
            {
                //connect to the data source
                using (DataContext context = new DataContext(CONNECTION_STRING))
                {
                    //Get the item from the database with the matching ID.
                    Geocache item = context.Geocache.FirstOrDefault(x => x.GeocacheID == id);

                    //Check that there was an item returned.
                    if (item != null)
                    {
                        //Remove it from the dataset and save the dataset.
                        context.Geocache.Remove(item);
                        context.SaveChanges();
                    }

                    //Check that there is no longer any item with the given ID.
                    if (context.Geocache.FirstOrDefault(x => x.GeocacheID == id) == null)
                    {
                        rcode = true;
                    }
                }
            }
            catch (Exception ex)
            {
                //Any error logging handled here.
                //If we want to fail silently, do not re-throw the exception.
                //If we want to bubble to failure, throw the exception
                //throw ex;
                //For now, lets fail silently
            }
            return rcode;
        }
Exemple #2
0
        /// <summary>
        /// Add the Geocache item to the data base.  
        /// If successful the geocacheItem.GeocacheID will be set to something other than 0.
        /// </summary>
        /// <param name="geocacheItem">A reference to a GeocacheItem.  Will set the GeocacheID property.</param>
        /// <returns>True if successful, otherwise false.</returns>
        internal bool StoreGeocacheItem(ref GeocacheItem geocacheItem)
        {
            //return value to signify if the method was successful or not.
            bool rcode = false;
            try
            {
                //reset the GeocacheID to 0
                if (geocacheItem.GeocacheID != 0)
                {
                    geocacheItem.GeocacheID = 0;
                }

                //connect to the data source
                using (DataContext context = new DataContext(CONNECTION_STRING))
                {
                    //Create a new data.Geocache item to be added to the record set.
                    //Use the values to be stored.
                    Geocache item = new Geocache()
                    {
                        Name = geocacheItem.Name,
                        Latitude = geocacheItem.Latitude,
                        Longitude = geocacheItem.Longitude
                    };

                    //check if a Geocache item already exists with the given name.
                    if (context.Geocache.FirstOrDefault(x => x.Name.Equals(item.Name)) != null)
                    {
                        //if so, throw an error.
                        throw new GeocacheException(HttpStatusCode.NotAcceptable, "Name already exists.");
                    }

                    //add the item to the dataset and same the dataset.
                    context.Geocache.Add(item);
                    context.SaveChanges();

                    //if successful, the item will have a new unique id.
                    geocacheItem.GeocacheID = item.GeocacheID;
                    if (geocacheItem.GeocacheID > 0)
                    {
                        rcode = true;
                    }
                }
            }
            catch (GeocacheException ex)
            {
                //Pass the exception on.
                throw ex;
            }
            catch (Exception ex)
            {
                //Any error logging handled here.
                //If we want to fail silently, do not re-throw the exception.
                //If we want to bubble to failure, throw the exception
                //throw ex;
                //For now, lets fail silently
            }

            return rcode;
        }