public IHttpActionResult Post([FromBody] MeteredSpacePOSTCollection postedMeteredSpaces)
        {
            if (postedMeteredSpaces == null)
            {
                Logger.Warning("POST to {0} with null model", RequestContext.RouteData.Route.RouteTemplate);
                return(BadRequest("Incoming data parsed to null entity model."));
            }

            if (postedMeteredSpaces.Count == 0)
            {
                Logger.Warning("POST to {0} with empty model", RequestContext.RouteData.Route.RouteTemplate);
                return(BadRequest("Incoming data parsed to empty entity model."));
            }

            MeteredSpace lastEntity    = null;
            Exception    lastException = null;

            for (int index = 0; index < postedMeteredSpaces.Count; index++)
            {
                try
                {
                    lastEntity = _meteredSpacesService.AddOrUpdate(postedMeteredSpaces[index]);
                }
                catch (Exception ex)
                {
                    lastException = ex;
                    Logger.Error(
                        ex,
                        String.Format(
                            "Server error on POST to {0} with model:{1}{2}",
                            RequestContext.RouteData.Route.RouteTemplate,
                            Environment.NewLine,
                            postedMeteredSpaces[index].ToXmlString()
                            )
                        );
                }
            }

            //temporary because WebApi routes are registered with Route.Name = null, hence cannot be looked up by name
            //we should return CreatedAtRoute (201 with a location header)
            //instead we just return 200 with the entity

            //return CreatedAtRoute(
            //    "MeteredSpaces",
            //    new { id = lastEntity.MeterId },
            //    _meteredSpacesService.ConvertToViewModel(lastEntity)
            //);

            if (lastException == null)
            {
                return(Ok(_meteredSpacesService.ConvertToViewModel(lastEntity)));
            }
            else
            {
                return(InternalServerError(lastException));
            }
        }
 public MeteredSpaceGET ConvertToViewModel(MeteredSpace entity)
 {
     return(new MeteredSpaceGET()
     {
         MeterId = entity.MeterId,
         Area = entity.Area,
         StreetAddress = entity.SubArea,
         Latitude = entity.Latitude,
         Longitude = entity.Longitude,
         Active = entity.Active
     });
 }
        public MeteredSpace AddOrUpdate(MeteredSpacePOST viewModel)
        {
            var posted = new MeteredSpace()
            {
                MeterId   = viewModel.PoleSerialNumber,
                Area      = viewModel.Area,
                SubArea   = viewModel.SubArea,
                Zone      = viewModel.Zone,
                Latitude  = viewModel.Lat,
                Longitude = viewModel.Long,
                Active    = viewModel.Status.HasValue ? !viewModel.Status.Equals(0) : default(bool?)
            };

            var existing = Get(posted.MeterId);

            if (existing == null)
            {
                _meteredSpacesRepo.Create(posted);
            }
            else
            {
                posted.Id        = existing.Id;
                posted.Area      = posted.Area ?? existing.Area;
                posted.SubArea   = posted.SubArea ?? existing.SubArea;
                posted.Zone      = posted.Zone ?? existing.Zone;
                posted.Latitude  = posted.Latitude ?? existing.Latitude;
                posted.Longitude = posted.Longitude ?? existing.Longitude;
                posted.Active    = posted.Active ?? existing.Active;

                _meteredSpacesRepo.Update(posted);
            }

            //invalidate the cache now that there is new data
            _cacheService.Remove(cacheKey);

            return(posted);
        }