Skip to content

Commit

Permalink
feat: added connections service #78
Browse files Browse the repository at this point in the history
  • Loading branch information
rbento1096 committed Feb 20, 2024
1 parent 577994c commit b55b8d1
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions front-end/src/app/tabs/connections.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { Injectable } from '@angular/core';
import { IDEAApiService } from '@idea-ionic/common';

import { Connection } from '@models/connection.model';

@Injectable({ providedIn: 'root' })
export class ConnectionsService {
private connections: Connection[];

/**
* The number of connections to consider for the pagination, when active.
*/
MAX_PAGE_SIZE = 24;

constructor(private api: IDEAApiService) {}

private async loadList(): Promise<void> {
this.connections = (await this.api.getResource(['connections'])).map(c => new Connection(c));
}

/**
* Get (and optionally filter) the list of connections.
* Note: it can be paginated.
* Note: it's a slice of the array.
*/
async getList(options: {
force?: boolean;
withPagination?: boolean;
pending?: boolean;
startPaginationAfterId?: string;
}): Promise<Connection[]> {
if (!this.connections || options.force) await this.loadList();
if (!this.connections) return null;

let filteredList = this.connections.slice();

filteredList = filteredList.filter(c => (options.pending ? c.isPending : !c.isPending));

if (options.withPagination && filteredList.length > this.MAX_PAGE_SIZE) {
let indexOfLastOfPreviousPage = 0;
if (options.startPaginationAfterId)
indexOfLastOfPreviousPage = filteredList.findIndex(x => x.connectionId === options.startPaginationAfterId) || 0;
filteredList = filteredList.slice(0, indexOfLastOfPreviousPage + this.MAX_PAGE_SIZE);
}

return filteredList;
}

/**
* Insert a new connection.
*/
async insert(userId: string): Promise<Connection> {
return new Connection(await this.api.postResource(['connections'], { body: { userId } }));
}
/**
* Delete a connection.
*/
async delete(connection: Connection): Promise<void> {
await this.api.deleteResource(['connections', connection.connectionId]);
}
}

0 comments on commit b55b8d1

Please sign in to comment.