// TAG GROUPS AND TAGS
        private static IDictionary <Guid, IEnumerable <TagGroup> > SeedTags(ModelBuilder b, IEnumerable <Guid> userIds)
        {
            var dict = new Dictionary <Guid, IEnumerable <TagGroup> >();

            foreach (var userId in userIds)
            {
                var tagGroups = ExerciseTagGroupsFactory.GetTagGroups().ToList();

                foreach (var tagGroup in tagGroups)
                {
                    tagGroup.Id = Guid.NewGuid();
                    tagGroup.ApplicationUserId = userId;

                    foreach (var tag in tagGroup.Tags)
                    {
                        tag.TagGroupId = tagGroup.Id;
                        tag.Id         = Guid.NewGuid();

                        b.Entity <Tag>().HasData(tag);
                    }
                }

                dict.Add(userId, tagGroups.Clone()); // clone the list

                tagGroups.ForEach(tg =>
                {
                    tg.Tags = null; // to avoid "Navigation property is set" error because you have to ONLY connect entities through pre-set IDs
                    b.Entity <TagGroup>().HasData(tg);
                });
            }

            return(dict);
        }
        private async Task <ApplicationUser> CreateAthlete(CreateUserRequest request)
        {
            var athlete = _mapper.Map <CreateUserRequest, Athlete>(request);
            var coach   = await _context.Coaches.FirstOrDefaultAsync(x => x.Id == request.CoachId);

            if (coach == null)
            {
                throw new NotFoundException(nameof(Coach), request.CoachId);
            }

            // map exercise type properties from coach to athlete
            athlete = ExerciseTagGroupsFactory.ApplyProperties <Athlete>(coach, athlete);

            await _context.Athletes.AddAsync(athlete);

            await _context.SaveChangesAsync();

            // send mail to complete registration
            try
            {
                athlete.Coach = coach;
                await _mediator.Send(new RegistrationEmailRequest(athlete));
            }
            catch (Exception e)
            {
                // log
                await _loggingService.LogInfo(e, "Mail not sent");
            }

            // return data
            return(_mapper.Map <ApplicationUser>(athlete));
        }
        private async Task <ApplicationUser> CreateSoloAthlete(CreateUserRequest request)
        {
            var soloAthlete = _mapper.Map <CreateUserRequest, SoloAthlete>(request);

            soloAthlete = ExerciseTagGroupsFactory.ApplyProperties <SoloAthlete>(soloAthlete);

            await _context.SoloAthletes.AddAsync(soloAthlete);

            await _context.SaveChangesAsync();

            // send mail to complete registration
            await _mediator.Send(new RegistrationEmailRequest(soloAthlete));

            // return data
            return(_mapper.Map <ApplicationUser>(soloAthlete));
        }
        private async Task <ApplicationUser> CreateCoach(CreateUserRequest request)
        {
            var coach = _mapper.Map <CreateUserRequest, Coach>(request);

            coach.CustomerId = await StripeConfiguration.AddCustomer(coach.GetFullName(), coach.Email); // add to stripe

            coach = ExerciseTagGroupsFactory.ApplyProperties <Coach>(coach);

            await _context.Coaches.AddAsync(coach);

            await _context.SaveChangesAsync();

            // send mail to complete registration
            await _mediator.Send(new RegistrationEmailRequest(coach));

            return(_mapper.Map <ApplicationUser>(coach));
        }
Exemplo n.º 5
0
        public async Task <ExternalLoginResponse> Handle(ExternalLoginRequest request, CancellationToken cancellationToken)
        {
            try
            {
                var user = await _context.Users.FirstOrDefaultAsync(x => x.Email == request.Email, cancellationToken);

                // TODO: Extract this
                if (user == null)
                {
                    var notificationSettings = EnumFactory.SeedEnum <NotificationType, NotificationSetting>((value, index) => new NotificationSetting()
                    {
                        Id = Guid.NewGuid(),
                        NotificationType = value,
                    }).ToList();

                    // call create user request..
                    var userId  = Guid.NewGuid();
                    var newUser = new ApplicationUser()
                    {
                        Id                   = userId,
                        AccountType          = request.AccountType,
                        Email                = request.Email,
                        FirstName            = request.FirstName,
                        LastName             = request.LastName,
                        ExternalLoginAccount = true,
                        UserSetting          = new UserSetting()
                        {
                            ApplicationUserId    = userId,
                            NotificationSettings = notificationSettings,
                        },
                    };

                    if (request.Avatar != null)
                    {
                        newUser.Avatar = request.Avatar;
                    }

                    newUser.CustomerId = await StripeConfiguration.AddCustomer(newUser.FullName, newUser.Email); // add to stripe

                    newUser = ExerciseTagGroupsFactory.ApplyProperties <ApplicationUser>(newUser);

                    await _context.Users.AddAsync(newUser, cancellationToken);

                    await _context.SaveChangesAsync(cancellationToken);

                    user = newUser;
                }
                else if (user.ExternalLoginAccount == false)
                {
                    throw new Exception("Can't external login user who exists but doesn't have external login enabled");
                }

                var token = _tokenGenerator.GenerateToken(user.Id);

                var userInfo = await _mediator.Send(new CurrentUserRequest(user.Id), cancellationToken);

                return(new ExternalLoginResponse(token, userInfo));
            }
            catch (Exception e)
            {
                throw new Exception(nameof(ExternalLoginRequest), e);
            }
        }