add repository patter for chat

This commit is contained in:
Peter Maquiran
2024-06-04 09:31:37 +01:00
parent f7d8059f45
commit c057606852
24 changed files with 416 additions and 257 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)
);
@@ -1,16 +0,0 @@
import { TestBed } from '@angular/core/testing';
import { RoomRemoteDataSourceService } from './room-remote-data-source.service';
describe('RoomRemoteDataSourceService', () => {
let service: RoomRemoteDataSourceService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(RoomRemoteDataSourceService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});
@@ -1,15 +1,16 @@
import { Injectable } from '@angular/core';
import { ok, err, Result } from 'neverthrow';
import { HttpService } from 'src/app/services/http.service';
import { RoomListOutPutDTO } from '../dto/roomOutputDTO';
import { RoomListOutPutDTO } from '../dto/roomListOutputDTO';
import { RoomListInputDTO } from '../dto/roomInputDTO';
import { RoomOutPutDTO } from '../dto/roomOutputDTO';
@Injectable({
providedIn: 'root'
})
export class RoomRemoteDataSourceService {
private baseUrl = 'https://gdapi-dev.dyndns.info/stage/api/v2'; // Your base URL
private baseUrl = 'https://gdapi-dev.dyndns.info/stage/api/v2/Chat'; // Your base URL
constructor(private httpService: HttpService) {}
@@ -21,11 +22,10 @@ export class RoomRemoteDataSourceService {
return await this.httpService.post<any>(`${this.baseUrl}/Messages/${id}/React`, reaction);
}
async createRoom(room: any): Promise<Result<any ,any>> {
return await this.httpService.post<any>(`${this.baseUrl}/Room`, room);
async createRoom(data: RoomListInputDTO): Promise<Result<RoomOutPutDTO ,any>> {
return await this.httpService.post<any>(`${this.baseUrl}/Room`, data);
}
async getRoomList(): Promise<Result<RoomListOutPutDTO ,any>> {
return await this.httpService.get<any>(`${this.baseUrl}/Room`);
}