public async Task <bool> Edit(int id, CDGDiseaseServiceModel diseaseServiceModel) { var diseaseTypeFromDb = context.CDGDiseaseTypes .SingleOrDefault(diseaseType => diseaseType.Name == diseaseServiceModel.CDGDiseaseType.Name); if (diseaseTypeFromDb == null) { throw new ArgumentNullException(nameof(diseaseTypeFromDb)); } CDGDisease diseaseFromDb = await this.context.CDGDiseases.SingleOrDefaultAsync(d => d.Id == id); if (diseaseFromDb == null) { throw new ArgumentNullException(nameof(diseaseFromDb)); } diseaseFromDb.Name = diseaseServiceModel.Name; diseaseFromDb.Description = diseaseServiceModel.Description; diseaseFromDb.CDGDiseaseType = diseaseTypeFromDb; this.context.CDGDiseases.Update(diseaseFromDb); int result = await this.context.SaveChangesAsync(); return(result > 0);; }
public async Task <CDGDiseaseServiceModel> GetCDGDiseaseById(int id) { CDGDiseaseServiceModel disease = await this.context.CDGDiseases.To <CDGDiseaseServiceModel>() .SingleOrDefaultAsync(d => d.Id == id); return(disease); }
public async Task <bool> CreateDisease(CDGDiseaseServiceModel cdgDiseaseServiceModel) { CDGDiseaseType diseaseTypeFromDb = context.CDGDiseaseTypes .SingleOrDefault(diseaseType => diseaseType.Name == cdgDiseaseServiceModel.CDGDiseaseType.Name); if (diseaseTypeFromDb == null) { throw new ArgumentNullException(nameof(diseaseTypeFromDb)); } //CDGDisease cdgDisease = AutoMapper.Mapper.Map<CDGDisease>(cdgDiseaseServiceModel); CDGDisease cdgDisease = new CDGDisease() { Name = cdgDiseaseServiceModel.Name, Description = cdgDiseaseServiceModel.Description, CDGDiseaseType = diseaseTypeFromDb, }; cdgDisease.CDGDiseaseType = diseaseTypeFromDb; await this.context.CDGDiseases.AddAsync(cdgDisease); int result = await this.context.SaveChangesAsync(); return(result > 0); }
public async Task Edit_WithCorrectData_ShouldEditCDGDiseaseCorrectly() { string errorMessagePrefix = "DiseasesService Method Edit() does not work properly."; var context = CDGBulgariaInmemoryFactory.InitializeContext(); await SeedData(context); this.diseasesService = new DiseasesService(context); CDGDiseaseServiceModel expectedData = context.CDGDiseases.First().To <CDGDiseaseServiceModel>(); expectedData.Name = "Edited Name"; expectedData.Description = "Edited Description"; expectedData.CDGDiseaseType.Name = "Edited Type"; await this.diseasesService.Edit(expectedData.Id, expectedData); CDGDiseaseServiceModel actualData = context.CDGDiseases.First().To <CDGDiseaseServiceModel>(); Assert.True(actualData.Name == expectedData.Name, errorMessagePrefix + "Name not edited properly"); Assert.True(actualData.Description == expectedData.Description, errorMessagePrefix + "Description not edited properly"); Assert.True(actualData.CDGDiseaseType.Name == expectedData.CDGDiseaseType.Name, errorMessagePrefix + "CDGDiseaseType not edited properly"); }
public async Task GetDiseaseById_WithNonExistentId_ShouldReturnNull() { string errorMessagePrefix = "DiseaseService Method GetDiseaseById() does not work properly."; var context = CDGBulgariaInmemoryFactory.InitializeContext(); await SeedData(context); this.diseasesService = new DiseasesService(context); CDGDiseaseServiceModel actualResult = await this.diseasesService.GetCDGDiseaseById(1000); Assert.True(actualResult == null, errorMessagePrefix); }
public async Task Delete_WithCorrectData_ShouldPassSuccesfully() { string errorMessagePrefix = "DiseasesService Method Delete() does not work properly."; var context = CDGBulgariaInmemoryFactory.InitializeContext(); await SeedData(context); this.diseasesService = new DiseasesService(context); CDGDiseaseServiceModel expectedData = context.CDGDiseases.First().To <CDGDiseaseServiceModel>(); bool actualData = await this.diseasesService.Delete(expectedData.Id); Assert.True(actualData, errorMessagePrefix); }
public async Task Edit_WithGivenNonExistenId_ShouldThrowArgumentNullException() { var context = CDGBulgariaInmemoryFactory.InitializeContext(); await SeedData(context); this.diseasesService = new DiseasesService(context); CDGDiseaseServiceModel expectedData = context.CDGDiseases.First().To <CDGDiseaseServiceModel>(); expectedData.Name = "Edited Name"; expectedData.Description = "Edited Description"; expectedData.CDGDiseaseType.Name = "Edited Type"; await this.diseasesService.Edit(expectedData.Id, expectedData); await Assert.ThrowsAsync <ArgumentNullException>(() => this.diseasesService.Edit(205, expectedData)); }
public async Task GetDiseaseById_WithExistentId_ShouldReturnCorrectResult() { string errorMessagePrefix = "DiseaseService Method GetDiseaseById() does not work properly."; var context = CDGBulgariaInmemoryFactory.InitializeContext(); await SeedData(context); this.diseasesService = new DiseasesService(context); var expectedResult = context.CDGDiseases.First(); CDGDiseaseServiceModel actualResult = await this.diseasesService.GetCDGDiseaseById(expectedResult.Id); Assert.True(expectedResult.Id == actualResult.Id, errorMessagePrefix + " " + "Id is not returned properly"); Assert.True(expectedResult.Name == actualResult.Name, errorMessagePrefix + " " + "Name is not returned properly"); Assert.True(expectedResult.Description == actualResult.Description, errorMessagePrefix + " " + "Name is not returned properly"); Assert.True(expectedResult.CDGDiseaseType.Name == actualResult.CDGDiseaseType.Name, errorMessagePrefix + " " + "CDGDiseaseType is not returned properly"); }
public async Task Edit_WithNonExistentDiseaseType_ShouldThrowArgumentNullException() { string errorMessagePrefix = "DiseasesService Edit() method does not work properly."; var context = CDGBulgariaInmemoryFactory.InitializeContext(); await SeedData(context); this.diseasesService = new DiseasesService(context); CDGDiseaseServiceModel expectedData = context.CDGDiseases.First().To <CDGDiseaseServiceModel>(); expectedData.Name = "Editted_Name"; expectedData.CDGDiseaseType = new CDGDiseaseTypeServiceModel { Name = "Non-Existent" }; await Assert.ThrowsAsync <ArgumentNullException>(() => this.diseasesService.Edit(expectedData.Id, expectedData)); }
public async Task <IActionResult> Edit(int id, CDGDiseaseEditInputModel diseaseEditInputModel) { if (!this.ModelState.IsValid) { var allCDGDiseaseTypes = await this.diseasesService.GetAllTypes().ToListAsync(); this.ViewData["types"] = allCDGDiseaseTypes.Select(cdgType => new CDGDiseaseCreateCDGDiseaseTypeViewModel() { Name = cdgType.Name }) .ToList(); return(this.View(diseaseEditInputModel)); } CDGDiseaseServiceModel diseaseServiceModel = diseaseEditInputModel.To <CDGDiseaseServiceModel>(); await this.diseasesService.Edit(id, diseaseServiceModel); return(this.Redirect("/")); }
public async Task Create_WithCorrectData_ShouldSuccesfullyCreate() { string errorMessagePrefix = "DiseasesService Method CreateDisease() does not work properly."; var context = CDGBulgariaInmemoryFactory.InitializeContext(); this.diseasesService = new DiseasesService(context); CDGDiseaseServiceModel diseaseServiceModel = new CDGDiseaseServiceModel() { Name = "PMM3", Description = "The next disease from this row of diseases", CDGDiseaseType = new CDGDiseaseTypeServiceModel { Name = "Mixed-Type" } }; bool actualResult = await this.diseasesService.CreateDisease(diseaseServiceModel); Assert.True(actualResult, errorMessagePrefix); }
public async Task <IActionResult> Create(CDGDiseaseCreateInputModel cdgDiseaseCreateInputModel) { if (!this.ModelState.IsValid) { var allCDGDiseaseTypes = await this.diseasesService.GetAllTypes().ToListAsync(); this.ViewData["types"] = allCDGDiseaseTypes.Select( diseaseType => new CDGDiseaseCreateCDGDiseaseTypeViewModel { Name = diseaseType.Name }) .ToList(); return(this.View()); } CDGDiseaseServiceModel cdgDiseaseServiceModel = cdgDiseaseCreateInputModel.To <CDGDiseaseServiceModel>(); await this.diseasesService.CreateDisease(cdgDiseaseServiceModel); return(this.Redirect("/Diseases/All")); }