public void Validate_ValidModel() { var model = new RecurringEventModel { EventParentId = 1, EventGroupId = 1, EventTypeId = 1, Name = "foo", StartTime = new TimeSpan(12, 30, 0), EndTime = new TimeSpan(13, 50, 0), DateRangeStart = new DateTime(2018, 1, 8), DateRangeEnd = new DateTime(2018, 4, 20), DaysOfWeek = new[] { "Tuesday", "Thursday" } }; var result = target.Validate(model); Assert.IsTrue(result.IsValid); target.ShouldNotHaveValidationErrorFor(x => x.EventParentId, model); target.ShouldNotHaveValidationErrorFor(x => x.EventGroupId, model); target.ShouldNotHaveValidationErrorFor(x => x.EventTypeId, model); target.ShouldNotHaveValidationErrorFor(x => x.Name, model); target.ShouldNotHaveValidationErrorFor(x => x.StartTime, model); target.ShouldNotHaveValidationErrorFor(x => x.EndTime, model); target.ShouldNotHaveValidationErrorFor(x => x.DateRangeStart, model); target.ShouldNotHaveValidationErrorFor(x => x.DateRangeEnd, model); target.ShouldNotHaveValidationErrorFor(x => x.DaysOfWeek, model); }
public IActionResult CreateRecurring([FromBody] RecurringEventModel model) { if (ModelState.IsValid) { var createdModels = service.InsertRecurring(model); return(Ok(createdModels)); } return(BadRequest(ModelState)); }
/// <summary> /// Inserts recurring <see cref="EventModel"/> classes. /// </summary> /// <param name="model">The <see cref="RecurringEventModel"/> class that specifies the <see cref="EventModel"/> classes to insert.</param> /// <returns>An <see cref="IEnumerable{T}"/> of all <see cref="EventModel"/> classes that were inserted.</returns> public IEnumerable <EventModel> InsertRecurring(RecurringEventModel model) { try { var insertedModels = new List <EventModel>(); var baseEventModel = mapper.Map <EventModel>(model); var daysOfWeek = model.DaysOfWeek.GetDaysOfWeek(); var dateRange = GetDateRange(model.DateRangeStart, model.DateRangeEnd); foreach (var date in dateRange) { var dateDaysOfWeek = date.GetDaysOfWeek(); if (daysOfWeek.HasFlag(dateDaysOfWeek)) { var eventModel = mapper.Map <EventModel>(baseEventModel); eventModel.StartDate = date; eventModel.StartTime = model.StartTime; eventModel.EndDate = date; eventModel.EndTime = model.EndTime; var entity = (Event) new Event { UserId = ApplicationUser.Id, CreatedOn = DateTime.Now }.InjectFrom <SmartInjection>(eventModel); context.InsertEvent(entity); eventModel.InjectFrom <SmartInjection>(GetById(entity.Id)); insertedModels.Add(eventModel); } } context.SaveChanges(); return(insertedModels); } catch (Exception) { throw; } }