コード例 #1
0
        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));
        }
コード例 #2
0
        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));
        }
コード例 #3
0
        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));
        }
コード例 #4
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);
            }
        }