public MonthResponse GetMonth(string userId, double?month, double?year) { try { var user = _appUserRepository.FindById(userId); if (user == null) { throw new RepositoryException(StatusCodes.Status404NotFound, $"User {userId} not found"); } var dt = DateTime.Now; var selectMonth = month.HasValue ? Convert.ToInt32(month) : dt.Month; var selectedYear = year.HasValue ? Convert.ToInt32(year) : dt.Year; var groupedPunches = Context.Punches .Where(p => p.User.Id == user.Id) .Where(p => p.MonthPunch.Month == selectMonth) .Where(p => p.YearPunch.Year == selectedYear) .GroupBy(p => p.DayPunch.Day) .ToList(); var response = new MonthResponse { Status = new OpResult { Success = true }, Punches = new MonthPunchesDto { User = user.Id, Month = selectMonth, Year = selectedYear, Punches = new List <DayPunchesDto>() } }; foreach (var dayPunches in groupedPunches) { var dayPunch = new DayPunchesDto(); dayPunch.GetRowedDayPunches(dayPunches.OrderBy(dp => dp.TimeDec).ToArray()); dayPunch.Day = dayPunches.Key; dayPunch.Month = selectMonth; dayPunch.Year = selectedYear; response.Punches.Punches.Add(dayPunch); } return(response); } catch (RepositoryException) { throw; } catch (Exception exception) { throw new RepositoryException(StatusCodes.Status400BadRequest, $"GetCurret month punches threw an exception: {exception.Message}", exception); } }
public Task <SwaggerResponse <MonthResponse> > PuGetMonthAsync(string userId, double?month, double?year) { var headers = new Dictionary <string, IEnumerable <string> >(); try { var response = _punchService.GetMonth(userId, month, year); return(Task.FromResult(new SwaggerResponse <MonthResponse>(StatusCodes.Status200OK, headers, response))); } catch (Exception exception) { var response = new MonthResponse { Status = new OpResult { Success = false, Result = $"Failed to get month {month} punches" } }; return(HandleException <MonthResponse>(exception, headers, response)); } }
public Task <SwaggerResponse <MonthResponse> > GetMonthAsync(double?month, double?year) { var headers = new Dictionary <string, IEnumerable <string> >(); try { var userId = _httpContextAccessor.HttpContext.User.FindFirst(cl => cl.Type.Equals("id")).Value; var response = _punchService.GetMonth(userId, month, year); return(Task.FromResult(new SwaggerResponse <MonthResponse>(StatusCodes.Status200OK, headers, response))); } catch (Exception exception) { var response = new MonthResponse { Status = new OpResult { Success = false, Result = $"Failed to get month {month} punches" } }; return(HandleException <MonthResponse>(exception, headers, response)); } }