2022-03-29 22:17:08 -04:00
|
|
|
import { Injectable } from '@nestjs/common';
|
|
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
|
|
import { Repository } from 'typeorm';
|
|
|
|
import { ChatRoom } from 'server/entities/chat_room.entity';
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class ChatRoomService {
|
|
|
|
constructor(
|
|
|
|
@InjectRepository(ChatRoom)
|
|
|
|
private chatRoomRepository: Repository<ChatRoom>,
|
|
|
|
) {}
|
|
|
|
|
|
|
|
create(chatRoom: ChatRoom) {
|
|
|
|
return this.chatRoomRepository.save(chatRoom);
|
|
|
|
}
|
|
|
|
|
|
|
|
all() {
|
|
|
|
return this.chatRoomRepository.find();
|
|
|
|
}
|
|
|
|
|
2022-03-31 00:15:20 -04:00
|
|
|
nearOrUserOwns({ lat, lng, userId }: { lat: number; lng: number; userId: number }) {
|
|
|
|
// SQL injection maybe?
|
2022-03-30 17:18:16 -04:00
|
|
|
return this.chatRoomRepository.query(
|
2022-03-31 00:15:20 -04:00
|
|
|
`SELECT * FROM chat_room WHERE calculate_distance(latitude, longitude, ${lat}, ${lng}, 'M') < 5 OR "userId" = ${userId}`,
|
2022-03-30 17:18:16 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
findById(id: number, relations: string[] = []) {
|
|
|
|
return this.chatRoomRepository.findOne(id, { relations });
|
|
|
|
}
|
|
|
|
|
|
|
|
save(chatRoom: ChatRoom) {
|
|
|
|
return this.chatRoomRepository.save(chatRoom);
|
|
|
|
}
|
|
|
|
|
|
|
|
remove(chatRoom: ChatRoom) {
|
|
|
|
return this.chatRoomRepository.remove(chatRoom);
|
2022-03-29 22:17:08 -04:00
|
|
|
}
|
|
|
|
}
|