public async Task <ActionResult <Location> > CreateALocation([FromBody] Location location)
        {
            var response = await locationsRepository.CreateALocation(location);

            var viewModel = mapper.Map <Location>(response);

            return(Created("GetALocation", viewModel));
        }
Exemple #2
0
        public async Task <IActionResult> CreateALocation([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) || String.IsNullOrEmpty(location.City))
            {
                var error = new BadRequestException("Province and City cannot be null or empty");
                return(StatusCode(StatusCodes.Status400BadRequest, new CustomException <BadRequestException>(error).GetException()));
            }

            try
            {
                var createdLocationID = await locationsRepository.CreateALocation(location);

                var response = new CreatedResponse <int>(createdLocationID, $"Successfully created location '{createdLocationID}'");
                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
                {
                    var error = new BadRequestException(errMessage);
                    return(StatusCode(StatusCodes.Status400BadRequest, new CustomException <BadRequestException>(error).GetException()));
                }
            }
        }