show member toroom

This commit is contained in:
Peter Maquiran
2024-06-07 12:29:53 +01:00
parent 486e0e8e72
commit c9b4f2d349
8 changed files with 152 additions and 14 deletions
@@ -7,6 +7,9 @@ import { RoomOutPutDTO } from '../../dto/room/roomOutputDTO';
import { AddMemberToRoomInputDTO, AddMemberToRoomInputDTOSchema } from '../../dto/room/addMemberToRoomInputDto';
import { ValidateSchema } from 'src/app/services/decorators/validate-schema.decorator';
import { DataSourceReturn } from '../../../type';
import { RoomByIdInputDTO, RoomByIdInputDTOSchema } from '../../dto/room/roomByIdInputDTO';
import { RoomByIdOutputDTO, RoomByIdOutputDTOSchema } from '../../dto/room/roomByIdOutputDTO';
import { APIReturn } from 'src/app/services/decorators/api-validate-schema.decorator';
@Injectable({
providedIn: 'root'
@@ -27,8 +30,10 @@ export class RoomRemoteDataSourceService {
return await this.httpService.get<RoomListOutPutDTO>(`${this.baseUrl}/Room`);
}
async getRoom(id: string): Promise<Result<any ,any>> {
return await this.httpService.get<any>(`${this.baseUrl}/Room/${id}`);
@ValidateSchema(RoomByIdInputDTOSchema)
@APIReturn(RoomByIdOutputDTOSchema)
async getRoom(id: RoomByIdInputDTO): DataSourceReturn<RoomByIdOutputDTO> {
return await this.httpService.get(`${this.baseUrl}/Room/${id}`);
}
async updateRoom(id: string, room: any): Promise<Result<any ,any>> {
@@ -1,12 +1,10 @@
import { Injectable } from '@angular/core';
import { AddMemberToRoomInputDTO } from '../../dto/room/addMemberToRoomInputDto';
import { RoomListItemOutPutDTO, 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 { UserList } from '../../../contacts/data-source/contacts-data-source.service';
const tableSchema = z.object({
@@ -17,20 +15,34 @@ const tableSchema = z.object({
expirationDate: z.any(),
roomType: z.any()
})
const TableMemberListSchema = z.object({
$roomIdUserId: z.string().optional(),
id: z.string(),
roomId: z.string(),
user: z.object({
wxUserId: z.number(),
wxFullName: z.string(),
wxeMail: z.string(),
userPhoto: z.string().nullable(),
}),
joinAt: z.string(),
})
export type TableRoom = z.infer<typeof tableSchema>
export type TableMemberList = z.infer<typeof TableMemberListSchema>
// Database declaration (move this to its own module also)
export const roomDataSource = new Dexie('FriendDatabase') as Dexie & {
room: EntityTable<TableRoom, 'id'>;
memberList: EntityTable<TableMemberList, '$roomIdUserId'>;
};
roomDataSource.version(1).stores({
room: 'id, createdBy, roomName, roomType, expirationDate'
room: 'id, createdBy, roomName, roomType, expirationDate',
memberList: '$roomIdUserId, id, user, joinAt, roomId',
});
@Injectable({
providedIn: 'root'
})
@@ -40,7 +52,6 @@ export class RoomLocalDataSourceService {
constructor() {}
async createRoom(data: TableRoom) {
try {
const result = await roomDataSource.room.add(data)
@@ -70,6 +81,17 @@ export class RoomLocalDataSourceService {
}
}
async addMember(data: TableMemberList) {
try {
data.$roomIdUserId = data.roomId + data.user.wxUserId
const result = await roomDataSource.memberList.add(data)
return ok(result)
} catch (e) {
return err(false)
}
}
async getRoomById(id: any) {
try {
const result = await roomDataSource.room.get(id)
@@ -77,7 +99,6 @@ export class RoomLocalDataSourceService {
} catch (e) {
return err(false)
}
}
getItemsLive(): Observable<RoomListOutPutDTO[]> {
@@ -88,5 +109,9 @@ export class RoomLocalDataSourceService {
return liveQuery(() => roomDataSource.room.get(id)) as any;
}
getRoomMemberByIdLive(roomId: any): Observable<TableMemberList[] | undefined> {
return liveQuery(() => roomDataSource.memberList.where('roomId').equals(roomId).toArray()) as any;
}
}