//[Authorize] // delete default assignment, request should have argument // in querystring or api should return no data public IActionResult DisplayUserMetrics(int days = 7) { // Assume the user is not authorized IActionResult result = Unauthorized(); //testing - set userName to User.Identity.Name for deployment string userName = String.IsNullOrEmpty(User.Identity.Name) ? "pleshekc" : User.Identity.Name; try { var visits = (List <VisitItem>)_db.GetVisitsByUserDateRange(userName, DateTime.UtcNow.AddDays(days * -1), DateTime.UtcNow); MetricsViewModel vm = new MetricsViewModel(); var uvm = _db.GetUserVisitMetrics(userName, days); VisitMetricsDTO uvmDTO = new VisitMetricsDTO() { AvgDuration = uvm.AvgDuration, CountVisits = uvm.CountVisits, SumDuration = uvm.SumDuration }; vm.AvgDuration = uvmDTO.AvgDuration; // Top Five Machines should be refactored for the same // dange range as the call to this action vm.TopFiveWorkouts = _db.TopFiveMachines(userName); visits.ForEach(visit => { VisitMetricsItem vmi = new VisitMetricsItem(); vmi.Workouts = _db.WorkoutsPerVisit(visit); vmi.Date = visit.CheckIn.Date; if (vmi.Workouts.Count > 0) { vmi.SumOfDuration = vmi.Workouts .Select(x => x.Duration).Sum(); } vm.WeeklyWorkouts.Add(vmi); }); result = Ok(vm); } catch (Exception) { result = BadRequest(new { Message = "Get workout failed." }); } return(result); }
public IActionResult Get(string userName, DateTime dtStart, DateTime dtEnd) { // Assume the user is not authorized IActionResult result = Unauthorized(); try { var visits = _db.GetVisitsByUserDateRange(userName, dtStart, dtEnd); var dtoArr = visits.Select(x => MapToDTO(x)).ToArray(); result = Ok(dtoArr); } catch (Exception) { result = BadRequest(new { Message = "Get visits failed." }); } return(result); }