private MentorShip UpdateStatusAddMsg(MentorStatusUpdateVm model) { var user = CurrentUser; var mentor = _db.MentorShips.Include(m => m.Messages).Single(m => m.MentorShipId == model.Id); if (!user.IsAdmin() && mentor.AlumniId != user.UserId && mentor.StudentId != user.UserId) { LogUnAuth(); throw new Exception(Strings.UnAuthAccess); } if (ModelState.IsValid) { if (mentor.Status != model.Status) { if (model.Status == MentorStatusType.AdminApproved) { if (model.AcUserId == 0) { ModelState.AddModelError("", "Select Mentor"); return null; } else { var mentorUser = _db.Users.Find(model.AcUserId); mentor.AlumniId = mentorUser.UserId; mentor.AlumniName = mentorUser.FullName; //MailSrv.SendMailAsync(mentorUser, "You have new mentoring request.", "NITCAA - Mentoring Request", null); } } mentor.Status = model.Status == MentorStatusType.Message ? mentor.Status : model.Status; var msg = GetUserMsg(model.Status); var message = new MentorMessage { MentorShipId = mentor.MentorShipId, Status = model.Status, SenderId = CurrentUserId, Date = DateTime.UtcNow, Text = string.IsNullOrWhiteSpace(model.Message) ? msg : model.Message, SenderName = CurrentUser.FullName, }; _db.Entry(mentor).State = System.Data.EntityState.Modified; _db.MentorMessages.Add(message); _db.SaveChanges(); } } return mentor; }
public ActionResult MentorRequest(MentorRequestVm model) { if (ModelState.IsValid) { var student = CurrentUser; var mentor = new MentorShip { StartDate = DateTime.UtcNow, AlumniId = 0, StudentId = model.StudentId, AlumniName = "NA", StudentName = student.FullName, }; var message = new MentorMessage { Status = mentor.Status, SenderId = student.UserId, Date = DateTime.UtcNow, Text = model.Message, MentorShipId = mentor.MentorShipId, SenderName = student.FullName }; _db.MentorShips.Add(mentor); _db.MentorMessages.Add(message); _db.SaveChanges(); model.IsDone = true; } return View(model); }