Example #1
0
            public async Task <UserReturnObject> Handle(Command request, CancellationToken cancellationToken)
            {
                if (await _context.Users.AnyAsync(x => x.UserName == request.Username))
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { username = "******" });
                }

                if (await _context.Users.AnyAsync(x => x.Email == request.Email))
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { email = "Email already in use" });
                }

                var user = new ApplicationUser
                {
                    Email                = request.Email,
                    Gender               = request.Gender,
                    UserName             = request.Username,
                    GravatarProfileImage = GravatarHelper.GetGravatarUri(request.Email, GravatarHelper.GravatarMissingImage.MonsterId, 90, false).ToString(),
                    CreatedAt            = DateTime.Now
                };

                var result = await _userManager.CreateAsync(user, request.Password);

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

                if (result.Succeeded)
                {
                    return(new UserReturnObject
                    {
                        GravatarProfileImage = user.GravatarProfileImage,
                        Username = user.UserName,
                        Token = _jwtGenerator.GenerateToken(user),
                        Email = user.Email
                    });
                }

                throw new Exception("Problem saving changes");
            }