Example #1
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;
        }
        public void Post()
        {
            // Arrange
            ValuesController controller = createController();

            GeocacheItem item = new GeocacheItem()
            {
                Name = DateTime.Now.ToString("yyMMddhhmmss"),
                Latitude = 123.456M,
                Longitude = 123.456M
            };

            // Act
            int geoID;
            HttpResponseMessage result = controller.Post(item);
            Assert.AreEqual(result.StatusCode, System.Net.HttpStatusCode.OK, "Returned something other than status OK(200).");
            Assert.IsTrue(result.TryGetContentValue<int>(out geoID), "No ID value returned.");
            Assert.IsTrue(geoID > 0, "The returned ID is not valid.");

            //Add the same item again.
            result = controller.Post(item);
            Assert.AreEqual(result.StatusCode, System.Net.HttpStatusCode.NotAcceptable, "Added more than 1 item with the same name.");
        }