public async Task <Unit> Handle(Command request, CancellationToken cancellationToken) { var modality = await _context.Modalities.FindAsync(request.ModalityId); if (modality == null) { throw new RestException(HttpStatusCode.NotFound, new { modality = "modality not found" }); } bool techInitialsExist = await _context.Technologists .Where(t => t.ModalityId == request.ModalityId) .AnyAsync(t => t.Initial == request.Initial); if (techInitialsExist) { throw new RestException(HttpStatusCode.BadRequest, new { technologist = "initial must be unique" }); } var technologist = new Technologist { Id = request.Id, Name = request.Name, Initial = request.Initial, ModalityId = request.ModalityId }; _context.Technologists.Add(technologist); //foreach will not be exceuted if LicenseIdList is empty foreach (var id in request.LicenseIdList) { var license = await _context.Licenses.FindAsync(id); if (license == null || license.ModalityId != technologist.ModalityId) { throw new RestException(HttpStatusCode.BadRequest, new { license = "Unable to save: Invalid License Added" }); } var technologistLicenses = new TechnologistLicense { License = license, Technologist = technologist }; _context.TechnologistLicenses.Add(technologistLicenses); } var success = await _context.SaveChangesAsync() > 0; if (success) { return(Unit.Value); } throw new Exception("Problem saving changes"); }
public ActionResult Edit([Bind(Include = "Id,FirstName,LastName,ContactEmail,ContactNumber,Rut,CountryId,SpecialityId,LaboratoryId")] Technologist technologist) { if (ModelState.IsValid) { db.Entry(technologist).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.CountryId = new SelectList(db.Countries, "CountryId", "Name", technologist.CountryId); ViewBag.LaboratoryId = new SelectList(db.Laboratories, "LaboratoryId", "Name", technologist.LaboratoryId); ViewBag.SpecialityId = new SelectList(db.Specialities, "SpecialityId", "Description", technologist.SpecialityId); return(View(technologist)); }
// GET: Technologists/Details/5 public ActionResult Details(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Technologist technologist = db.Technologist.Find(id); if (technologist == null) { return(HttpNotFound()); } return(View(technologist)); }
public ActionResult DeleteConfirmed(int id) { Technologist technologist = db.Technologist.Find(id); db.People.Remove(technologist); db.SaveChanges(); var UserManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(ddb)); var user = UserManager.FindByEmail(technologist.ContactEmail); UserManager.Delete(user); return(RedirectToAction("Index")); }
// GET: Technologists/Edit/5 public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Technologist technologist = db.Technologist.Find(id); if (technologist == null) { return(HttpNotFound()); } ViewBag.CountryId = new SelectList(db.Countries, "CountryId", "Name", technologist.CountryId); ViewBag.LaboratoryId = new SelectList(db.Laboratories, "LaboratoryId", "Name", technologist.LaboratoryId); ViewBag.SpecialityId = new SelectList(db.Specialities, "SpecialityId", "Description", technologist.SpecialityId); return(View(technologist)); }
public ActionResult Create([Bind(Include = "Id,FirstName,LastName,ContactEmail,ContactNumber,Rut,CountryId,SpecialityId,LaboratoryId, Password")] Technologist technologist) { if (ModelState.IsValid) { var roleManager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(ddb)); var UserManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(ddb)); var user = new ApplicationUser { UserName = technologist.ContactEmail, Email = technologist.ContactEmail }; var result = UserManager.Create(user, technologist.Password); if (result.Succeeded) { //creo al paciente con los datos del usuario registrado Technologist nt = new Technologist(); nt.FirstName = technologist.FirstName; nt.LastName = technologist.LastName; nt.ContactEmail = technologist.ContactEmail; nt.CountryId = technologist.CountryId; nt.Rut = technologist.Rut; nt.ContactNumber = technologist.ContactNumber; nt.LaboratoryId = technologist.LaboratoryId; nt.SpecialityId = technologist.SpecialityId; nt.Password = user.PasswordHash; //guardo al nuevo paciente var dbTech = db.Technologist; dbTech.Add(nt); db.SaveChanges(); //Asignacion de rol var userManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(ddb)); var userRol = userManager.FindByName(nt.ContactEmail); userManager.AddToRole(userRol.Id, "Tecnologo"); //redireciono return(RedirectToAction("Index")); } ViewBag.message = result.Errors; ViewBag.CountryId = new SelectList(db.Countries, "CountryId", "Name", technologist.CountryId); ViewBag.LaboratoryId = new SelectList(db.Laboratories, "LaboratoryId", "Name", technologist.LaboratoryId); ViewBag.SpecialityId = new SelectList(db.Specialities, "SpecialityId", "Description", technologist.SpecialityId); return(View(technologist)); } ViewBag.CountryId = new SelectList(db.Countries, "CountryId", "Name", technologist.CountryId); ViewBag.LaboratoryId = new SelectList(db.Laboratories, "LaboratoryId", "Name", technologist.LaboratoryId); ViewBag.SpecialityId = new SelectList(db.Specialities, "SpecialityId", "Description", technologist.SpecialityId); return(View(technologist)); }
public void Should_Update_Technologist_By_Removing_License() { var context = GetDbContext(); var modalityId = Guid.NewGuid(); context.Modalities.Add(new Domain.Modality { Id = modalityId, Name = "Test Modality", DisplayName = "TM" }); context.SaveChanges(); var licenceId1 = Guid.NewGuid(); var licenseId2 = Guid.NewGuid(); context.Licenses.Add(new Domain.License { Id = licenceId1, Name = "License 1", DisplayName = "L1", ModalityId = modalityId }); context.Licenses.Add(new Domain.License { Id = licenseId2, Name = "License 2", DisplayName = "L2", ModalityId = modalityId }); context.SaveChanges(); var licenseIdList = new List <Guid> { licenceId1, licenseId2 }; var technologistId = Guid.NewGuid(); Technologist technologist = new Technologist { Id = technologistId, Name = "Test Technologist", ModalityId = modalityId, Initial = "TT", }; context.Technologists.Add(technologist); foreach (var id in licenseIdList) { var license = context.Licenses.Find(id); var technologistLicenses = new TechnologistLicense { License = license, Technologist = technologist }; context.TechnologistLicenses.Add(technologistLicenses); } context.SaveChanges(); var sut = new Edit.Handler(context); var result = sut.Handle(new Edit.Command { Id = technologistId, Name = "Updated Technologist", Initial = "UT", LicenseIdList = new List <Guid> { licenseId2 } }, CancellationToken.None).Result; var editedTechnologist = context.Technologists.Include(x => x.Licenses).ThenInclude(x => x.License).FirstOrDefault(x => x.Id == technologistId); var editedTechnologistLicense = editedTechnologist.Licenses.SingleOrDefault(); Assert.Equal("Updated Technologist", editedTechnologist.Name); Assert.Single(editedTechnologist.Licenses); Assert.Equal("License 2", editedTechnologistLicense.License.Name); }