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

                var attendee = new PostsAttendee
                {
                    AppUser = user,
                    Post    = request.Post,
                    IsHost  = true
                };

                request.Post.Attendees.Add(attendee);

                _context.Posts.Add(request.Post);

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

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

                return(Result <Unit> .Success(Unit.Value));
            }
示例#2
0
            public async Task <Result <Unit> > Handle(Command request, CancellationToken cancellationToken)
            {
                var post = await _context.Posts
                           .Include(a => a.Attendees)
                           .ThenInclude(u => u.AppUser)
                           .FirstOrDefaultAsync(x => x.Id == request.Id);

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

                var user = await _context.Users.FirstOrDefaultAsync(x =>
                                                                    x.UserName == _userAccessor.GetUsername());

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

                var hostUsername = post.Attendees.FirstOrDefault(x => x.IsHost)?.AppUser?.UserName;

                var attendance = post.Attendees.FirstOrDefault(x => x.AppUser.UserName == user.UserName);

                if (attendance != null && hostUsername == user.UserName)
                {
                    post.IsCancelled = !post.IsCancelled;
                }

                if (attendance != null && hostUsername != user.UserName)
                {
                    post.Attendees.Remove(attendance);
                }

                if (attendance == null)
                {
                    attendance = new PostsAttendee
                    {
                        AppUser = user,
                        Post    = post,
                        IsHost  = false,
                    };

                    post.Attendees.Add(attendance);
                }

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

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