public IHttpActionResult Get(int id) { using (var dbContext = new TestDbContext()) { DateTime dateCurrent = DateTime.Now.Date; DateTime firstDayOfWeek = FirstDayOfWeek(dateCurrent); DateTime lastDayOfWeek = LastDayOfWeek(dateCurrent); var listWeeklyAvailability = dbContext.Weeklies .Where(o => o.UserId == id && o.FirstDayOfWeek == firstDayOfWeek && o.LastDayOfWeek == lastDayOfWeek) .Select(s => new WeeklyViewModel { Id = s.Id, Sun = s.Sun, SunSelected = s.SunSelected, Mon = s.Mon, MonSelected = s.MonSelected, Tue = s.Tue, TueSelected = s.TueSelected, Wed = s.Wed, WedSelected = s.WedSelected, Thu = s.Thu, ThuSelected = s.ThuSelected, Fri = s.Fri, FriSelected = s.FriSelected, Sat = s.Sat, SatSelected = s.SatSelected, FirstDayOfWeek = s.FirstDayOfWeek.ToString(), LastDayOfWeek = s.LastDayOfWeek.ToString(), UserId = s.UserId, }).ToList(); AvailabilityViewModel weeklyViewModel; if (listWeeklyAvailability.Count > 0) { weeklyViewModel = new AvailabilityViewModel { Id = 0, UserId = listWeeklyAvailability[0].UserId, ListWeeklyViewModels = listWeeklyAvailability }; } else { weeklyViewModel = new AvailabilityViewModel { Id = 0, UserId = 0, ListWeeklyViewModels = GenerationWeeklyAvailabilitie(id) }; } var test = weeklyViewModel; return Ok(weeklyViewModel); } }
public IHttpActionResult Post(AvailabilityViewModel item) { using (var dbContext = new TestDbContext()) { Mapper.CreateMap<WeeklyViewModel, Weekly>() .ForMember(d => d.FirstDayOfWeek, o => o.Ignore()) .ForMember(d => d.LastDayOfWeek, o => o.Ignore()) .AfterMap((s, d) => { d.FirstDayOfWeek = DateTime.Parse(s.FirstDayOfWeek); d.LastDayOfWeek = DateTime.Parse(s.LastDayOfWeek); }); foreach (var data in item.ListWeeklyViewModels) { var weekly = Mapper.Map<WeeklyViewModel,Weekly>(data); weekly.UserId = item.UserId; dbContext.Weeklies.Add(weekly); } dbContext.SaveChanges(); return Ok(); } }
public int Put(int id, AvailabilityViewModel item) { using (var dbContext = new TestDbContext()) { DateTime dateCurrent = DateTime.Now.Date; DateTime firstDayOfWeek = FirstDayOfWeek(dateCurrent); DateTime lastDayOfWeek = LastDayOfWeek(dateCurrent); dbContext.Weeklies.RemoveRange( dbContext.Weeklies.Where( o => o.UserId == id && o.FirstDayOfWeek == firstDayOfWeek && o.LastDayOfWeek == lastDayOfWeek)); #region //var listOldWeekly = dbContext.Weeklies // .Where(o => o.UserId == id && o.FirstDayOfWeek == firstDayOfWeek && o.LastDayOfWeek == lastDayOfWeek) // .Select(s => new WeeklyViewModel // { // Id = s.Id, // Sun = s.Sun, // SunSelected = s.SunSelected, // Mon = s.Mon, // MonSelected = s.MonSelected, // Tue = s.Tue, // TueSelected = s.TueSelected, // Wed = s.Wed, // WedSelected = s.WedSelected, // Thu = s.Thu, // ThuSelected = s.ThuSelected, // Fri = s.Fri, // FriSelected = s.FriSelected, // Sat = s.Sat, // SatSelected = s.SatSelected, // UserId = s.UserId, // }).ToList(); //if (listOldWeekly.Count > 0) //{ // foreach (var data in listOldWeekly) // { // var weekly = new Weekly(); // weekly.Sun = data.Sun; // weekly.SunSelected = data.SunSelected; // weekly.Mon = data.Mon; // weekly.MonSelected = data.MonSelected; // weekly.Tue = data.Tue; // weekly.TueSelected = data.TueSelected; // weekly.Wed = data.Wed; // weekly.WedSelected = data.WedSelected; // weekly.Thu = data.Thu; // weekly.ThuSelected = data.ThuSelected; // weekly.Fri = data.Fri; // weekly.FriSelected = data.FriSelected; // weekly.Sat = data.Sat; // weekly.SatSelected = data.SatSelected; // weekly.FirstDayOfWeek = DateTime.Parse(data.FirstDayOfWeek); // weekly.LastDayOfWeek = DateTime.Parse(data.LastDayOfWeek); // weekly.UserId = data.UserId; // dbContext.Weeklies.Add(weekly); // } //} #endregion Mapper.CreateMap<WeeklyViewModel, Weekly>() .ForMember(d => d.FirstDayOfWeek, o => o.Ignore()) .ForMember(d => d.LastDayOfWeek, o => o.Ignore()) .AfterMap((s, d) => { d.FirstDayOfWeek = DateTime.Parse(s.FirstDayOfWeek); d.LastDayOfWeek = DateTime.Parse(s.LastDayOfWeek); }); foreach (var data in item.ListWeeklyViewModels) { var weekly = Mapper.Map<WeeklyViewModel, Weekly>(data); weekly.UserId = item.UserId; dbContext.Weeklies.Add(weekly); } dbContext.SaveChanges(); return id; } }