public async Task <IActionResult> Edit(EditCertificationViewModel model) { model.Users = Request.Form["user"]; if (ModelState.IsValid && model.DatetimeEnd != null) { Certification certification = await _db.Certifications.FindAsync(model.Id); if (certification == null) { return(NotFound()); } certification.Name = model.Name; certification.Description = model.Description; certification.DatetimeEnd = (DateTime)model.DatetimeEnd; certification.Topic = _db.Topics.Where(x => x.Name == model.Topic).FirstOrDefault(); var usersByCertification = _db.UserCertifications .Where(e => e.CertificationId == certification.Id) .ToList(); _db.UserCertifications.RemoveRange(usersByCertification); foreach (string userId in Request.Form["user"]) { User user = await _userManager.FindByIdAsync(userId); UserCertifications userCertifications = new UserCertifications { UserId = userId, CertificationId = certification.Id, User = user, Certification = certification }; await _db.UserCertifications.AddAsync(userCertifications); } await _db.SaveChangesAsync(); return(RedirectToAction("Index")); } return(View(model)); }
public async Task <IActionResult> Create(CreateCertificationViewModel model) { if (ModelState.IsValid && model.DatetimeEnd != null) { var topic = _db.Topics.FirstOrDefault(x => x.Name == model.Topic); Certification certification = new Certification { Id = Guid.NewGuid().ToString(), Name = model.Name, Description = model.Description, DatetimeStart = DateTime.Today, DatetimeEnd = (DateTime)model.DatetimeEnd, TopicId = topic.Id, Topic = topic, IsActive = true }; await _db.Certifications.AddAsync(certification); foreach (string userId in Request.Form["user"]) { User user = await _userManager.FindByIdAsync(userId); UserCertifications userCertifications = new UserCertifications { UserId = userId, CertificationId = certification.Id, User = user, Certification = certification }; await _db.UserCertifications.AddAsync(userCertifications); } await _db.SaveChangesAsync(); return(RedirectToAction("Index")); } model.TopicList = new List <string>(); return(View(model)); }