Exemplo n.º 1
0
        //  public ActionResult SaveComment(string commentaire, string nom, string note, string nomSeo)
        public ActionResult SaveComment(SaveCommentViewModel comment)
        {
            Avis nouvelAvis = new Avis
            {
                DateAvis    = DateTime.Now,
                Description = comment.Commentaire,
                Nom         = comment.Nom
            };

            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;

            using (var context = new AvisEntities())
            {
                var formationEntity = context.Formations
                                      .FirstOrDefault(f => f.NomSeo == comment.NomSeo);

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

                nouvelAvis.IdFormation = formationEntity.Id;

                context.Avis.Add(nouvelAvis);
                context.SaveChanges();
            }

            return(RedirectToAction("DetailsFormation", "Formation", new { nomSeo = comment.NomSeo }));
        }
Exemplo n.º 2
0
        // [ValidateAntiForgeryToken]
        public async Task <IActionResult> EditComment(int id, [FromBody] SaveCommentViewModel saveCommentViewModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var comment = _repository.GetCommentById(id);

            var authorizationResult = await authorizationService.AuthorizeAsync(User, comment, "CanCrudOwnComment");

            if (authorizationResult.Succeeded)
            {
                comment.Body         = saveCommentViewModel.Body;
                comment.DateModified = DateTime.Now;
                _unitOfWork.Complete();
                return(Ok(CreateCommentViewModel(comment)));
            }
            else if (User.Identity.IsAuthenticated)
            {
                return(new ForbidResult());
            }
            else
            {
                return(new ChallengeResult());
            }
        }
Exemplo n.º 3
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 }));
        }