|
@@ -6,15 +6,19 @@ import { Role } from './role.entity';
|
|
|
|
|
|
|
|
const SALT_ROUNDS = 10;
|
|
const SALT_ROUNDS = 10;
|
|
|
|
|
|
|
|
|
|
+import { FollowService } from '../follows/follow.service';
|
|
|
|
|
+
|
|
|
export class UserService {
|
|
export class UserService {
|
|
|
private userRepository: Repository<User>;
|
|
private userRepository: Repository<User>;
|
|
|
private roleRepository: Repository<Role>;
|
|
private roleRepository: Repository<Role>;
|
|
|
|
|
+ private followService: FollowService;
|
|
|
private readonly dataSource: DataSource;
|
|
private readonly dataSource: DataSource;
|
|
|
|
|
|
|
|
constructor(dataSource: DataSource) {
|
|
constructor(dataSource: DataSource) {
|
|
|
this.dataSource = dataSource;
|
|
this.dataSource = dataSource;
|
|
|
this.userRepository = this.dataSource.getRepository(User);
|
|
this.userRepository = this.dataSource.getRepository(User);
|
|
|
this.roleRepository = this.dataSource.getRepository(Role);
|
|
this.roleRepository = this.dataSource.getRepository(Role);
|
|
|
|
|
+ this.followService = new FollowService(dataSource);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
async createUser(userData: Partial<User>): Promise<User> {
|
|
async createUser(userData: Partial<User>): Promise<User> {
|
|
@@ -30,12 +34,19 @@ export class UserService {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- async getUserById(id: number): Promise<User | null> {
|
|
|
|
|
|
|
+ async getUserById(id: number, currentUserId?: number): Promise<User | null> {
|
|
|
try {
|
|
try {
|
|
|
- return await this.userRepository.findOne({
|
|
|
|
|
|
|
+ const user = await this.userRepository.findOne({
|
|
|
where: { id },
|
|
where: { id },
|
|
|
relations: ['roles']
|
|
relations: ['roles']
|
|
|
});
|
|
});
|
|
|
|
|
+
|
|
|
|
|
+ if (user && currentUserId && currentUserId !== id) {
|
|
|
|
|
+ const isFollowing = await this.followService.isFollowing(currentUserId, id);
|
|
|
|
|
+ return { ...user, isFollowing } as User;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return user ? { ...user, isFollowing: false } as User : null;
|
|
|
} catch (error) {
|
|
} catch (error) {
|
|
|
console.error('Error getting user:', error);
|
|
console.error('Error getting user:', error);
|
|
|
throw new Error('Failed to get user');
|
|
throw new Error('Failed to get user');
|
|
@@ -92,6 +103,7 @@ export class UserService {
|
|
|
page: number;
|
|
page: number;
|
|
|
pageSize: number;
|
|
pageSize: number;
|
|
|
keyword?: string;
|
|
keyword?: string;
|
|
|
|
|
+ currentUserId?: number;
|
|
|
}): Promise<[User[], number]> {
|
|
}): Promise<[User[], number]> {
|
|
|
try {
|
|
try {
|
|
|
const { page, pageSize, keyword } = params;
|
|
const { page, pageSize, keyword } = params;
|
|
@@ -110,7 +122,33 @@ export class UserService {
|
|
|
);
|
|
);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- return await queryBuilder.getManyAndCount();
|
|
|
|
|
|
|
+ const [users, total] = await queryBuilder.getManyAndCount();
|
|
|
|
|
+
|
|
|
|
|
+ // 如果提供了当前用户ID,添加关注状态
|
|
|
|
|
+ const { currentUserId } = params;
|
|
|
|
|
+ if (currentUserId) {
|
|
|
|
|
+ const usersWithFollowStatus = await Promise.all(
|
|
|
|
|
+ users.map(async (user) => {
|
|
|
|
|
+ if (user.id === currentUserId) {
|
|
|
|
|
+ return { ...user, isFollowing: false };
|
|
|
|
|
+ }
|
|
|
|
|
+ const isFollowing = await this.followService.isFollowing(
|
|
|
|
|
+ currentUserId,
|
|
|
|
|
+ user.id
|
|
|
|
|
+ );
|
|
|
|
|
+ return { ...user, isFollowing };
|
|
|
|
|
+ })
|
|
|
|
|
+ ) as User[];
|
|
|
|
|
+ return [usersWithFollowStatus, total];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 默认添加isFollowing: false
|
|
|
|
|
+ const usersWithFollowStatus = users.map(user => ({
|
|
|
|
|
+ ...user,
|
|
|
|
|
+ isFollowing: false
|
|
|
|
|
+ })) as User[];
|
|
|
|
|
+
|
|
|
|
|
+ return [usersWithFollowStatus, total];
|
|
|
} catch (error) {
|
|
} catch (error) {
|
|
|
console.error('Error getting users with pagination:', error);
|
|
console.error('Error getting users with pagination:', error);
|
|
|
throw new Error('Failed to get users');
|
|
throw new Error('Failed to get users');
|