Beispiel #1
0
        public async Task <UpdateNotesResponse> ExecuteAsync(UpdateNotesModel request, CancellationToken cancellationToken = default)
        {
            _logger.LogInformation($"{nameof(UpdateNotesCommand)}.{nameof(ExecuteAsync)} hit");
            await using var transaction = await _noteRepository.BeginTransactionAsync(cancellationToken);

            try
            {
                _logger.LogInformation("Getting existing notes");
                var existingNotes = _noteRepository.Get(request.Notes.Select(note => note.Id)).Where(note => note.UserId == request.UserId);

                _logger.LogInformation("Updating notes");
                foreach (var note in request.Notes)
                {
                    var noteToUpdate = existingNotes.FirstOrDefault(n => n.Id == note.Id);

                    if (noteToUpdate == null)
                    {
                        _logger.LogWarning($"Note with id: {note.Id} does not exist. rolling back update");
                        await transaction.RollbackAsync(cancellationToken);

                        _logger.LogInformation("update rolled back");
                        return(new UpdateNotesResponse
                        {
                            Success = false,
                            Notes = request.Notes
                        });
                    }

                    noteToUpdate.NoteText = note.Text;
                }

                await _noteRepository.SaveChangesAsync(cancellationToken);

                await transaction.CommitAsync(cancellationToken);
            }
            catch (Exception e)
            {
                await transaction.RollbackAsync(cancellationToken);

                _logger.LogError(e.Message);

                return(new UpdateNotesResponse
                {
                    Success = false,
                    Notes = request.Notes
                });
            }

            _logger.LogInformation("Finished updating notes");
            return(new UpdateNotesResponse
            {
                Success = true,
                Notes = request.Notes
            });
        }
Beispiel #2
0
        public async Task <IActionResult> Put([FromBody] UpdateNotesModel request, CancellationToken cancellationToken = default)
        {
            _logger.LogTrace($"{nameof(UpdateNotesController)}.{nameof(Put)} hit");
            var errors = request.Validate();

            if (errors.Any())
            {
                return(BadRequest(errors));
            }

            var response = await _command.ExecuteAsync(request, cancellationToken);

            return(Ok(response));
        }