public async Task UpdateDayAsync(TimesheetDay day) { var currentDay = await context.TimesheetDays.Include(td => td.Works).FirstOrDefaultAsync(td => td.Id == day.Id); if (currentDay != null) { currentDay.Comments = day.Comments; if (currentDay.Works.Count > 0) { var worksToRemove = currentDay.Works.Where(w => !day.Works.Select(c => c.Id).Contains(w.Id)) .Select(w => w.Id); var worksToAdd = day.Works.Where(w => !currentDay.Works.Select(c => c.Id).Contains(w.Id)) .Select(w => w.Id); await context.TimesheetWorks.Where(tw => worksToRemove.Contains(tw.Id)).ForEachAsync(work => { currentDay.Works.Remove(work); }); await context.TimesheetWorks.Where(tw => worksToAdd.Contains(tw.Id)).ForEachAsync(work => { currentDay.Works.Add(work); }); } else { foreach (var work in day.Works) { currentDay.Works.Add(work); } } await context.SaveChangesAsync(); } }
public async Task <Timesheet> CreateAsync(DateTime date) { var timesheet = new Timesheet(); timesheet.Date = date; timesheet.Cost = 0; timesheet.Days = new List <TimesheetDay>(); int daysInMonth = DateTime.DaysInMonth(date.Year, date.Month); for (int i = 1; i <= daysInMonth; i++) { var day = new TimesheetDay { Works = new List <TimesheetWork>(), Date = new DateTime(timesheet.Date.Year, timesheet.Date.Month, i) }; timesheet.Days.Add(day); } await context.Timesheets.AddAsync(timesheet); await context.SaveChangesAsync(); return(timesheet); }
public IActionResult Add(AddTimesheetDayViewModel addTimesheetDayViewModel) { TimesheetDay newTimesheetDay = new TimesheetDay { Name = addTimesheetDayViewModel.Name }; context.TimesheetDays.Add(newTimesheetDay); context.SaveChanges(); return(Redirect("/Timesheetday/Index")); }
public IActionResult Add(AddTimesheetViewModel addTimesheetViewModel) { TimesheetDay newTimesheetDay = context.TimesheetDays.Single(c => c.ID == addTimesheetViewModel.DayOfTheWeekID); //context.TimesheetDays. Timesheet newTimesheet = new Timesheet { Name = addTimesheetViewModel.Name, DayOfTheWeek = newTimesheetDay }; context.Timesheets.Add(newTimesheet); context.SaveChanges(); return(Redirect("/Timesheet")); }
public static TimesheetDayViewModel ToViewModel(TimesheetDay timesheetDay) { var viewModel = new TimesheetDayViewModel() { CreatedDate = timesheetDay.CreatedDate, ID = timesheetDay.ID, Date = timesheetDay.Date, Notes = timesheetDay.Notes, TimesheetWeekID = timesheetDay.TimesheetWeekID, Periods = new List <TimesheetPeriodViewModel>() }; foreach (TimesheetPeriod period in timesheetDay.Periods) { viewModel.Periods.Add(TimesheetPeriodViewModel.ToViewModel(period)); } return(viewModel); }
public async Task UpdateDayAsync(TimesheetDay day) { await _timesheetDataProvider.UpdateDayAsync(day); }
public async Task <IActionResult> UpdateDay(TimesheetDay day) { await _timesheetService.UpdateDayAsync(day); return(Ok(true)); }