public Event Update(Event model) { using (var ctx = new OSGContext()) { var eventToUpdate = ctx.Event.FirstOrDefault(_event => _event.Id == model.Id); if (eventToUpdate != null) { eventToUpdate.Date = model.Date; eventToUpdate.Description = model.Description; eventToUpdate.Title = model.Title; //eventToUpdate.Trainers = model.Trainers.ToList(); foreach (var trainer in model.Trainers) { var trainerFromCtx = ctx.Trainer.FirstOrDefault(ctxTrainer => ctxTrainer.Id == trainer.Id); if (trainerFromCtx != null) { eventToUpdate.Trainers.Add(trainerFromCtx); } } ctx.SaveChanges(); return(eventToUpdate); } } return(model); }
public News Create(News model) { using (var ctx = new OSGContext()) { ctx.News.Attach(model); var newsToReturn = ctx.News.Add(model); ctx.SaveChanges(); return(newsToReturn); } }
public Event Create(Event model) { using (var ctx = new OSGContext()) { ctx.Event.Attach(model); var eventToReturn = ctx.Event.Add(model); ctx.SaveChanges(); return(eventToReturn); } }
public Comment Create(Comment model) { using (var ctx = new OSGContext()) { ctx.Comment.Attach(model); var commentToReturn = ctx.Comment.Add(model); ctx.SaveChanges(); return(commentToReturn); } }
public Trainer Create(Trainer model) { using (var ctx = new OSGContext()) { // Calling attach here makes sure the model(Trainer)s events are tracked by the context // This way it will make a reference to an already existing event in the DB, instead of adding a new one, if it exists. // If the Event does not already exist, it will create it. ctx.Trainer.Attach(model); var trainerToReturn = ctx.Trainer.Add(model); ctx.SaveChanges(); return(trainerToReturn); } }
public bool Delete(Event model) { using (var ctx = new OSGContext()) { var eventToDelete = ctx.Event.FirstOrDefault(_event => _event.Id == model.Id); if (eventToDelete != null) { ctx.Event.Remove(eventToDelete); ctx.SaveChanges(); return(true); } return(false); } }
public bool Delete(News model) { using (var ctx = new OSGContext()) { var newsToDelete = ctx.News.FirstOrDefault(news => news.Id == model.Id); if (newsToDelete != null) { ctx.News.Remove(newsToDelete); ctx.SaveChanges(); return(true); } return(false); } }
// Delete a trainer with a given ID, if the ID can be found in the DB. Do nothing and return false if the Trainer does not exist. public bool Delete(Trainer model) { using (var ctx = new OSGContext()) { var trainerToDelete = ctx.Trainer.FirstOrDefault(trainer => trainer.Id == model.Id); if (trainerToDelete != null) { ctx.Trainer.Remove(trainerToDelete); ctx.SaveChanges(); return(true); } return(false); } }
public News Update(News model) { using (var ctx = new OSGContext()) { var newsToUpdate = ctx.News.FirstOrDefault(news => news.Id == model.Id); if (newsToUpdate != null) { newsToUpdate.Date = model.Date; newsToUpdate.Description = model.Description; newsToUpdate.Picture = model.Picture; newsToUpdate.Title = model.Title; ctx.SaveChanges(); return(newsToUpdate); } return(model); } }
//Update a comments name and text. (This is not used, but avaliable for use later) public Comment Update(Comment model) { using (var ctx = new OSGContext()) { var commentToUpdate = ctx.Comment.FirstOrDefault(comment => comment.Id == model.Id); if (commentToUpdate != null) { commentToUpdate.Name = model.Name; commentToUpdate.CommentText = model.CommentText; //There is no need to update the news which a comment is linked to, since you can't move //comments from one news to another.. // ctx.SaveChanges(); return(commentToUpdate); } return(model); } }
// Updates the Trainers properies, except for Events. Use the EventManagers update to update an Event with Trainers. public Trainer Update(Trainer model) { using (var ctx = new OSGContext()) { var trainerToUpdate = ctx.Trainer.FirstOrDefault(trainer => trainer.Id == model.Id); if (trainerToUpdate != null) { trainerToUpdate.Description = model.Description; trainerToUpdate.Email = model.Email; trainerToUpdate.Events = model.Events.ToList(); trainerToUpdate.FirstName = model.FirstName; trainerToUpdate.LastName = model.LastName; trainerToUpdate.PhoneNo = model.PhoneNo; trainerToUpdate.Picture = model.Picture; ctx.SaveChanges(); return(trainerToUpdate); } return(model); } }