mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-18 20:47:54 +00:00
64 lines
1.6 KiB
TypeScript
64 lines
1.6 KiB
TypeScript
|
|
import { Injectable } from '@angular/core';
|
||
|
|
import { AddMemberToRoomInputDTO } from '../../dto/room/addMemberToRoomInputDto';
|
||
|
|
import { RoomListOutPutDTO } from '../../dto/room/roomListOutputDTO';
|
||
|
|
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';
|
||
|
|
import { MessageInputDTO } from '../../dto/message/messageInputDtO';
|
||
|
|
|
||
|
|
|
||
|
|
const tableSchema = z.object({
|
||
|
|
id: z.any().optional(),
|
||
|
|
roomId: z.string().uuid(),
|
||
|
|
senderId: z.number(),
|
||
|
|
message: z.string(),
|
||
|
|
messageType: z.number(),
|
||
|
|
canEdit: z.boolean(),
|
||
|
|
oneShot: z.boolean(),
|
||
|
|
requireUnlock: z.boolean(),
|
||
|
|
})
|
||
|
|
|
||
|
|
export type TableMessage = z.infer<typeof tableSchema>
|
||
|
|
|
||
|
|
// Database declaration (move this to its own module also)
|
||
|
|
export const messageDataSource = new Dexie('chat-message') as Dexie & {
|
||
|
|
message: EntityTable<TableMessage, 'id'>;
|
||
|
|
};
|
||
|
|
|
||
|
|
messageDataSource.version(1).stores({
|
||
|
|
message: '++id, roomId, senderId, message, messageType, canEdit, oneShot, requireUnlock'
|
||
|
|
});
|
||
|
|
|
||
|
|
@Injectable({
|
||
|
|
providedIn: 'root'
|
||
|
|
})
|
||
|
|
export class MessageLocalDataSourceService {
|
||
|
|
|
||
|
|
private baseUrl = 'https://gdapi-dev.dyndns.info/stage/api/v2/Chat'; // Your base URL
|
||
|
|
|
||
|
|
constructor() {}
|
||
|
|
|
||
|
|
|
||
|
|
async createMessage(data: MessageInputDTO) {
|
||
|
|
|
||
|
|
try {
|
||
|
|
const result = await messageDataSource.message.add(data)
|
||
|
|
return ok(result)
|
||
|
|
} catch (e) {
|
||
|
|
return err(false)
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
getItemsLive(): Observable<RoomListOutPutDTO[]> {
|
||
|
|
return liveQuery(() => messageDataSource.message.toArray()) as any;
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
addIdToMessage() {}
|
||
|
|
|
||
|
|
}
|
||
|
|
|