public async Task <ActionResult> DeleteConfirmed(int id) { IReadWriteRepository <ServiceData.Models.Study> _studyRepository = new StudyRepository(); await _studyRepository.Delete(id); return(RedirectToAction("Index")); }
// GET: Studies public async Task <ActionResult> Index() { if (!await IsAdmin()) { return(new HttpUnauthorizedResult()); } await LoadViewBag(); IReadWriteRepository <ServiceData.Models.Study> _studyRepository = new StudyRepository(); List <ServiceData.Models.Study> found = _studyRepository.Search(st => st.Manager.Email == User.Identity.Name) .OrderByDescending(st => st.CreatedAt).ToList(); List <Models.Study> toRet = new List <Models.Study>(); foreach (ServiceData.Models.Study sh in found) { toRet.Add(Models.Study.ToAppModel(sh, true)); } ViewData["Title"] = "Your Managed Studies"; return(View(toRet)); }
public async Task UpdateAsync_given_study_returns_true() { // Arrange var context = new Mock <IContext>(); var entity = new Study { Id = 11 }; context.Setup(c => c.Studies.FindAsync(11)).ReturnsAsync(entity); using (var repository = new StudyRepository(context.Object)) { var study = new StudyDTO { Id = 11, Title = "StudyTitle", Description = "StudyDescription" }; // Act var result = await repository.UpdateAsync(study); // Assert Assert.True(result); } }
public async Task UpdateAsync_given_study_updates_study() { // Arrange var context = new Mock <IContext>(); var entity = new Study { Id = 11 }; context.Setup(c => c.Studies.FindAsync(11)).ReturnsAsync(entity); using (var repository = new StudyRepository(context.Object)) { var study = new StudyDTO { Id = 11, Title = "StudyTitle", Description = "StudyDescription" }; // Act await repository.UpdateAsync(study); } // Assert Assert.Equal("StudyTitle", entity.Title); Assert.Equal("StudyDescription", entity.Description); }
public async Task UpdateAsync_given_study_calls_SaveChangesAsync() { // Arrange var context = new Mock <IContext>(); var entity = new Study { Id = 11 }; context.Setup(c => c.Studies.FindAsync(11)).ReturnsAsync(entity); using (var repository = new StudyRepository(context.Object)) { var study = new StudyDTO { Id = 11, Title = "StudyTitle", Description = "StudyDescription" }; // Act await repository.UpdateAsync(study); } // Assert context.Verify(c => c.SaveChangesAsync(default(CancellationToken))); }
public ExclusionLogic(PublicationRepository publicationRepository, ExclusionCriterionRepository exclusionCriterionRepository, StudyRepository studyRepository) { _publicationRepository = publicationRepository; _exclusionCriterionRepository = exclusionCriterionRepository; _studyRepository = studyRepository; }
public async Task ReadAsync_returns_list_of_studies() { // Arrange // Setup InMemory DB with study var builder = new DbContextOptionsBuilder <Context>() .UseInMemoryDatabase(nameof(ReadAsync_returns_list_of_studies)); using (var context = new Context(builder.Options)) { var entity = new Study { Id = 1, Title = "StudyTitle", Description = "StudyDescription" }; context.Studies.Add(entity); await context.SaveChangesAsync(); using (var repository = new StudyRepository(context)) { // Act var studies = await repository.ReadAsync(); var study = studies.Single(); // Assert Assert.Equal("StudyTitle", study.Title); Assert.Equal("StudyDescription", study.Description); } } }
public async Task FindAsync_given_id_returns_study() { // Arrange // Setup InMemory DB with study var builder = new DbContextOptionsBuilder <Context>() .UseInMemoryDatabase(nameof(FindAsync_given_id_returns_study)); using (var context = new Context(builder.Options)) { var entity = new Study { Title = "StudyTitle", Description = "StudyDescription" }; context.Studies.Add(entity); await context.SaveChangesAsync(); var id = entity.Id; using (var repository = new StudyRepository(context)) { // Act var study = await repository.FindAsync(id); // Assert Assert.Equal("StudyTitle", study.Title); Assert.Equal("StudyDescription", study.Description); } } }
public async Task CreateAsync_given_study_returns_id() { // Arrange var entity = default(Study); var context = new Mock <IContext>(); context.Setup(c => c.Studies.Add(It.IsAny <Study>())).Callback <Study>(t => entity = t); context.Setup(c => c.SaveChangesAsync(default(CancellationToken))) .Returns(Task.FromResult(0)) .Callback(() => entity.Id = 11); using (var repository = new StudyRepository(context.Object)) { var study = new StudyNoIdDTO { Title = "StudyTitle", Description = "StudyDescription" }; // Act var result = await repository.CreateAsync(study); // Assert Assert.Equal(11, result); } }
public void Dispose_disposes_context() { var context = new Mock <IContext>(); using (var repository = new StudyRepository(context.Object)) { } context.Verify(c => c.Dispose()); }
public async Task FindAsync_given_nonexisting_id_returns_null() { // Arrange // Setup empty InMemory DB var builder = new DbContextOptionsBuilder <Context>() .UseInMemoryDatabase(nameof(FindAsync_given_nonexisting_id_returns_null)); using (var context = new Context(builder.Options)) using (var repository = new StudyRepository(context)) { // Act var study = await repository.FindAsync(11); // Assert Assert.Null(study); } }
public async Task DeleteAsync_given_study_removes() { // Arrange var entity = new Study(); var context = new Mock <IContext>(); context.Setup(c => c.Studies.FindAsync(11)).ReturnsAsync(entity); using (var repository = new StudyRepository(context.Object)) { // Act await repository.DeleteAsync(11); } // Assert context.Verify(c => c.Studies.Remove(entity)); }
public async Task ReadAsync_with_no_studies_returns_emptylist() { // Arrange // Setup empty InMemory DB var builder = new DbContextOptionsBuilder <Context>() .UseInMemoryDatabase(nameof(ReadAsync_with_no_studies_returns_emptylist)); using (var context = new Context(builder.Options)) using (var repository = new StudyRepository(context)) { // Act var study = await repository.ReadAsync(); // Assert Assert.Empty(study); } }
public async Task DeleteAsync_given_nonexisting_study_doesnot_call_SaveChangesAsync() { // Arrange var entity = new Study(); var context = new Mock <IContext>(); context.Setup(c => c.Studies.FindAsync(11)).ReturnsAsync(default(Study)); using (var repository = new StudyRepository(context.Object)) { // Act await repository.DeleteAsync(11); } // Assert context.Verify(c => c.SaveChangesAsync(default(CancellationToken)), Times.Never); }
// GET: Studies/Delete/5 public async Task <ActionResult> Delete(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } await LoadViewBag(); IReadWriteRepository <ServiceData.Models.Study> _studyRepository = new StudyRepository(); ServiceData.Models.Study study = _studyRepository.GetById(id.Value); if (study == null) { return(HttpNotFound()); } return(View(Models.Study.ToAppModel(study, false))); }
// GET: Studies/Details/5 public async Task <ActionResult> Details(int?id) { if (!await IsAdmin()) { return(new HttpUnauthorizedResult()); } await LoadViewBag(); IReadWriteRepository <ServiceData.Models.Study> _studyRepository = new StudyRepository(); ServiceData.Models.Study study = _studyRepository.GetById(id.Value); if (study == null) { return(HttpNotFound()); } return(View(Models.Study.ToAppModel(study, false))); }
public async Task CreateAsync_given_study_calls_SaveChangesAsync() { var context = new Mock <IContext>(); context.Setup(c => c.Studies.Add(It.IsAny <Study>())); using (var repository = new StudyRepository(context.Object)) { var study = new StudyNoIdDTO { Title = "StudyTitle", Description = "StudyDescription" }; // Act var result = await repository.CreateAsync(study); // Assert context.Verify(c => c.SaveChangesAsync(default(CancellationToken))); } }
public async Task <ActionResult> Create([Bind(Include = "Name,Code")] Models.Study study) { IReadWriteRepository <ServiceData.Models.User> _userRepository = new UserRepository(); ServiceData.Models.User thisUser = await _userRepository.Search(u => u.Email == User.Identity.Name).FirstOrDefaultAsync(); study.Active = true; study.CreatedAt = DateTime.UtcNow; study.Manager = Models.User.ToAppModel(thisUser); study.ManagerId = thisUser.Id; IReadWriteRepository <ServiceData.Models.Study> _studyRepository = new StudyRepository(); bool existing = await _studyRepository.Search(st => st.Code == study.Code).AnyAsync(); if (ModelState.IsValid && !existing) { _studyRepository.Insert(Models.Study.ToServiceModel(study, true)); } return(RedirectToAction("Index")); }
public async Task <ActionResult> Details([Bind(Include = "Email,Id")] AddParticipantStruct newPart) { IReadWriteRepository <ServiceData.Models.User> _userRepository = new UserRepository(); ServiceData.Models.User res = await _userRepository.Search(u => u.Email == newPart.Email).FirstOrDefaultAsync(); IReadWriteRepository <ServiceData.Models.Study> _studyRepository = new StudyRepository(); ServiceData.Models.Study study = _studyRepository.GetById(newPart.Id); if (study == null) { return(RedirectToAction("Index")); } else if (res != null && study.Manager.Email == User.Identity.Name && !study.StudyEnrolments.Any(en => en.UserId == res.Id)) { Models.StudyEnrolment enrol = new Models.StudyEnrolment { CreatedAt = DateTime.UtcNow, Study = Models.Study.ToAppModel(study, false), StudyId = study.Id, Enrolled = true, User = Models.User.ToAppModel(res), UserId = res.Id }; IReadWriteRepository <ServiceData.Models.StudyEnrolment> _enrolRepository = new StudyEnrolmentRepository(); var serviceMod = Models.StudyEnrolment.ToServiceModel(enrol, true, true); var finalRes = _enrolRepository.Insert(serviceMod); return(RedirectToAction("Details", new { id = study.Id })); } return(RedirectToAction("Details", new { id = study?.Id })); }
public async Task CreateAsync_given_study_adds_to_database() { // Arrange var entity = default(Study); var context = new Mock <IContext>(); context.Setup(c => c.Studies.Add(It.IsAny <Study>())).Callback <Study>(t => entity = t); using (var repository = new StudyRepository(context.Object)) { var study = new StudyNoIdDTO { Title = "StudyTitle", Description = "StudyDescription" }; // Act await repository.CreateAsync(study); // Assert Assert.Equal("StudyTitle", entity.Title); Assert.Equal("StudyDescription", entity.Description); } }
public StudyController(IConfiguration configuration) { customerRepository = new StudyRepository(configuration); }
public StudyAdapter GetStudyAdapter() { var studyRepository = new StudyRepository(); return new StudyAdapter(studyRepository); }