Example #1
0
            public async Task <Result> Handle(Command command, CancellationToken cancellationToken)
            {
                var user = new UserEntity()
                {
                    FirstName = command.FirstName,
                    LastName  = command.LastName,
                    Email     = command.Email
                };

                await _context
                .RepositoryAsync <UserEntity, Guid>()
                .AddAsync(user);

                var @event = Mapping.Map <UserEntity, UserCreatedEvent>(user);

                @event.Id = user.Id;

                // TODO: Cover under transaction
                await _context.SaveChangesAsync(cancellationToken);

                // TODO: Configure mongo db or EF outbox
                //await _eventBus.CommitAsync(@event);

                _logger.LogInformation($"User has been successfully created with - {user.Id} identifier");

                var result = new Result
                {
                    Id = user.Id
                };

                return(result);
            }
Example #2
0
            public async Task <Result> Handle(Command command, CancellationToken cancellationToken)
            {
                var result = new Result();

                var user = await _context.QueryRepository <UserEntity, Guid>()
                           .Queryable()
                           .FirstOrDefaultAsync(x => x.Id == command.Id, cancellationToken);

                if (user is null)
                {
                    result.ResultMessage = "Unknown user identifier";
                    return(result);
                }

                await _context.RepositoryAsync <UserEntity, Guid>().DeleteAsync(user);

                var @event = Mapping.Map <UserEntity, UserDeletedEvent>(user);

                @event.UserId = user.Id;

                await _context.SaveChangesAsync(cancellationToken);

                await _eventBus.CommitAsync(@event);

                // await using var context = new UserDataContext();
                // await using var transaction = await context.Database.BeginTransactionAsync(cancellationToken);
                //
                // try
                // {
                //     await _context.SaveChangesAsync(cancellationToken);
                //
                //     await _eventBus.CommitAsync(@event);
                //
                //     await transaction.CommitAsync(cancellationToken);
                // }
                // catch (Exception)
                // {
                //     await transaction.RollbackAsync(cancellationToken);
                // }

                return(result);
            }
Example #3
0
 public IRepositoryAsync <SongEntity, Guid> RepositoryAsync()
 {
     _context.SaveChanges();
     return(_context.RepositoryAsync <SongEntity, Guid>());
 }