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 SensorEvent AddOrUpdate(SensorEventPOST viewModel)
        {
            //try to get an existing meter, by looking in a local cache first
            var meteredSpace = _meteredSpacesService.Get(viewModel.MeteredSpace.MeterID);

            if (meteredSpace == null)
            {
                //record this meter in the db
                meteredSpace = _meteredSpacesService.AddOrUpdate(viewModel.MeteredSpace);
            }

            var posted = new SensorEvent()
            {
                ClientId         = viewModel.ClientID,
                EventTime        = DateTime.Parse(viewModel.EventTime),
                EventType        = viewModel.EventType,
                MeteredSpace     = meteredSpace,
                ReceivedTime     = _clock.UtcNow,
                SessionId        = long.Parse(viewModel.MeteredSpace.SessionID),
                TransmissionId   = long.Parse(viewModel.TransmissionID),
                TransmissionTime = DateTime.Parse(viewModel.TransmissionDateTime)
            };

            var existing = _sensorEventsRepo.Get(x => x.TransmissionId == posted.TransmissionId);

            if (existing == null)
            {
                _sensorEventsRepo.Create(posted);
            }
            else
            {
                posted.Id = existing.Id;
                _sensorEventsRepo.Update(posted);
            }

            return(posted);
        }