Example #1
0
        public async Task <ActionResult <Establishment> > Put(string id, [FromBody] EstablishmentRequest body)
        {
            // Validating 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));
            }

            Establishment updatedEstablishment;

            try {
                logger.LogInformation("Trying to get a establishemnt 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));
                }

                updatedEstablishment = new Establishment()
                {
                    Id        = id,
                    Name      = body.Name.FirstCharToUpper(),
                    Type      = body.Type.FirstCharToUpper(),
                    CreatedAt = actualEstablishment.CreatedAt,
                    UpdatedAt = DateTime.Now
                };

                logger.LogInformation("Trying to update establishment with id: " + id);
                var replaceResult = await establishmentService.UpdateById(id, updatedEstablishment);

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

            logger.LogInformation("Action PUT for /api/establishments returns 200");
            return(Ok(updatedEstablishment));
        }