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 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 }