Example #1
0
        public async Task <ActionResult <ResponseDetails> > Delete(string id)
        {
            if (id == null || !Regex.IsMatch(id, "^[0-9a-fA-F]{24}$"))
            {
                string errorMessage = responseMessages.IncorretIdFormat;
                logger.LogInformation("Error: " + errorMessage);
                return(httpResponseHelper.ErrorResponse(errorMessage, 400));
            }

            try {
                logger.LogInformation("Trying to get a establishment with given id");
                var actualEstablishment = await establishmentService.GetById(id);

                if (actualEstablishment == null)
                {
                    string errorMessage = responseMessages.NotFoundGivenId.Replace("$", "estabelecimento");
                    logger.LogInformation("Error: " + errorMessage);
                    return(httpResponseHelper.ErrorResponse(errorMessage, 404));
                }

                var deleteResult = await establishmentService.RemoveById(id);

                if (!deleteResult.IsAcknowledged)
                {
                    string errorMessage = responseMessages.CantRemove;
                    logger.LogInformation("Error: " + errorMessage);
                    return(httpResponseHelper.ErrorResponse(errorMessage, 406));
                }
            } catch (Exception ex) {
                logger.LogInformation("Exception: " + ex.Message);
                throw ex;
            }

            logger.LogInformation("Action DELETE for /api/establishments returns 200");
            return(Ok(new ResponseDetails()
            {
                Message = responseMessages.DeletedSuccess.Replace("$", "estabelecimento"), StatusCode = 200
            }));
        }