Beispiel #1
0
        public ActionResult Delete(ContenuModel item)
        {
            using (var context = new DataContext())
            {
                var contenuToDelete =
                    context
                    .Contenus
                    .SingleOrDefault
                        (p =>
                        p.ContenuId ==
                        item.ContenuId);

                if (contenuToDelete == null)
                {
                    return
                        (new HttpStatusCodeResult(500,
                                                  "Projet non existant!"));
                }


                context.Contenus.Remove(contenuToDelete);
                context.SaveChanges();
                return(new HttpStatusCodeResult(HttpStatusCode.OK));
            }
        }
Beispiel #2
0
        public ActionResult Save(ContenuModel contenu)
        {
            if (contenu == null)
            {
                return
                    (new HttpStatusCodeResult
                         (500,
                         "Impossible de créer un contenu vide"));
            }

            //Ajout d'un nouveau projet
            if (contenu.ContenuId <= 0)
            {
                using (var context = new DataContext())
                {
                    if (
                        context
                        .Contenus.Select(c => new ContenuModel
                    {
                        ContenuId = c.ContenuId,
                        Description = c.Description,
                        ProjectId = c.ProjectId,
                        ProjectTitle = c.Project.Title,
                        Title = c.Title
                    })
                        .SingleOrDefault
                            (p =>
                            p.Title.ToLower() ==
                            contenu.Title.ToLower()) != null)
                    {
                        return
                            (new HttpStatusCodeResult(500,
                                                      "Contenu existant!"));
                    }

                    context
                    .Contenus
                    .Add(new Contenu()
                    {
                        Title       = contenu.Title,
                        Description = contenu.Description
                    });
                    context.SaveChanges();
                }
                return(new HttpStatusCodeResult(HttpStatusCode.OK));
            }
            //Mise à jour d'un projet
            using (var context = new DataContext())
            {
                var contenuToUpdate =
                    context
                    .Contenus.Select(c => new ContenuModel
                {
                    ContenuId    = c.ContenuId,
                    Description  = c.Description,
                    ProjectId    = c.ProjectId,
                    ProjectTitle = c.Project.Title,
                    Title        = c.Title
                })
                    .SingleOrDefault
                        (p =>
                        p.ContenuId ==
                        contenu.ContenuId);

                if (contenuToUpdate == null)
                {
                    return
                        (new HttpStatusCodeResult(500,
                                                  "Projet non existant!"));
                }

                contenuToUpdate.Title       = contenu.Title;
                contenuToUpdate.Description = contenu.Description;
                context.SaveChanges();

                return(new HttpStatusCodeResult(HttpStatusCode.OK));
            }
        }