Example #1
0
        public async Task <AOResult <AuthorizationTokenResponse> > RegisterAsync(RegistrationRequest request)
        {
            return(await BaseInvokeAsync(async() =>
            {
                if (_myHelperDbContext.AppUsers.Any(x => x.Email == request.Email || x.Username == request.UserName))
                {
                    return AOBuilder.SetError <AuthorizationTokenResponse>(Constants.Errors.UserAlreadyRegistered);
                }

                var appUser = new AppUser
                {
                    Username = request.UserName,
                    Email = request.Email,
                    Password = HashPasswordHelper.Hash(request.Password),
                    UserRole = EUserRole.User,
                    CreatedDate = DateTime.Now
                };

                await _myHelperDbContext.AddAsync(appUser);
                await _myHelperDbContext.SaveChangesAsync();

                var tokenInfo = _tokenService.CreateToken(GetClaimsFromAppUser(appUser));

                var authorizationTokenResponse = new AuthorizationTokenResponse()
                {
                    Token = tokenInfo.Token,
                    ExpirationDate = tokenInfo.ExpiredDate,
                    AppUserViewModel = _mapper.Map <AppUser, AppUserViewModel>(appUser)
                };

                return AOBuilder.SetSuccess(authorizationTokenResponse);
            }, request));
        }
Example #2
0
        public void SeedDb()
        {
            if (_hostingEnvironment.IsEnvironment(Constants.HostEnvironment.Docker))
            {
                _myHelperDbContext.Database.EnsureCreated();
            }

            using (var transaction = _myHelperDbContext.Database.BeginTransaction())
            {
                try
                {
                    if (!_myHelperDbContext.AppUsers.Any(x => x.UserRole == EUserRole.Admin))
                    {
                        var user = new AppUser
                        {
                            Email       = "*****@*****.**",
                            Username    = "******",
                            Password    = HashPasswordHelper.Hash("admin"),
                            UserRole    = EUserRole.Admin,
                            CreatedDate = DateTime.Now
                        };

                        _myHelperDbContext.AppUsers.Add(user);
                        _myHelperDbContext.SaveChanges();
                    }

                    if (!_myHelperDbContext.Tags.Any() && !_myHelperDbContext.Notes.Any() && !_myHelperDbContext.NoteTags.Any())
                    {
                        var note = new Note
                        {
                            Name        = "Ef Core Migrations",
                            Description = "1. dotnet ef migrations add name 2. dotnet ef database update",
                            CreateDate  = DateTime.Now,
                            UpdateDate  = DateTime.Now,
                            AppUser     = _myHelperDbContext.AppUsers.FirstOrDefault()
                        };

                        var tags = new Tag[]
                        {
                            new Tag {
                                Name = "entity framework"
                            },
                            new Tag {
                                Name = "migrations"
                            }
                        };

                        _myHelperDbContext.AddRange(
                            new NoteTag {
                            Note = note, Tag = tags[0]
                        },
                            new NoteTag {
                            Note = note, Tag = tags[1]
                        }
                            );
                        _myHelperDbContext.SaveChanges();
                    }

                    transaction.Commit();
                }
                catch
                {
                    transaction.Rollback();
                }
            }
        }