public Task <SwaggerResponse <YearResponse> > GetYearAsync(double?year) { var headers = new Dictionary <string, IEnumerable <string> >(); try { var userId = _httpContextAccessor.HttpContext.User.FindFirst(cl => cl.Type.Equals("id")).Value; var response = _unitOfWork.YearPunches.GetYear(userId, year); return(Task.FromResult(new SwaggerResponse <YearResponse>(StatusCodes.Status200OK, headers, response))); } catch (Exception exception) { var response = new YearResponse { Status = new OpResult { Success = false, Result = $"Failed to get year {year} punches" } }; return(HandleException <YearResponse>(exception, headers, response)); } }
public YearResponse GetYear(string userId, 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 selectYear = year.HasValue ? Convert.ToInt32(year) : dt.Year; var groupedPunches = Context.Punches .Where(p => p.User.Id == user.Id) .Where(p => p.YearPunch.Year == selectYear) .OrderBy(p => p.MonthPunch.Month) .GroupBy(p => p.MonthPunch.Month) .Select(p => new { Month = p.Key, Groups = p.OrderBy(q => q.DayPunch.Day).GroupBy(q => q.DayPunch.Day) }); var response = new YearResponse { Status = new OpResult { Success = true } }; var yearchPunchVm = new YearPunchesDto(); response.Punches = yearchPunchVm; yearchPunchVm.User = user.Id; yearchPunchVm.Year = selectYear; yearchPunchVm.Punches = new List <MonthPunchesDto>(); foreach (var groupPunch in groupedPunches) { var monthPunchDto = new MonthPunchesDto(); yearchPunchVm.Punches.Add(monthPunchDto); monthPunchDto.User = user.Id; monthPunchDto.Month = groupPunch.Month; monthPunchDto.Year = dt.Year; monthPunchDto.Punches = new List <DayPunchesDto>(); foreach (var dayPunches in groupPunch.Groups) { var dayPunch = new DayPunchesDto(); dayPunch.GetRowedDayPunches(dayPunches.OrderBy(dp => dp.TimeDec).ToArray()); dayPunch.Day = dayPunches.Key; dayPunch.Month = dt.Month; dayPunch.Year = dt.Year; monthPunchDto.Punches.Add(dayPunch); } } return(response); } catch (RepositoryException) { throw; } catch (Exception exception) { throw new RepositoryException(StatusCodes.Status400BadRequest, $"GetCurret year punches threw an exception: {exception.Message}", exception); } }
public IHttpActionResult GetAvailableYears() { return(Ok(new AvailableYearsResponse { AvailableYears = this.Db.years.ToList().Select(x => YearResponse.From(x)).ToList() })); }