create room

This commit is contained in:
Peter Maquiran
2024-06-04 16:21:11 +01:00
parent 4fb5bfc4d0
commit 541d12121e
23 changed files with 289 additions and 82 deletions
@@ -0,0 +1,68 @@
import { createAction, createFeatureSelector, createReducer, createSelector, on, props } from "@ngrx/store";
export interface Room {
roomName: string;
text: string;
timestamp: number;
}
export interface ChatRoom {
[roomId: string]: Room[];
}
export interface RoomRemoteDataSourceState {
chatRooms: ChatRoom;
}
export const initialState: RoomRemoteDataSourceState = {
chatRooms: {
// add more rooms as needed
}
};
export const addMessage = createAction(
'[Chat] Add Message',
props<{ roomId: string; message: any }>()
);
export const addRoom = createAction(
'[Chat] Add Room',
props<Room>()
);
const _chatReducer = createReducer(
initialState,
on(addMessage, (state, { roomId, message }) => ({
...state,
chatRooms: {
...state.chatRooms,
[roomId]: [...(state.chatRooms[roomId] || []), message]
}
})),
on(addRoom, (state, roomData: Room) => ({
...state,
chatRooms: {
...state.chatRooms,
[roomData.roomName]: roomData
}
}))
);
export function chatReducer(state, action) {
return _chatReducer(state, action);
}
// Create a feature selector
export const selectChatState = createFeatureSelector<RoomRemoteDataSourceState>('chat');
// Create a selector for a specific room's messages
export const selectMessagesByRoom = (roomId: string) => createSelector(
selectChatState,
(state: RoomRemoteDataSourceState) => state.chatRooms[roomId] || []
);
// Create a selector for the list of rooms
export const selectAllRooms = createSelector(
selectChatState,
(state: RoomRemoteDataSourceState) => Object.keys(state.chatRooms)
);
@@ -0,0 +1,51 @@
import { Injectable } from '@angular/core';
import { Result } from 'neverthrow';
import { HttpService } from 'src/app/services/http.service';
import { RoomListOutPutDTO } from '../../dto/room/roomListOutputDTO';
import { RoomInputDTO, RoomInputDTOSchema } from '../../dto/room/roomInputDTO';
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';
@Injectable({
providedIn: 'root'
})
export class RoomRemoteDataSourceService {
private baseUrl = 'https://gdapi-dev.dyndns.info/stage/api/v2/Chat'; // Your base URL
constructor(private httpService: HttpService) {}
@ValidateSchema(RoomInputDTOSchema)
async createRoom(data: RoomInputDTO): DataSourceReturn<RoomOutPutDTO> {
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 getRoom(id: string): Promise<Result<any ,any>> {
return await this.httpService.get<any>(`${this.baseUrl}/Room/${id}`);
}
async updateRoom(id: string, room: any): Promise<Result<any ,any>> {
return await this.httpService.put<any>(`${this.baseUrl}/Room/${id}`, room);
}
async deleteRoom(id: string): Promise<Result<any ,any>> {
return await this.httpService.delete<any>(`${this.baseUrl}/Room/${id}`);
}
@ValidateSchema(AddMemberToRoomInputDTOSchema)
async addMemberToRoom(data: AddMemberToRoomInputDTO): DataSourceReturn<AddMemberToRoomInputDTO> {
return await this.httpService.post<any>(`${this.baseUrl}/Room/${data.id}/Member`, data.member);
}
async removeMemberFromRoom(id: string, member: any): Promise<Result<any ,any>> {
return await this.httpService.delete<any>(`${this.baseUrl}/Room/${id}/Member`);
}
}
@@ -0,0 +1,94 @@
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';
const tableSchema = z.object({
id: z.string(),
roomName: z.string(),
createdBy: z.any(),
createdAt: z.any(),
expirationDate: z.any(),
roomType: z.any()
})
export type Table = z.infer<typeof tableSchema>
// Database declaration (move this to its own module also)
export const roomDataSource = new Dexie('FriendDatabase') as Dexie & {
room: EntityTable<Table, 'id'>;
};
roomDataSource.version(1).stores({
room: '++id, createdBy, roomName, roomType, expirationDate'
});
@Injectable({
providedIn: 'root'
})
export class RoomLocalDataSourceService {
private baseUrl = 'https://gdapi-dev.dyndns.info/stage/api/v2/Chat'; // Your base URL
constructor() {}
async createRoom(data: RoomOutPutDTO) {
const roomData = {
createdBy: {
wxUserId: 0,
wxFullName: "",
wxeMail: "",
userPhoto: ""
},
roomName: data.data.roomName,
id: data.data.id,
roomType: 21,
expirationDate: '',
}
try {
const result = await roomDataSource.room.add(roomData)
return ok(result)
} catch (e) {
return err(false)
}
}
async getRoomList() {
}
async getRoom(id: string){
}
async updateRoom(id: string, room: any) {
}
async deleteRoom(id: string){
}
async addMemberToRoom(data: AddMemberToRoomInputDTO) {
}
async removeMemberFromRoom(id: string, member: any){
}
getItemsLive(): Observable<RoomListOutPutDTO[]> {
return liveQuery(() => roomDataSource.room.toArray()) as any;
}
}