Exemple #1
0
 public Comment ReadByID(int Id)
 {
     using (var ctx = new OSGContext())
     {
         return(ctx.Comment.Include("News").FirstOrDefault(comment => comment.Id == Id));
     }
 }
Exemple #2
0
 public News ReadByID(int Id)
 {
     using (var ctx = new OSGContext())
     {
         return(ctx.News.Include("Comments").FirstOrDefault(news => news.Id == Id));
     }
 }
Exemple #3
0
 // Returns a IEnumerable with Trainers including their Events.
 public IEnumerable <Trainer> ReadAll()
 {
     using (var ctx = new OSGContext())
     {
         return(ctx.Trainer.Include("Events").ToList());
     }
 }
Exemple #4
0
 // Returns a specific Trainer with a given ID, and include his Events for use if needed.
 public Trainer ReadByID(int Id)
 {
     using (var ctx = new OSGContext())
     {
         return(ctx.Trainer.Include("Events").FirstOrDefault(trainer => trainer.Id == Id));
     }
 }
Exemple #5
0
 public IEnumerable <Event> ReadAll()
 {
     using (var ctx = new OSGContext())
     {
         return(ctx.Event.Include("Trainers").ToList());
     }
 }
Exemple #6
0
 public Event ReadByID(int Id)
 {
     using (var ctx = new OSGContext())
     {
         return(ctx.Event.Include("Trainers").FirstOrDefault(_event => _event.Id == Id));
     }
 }
Exemple #7
0
        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);
        }
Exemple #8
0
 public IEnumerable <News> ReadAll()
 {
     using (var ctx = new OSGContext())
     {
         return(ctx.News.Include("Comments").ToList());
     }
 }
Exemple #9
0
 public IEnumerable <Event> ReadByMonth(DateTime month)
 {
     using (var ctx = new OSGContext())
     {
         return(ctx.Event.Include("Trainers").Where(x => x.Date.Month == month.Month && x.Date.Year == month.Year).ToList());
     }
 }
Exemple #10
0
 public News Create(News model)
 {
     using (var ctx = new OSGContext())
     {
         ctx.News.Attach(model);
         var newsToReturn = ctx.News.Add(model);
         ctx.SaveChanges();
         return(newsToReturn);
     }
 }
Exemple #11
0
 public Comment Create(Comment model)
 {
     using (var ctx = new OSGContext())
     {
         ctx.Comment.Attach(model);
         var commentToReturn = ctx.Comment.Add(model);
         ctx.SaveChanges();
         return(commentToReturn);
     }
 }
Exemple #12
0
 public Event Create(Event model)
 {
     using (var ctx = new OSGContext())
     {
         ctx.Event.Attach(model);
         var eventToReturn = ctx.Event.Add(model);
         ctx.SaveChanges();
         return(eventToReturn);
     }
 }
Exemple #13
0
 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);
     }
 }
Exemple #14
0
 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);
     }
 }
Exemple #15
0
 // 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);
     }
 }
Exemple #16
0
 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);
     }
 }
Exemple #17
0
 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);
     }
 }
Exemple #18
0
 //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);
     }
 }
Exemple #19
0
 // 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);
     }
 }