[ResponseType(typeof(Training))] //post data for craete new tarining public IHttpActionResult PostTraining(Models.TrainingModel training) //bind training model { try { Schedule sch = new Schedule(); //object schedule entity sch.StartTime = DateTime.ParseExact(training.StartTime, "yyyy-MM-dd HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture); sch.EndTime = DateTime.ParseExact(training.EndTime, "yyyy-MM-dd HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture); var obj = db.Schedules.Add(sch); sch.RoomId = training.RoomId; db.SaveChanges(); //save data in schedule table Training tra = new Training(); //object of Training entity tra.Topic = training.Topic; tra.Description = training.Description; tra.ScheduleId = obj.ScheduleId; //getting Scheduleid from schedule table tra.CreatedAt = DateTime.Now; tra.UpdatedAt = DateTime.Now; tra.UserId = training.UserId; db.Trainings.Add(tra); //save data in training db db.SaveChanges(); } catch (Exception e) { return(BadRequest(e.Message)); } return(CreatedAtRoute("DefaultApi", new { id = training.TrainingId }, training)); }
public IHttpActionResult PutTraining(int id, Models.TrainingModel training) { if (!ModelState.IsValid) { return(NotFound()); } try { using (var ctx = new TrainingAndMeetingDBEntities()) { Training tra = new Training(); Schedule sch = new Schedule(); RoomDetail rm = new RoomDetail(); var existingtraining = ctx.Trainings.Where(s => s.TrainingId == id) .FirstOrDefault <Training>(); var existingschedule = ctx.Schedules.Where(s => s.ScheduleId == existingtraining.ScheduleId) .FirstOrDefault <Schedule>(); if (existingtraining != null) { existingschedule.StartTime = DateTime.ParseExact(training.StartTime, "yyyy-MM-dd HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture); existingschedule.EndTime = DateTime.ParseExact(training.EndTime, "yyyy-MM-dd HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture); existingschedule.RoomId = training.RoomId; var obj = db.Schedules.Add(sch); ctx.SaveChanges(); existingtraining.Topic = training.Topic; //add value in training table existingtraining.Description = training.Description; ctx.SaveChanges(); } else { return(NotFound()); } } } catch (Exception e) { return(BadRequest(e.Message)); } return(Ok(training)); }