public async Task <ApiResponse> UpdatePrestationCategory(int id, [FromBody] UpdatePrestationCategoryDto updateCategoryDto)
        {
            var validator = new UpdateCategoryDtoValidator();

            try
            {
                var validationResult = await validator.ValidateAsync(updateCategoryDto);

                if (!validationResult.IsValid)
                {
                    throw new ApiException(validationResult);
                }

                var categoryToBeUpdate = await _prestationService.GetPrestationCategoryById(id);

                if (id == 0 || categoryToBeUpdate == null)
                {
                    throw new ApiException("La catégorie n'existe pas", Status404NotFound);
                }

                var category = _mapper.Map <UpdatePrestationCategoryDto, PrestationCategory>(updateCategoryDto);

                await _prestationService.UpdatePrestationCategory(categoryToBeUpdate, category);

                var updatedCategory = await _prestationService.GetPrestationCategoryById(id);

                var updatedCategoryDto = _mapper.Map <PrestationCategory, GetPrestationCategoryDto>(updatedCategory);

                return(new ApiResponse($"La catégorie avec pour id: {id} a été correctement modifié", updatedCategoryDto, Status201Created));
            }
            catch (CategoryAlreadyExistException 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;
            }
        }
Ejemplo n.º 2
0
        public async Task Put_Update_PrestationCategory_Error_Not_Found()
        {
            #region Arrange
            var initTest   = InitPrestationTest(nameof(Put_Update_PrestationCategory_Error_Not_Found));
            var dbContext  = initTest.dbContext;
            var controller = (PrestationCategoriesController)initTest.controller;
            #endregion
            int idCategory     = 50;
            var updateCategory = new UpdatePrestationCategoryDto()
            {
                Name = "Maquillage modifié"
            };
            #region Act
            var apiException = await Assert.ThrowsAsync <ApiException>(() => controller.UpdatePrestationCategory(idCategory, updateCategory));

            dbContext.Dispose();
            #endregion

            #region Assert
            Assert.Equal(404, apiException.StatusCode);
            #endregion
        }
Ejemplo n.º 3
0
        public async Task Put_Update_PrestationCategory_Return_Ok()
        {
            #region Arrange
            var initTest   = InitPrestationTest(nameof(Put_Update_PrestationCategory_Return_Ok));
            var dbContext  = initTest.dbContext;
            var controller = (PrestationCategoriesController)initTest.controller;
            #endregion
            var updateCategory = new UpdatePrestationCategoryDto()
            {
                Name = "TEST_CATEGORY_UPDATE",
            };
            int idCategory = 1;
            #region Act
            var response = await controller.UpdatePrestationCategory(idCategory, updateCategory);

            dbContext.Dispose();
            #endregion

            #region Assert
            Assert.IsType <ApiResponse>(response);
            Assert.Equal(201, response.StatusCode);
            #endregion
        }