public virtual ActionResult Comment(int id,string token, Comment com) { if (ModelState.IsValid) { var context = ModelFactory.GetUnitOfWork(); var mRepo = ModelFactory.GetRepository<IMemberRepository>(context); var lRepo = ModelFactory.GetRepository<ILocalisationRepository>(context); var member = mRepo.GetMemberFromToken(token); var localisation = lRepo.Get(id); try { var error = Worki.Resources.Validation.ValidationString.ErrorWhenSave; com.Localisation = localisation; com.PostUserID = member.MemberId; com.Date = System.DateTime.UtcNow; com.RatingDispo = com.Rating; com.RatingPrice = com.Rating; com.RatingWifi = com.Rating; com.RatingWelcome = com.Rating; com.Validate(ref error); localisation.Comments.Add(com); context.Commit(); } catch (System.Data.Entity.Validation.DbEntityValidationException dbEx) { return new ObjectResult<AuthJson>(null, 400, dbEx.GetErrors()); } catch (Exception ex) { _Logger.Error("Comment", ex); context.Complete(); return new ObjectResult<LocalisationJson>(null, 400, ex.Message); } var newContext = ModelFactory.GetUnitOfWork(); lRepo = ModelFactory.GetRepository<ILocalisationRepository>(newContext); localisation = lRepo.Get(id); return new ObjectResult<LocalisationJson>(localisation.GetJson(this), 200, "ok"); } else { return new ObjectResult<LocalisationJson>(null, 400, ModelState.GetErrors()); } }
public virtual PartialViewResult PostComment(int id, Comment com) { var context = ModelFactory.GetUnitOfWork(); var lRepo = ModelFactory.GetRepository<ILocalisationRepository>(context); var mRepo = ModelFactory.GetRepository<IMemberRepository>(context); var error = Worki.Resources.Validation.ValidationString.ErrorWhenSave; var localisation = lRepo.Get(id); try { var member = mRepo.GetMember(User.Identity.Name); Member.Validate(member); if (ModelState.IsValid) { com.Localisation = localisation; com.PostUserID = member.MemberId; com.Date = System.DateTime.UtcNow; com.Validate(ref error); localisation.Comments.Add(com); context.Commit(); return PartialView(MVC.Shared.Views._LocalisationSingleComment, com); } else { throw new ModelStateException(ModelState); } } catch (Exception ex) { _Logger.Error("PostComment", ex); context.Complete(); ModelState.AddModelError("", error); throw new ModelStateException(ModelState); } }