public async Task <ApiResponse> UpdatePrestation(int id, [FromBody] UpdatePrestationDto updatePrestationDto) { var validator = new UpdatePrestationDtoValidator(); try { var validationResult = await validator.ValidateAsync(updatePrestationDto); if (id == 0 || !validationResult.IsValid) { throw new ApiException(validationResult); } var prestationToBeUpdate = await _prestationService.GetPrestationById(id); if (id == 0 || prestationToBeUpdate == null) { throw new ApiException(ErrorMessage.Err_Prestation_Id_Does_Not_Exist, Status404NotFound); } var prestation = _mapper.Map <UpdatePrestationDto, Prestation>(updatePrestationDto); await _prestationService.UpdatePrestation(prestationToBeUpdate, prestation); var updatedPrestation = await _prestationService.GetPrestationById(id); var updatedPrestationDto = _mapper.Map <Prestation, GetPrestationDto>(updatedPrestation); return(new ApiResponse($"La prestation avec pour id: {id} a été correctement modifié", updatedPrestationDto, Status201Created)); } catch (CategoryDoesNotExistException ex) { _logger.LogError("There was an error on '{0}' invocation: {1}", MethodBase.GetCurrentMethod(), ex); var apiException = new ApiException(ex.Message, Status400BadRequest); apiException.CustomError = ex.Message; throw apiException; } catch (Exception ex) { _logger.LogError("There was an error on '{0}' invocation: {1}", MethodBase.GetCurrentMethod(), ex); throw; } }
public async Task Put_Update_Prestation_Return_Error_Prestation_Id_Does_Not_Exist() { #region Arrange var dbContext = DbContextMocker.GetElegantGlamourDbContext(nameof(Put_Update_Prestation_Return_Error_Prestation_Id_Does_Not_Exist)); var config = new MapperConfiguration(cfg => { cfg.AddProfile(new MappingProfile()); }); var mapper = new Mapper(config); var mockUnitOfWork = new UnitOfWork(dbContext); var mockPrestationService = new PrestationService(mockUnitOfWork); var mockLogger = Mock.Of <ILogger <PrestationsController> >(); var controller = new PrestationsController(mockPrestationService, mapper, mockLogger); #endregion var updatePrestation = new UpdatePrestationDto() { Description = "this is description", Price = 50, Title = "test title", Duration = 60, PrestationCategoryId = 1 }; int idPrestation = 8787887; #region Act var apiException = await Assert.ThrowsAsync <ApiException>(() => controller.UpdatePrestation(idPrestation, updatePrestation)); dbContext.Dispose(); #endregion #region Assert Assert.Equal(404, apiException.StatusCode); Assert.Contains(ErrorMessage.Err_Prestation_Id_Does_Not_Exist, apiException.Message); #endregion }
public async Task Put_Update_Prestation_Return_Ok() { #region Arrange var dbContext = DbContextMocker.GetElegantGlamourDbContext(nameof(Put_Update_Prestation_Return_Ok)); var config = new MapperConfiguration(cfg => { cfg.AddProfile(new MappingProfile()); }); var mapper = new Mapper(config); var mockUnitOfWork = new UnitOfWork(dbContext); var mockPrestationService = new PrestationService(mockUnitOfWork); var mockLogger = Mock.Of <ILogger <PrestationsController> >(); var controller = new PrestationsController(mockPrestationService, mapper, mockLogger); #endregion var updatePrestation = new UpdatePrestationDto() { Description = "this is description updated", Price = 60, Title = "test title updated", Duration = 120, PrestationCategoryId = 1 }; int idPrestation = 1; #region Act var response = await controller.UpdatePrestation(idPrestation, updatePrestation); dbContext.Dispose(); #endregion #region Assert Assert.IsType <ApiResponse>(response); Assert.Equal(201, response.StatusCode); #endregion }
public async Task Put_Update_Prestation_Return_Error_Category_Empty() { #region Arrange var dbContext = DbContextMocker.GetElegantGlamourDbContext(nameof(Put_Update_Prestation_Return_Error_Category_Empty)); var config = new MapperConfiguration(cfg => { cfg.AddProfile(new MappingProfile()); }); var mapper = new Mapper(config); var mockUnitOfWork = new UnitOfWork(dbContext); var mockPrestationService = new PrestationService(mockUnitOfWork); var mockLogger = Mock.Of <ILogger <PrestationsController> >(); var controller = new PrestationsController(mockPrestationService, mapper, mockLogger); #endregion var updatePrestation = new UpdatePrestationDto() { Title = "Test", Description = "Test", Price = 50, Duration = 50 }; int idPrestation = 1; #region Act var apiException = await Assert.ThrowsAsync <ApiException>(() => controller.UpdatePrestation(idPrestation, updatePrestation)); dbContext.Dispose(); #endregion #region Assert Assert.Equal(400, apiException.StatusCode); Assert.Contains(ErrorMessage.Err_Category_Not_Empty, apiException.CustomError.ToString()); #endregion }