public IEnumerable <PayPeriodViewModel> CreateViewModel(IEnumerable <PayPeriod> payPeriod, IEnumerable <PayPeriodCriteria> payPeriodCriterias, IEnumerable <OrderedPair <long, string> > userOrderedPairs) { var list = new List <PayPeriodViewModel>(); foreach (var period in payPeriod) { var createdBy = userOrderedPairs.First(x => x.FirstValue == period.CreatedBy); var modifiedBy = userOrderedPairs.FirstOrDefault(x => x.FirstValue == period.ModifiedBy); var criterias = payPeriodCriterias.Where(x => x.PayPeriodId == period.Id); var payPeriodView = new PayPeriodViewModel { PayPerioidId = period.Id, CreatedOn = period.CreatedOn, ModifiedOn = period.ModifiedOn, StartDate = period.StartDate, NumberOfWeek = period.NumberOfWeeks, IsPublished = period.IsPublished, Createdby = createdBy.SecondValue, Modifiedby = modifiedBy != null ? modifiedBy.SecondValue : string.Empty, Name = period.Name, Criteria = CreateViewModel(criterias) }; list.Add(payPeriodView); } return(list); }
public PayPeriodViewModel Update([FromBody] PayPeriodViewModel viewModel) { if (!ModelState.IsValid) { throw new HttpApiException("Invalid period model"); } var model = viewModel.ToModel(); _db.Entry(model).State = EntityState.Modified; _db.SaveChanges(); return(model.ToViewModel()); }
public static Period ToModel(this PayPeriodViewModel viewModel) { if (viewModel == null) { return(null); } return(new Period { Id = viewModel.Id.GetValueOrDefault(), StartDate = viewModel.StartDate, EndDate = viewModel.EndDate, OpenedDate = viewModel.OpenedDate, ClosedDate = viewModel.ClosedDate, Active = viewModel.IsActive, Visible = viewModel.IsVisible // IsOpened, IsUninitialized & Status are read-only calculated fields }); }
public PayPeriodViewModel Create([FromBody] PayPeriodViewModel viewModel) { if (!ModelState.IsValid) { throw new HttpApiException("Invalid period model"); } if (_db.Periods.Any(x => x.StartDate == viewModel.StartDate)) { throw new HttpApiException(string.Format("Pay period starting on {0} already exists", viewModel.StartDate.ToJsonShortDate())); } var model = viewModel.ToModel(); model.Id = Guid.NewGuid(); _db.Periods.Add(model); _db.SaveChanges(); return(model.ToViewModel()); // will grab extra fields }