public WeekResponse GetWeek(string userId, double?week, 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 selectWeek = week.HasValue ? Convert.ToInt32(week) : _timeService.GetWeekNumber(dt); var selectYear = year.HasValue ? Convert.ToInt32(year) : dt.Year; var punches = Context.Punches .Where(p => p.User.Id == user.Id) .Where(p => p.WeekPunch.Week == selectWeek) .Where(p => p.YearPunch.Year == selectYear) .GroupBy(p => p.DayPunch.Day) .ToList(); var response = new WeekResponse { Status = new OpResult { Success = true }, Punches = new WeekPunchesDto { User = user.Id, Week = selectWeek, Year = selectYear, DayPunches = new List <DayPunchesDto>() } }; foreach (var dayPunches in punches) { var dayPunch = new DayPunchesDto(); dayPunch.GetRowedDayPunches(dayPunches.OrderBy(dp => dp.TimeDec).ToArray()); dayPunch.Day = dayPunches.Key; var p0 = dayPunch.Punches.FirstOrDefault(); dayPunch.Month = p0 != null ? p0.Enter != null ? p0.Enter.Time.Value.Month : p0.Leave != null ? p0.Leave.Time.Value.Month : dt.Month : dt.Month; dayPunch.Year = selectYear; response.Punches.DayPunches.Add(dayPunch); } return(response); } catch (RepositoryException) { throw; } catch (System.Exception exception) { throw new RepositoryException(StatusCodes.Status400BadRequest, $"GetCurret week punches threw an exception: {exception.Message}", exception); } }
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 DayResponse GetDay(string userId, double?day, 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 selectDay = day.HasValue ? Convert.ToInt32(day) : dt.Day; var selectMonth = month.HasValue ? Convert.ToInt32(month) : dt.Month; var selectYear = year.HasValue ? Convert.ToInt32(year) : dt.Year; var punches = Context.Punches .Where(p => p.User.Id == user.Id) .Where(p => p.DayPunch.Day == selectDay) .Where(p => p.MonthPunch.Month == selectMonth) .Where(p => p.YearPunch.Year == selectYear) .ToList(); var response = new DayResponse { Status = new OpResult { Success = true }, }; var dayPunch = new DayPunchesDto(); dayPunch.GetRowedDayPunches(punches.OrderBy(dp => dp.TimeDec).ToArray()); dayPunch.Day = selectDay; dayPunch.Month = selectMonth; dayPunch.Year = selectYear; response.Punches = dayPunch; return(response); } catch (RepositoryException) { throw; } catch (Exception exception) { throw new RepositoryException(StatusCodes.Status400BadRequest, $"GetCurret day punches threw an exception: {exception.Message}", exception); } }
public static DayPunchesDto GetRowedDayPunches(this DayPunchesDto dayPunchesDto, Punch[] punchArray) { if (dayPunchesDto == null) { throw new System.ArgumentNullException(nameof(dayPunchesDto)); } var dayTotal = 0.0; dayPunchesDto.Punches = new List <PunchRowDto>(); var dayPunchesArray = punchArray.OrderBy(dp => dp.TimeDec).ToArray(); var i = 0; do { var row = new PunchRowDto(); if (punchArray.Any()) { var punch = dayPunchesArray[i]; if (punch.Direction) { row.Enter = new PunchDto(); row.Enter.SetPunch(punch); if (i < dayPunchesArray.Length - 1 && !dayPunchesArray[i + 1].Direction) { i++; punch = dayPunchesArray[i]; row.Leave = new PunchDto(); row.Leave.SetPunch(punch); } } else { row.Leave = new PunchDto(); row.Leave.SetPunch(punch); } } // calc dayTotal dayTotal += row.GetRowTotal(); dayPunchesDto.Punches.Add(row); i++; } while (i < punchArray.Count()); dayPunchesDto.Daytotal = dayTotal; return(dayPunchesDto); }
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); } }