Ejemplo n.º 1
0
            public async Task <Result <Unit> > Handle(Command request, CancellationToken cancellationToken)
            {
                var user = await _context.Users.FirstOrDefaultAsync(x => x.UserName == _userAccessor.GetUsername());

                var participant = new TodoParticipant
                {
                    User    = user,
                    Todo    = request.Todo,
                    IsOwner = true
                };

                request.Todo.Participants.Add(participant);
                request.Todo.Category = await _context.Categories.FirstOrDefaultAsync(x => x.Id == request.Todo.Category.Id);

                _context.Todos.Add(request.Todo);

                var result = await _context.SaveChangesAsync() > 0;

                if (!result)
                {
                    return(Result <Unit> .Failure("Failed to create todo"));
                }

                return(Result <Unit> .Success(Unit.Value));
            }
Ejemplo n.º 2
0
            public async Task <Result <Unit> > Handle(Command request, CancellationToken cancellationToken)
            {
                var todo = await _context.Todos.Include(t => t.Participants).ThenInclude(u => u.User).FirstOrDefaultAsync(user => user.Id == request.Id);

                if (todo == null)
                {
                    return(null);
                }
                var user = await _context.Users.FirstOrDefaultAsync(u => u.UserName == _userAccessor.GetUsername());

                if (user == null)
                {
                    return(null);
                }

                var ownerUserName = todo.Participants.FirstOrDefault(p => p.IsOwner)?.User?.UserName;

                var participation = todo.Participants.FirstOrDefault(p => p.User.UserName == user.UserName);

                if (participation == null)
                {
                    participation = new TodoParticipant
                    {
                        User    = user,
                        Todo    = todo,
                        IsOwner = false,
                    };
                    todo.Participants.Add(participation);
                }

                var result = await _context.SaveChangesAsync() > 0;

                return(result ? Result <Unit> .Success(Unit.Value) : Result <Unit> .Failure("Problem updating participation"));
            }