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 Task <SwaggerResponse <WeekResponse> > GetWeekAsync(double?week, 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.WeekPunches.GetWeek(userId, week, year); return(Task.FromResult(new SwaggerResponse <WeekResponse>(StatusCodes.Status200OK, headers, response))); } catch (Exception exception) { var response = new WeekResponse { Status = new OpResult { Success = false, Result = $"Failed to get week {week} punches" } }; return(HandleException <WeekResponse>(exception, headers, response)); } }