Ejemplo n.º 1
0
        //public ActionResult SaveComment(string commentaire, string nom, string note, string nomSeo)
        public ActionResult SaveComment(SaveCommentViewModel comment)
        {
            using (var context = new AvisEntities())
            {
                var formationEntity = context.Formation.FirstOrDefault(f => f.NomSeo == comment.nomSeo);

                if (formationEntity == null)
                {
                    return(RedirectToAction("Acceuil", "Home"));
                }

                Avis nouvelAvis = new Avis();

                nouvelAvis.DateAvis    = DateTime.Now;
                nouvelAvis.Description = comment.commentaire;

                nouvelAvis.UserId = User.Identity.GetUserId();

                var userId = User.Identity.GetUserId();

                var mgerUnicite = new UniqueAvisVerification();
                if (!mgerUnicite.EstAuthoriseACommenter(userId, formationEntity.Id))
                {
                    TempData["Message"] = "Désolé, vous ne pouvez poster qu'un seul avis par formation";
                    return(RedirectToAction("DetailsFormation", "Formation", new { nomSeo = comment.nomSeo }));
                }

                var mger = new PersonneManager();
                nouvelAvis.Nom = mger.GetNomFromUserId(userId);



                double dNote = 0;

                if (!double.TryParse(comment.note, NumberStyles.Any, CultureInfo.InvariantCulture, out dNote))
                {
                    throw new Exception("impossible de parser la note " + comment.note);
                }

                nouvelAvis.Note = dNote;

                nouvelAvis.IdFormation = formationEntity.Id;
                context.Avis.Add(nouvelAvis);
                context.SaveChanges();
            }

            return(RedirectToAction("DetailsFormation", "Formation", new { nomSeo = comment.nomSeo }));
        }
        public ActionResult SaveComment(SaveCommentViewModel comment)
        {
            using (var context = new AvisEntities())
            {
                var formationEntity = context.Formation.FirstOrDefault(f => f.NomSeo == comment.NomSeo);
                if (formationEntity == null)
                {
                    return(RedirectToAction("Accueil", "Home"));
                }

                Avis nouvelAvis = new Avis();
                nouvelAvis.DateAvis = DateTime.Now;

                var userId = User.Identity.GetUserId();

                var mgerUnicite = new UniqueAvisVerification();
                if (!mgerUnicite.EstAutoriseACommenter(userId, formationEntity.Id))
                {
                    TempData["Message"] = "Désolé, un seul avis par compte utilisateur.";
                    return(RedirectToAction("DetailsFormation", "Formation", new { nomSeo = comment.NomSeo }));
                }

                var mger = new PersonneManager();
                nouvelAvis.Nom         = mger.GetNomFromUserId(userId);
                nouvelAvis.Description = comment.Commentaire;

                nouvelAvis.UserId = userId;

                double dNote = 0;

                if (!double.TryParse(comment.Note, System.Globalization.NumberStyles.Any, CultureInfo.InvariantCulture, out dNote))
                {
                    throw new Exception("Impossible de parser la note " + comment.Note);
                }

                nouvelAvis.Note = dNote;

                nouvelAvis.IdFormation = formationEntity.Id;

                context.Avis.Add(nouvelAvis);

                try
                {
                    context.SaveChanges();
                }
                catch (DbEntityValidationException e)
                {
                    foreach (var eve in e.EntityValidationErrors)
                    {
                        Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                          eve.Entry.Entity.GetType().Name, eve.Entry.State);
                        foreach (var ve in eve.ValidationErrors)
                        {
                            Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                              ve.PropertyName, ve.ErrorMessage);
                        }
                    }
                    throw;
                }

                //context.SaveChanges();
            }

            return(RedirectToAction("DetailsFormation", "Formation", new { nomSeo = comment.NomSeo }));
        }