Exemplo n.º 1
0
        // POST api/Default1
        public HttpResponseMessage Add([FromBody] Position position, [FromUri]  string token)
        {
            if (string.IsNullOrWhiteSpace(token))
                throw new AuthenticationException();

            if (ModelState.IsValid)
            {
                LocationTrack locationTrack = CreateLocationTrack(position, token);
                DateTime trackDate = locationTrack.TimeStamp.Date;
                var locationTracks = _db.LocationTracks.Where(l => l.TimeStamp > trackDate && l.UserProfile.UserId == locationTrack.UserProfile.UserId).ToList();
                StatisticsItem statisticsItem = _db.Statistics.FirstOrDefault(s => s.Day == trackDate &&
                    s.UserProfile.UserId == locationTrack.UserProfile.UserId && s.Consumer == "Car");
                if (statisticsItem == null)
                {
                    statisticsItem = new StatisticsItem();
                    _db.Statistics.Add(statisticsItem);
                }
                statisticsItem.CalculateStatistics(locationTracks, locationTrack);
                _db.LocationTracks.Add(locationTrack);

                _db.SaveChanges();

                var response = Request.CreateResponse(HttpStatusCode.Created, position);
                //response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = positio }));
                return response;
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }
Exemplo n.º 2
0
        private void UpdateStatistics(UserProfile user, int pagesPrinted)
        {
            var today = DateTime.UtcNow.Date;
            var item = _db.Statistics.FirstOrDefault(x => x.UserProfile.UserId == user.UserId && x.Day == today && x.Consumer == Consumers.Printing);
            if (item == null)
            {
                item = new StatisticsItem
                           {
                               Id = Guid.NewGuid(),
                               Consumer = Consumers.Printing,
                               Day = today,
                               UserProfile = user,
                               Value = PrintingCalculator.Calculate(pagesPrinted)
                           };
                _db.Statistics.Add(item);
                return;
            }

            item.Value += PrintingCalculator.Calculate(pagesPrinted);
            return;
        }