Skip to content

Commit

Permalink
feat(dao): add utility method for getting the result count
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Reed committed Jun 15, 2017
1 parent 355fa52 commit 2c548b5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
16 changes: 16 additions & 0 deletions src/dao/AbstractDAO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import {Filter} from '../api/Filter';
import {OnmsHTTPOptions} from '../api/OnmsHTTPOptions';
import {OnmsResult} from '../api/OnmsResult';

import {log, catDao} from '../api/Log';
import {Category} from 'typescript-logging';

/**
* Abstract data access layer
* @module AbstractDAO
Expand Down Expand Up @@ -34,6 +37,19 @@ export abstract class AbstractDAO<K, T> {
/** find all model objects given a filter */
public abstract find(filter?: Filter): Promise<T[]>;

/** extract the count or totalCount values from response data */
protected getCount(data: any) {
let count = 0;
if (data.count !== undefined) {
count = parseInt(data.count, 10);
} else if (data.totalCount !== undefined) {
count = parseInt(data.totalCount, 10);
} else {
log.warn('data is missing count and totalCount properties', catDao);
}
return count;
}

/** given an optional filter, generate an {@link OnmsHTTPOptions} object for DAO calls */
protected getOptions(filter?: Filter): OnmsHTTPOptions {
const ret = new OnmsHTTPOptions();
Expand Down
6 changes: 1 addition & 5 deletions src/dao/AlarmDAO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,7 @@ export class AlarmDAO extends AbstractDAO<number, OnmsAlarm> {
return this.http.get('rest/alarms', opts).then((result) => {
let data = result.data;

let count = 0;
if (data.totalCount) {
count = parseInt(data.totalCount, 10);
}
if (count > 0 && data.alarm) {
if (this.getCount(data) > 0 && data.alarm) {
data = data.alarm;
} else {
data = [];
Expand Down
9 changes: 1 addition & 8 deletions src/dao/EventDAO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,7 @@ export class EventDAO extends AbstractDAO<number, OnmsEvent> {
return this.http.get('rest/events', opts).then((result) => {
let data = result.data;

let count = 0;
if (data.totalCount) {
count = parseInt(data.totalCount, 10);
}
if (data.count) {
count = parseInt(data.count, 10);
}
if (count > 0 && data.event) {
if (this.getCount(data) > 0 && data.event) {
data = data.event;
} else {
data = [];
Expand Down

0 comments on commit 2c548b5

Please sign in to comment.