public async Task GetAllDietMeals_WhenCalled_ReturnsAllDietMeals() { // Arrange var dietDto = _fixture.Items.FirstOrDefault(); var expectedDietDto = new DietDto() { Id = dietDto.Id, Name = dietDto.Name, Description = dietDto.Description, DietMeal = new List <DietMeal> { new DietMeal { DietId = dietDto.Id, MealId = 1 } } }; _mockRepository.Setup(r => r.GetAllDietMealsAsync(dietDto.Id)).ReturnsAsync(expectedDietDto); // Act var dietResult = await _sut.GetAllDietMeals(dietDto.Id); // Assert _mockRepository.Verify(r => r.GetAllDietMealsAsync(It.IsAny <int>()), Times.Once); dietResult.Should().NotBeNull(); dietResult.Should().BeEquivalentTo(expectedDietDto); }
public async Task <DietDto> UpdateAsync(int id, DietDto entity) { _logger.LogInformation("Entered in Update with id {}, dietDto {}", id, entity); Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); Diet dietFromDb = await _ctx.Diets.FindAsync(id); if (dietFromDb == null) { stopWatch.Stop(); _logger.LogInformation("Elapsed time in {0} ms", stopWatch.ElapsedMilliseconds); _logger.LogError("Diet with id {} not found", id); return(null); } Diet dietToUpdate = _mapper.Map <DietDto, Diet>(entity); _ctx.Entry(dietFromDb).CurrentValues.SetValues(dietToUpdate); await _ctx.SaveChangesAsync(); await _patientRepository.DisassociateDietAsync(id); entity.PatientDto.DietId = dietToUpdate.Id; await _patientRepository.UpdateAsync(entity.PatientDto.Id, entity.PatientDto); stopWatch.Stop(); _logger.LogInformation("Elapsed time in {0} ms", stopWatch.ElapsedMilliseconds); return(_mapper.Map <Diet, DietDto>(dietToUpdate)); }
public async Task <IList <DietDto> > GetAllAsync() { _logger.LogInformation("Entered in GetAllAsync"); Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); IList <Diet> diets = await _ctx.Diets.ToListAsync(); IList <DietDto> dietsDto = new List <DietDto>(); foreach (Diet diet in diets) { Patient patientFromDb = await _ctx.Patients.Include(p => p.Diet).FirstOrDefaultAsync(p => p.DietId == diet.Id); PatientDto patientDto = _mapper.Map <Patient, PatientDto>(patientFromDb); DietDto dietDto = _mapper.Map <Diet, DietDto>(diet); dietDto.PatientDto = patientDto; dietsDto.Add(dietDto); } stopWatch.Stop(); _logger.LogInformation("Elapsed time in {0} ms", stopWatch.ElapsedMilliseconds); return(dietsDto); }
public async Task Create_WhenCalled_ReturnsDiet() { // Arrange var dietDto = new DietDto { Name = "Diet 1", Description = "Diet 1" }; var expectedDietDto = new DietDto { Id = 1, Name = dietDto.Name, Description = dietDto.Description }; _mockRepository.Setup(r => r.CreateAsync(dietDto)).ReturnsAsync(expectedDietDto); // Act var dietResult = await _sut.CreateAsync(dietDto); // Assert _mockRepository.Verify(r => r.CreateAsync(It.IsAny <DietDto>()), Times.Once); dietResult.Should().NotBeNull(); dietResult.Should().BeEquivalentTo(expectedDietDto); }
public async Task <DietDto> GetAsync(int id) { _logger.LogInformation("Entered in Get with id {}", id); Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); Diet diet = await _ctx.Diets.Include(d => d.DietMeal).FirstOrDefaultAsync(d => d.Id == id); if (diet == null) { stopWatch.Stop(); _logger.LogInformation("Elapsed time in {0} ms", stopWatch.ElapsedMilliseconds); _logger.LogError("Diet with id {} not found", id); return(null); } Patient patientFromDb = await _ctx.Patients.Include(p => p.Diet).FirstOrDefaultAsync(p => p.DietId == id); PatientDto patientDto = _mapper.Map <Patient, PatientDto>(patientFromDb); DietDto dietDto = new DietDto { PatientDto = patientDto }; stopWatch.Stop(); _logger.LogInformation("Elapsed time in {0} ms", stopWatch.ElapsedMilliseconds); return(_mapper.Map <Diet, DietDto>(diet, dietDto)); }
public bool CheckIfUnique(string parameter, DietDto entity) { if (parameter.Equals(NAME_PARAMETER)) { return(_ctx.Diets.Any(d => d.Name == entity.Name)); } return(false); }
public async Task Create(DietDto entity) { Diet dietToAdd = _mapper.Map <DietDto, Diet>(entity); await _ctx.Diets.AddAsync(dietToAdd); await _ctx.SaveChangesAsync(); entity.PatientDto.DietId = dietToAdd.Id; await _patientRepository.Update(entity.PatientDto.Id, entity.PatientDto); }
public async Task Update(int id, DietDto entity) { Diet dietFromDb = await _ctx.Diets.FindAsync(id); Diet dietToUpdate = _mapper.Map <DietDto, Diet>(entity); _ctx.Entry(dietFromDb).CurrentValues.SetValues(dietToUpdate); await _ctx.SaveChangesAsync(); await _patientRepository.DisassociateDiet(id); entity.PatientDto.DietId = dietToUpdate.Id; await _patientRepository.Update(entity.PatientDto.Id, entity.PatientDto); }
public async Task <DietDto> Get(int id) { Diet diet = await _ctx.Diets.Include(d => d.DietMeal).FirstOrDefaultAsync(d => d.Id == id); Patient patientFromDb = await _ctx.Patients.Include(p => p.Diet).FirstOrDefaultAsync(p => p.DietId == id); PatientDto patientDto = _mapper.Map <Patient, PatientDto>(patientFromDb); DietDto dietDto = new DietDto { PatientDto = patientDto }; return(_mapper.Map <Diet, DietDto>(diet, dietDto)); }
public async Task <IList <DietDto> > GetAll() { IList <Diet> diets = await _ctx.Diets.ToListAsync(); IList <DietDto> dietsDto = new List <DietDto>(); foreach (Diet diet in diets) { Patient patientFromDb = await _ctx.Patients.Include(p => p.Diet).FirstOrDefaultAsync(p => p.DietId == diet.Id); PatientDto patientDto = _mapper.Map <Patient, PatientDto>(patientFromDb); DietDto dietDto = _mapper.Map <Diet, DietDto>(diet); dietDto.PatientDto = patientDto; dietsDto.Add(dietDto); } return(dietsDto); }
public async Task <IActionResult> Create([FromBody] DietDto dietDto) { _logger.LogInformation("Entered in [POST] /diets endpoint with dietDto {}", dietDto); IActionResult result; var newDiet = await _dietService.CreateAsync(dietDto); if (newDiet != null) { result = Created("", newDiet); } else { result = BadRequest(); } return(result); }
public async Task <DietDto> CreateAsync(DietDto entity) { _logger.LogInformation("Entered in Create with dietDto {}", entity); Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); Diet dietToAdd = _mapper.Map <DietDto, Diet>(entity); Diet newDiet = (await _ctx.Diets.AddAsync(dietToAdd)).Entity; await _ctx.SaveChangesAsync(); entity.PatientDto.DietId = dietToAdd.Id; await _patientRepository.UpdateAsync(entity.PatientDto.Id, entity.PatientDto); stopWatch.Stop(); _logger.LogInformation("Elapsed time in {0} ms", stopWatch.ElapsedMilliseconds); return(_mapper.Map <Diet, DietDto>(newDiet)); }
public async Task <IActionResult> Update(int id, [FromBody] DietDto dietDto) { _logger.LogInformation("Entered in [PUT] /diets endpoint with id {}, dietDto {}", id, dietDto); IActionResult result; var updatedDiet = await _dietService.UpdateAsync(id, dietDto); _logger.LogDebug("Updated diet: {}", updatedDiet); if (updatedDiet != null) { result = Ok(updatedDiet); } else { result = BadRequest(); } return(result); }
public async Task Update_WhenCalled_ReturnsDiet() { // Arrange var dietDto = _fixture.Items.FirstOrDefault(); var dietId = dietDto.Id; var expectedDietDto = new DietDto { Id = dietDto.Id, Name = "Diet 11", Description = dietDto.Description }; _mockRepository.Setup(r => r.UpdateAsync(dietId, dietDto)).ReturnsAsync(expectedDietDto); // Act var dietResult = await _sut.UpdateAsync(dietId, dietDto); // Assert _mockRepository.Verify(r => r.UpdateAsync(It.IsAny <int>(), It.IsAny <DietDto>()), Times.Once); dietResult.Should().NotBeNull(); dietResult.Should().BeEquivalentTo(expectedDietDto); }
public bool CheckIfUnique(string parameter, DietDto entityDto) { return(_dietRepository.CheckIfUnique(parameter, entityDto)); }
public async Task <DietDto> UpdateAsync(int id, DietDto entityDto) { return(await _dietRepository.UpdateAsync(id, entityDto)); }
public async Task Create(DietDto entityDto) { await _dietRepository.Create(entityDto); }
public async Task <DietDto> CreateAsync(DietDto entityDto) { return(await _dietRepository.CreateAsync(entityDto)); }
public async Task Update(int id, DietDto entityDto) { await _dietRepository.Update(id, entityDto); }