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 async Task <Unit> Handle(Command request, CancellationToken cancellationToken) { var technologist = await _context.Technologists .Include(t => t.Licenses) .FirstOrDefaultAsync(t => t.Id == request.Id); if (technologist == null) { throw new Exception("Could not find technologist"); } technologist.Name = request.Name ?? technologist.Name; technologist.Initial = request.Initial ?? technologist.Initial; technologist.Licenses.Clear(); //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 }; technologist.Licenses.Add(technologistLicenses); } //handler logic var success = await _context.SaveChangesAsync() > 0; if (success) { return(Unit.Value); } throw new Exception("Problem saving changes"); }
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); }