Exemple #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));
                }

                //Send to RabbitMQ to event handler observer pattern
                Emitter.EstablishmentUpdated(updatedEstablishment, rabbitConnector.ConnectionString);
            }
            catch (Exception ex)
            {
                logger.LogInformation($"Message: {ex.Message}");
                logger.LogTrace($"Stack Trace: {ex.StackTrace}");
                throw;
            }

            logger.LogInformation("Action PUT for /api/establishments returns 200");
            return(Ok(updatedEstablishment));
        }
Exemple #2
0
        private async void UpdateEstablishment(EstablishmentUpdatedEvent establishment)
        {
            var updatedEstablishment = new Establishment()
            {
                Id        = establishment.Id,
                Name      = establishment.Name,
                Type      = establishment.Type,
                CreatedAt = establishment.CreatedAt,
                UpdatedAt = establishment.UpdatedAt
            };

            await establishmentService.UpdateById(updatedEstablishment.Id, updatedEstablishment);
        }