Merge branch 'feature/chat-new-api-peter' into feature/chat-new-api-eudes

This commit is contained in:
Eudes Inácio
2024-06-05 11:58:05 +01:00
7 changed files with 59 additions and 16 deletions
@@ -23,8 +23,8 @@ export class RoomRemoteDataSourceService {
return await this.httpService.post<RoomOutPutDTO>(`${this.baseUrl}/Room`, data);
}
async getRoomList(): Promise<Result<RoomListOutPutDTO ,any>> {
return await this.httpService.get<any>(`${this.baseUrl}/Room`);
async getRoomList(): Promise<DataSourceReturn<RoomListOutPutDTO>> {
return await this.httpService.get<RoomListOutPutDTO>(`${this.baseUrl}/Room`);
}
async getRoom(id: string): Promise<Result<any ,any>> {
@@ -24,10 +24,13 @@ export const roomDataSource = new Dexie('FriendDatabase') as Dexie & {
room: EntityTable<TableRoom, 'id'>;
};
roomDataSource.version(1).stores({
room: '++id, createdBy, roomName, roomType, expirationDate'
room: 'id, createdBy, roomName, roomType, expirationDate'
});
@Injectable({
providedIn: 'root'
})
@@ -38,10 +41,38 @@ export class RoomLocalDataSourceService {
constructor() {}
async createRoom(data: RoomOutPutDTO) {
async createRoom(data: TableRoom) {
try {
const result = await roomDataSource.room.add(data.data)
const result = await roomDataSource.room.add(data)
return ok(result)
} catch (e) {
return err(false)
}
}
async updateRoom(data: TableRoom) {
try {
const result = await roomDataSource.room.update(data.id, data);
return ok(result)
} catch (e) {
return err(false)
}
}
async createOrUpdateRoom(data: TableRoom) {
const createResult = await this.createRoom(data)
if(createResult.isOk()) {
return this.updateRoom(data)
} else {
return createResult
}
}
async getRoomById(id: any) {
try {
const result = await roomDataSource.room.get(id)
return ok(result)
} catch (e) {
return err(false)
@@ -15,8 +15,15 @@ const RoomListItemOutPutDTOSchema = z.object({
expirationDate: z.string().datetime(),
roomType: z.number()
});
// Define the schema for the entire response
const RoomListOutPutDTOSchema = z.object({
success: z.boolean(),
message: z.string(),
data: z.array(RoomListItemOutPutDTOSchema),
});
export type RoomListItemOutPutDTO = z.infer<typeof RoomListItemOutPutDTOSchema>
const RoomListOutPutDTOSchema = z.array(RoomListItemOutPutDTOSchema);
export type RoomListOutPutDTO = z.infer<typeof RoomListOutPutDTOSchema>
@@ -21,6 +21,12 @@ export class RoomRepositoryService {
async list() {
const result = await this.roomRemoteDataSourceService.getRoomList()
if(result.isOk()) {
for( const roomData of result.value.data) {
this.roomLocalDataSourceService.createOrUpdateRoom(roomData)
}
}
return result
}
@@ -35,7 +41,7 @@ export class RoomRepositoryService {
console.log('result', result)
this.roomMemoryDataSourceService.dispatch( addRoom(result.value) )
this.roomLocalDataSourceService.createRoom(result.value)
this.roomLocalDataSourceService.createRoom(result.value.data)
}
return result