Example #1
0
        public async Task <Unit> Handle(CreateUserCommand request, CancellationToken cancellationToken)
        {
            var user = new User(new Random().Next(), request.Name, request.Email, request.BirthDate);

            _context.User.Add(user);

            await _context.SaveChangesAsync(cancellationToken);

            await _mediator.Publish(new UserActionNotification
            {
                Id     = user.Id,
                Name   = request.Name,
                Email  = request.Email,
                Action = ActionNotificationEnum.Created
            }, cancellationToken);

            return(Unit.Value);
        }
        public async Task <Unit> Handle(DeleteUserCommand request, CancellationToken cancellationToken)
        {
            var user = await _context.User.FindAsync(request.Id);

            if (user == null)
            {
                throw new NotFoundException(nameof(User), request.Id);
            }

            _context.User.Remove(user);

            await _context.SaveChangesAsync(cancellationToken);

            await _mediator.Publish(new UserActionNotification
            {
                Id     = user.Id,
                Action = ActionNotificationEnum.Deleted
            }, cancellationToken);

            return(Unit.Value);
        }
        public async Task <Unit> Handle(UpdateUserCommand request, CancellationToken cancellationToken)
        {
            var user = await _context.User.SingleOrDefaultAsync(c => c.Id == request.Id, cancellationToken);

            if (user == null)
            {
                throw new NotFoundException(nameof(User), request.Id);
            }

            user.Update(request.Name, request.Email, request.BirthDate);

            await _context.SaveChangesAsync(cancellationToken);

            await _mediator.Publish(new UserActionNotification
            {
                Id     = user.Id,
                Name   = request.Name,
                Email  = request.Email,
                Action = ActionNotificationEnum.Updated
            }, cancellationToken);

            return(Unit.Value);
        }