add generic repository

This commit is contained in:
Peter Maquiran
2024-08-07 16:31:31 +01:00
parent b61b87ce6c
commit 75a8822e57
6 changed files with 59 additions and 59 deletions
@@ -21,7 +21,7 @@ const TableMemberListSchema = z.object({
export type ITableMemberList = z.infer<typeof TableMemberListSchema>
type ITableMemberListSchema = EntityTable<ITableMemberList, '$roomIdUserId'>
type ITableMemberListSchema = EntityTable<ITableMemberList, '$roomIdUserId'>
// Database declaration (move this to its own module also)
export const roomMemberList = new Dexie('roomMemberList') as Dexie & {
memberList: EntityTable<ITableMemberList, '$roomIdUserId'>;
@@ -34,7 +34,7 @@ roomMemberList.version(1).stores({
@Injectable({
providedIn: 'root'
})
export class MemberListLocalDataSourceService extends DexieRepository<ITableMemberListSchema, ITableMemberList> {
export class MemberListLocalDataSourceService extends DexieRepository<ITableMemberList> {
constructor() {
super(roomMemberList.memberList);
@@ -6,6 +6,7 @@ import { from, Observable, Subject } from 'rxjs';
import { filter } from 'rxjs/operators';
import { MessageInputDTO } from '../../dto/message/messageInputDtO';
import { MessageEntity } from '../../../domain/entity/message';
import { DexieRepository } from 'src/app/infra/repository/dexie/dexie-repository.service';
const tableSchema = z.object({
@@ -67,12 +68,12 @@ messageDataSource.message.mapToClass(MessageEntity);
@Injectable({
providedIn: 'root'
})
export class MessageLocalDataSourceService {
export class MessageLocalDataSourceService extends DexieRepository<TableMessage> {
messageSubject = new Subject();
constructor() {
super(messageDataSource.message)
// messageDataSource.message.hook('creating', (primKey, obj, trans) => {
// // const newMessage = await trans.table('message').get(primKey);
// this.messageSubject.next(obj);
@@ -154,6 +155,17 @@ export class MessageLocalDataSourceService {
}
async createManyMessage(data: MessageInputDTO[]) {
try {
const result = await messageDataSource.message.bulkAdd(data)
this.messageSubject.next({roomId: data[0].roomId});
return ok(result)
} catch (e) {
return err(false)
}
}
async messageExist({id}) {
try {
@@ -177,19 +189,6 @@ export class MessageLocalDataSourceService {
}
async update(data: TableMessage ) {
try {
console.log('update images 22222')
const result = await messageDataSource.message.update(data.$id, data)
return ok(result)
} catch (e) {
return err(false)
}
}
// not used
async updateByMessageId(data: TableMessage ) {
@@ -207,7 +206,7 @@ export class MessageLocalDataSourceService {
const findResult = await this.findMessageById(data.id)
if(findResult.isOk()) {
return this.update({...findResult.value, ...data})
return this.update(findResult.value.$id, data)
} else {
return this.createMessage(data)
}
@@ -61,9 +61,9 @@ export class MessageRepositoryService {
$id : localActionResult.value
}
return this.messageLocalDataSourceService.update({...clone, sending: false, roomId: entity.roomId})
return this.messageLocalDataSourceService.update(localActionResult.value, {...clone, sending: false, roomId: entity.roomId})
} else {
await this.messageLocalDataSourceService.update({sending: false, $id: localActionResult.value})
await this.messageLocalDataSourceService.update(localActionResult.value, {sending: false, $id: localActionResult.value})
return err('no connection')
}
@@ -123,8 +123,9 @@ export class MessageRepositoryService {
for(const message of addedItems) {
let clone: TableMessage = message
clone.roomId = id
await this.messageLocalDataSourceService.createMessage(clone)
}
await this.messageLocalDataSourceService.createManyMessage(addedItems.reverse())
}
return result
}
@@ -34,7 +34,7 @@ export class SyncMessageRepositoryService {
const requestId = InstanceId +'@'+ uuidv4();
const DTO = MessageMapper.fromDomain(message, requestId)
await this.messageLocalDataSourceService.update({sending: true, $id: message.$id})
await this.messageLocalDataSourceService.update(message.$id, { sending: true })
const sendMessageResult = await this.messageLiveSignalRDataSourceService.sendMessage<MessageOutPutDataDTO>(DTO)
if(sendMessageResult.isOk()) {
@@ -50,9 +50,9 @@ export class SyncMessageRepositoryService {
$id : message.$id
}
this.messageLocalDataSourceService.update({...clone, sending: false, roomId: message.roomId})
this.messageLocalDataSourceService.update(message.$id, {...clone, sending: false, roomId: message.roomId})
} else {
this.messageLocalDataSourceService.update({sending: false, $id: message.$id})
this.messageLocalDataSourceService.update(message.$id, {sending: false})
}
}