Files
doneit-web/src/app/services/Repositorys/chat/data-source/room/rooom-local-data-source.service.ts
T

72 lines
1.7 KiB
TypeScript
Raw Normal View History

2024-06-04 16:21:11 +01:00
import { Injectable } from '@angular/core';
import { AddMemberToRoomInputDTO } from '../../dto/room/addMemberToRoomInputDto';
2024-06-05 10:28:38 +01:00
import { RoomListItemOutPutDTO, RoomListOutPutDTO } from '../../dto/room/roomListOutputDTO';
2024-06-04 16:21:11 +01:00
import { Dexie, EntityTable, liveQuery } from 'Dexie';
import { err, ok } from 'neverthrow';
import { Observable } from 'rxjs';
import { RoomOutPutDTO } from '../../dto/room/roomOutputDTO';
import { z } from 'zod';
const tableSchema = z.object({
id: z.string(),
roomName: z.string(),
createdBy: z.any(),
createdAt: z.any(),
expirationDate: z.any(),
roomType: z.any()
})
2024-06-04 16:26:50 +01:00
export type TableRoom = z.infer<typeof tableSchema>
2024-06-04 16:21:11 +01:00
// Database declaration (move this to its own module also)
export const roomDataSource = new Dexie('FriendDatabase') as Dexie & {
2024-06-04 16:26:50 +01:00
room: EntityTable<TableRoom, 'id'>;
2024-06-04 16:21:11 +01:00
};
roomDataSource.version(1).stores({
room: '++id, createdBy, roomName, roomType, expirationDate'
});
2024-06-05 11:09:03 +01:00
interface CreateRoomParams {
id?: string;
roomName?: string;
createdBy?: any;
createdAt?: Date;
expirationDate?: Date;
roomType?: any;
}
2024-06-04 16:21:11 +01:00
@Injectable({
providedIn: 'root'
})
export class RoomLocalDataSourceService {
private baseUrl = 'https://gdapi-dev.dyndns.info/stage/api/v2/Chat'; // Your base URL
constructor() {}
2024-06-05 11:09:03 +01:00
async createRoom(data: CreateRoomParams) {
2024-06-04 16:21:11 +01:00
try {
2024-06-05 11:09:03 +01:00
const result = await roomDataSource.room.add(data)
2024-06-04 16:21:11 +01:00
return ok(result)
} catch (e) {
return err(false)
}
}
getItemsLive(): Observable<RoomListOutPutDTO[]> {
return liveQuery(() => roomDataSource.room.toArray()) as any;
}
2024-06-05 10:28:38 +01:00
getRoomByIdLive(id: any): Observable<RoomListItemOutPutDTO | undefined> {
return liveQuery(() => roomDataSource.room.get(id)) as any;
}
2024-06-04 16:21:11 +01:00
}