Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | 2x 2x 2x 2x 5x 5x 1x 5x 2x 1x 1x 4x 1x | import { Moment } from 'moment';
import { AbstractDAO } from './AbstractDAO';
import { TwitarrHTTPOptions } from '../api/TwitarrHTTPOptions';
import { Event } from '../model/Event';
/** @hidden */
const sortEvents = (a: Event, b: Event) => {
let ret = 0;
if (a) {
if (b) {
ret = a.start_time.valueOf() - b.start_time.valueOf();
if (ret === 0) {
if (a.end_time) {
if (b.end_time) {
ret = a.end_time.valueOf() - b.end_time.valueOf();
} else {
return -1;
}
} else if (b.end_time) {
return 1;
}
}
} else {
return -1;
}
} else if (b) {
return 1;
}
if (ret === 0) {
return a.title.localeCompare(b.title);
}
return ret;
};
export class EventDAO extends AbstractDAO {
/**
* Retrieve the complete list of events.
*/
public async all() {
return this.http
.get('/api/v2/event', new TwitarrHTTPOptions().withParameter('app', 'plain'))
.then(result => this.handleErrors(result))
.then(data => {
const events = data.events.map(e => Event.fromRest(e));
return events.sort(sortEvents);
});
}
/**
* Retrieve an individual event.
*/
public async get(id: string) {
return this.http
.get('/api/v2/event/' + id, new TwitarrHTTPOptions().withParameter('app', 'plain'))
.then(result => this.handleErrors(result))
.then(data => {
return Event.fromRest(data.event);
});
}
/**
* Favorite an event.
*/
public async favorite(id: string) {
return this.http
.post('/api/v2/event/' + id + '/favorite', new TwitarrHTTPOptions().withParameter('app', 'plain'))
.then(result => this.handleErrors(result))
.then(data => {
return data.event !== undefined;
// return Event.fromRest(data.event);
});
}
/**
* Un-favorite an event.
*/
public async unfavorite(id: string) {
return this.http
.httpDelete('/api/v2/event/' + id + '/favorite', new TwitarrHTTPOptions().withParameter('app', 'plain'))
.then(result => this.handleErrors(result))
.then(data => {
return data.event !== undefined;
// return Event.fromRest(data.event);
});
}
/**
* Remove an individual event. (Must be an admin.)
*/
public async remove(id: string) {
return this.http
.httpDelete('/api/v2/event/' + id, new TwitarrHTTPOptions().withParameter('app', 'plain'))
.then(result => this.handleErrors(result))
.then(() => {
return true;
});
}
/**
* Update an individual event. (Must be an admin.)
*/
public async update(id: string, title?: string, description?: string, location?: string, start_time?: Moment, end_time?: Moment) {
const options = new TwitarrHTTPOptions().withParameter('app', 'plain').withData({});
if (title) {
options.data.title = title;
}
if (description) {
options.data.description = description;
}
if (location) {
options.data.location = location;
}
if (start_time) {
options.data.start_time = start_time.valueOf();
}
if (end_time) {
options.data.end_time = end_time.valueOf();
}
return this.http
.post('/api/v2/event/' + id, options)
.then(result => this.handleErrors(result))
.then(data => {
return Event.fromRest(data.event);
});
}
/**
* Retrieve the list of events for a given day.
*/
public async getDay(date: Moment, mine?: boolean) {
const epoch = date.valueOf();
let url = '/api/v2/event/day/' + epoch;
if (mine) {
url = '/api/v2/event/mine/' + epoch;
}
return this.http
.get(url, new TwitarrHTTPOptions().withParameter('app', 'plain'))
.then(result => this.handleErrors(result))
.then(data => {
const events = data.events.map(e => Event.fromRest(e));
return events.sort(sortEvents);
});
}
}
|