mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-19 04:57:52 +00:00
add repository patter for chat
This commit is contained in:
@@ -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`);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
import { z } from "zod";
|
||||
|
||||
const OutPutDTOSchema = z.object({
|
||||
success: z.string(),
|
||||
message: z.string(),
|
||||
data: z.any()
|
||||
});
|
||||
|
||||
export type OutPutDTO = z.infer<typeof OutPutDTOSchema>
|
||||
@@ -0,0 +1,11 @@
|
||||
import { z } from "zod";
|
||||
|
||||
export const RoomListInputDTOSchema = z.object({
|
||||
roomName: z.string(),
|
||||
createdBy: z.number(),
|
||||
roomType: z.number(),
|
||||
expirationDate: z.string().datetime().nullable()
|
||||
});
|
||||
|
||||
|
||||
export type RoomListInputDTO = z.infer<typeof RoomListInputDTOSchema>
|
||||
@@ -0,0 +1,21 @@
|
||||
import { z } from "zod";
|
||||
|
||||
const CreatedBySchema = z.object({
|
||||
wxUserId: z.number(),
|
||||
wxFullName: z.string(),
|
||||
wxeMail: z.string().email(),
|
||||
userPhoto: z.string()
|
||||
});
|
||||
|
||||
const RoomListOutPutDTOSchema = z.object({
|
||||
id: z.string(),
|
||||
roomName: z.string(),
|
||||
createdBy: CreatedBySchema,
|
||||
createdAt: z.string().datetime(),
|
||||
expirationDate: z.string().datetime(),
|
||||
roomType: z.number()
|
||||
});
|
||||
|
||||
|
||||
|
||||
export type RoomListOutPutDTO = z.infer<typeof RoomListOutPutDTOSchema>
|
||||
@@ -1,21 +1,9 @@
|
||||
import { z } from "zod";
|
||||
|
||||
const CreatedBySchema = z.object({
|
||||
wxUserId: z.number(),
|
||||
wxFullName: z.string(),
|
||||
wxeMail: z.string().email(),
|
||||
userPhoto: z.string()
|
||||
const RoomOutPutDTOSchema = z.object({
|
||||
success: z.string(),
|
||||
message: z.string(),
|
||||
data: z.any()
|
||||
});
|
||||
|
||||
const RoomListOutPutDTOSchema = z.object({
|
||||
id: z.string(),
|
||||
roomName: z.string(),
|
||||
createdBy: CreatedBySchema,
|
||||
createdAt: z.string().datetime(),
|
||||
expirationDate: z.string().datetime(),
|
||||
roomType: z.number()
|
||||
});
|
||||
|
||||
|
||||
|
||||
export type RoomListOutPutDTO = z.infer<typeof RoomListOutPutDTOSchema>
|
||||
export type RoomOutPutDTO = z.infer<typeof RoomOutPutDTOSchema>
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { RoomRemoteDataSourceService } from '../data-source/room-remote-data-source.service'
|
||||
import { RoomListInputDTO, RoomListInputDTOSchema } from '../dto/roomInputDTO';
|
||||
import { IRepositoryReturn } from '../../type';
|
||||
import { ValidateSchema } from 'src/app/services/decorators/validate-schema.decorator';
|
||||
import { addRoom, RoomRemoteDataSourceState } from '../data-source/room-memory-data-source';
|
||||
import { Store } from '@ngrx/store';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class RoomRepositoryService {
|
||||
|
||||
constructor(
|
||||
private roomRemoteDataSourceService: RoomRemoteDataSourceService,
|
||||
private roomMemoryDataSourceService: Store<RoomRemoteDataSourceState>
|
||||
) { }
|
||||
|
||||
list() {
|
||||
this.roomRemoteDataSourceService.getRoomList()
|
||||
}
|
||||
|
||||
@ValidateSchema(RoomListInputDTOSchema)
|
||||
async create(data: RoomListInputDTO): IRepositoryReturn<any> {
|
||||
const result = await this.roomRemoteDataSourceService.createRoom(data)
|
||||
|
||||
if(result.isOk()) {
|
||||
this.roomMemoryDataSourceService.dispatch(addRoom({
|
||||
text: '',
|
||||
timestamp: 0,
|
||||
roomName: data.roomName
|
||||
}))
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { RoomRemoteDataSourceService } from '../data-source/room-remote-data-source.service'
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class RoomRepositoryService {
|
||||
|
||||
constructor(
|
||||
private roomRemoteDataSourceService: RoomRemoteDataSourceService
|
||||
) { }
|
||||
|
||||
list() {
|
||||
this.roomRemoteDataSourceService.getRoomList()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user