예제 #1
0
        public async Task <IActionResult> DeleteALocation([FromRoute] int locationID)
        {
            if (locationID == 0)
            {
                var error = new BadRequestException("The given location ID is invalid");
                return(StatusCode(StatusCodes.Status400BadRequest, new CustomException <BadRequestException>(error).GetException()));
            }

            try
            {
                var deleted = await locationsRepository.DeleteALocation(locationID);

                if (deleted == null)
                {
                    var error = new NotFoundException("The given location cannot be found on database");
                    return(StatusCode(StatusCodes.Status404NotFound, new CustomException <NotFoundException>(error).GetException()));
                }
                var response = new DeletedResponse <int>(deleted.Id, $"Successfully deleted location '{deleted.Id}'");
                return(StatusCode(StatusCodes.Status200OK, 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()));
                }
            }
        }
        public async Task <ActionResult <Location> > DeleteALocation([FromRoute] string code)
        {
            var response = await locationsRepository.DeleteALocation(code);

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

            return(Ok(viewModel));
        }