public async Task <IActionResult> Send(SendMessageViewModel model) { if (ModelState.IsValid) { var userId = userManager.GetUserId(HttpContext.User); var recieverId = await userManager.FindByNameAsync(model.UserName); if (recieverId != null) { Msg newMsg = new Msg() { SenderId = userId, Content = Encrypt(model.Content), RecieverId = recieverId.Id }; msgRepository.Add(newMsg); ViewBag.SuccessTitle = "Send Confirmed"; ViewBag.SuccessMessage = "Your message has been sent."; return(View("Success")); } else { ModelState.AddModelError(String.Empty, "Invalid Username"); } } return(View()); }
void SaveMsg(Msg msg) { //recover validator var validator = EntityValidatorFactory.CreateValidator(); if (validator.IsValid(msg)) //if favorite is valid { //add the favorite into the repository _msgRepository.Add(msg); //commit the unit of work _msgRepository.UnitOfWork.Commit(); } else //customer is not valid, throw validation errors { throw new ApplicationValidationErrorsException(validator.GetInvalidMessages <Msg>(msg)); } }