Beispiel #1
0
        public async Task UpdateProfileAsync(long id, GoblinIdentityUpdateProfileModel model,
                                             CancellationToken cancellationToken = default)
        {
            var userEntity = await _userRepo.Get(x => x.Id == id)
                             .FirstOrDefaultAsync(cancellationToken)
                             .ConfigureAwait(true);

            if (userEntity == null)
            {
                throw new GoblinException(nameof(GoblinIdentityErrorCode.UserNotFound),
                                          GoblinIdentityErrorCode.UserNotFound);
            }

            using var transaction =
                      await GoblinUnitOfWork.BeginTransactionAsync(cancellationToken).ConfigureAwait(true);

            if (model.IsUpdateRoles)
            {
                _userRoleRepo.DeleteWhere(x => x.UserId == userEntity.Id);

                await GoblinUnitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(true);

                // User Roles

                if (model.Roles?.Any() == true)
                {
                    model.Roles = model.Roles.Select(x => x.Trim()).ToList();

                    var roleEntities = await _roleRepo.Get(x => model.Roles.Contains(x.Name))
                                       .ToListAsync(cancellationToken).ConfigureAwait(true);

                    foreach (var roleEntity in roleEntities)
                    {
                        _userRoleRepo.Add(new UserRoleEntity
                        {
                            UserId = userEntity.Id,
                            RoleId = roleEntity.Id
                        });
                    }
                }
            }

            model.MapTo(userEntity);

            _userRepo.Update(userEntity,
                             x => x.AvatarUrl,
                             x => x.FullName,
                             x => x.Bio,
                             x => x.GithubId,
                             x => x.SkypeId,
                             x => x.FacebookId,
                             x => x.WebsiteUrl,
                             x => x.CompanyName,
                             x => x.CompanyUrl
                             );

            await GoblinUnitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(true);

            transaction.Commit();
        }
Beispiel #2
0
        public virtual async Task CrawlPostsAsync(CancellationToken cancellationToken = default)
        {
            var startTime = GoblinDateTimeHelper.SystemTimeNow;

            var sourceEntity = await GetSourceEntity(cancellationToken);

            var crawledPostUrl = await CrawlPostUrlAsync(cancellationToken).ConfigureAwait(true);

            var postUrls = crawledPostUrl.TakeWhile(url => url != sourceEntity.LastCrawledPostUrl).ToList();

            var postsMetadata = await GoblinCrawlerHelper.GetListMetadataModelsAsync(postUrls).ConfigureAwait(true);

            using var transaction =
                      await GoblinUnitOfWork.BeginTransactionAsync(cancellationToken).ConfigureAwait(true);

            // Posts Metadata to Post Crawled Database

            await GoblinCrawlerHelper.SavePostEntitiesAsync(Domain, postsMetadata, startTime, _postRepo, GoblinUnitOfWork).ConfigureAwait(true);

            // Update Source

            sourceEntity.LastCrawlStartTime       = startTime;
            sourceEntity.LastCrawlEndTime         = GoblinDateTimeHelper.SystemTimeNow;
            sourceEntity.TimeSpent                = sourceEntity.LastCrawlEndTime.Subtract(sourceEntity.LastCrawlStartTime);
            sourceEntity.TotalPostCrawledLastTime = postsMetadata.Count;
            sourceEntity.TotalPostCrawled        += postsMetadata.Count;

            if (!string.IsNullOrWhiteSpace(postsMetadata.FirstOrDefault()?.OriginalUrl))
            {
                sourceEntity.LastCrawledPostUrl = postsMetadata.FirstOrDefault()?.OriginalUrl;
            }

            _sourceRepo.Update(sourceEntity,
                               x => x.LastCrawlStartTime,
                               x => x.LastCrawlEndTime,
                               x => x.TimeSpent,
                               x => x.TotalPostCrawledLastTime,
                               x => x.TotalPostCrawled,
                               x => x.LastCrawledPostUrl
                               );

            await GoblinUnitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(true);

            transaction.Commit();
        }
Beispiel #3
0
        public async Task <GoblinIdentityRoleModel> UpsertAsync(GoblinIdentityUpsertRoleModel model,
                                                                CancellationToken cancellationToken = default)
        {
            using var transaction =
                      await GoblinUnitOfWork.BeginTransactionAsync(cancellationToken).ConfigureAwait(true);

            // Handle Role

            var roleEntity = await _roleRepo
                             .Get(x => x.Name == model.Name)
                             .FirstOrDefaultAsync(cancellationToken)
                             .ConfigureAwait(true);

            if (roleEntity == null)
            {
                roleEntity = new RoleEntity
                {
                    Name = model.Name
                };

                _roleRepo.Add(roleEntity);
            }

            // Save Change
            await GoblinUnitOfWork.SaveChangesAsync(cancellationToken);

            // Handle Permission

            if (model.Permissions?.Any() == true)
            {
                model.Permissions = model.Permissions.Select(x => x.Trim()).ToList();

                var existsPermissionEntities =
                    await _permissionRepo
                    .Get(x => model.Permissions.Contains(x.Name))
                    .ToListAsync(cancellationToken).ConfigureAwait(true);

                var existsPermissions = existsPermissionEntities.Select(x => x.Name).ToList();

                foreach (var permission in model.Permissions)
                {
                    if (!existsPermissions.Contains(permission))
                    {
                        var permissionEntity = new PermissionEntity
                        {
                            Name = permission
                        };

                        _permissionRepo.Add(permissionEntity);

                        existsPermissionEntities.Add(permissionEntity);
                    }
                }

                // Save Change
                await GoblinUnitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(true);

                // Relationship Role and Permission

                var rolePermissions = await _rolePermissionRepo.Get(x => x.RoleId == roleEntity.Id)
                                      .ToListAsync(cancellationToken)
                                      .ConfigureAwait(true);

                foreach (var permissionEntity in existsPermissionEntities)
                {
                    if (rolePermissions.Any(x => x.PermissionId == permissionEntity.Id))
                    {
                        continue;
                    }

                    _rolePermissionRepo.Add(new RolePermissionEntity
                    {
                        RoleId       = roleEntity.Id,
                        PermissionId = permissionEntity.Id
                    });
                }

                // Save Change
                await GoblinUnitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(true);
            }

            transaction.Commit();

            var roleModel = await GetAsync(roleEntity.Name, cancellationToken).ConfigureAwait(true);

            return(roleModel);
        }
Beispiel #4
0
        public async Task <GoblinIdentityEmailConfirmationModel> RegisterAsync(GoblinIdentityRegisterModel model,
                                                                               CancellationToken cancellationToken = default)
        {
            model.Email = model.Email?.Trim().ToLowerInvariant();

            model.UserName = model.UserName?.Trim().ToLowerInvariant();

            CheckUniqueEmail(model.Email);

            CheckUniqueUserName(model.UserName);

            using var transaction =
                      await GoblinUnitOfWork.BeginTransactionAsync(cancellationToken).ConfigureAwait(true);

            var userEntity = model.MapTo <UserEntity>();

            userEntity.PasswordLastUpdatedTime            =
                userEntity.RevokeTokenGeneratedBeforeTime = GoblinDateTimeHelper.SystemTimeNow;

            userEntity.PasswordHash =
                PasswordHelper.HashPassword(model.Password, userEntity.PasswordLastUpdatedTime);

            userEntity.EmailConfirmToken = StringHelper.Generate(6, false, false);

            userEntity.EmailConfirmTokenExpireTime =
                GoblinDateTimeHelper.SystemTimeNow.Add(SystemSetting.Current.EmailConfirmTokenLifetime);

            _userRepo.Add(userEntity);

            await GoblinUnitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(true);

            // User Roles

            if (model.Roles?.Any() == true)
            {
                model.Roles = model.Roles.Select(x => x.Trim()).ToList();

                var roleEntities = await _roleRepo.Get(x => model.Roles.Contains(x.Name)).ToListAsync(cancellationToken)
                                   .ConfigureAwait(true);

                foreach (var roleEntity in roleEntities)
                {
                    _userRoleRepo.Add(new UserRoleEntity
                    {
                        UserId = userEntity.Id,
                        RoleId = roleEntity.Id
                    });
                }

                await GoblinUnitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(true);
            }

            transaction.Commit();

            // Email Confirmation Code

            var emailConfirmationModel = new GoblinIdentityEmailConfirmationModel
            {
                Id = userEntity.Id,
                EmailConfirmToken           = userEntity.EmailConfirmToken,
                EmailConfirmTokenExpireTime = userEntity.EmailConfirmTokenExpireTime
            };

            return(emailConfirmationModel);
        }