public HttpResponseMessage CreateAppointment(AppointmentModel model, [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string accessToken) { return this.ExecuteOperationAndHandleExceptions(() => { var context = new TasksManagerDbContext(); var meUser = this.GetUserByAccessToken(accessToken, context); this.ValidateAppointment(model); var appointmentEntity = model.ToEntity(); appointmentEntity.Owner = meUser; appointmentEntity.State = context.States.FirstOrDefault(st => st.Value == "upcomming"); meUser.Appointments.Add(appointmentEntity); context.SaveChanges(); var responseModel = new AppointmentCreatedModel() { Id = appointmentEntity.Id, Owner = appointmentEntity.Owner.Username }; var response = this.Request.CreateResponse(HttpStatusCode.Created, responseModel); return response; }); }
private void ValidateAppointment(AppointmentModel model) { if (model == null || string.IsNullOrEmpty(model.Subject) || string.IsNullOrEmpty(model.Description) || model.Duration <= 0 || string.IsNullOrEmpty(model.AppointmentDate)) { throw new ArgumentNullException("Invalid Appointment"); } var date = DateTime.Parse(model.AppointmentDate); if (date < DateTime.Now) { throw new ArgumentNullException("Invalid Appointment Date"); } }