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 atendee = new ActivityAtendee
                {
                    AppUser  = user,
                    Activity = request.Activity,
                    IsHost   = true
                };

                request.Activity.Atendees.Add(atendee);

                _context.Activities.Add(request.Activity);

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

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

                return(Result <Unit> .Success(Unit.Value));
            }
Ejemplo n.º 2
0
            public async Task <Result <Unit> > Handle(Command request, CancellationToken cancellationToken)
            {
                var activity = await _context.Activities
                               .Include(a => a.Atendees).ThenInclude(u => u.AppUser)
                               .SingleOrDefaultAsync(x => x.Id == request.Id);

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

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

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

                var hostUsername = activity.Atendees.FirstOrDefault(x => x.IsHost)?.AppUser?.UserName;

                var attendance = activity.Atendees.FirstOrDefault(x => x.AppUser.UserName == user.UserName);

                if (attendance != null && hostUsername == user.UserName)
                {
                    activity.IsCanceled = !activity.IsCanceled;
                }

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

                if (attendance == null)
                {
                    attendance = new ActivityAtendee
                    {
                        AppUser  = user,
                        Activity = activity,
                        IsHost   = false
                    };

                    activity.Atendees.Add(attendance);
                }

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

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