Exemplo n.º 1
0
        public async Task <IActionResult> CreateAProvince([FromBody] LocationResource location)
        {
            if (location == null)
            {
                var error = new BadRequestException("The given request body is null / Request Body cannot be read");
                return(StatusCode(StatusCodes.Status400BadRequest, new CustomException <BadRequestException>(error).GetException()));
            }

            if (String.IsNullOrEmpty(location.Province))
            {
                var error = new BadRequestException("Province cannot be null or empty");
                return(StatusCode(StatusCodes.Status400BadRequest, new CustomException <BadRequestException>(error).GetException()));
            }

            try
            {
                var createdProvinceName = await locationsRepository.CreateAProvince(location.Province);

                var response = new CreatedResponse <string>(createdProvinceName, $"Successfully created province '{createdProvinceName}'");
                // Log.Information("@{a}", response);
                return(StatusCode(StatusCodes.Status201Created, response));
            }
            catch (Exception err)
            {
                var errMessage = $"Source: {err.Source}\n  Message: {err.Message}\n  StackTrace: {err.StackTrace}\n";
                if (err is SqlException)
                {
                    var error = new InternalServerException(errMessage);
                    return(StatusCode(StatusCodes.Status500InternalServerError, new CustomException <InternalServerException>(error).GetException()));
                }
                else
                {
                    // Log.Information("second bad request");
                    var error = new BadRequestException(errMessage);
                    return(StatusCode(StatusCodes.Status400BadRequest, new CustomException <BadRequestException>(error).GetException()));
                }
            }
        }