private async void UpdateRelease(ReleaseUpdatedEvent release) { var newRelease = new Release() { Id = release.Id, Date = release.Date, PaymentMethod = (PaymentMethod)Enum.Parse(typeof(PaymentMethod), release.PaymentMethod), EstablishmentName = release.EstablishmentName, Amount = release.Amount, CreatedAt = release.CreatedAt, UpdatedAt = release.UpdatedAt }; await releaseService.UpdateById(newRelease.Id, newRelease); }
public async Task <ActionResult <Release> > Put(string id, [FromBody] ReleaseRequest 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)); } Release updatedRelease; try { logger.LogInformation("Trying to get a release with given id"); var actualRelease = await releasesService.GetById(id); if (actualRelease == null) { string errorMessage = responseMessages.NotFoundGivenId.Replace("$", "lançamento"); logger.LogInformation("Error: " + errorMessage); return(httpResponseHelper.ErrorResponse(errorMessage, 404));; } logger.LogInformation("Trying to get associated establishment"); var establishment = await establishmentService.GetByName(body.EstablishmentName); if (establishment == null) { string errorMessage = responseMessages.CantFoundGivenName.Replace("$", "estabelecimento"); logger.LogInformation("Error: " + errorMessage); return(httpResponseHelper.ErrorResponse(errorMessage, 404)); } updatedRelease = new Release() { Id = id, Date = body.Date, PaymentMethod = body.PaymentMethod, Amount = body.Amount, EstablishmentName = establishment.Name, CreatedAt = actualRelease.CreatedAt, UpdatedAt = DateTime.Now }; logger.LogInformation("Trying to update release with id: " + id); var replaceResult = await releasesService.UpdateById(id, updatedRelease); 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.ReleaseUpdated(updatedRelease, rabbitConnector.ConnectionString); } catch (Exception ex) { logger.LogInformation($"Message: {ex.Message}"); logger.LogTrace($"Stack Trace: {ex.StackTrace}"); throw; } logger.LogInformation("Action PUT for /api/releases returns 200"); return(Ok(updatedRelease)); }