Some data handling changes
This commit is contained in:
parent
96d74a7732
commit
c67a0bf590
12 changed files with 66 additions and 39 deletions
|
|
@ -1,5 +1,5 @@
|
|||
import {APIModel} from './APIModel';
|
||||
import {APIRequestResult} from './APIRequestResult';
|
||||
import {MediaTypeModel} from '../models/MediaTypeModel';
|
||||
|
||||
export class APIManager {
|
||||
apis: APIModel[];
|
||||
|
|
@ -8,10 +8,10 @@ export class APIManager {
|
|||
this.apis = [];
|
||||
}
|
||||
|
||||
async query(query: string, types: string[] = []): Promise<APIRequestResult[]> {
|
||||
async query(query: string, types: string[] = []): Promise<MediaTypeModel[]> {
|
||||
console.log('MDB | api manager queried');
|
||||
|
||||
let res: APIRequestResult[] = [];
|
||||
let res: MediaTypeModel[] = [];
|
||||
|
||||
for (const api of this.apis) {
|
||||
if (types.length === 0 || api.hasTypeOverlap(types)) {
|
||||
|
|
|
|||
|
|
@ -1,7 +1,8 @@
|
|||
import {APIRequestResult} from './APIRequestResult';
|
||||
import {MediaTypeModel} from '../models/MediaTypeModel';
|
||||
|
||||
export abstract class APIModel {
|
||||
name: string;
|
||||
apiName: string;
|
||||
apiUrl: string;
|
||||
types: string[];
|
||||
|
||||
/**
|
||||
|
|
@ -9,14 +10,7 @@ export abstract class APIModel {
|
|||
*
|
||||
* @param title the title to query for
|
||||
*/
|
||||
abstract getByTitle(title: string): Promise<APIRequestResult[]>;
|
||||
|
||||
/**
|
||||
* This function should return the metadata corresponding to the api result. An implementation should check first, if the result is from this api.
|
||||
*
|
||||
* @param item
|
||||
*/
|
||||
abstract getMataDataFromResult(item: APIRequestResult): string;
|
||||
abstract getByTitle(title: string): Promise<MediaTypeModel[]>;
|
||||
|
||||
hasType(type: string): boolean {
|
||||
return this.types.contains(type);
|
||||
|
|
|
|||
|
|
@ -1,7 +0,0 @@
|
|||
export interface APIRequestResult {
|
||||
title: string,
|
||||
type: string,
|
||||
description?: string,
|
||||
apiName: string,
|
||||
data: any,
|
||||
}
|
||||
|
|
@ -1,24 +1,25 @@
|
|||
import {APIModel} from '../APIModel';
|
||||
import {APIRequestResult} from '../APIRequestResult';
|
||||
import {MediaTypeModel} from '../../models/MediaTypeModel';
|
||||
|
||||
export class TestAPI extends APIModel {
|
||||
constructor() {
|
||||
super();
|
||||
this.name = 'testAPI';
|
||||
this.apiName = 'testAPI';
|
||||
this.apiUrl = 'www.test.api';
|
||||
this.types = ['test'];
|
||||
}
|
||||
|
||||
async getByTitle(title: string): Promise<APIRequestResult[]> {
|
||||
console.log(`MDB | api "${this.name}" queried`);
|
||||
async getByTitle(title: string): Promise<MediaTypeModel[]> {
|
||||
console.log(`MDB | api "${this.apiName}" queried`);
|
||||
|
||||
return [
|
||||
{title: 'test1', type: this.types[0], apiName: this.name, data: {length: 126}} as APIRequestResult,
|
||||
{title: 'test2', type: this.types[0], apiName: this.name, description: 'some test description', data: {}} as APIRequestResult,
|
||||
{title: 'test1', type: this.types[0], dataSource: this.apiName} as MediaTypeModel,
|
||||
{title: 'test2', type: this.types[0], dataSource: this.apiName} as MediaTypeModel,
|
||||
];
|
||||
}
|
||||
|
||||
getMataDataFromResult(item: APIRequestResult): string {
|
||||
if (item.apiName !== this.name) {
|
||||
getMataDataFromResult(item: MediaTypeModel): string {
|
||||
if (item.dataSource !== this.apiUrl) {
|
||||
return '';
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import {Plugin} from 'obsidian';
|
||||
import {DEFAULT_SETTINGS, MediaDbPluginSettings, MediaDbSettingTab} from './settings/settings';
|
||||
import {DEFAULT_SETTINGS, MediaDbPluginSettings, MediaDbSettingTab} from './settings/Settings';
|
||||
import {MediaDbSearchModal} from './modals/MediaDbSearchModal';
|
||||
import {APIManager} from './api/APIManager';
|
||||
import {TestAPI} from './api/apis/TestAPI';
|
||||
|
|
|
|||
|
|
@ -1,14 +1,14 @@
|
|||
import {App, SuggestModal} from 'obsidian';
|
||||
import {APIRequestResult} from '../api/APIRequestResult';
|
||||
import {APIManager} from '../api/APIManager';
|
||||
import {MediaTypeModel} from '../models/MediaTypeModel';
|
||||
|
||||
export class MediaDbSearchModal extends SuggestModal<APIRequestResult> {
|
||||
export class MediaDbSearchModal extends SuggestModal<MediaTypeModel> {
|
||||
query: string;
|
||||
isBusy: boolean;
|
||||
apiManager: APIManager;
|
||||
onChoose: (err: Error, result?: APIRequestResult) => void;
|
||||
onChoose: (err: Error, result?: MediaTypeModel) => void;
|
||||
|
||||
constructor(app: App, apiManager: APIManager, onChoose?: (err: Error, result?: APIRequestResult) => void) {
|
||||
constructor(app: App, apiManager: APIManager, onChoose?: (err: Error, result?: MediaTypeModel) => void) {
|
||||
super(app);
|
||||
|
||||
this.apiManager = apiManager;
|
||||
|
|
@ -16,7 +16,7 @@ export class MediaDbSearchModal extends SuggestModal<APIRequestResult> {
|
|||
this.isBusy = false;
|
||||
}
|
||||
|
||||
async search(force: boolean = false): Promise<APIRequestResult[]> {
|
||||
async search(force: boolean = false): Promise<MediaTypeModel[]> {
|
||||
if (!this.query) {
|
||||
return;
|
||||
}
|
||||
|
|
@ -48,18 +48,18 @@ export class MediaDbSearchModal extends SuggestModal<APIRequestResult> {
|
|||
}
|
||||
}
|
||||
|
||||
async getSuggestions(query: string): Promise<APIRequestResult[]> {
|
||||
async getSuggestions(query: string): Promise<MediaTypeModel[]> {
|
||||
this.query = query;
|
||||
|
||||
return await this.search();
|
||||
}
|
||||
|
||||
renderSuggestion(item: APIRequestResult, el: HTMLElement): void {
|
||||
renderSuggestion(item: MediaTypeModel, el: HTMLElement): void {
|
||||
el.createEl('div', {text: item.title});
|
||||
el.createEl('small', {text: item.type});
|
||||
}
|
||||
|
||||
onChooseSuggestion(item: APIRequestResult, evt: MouseEvent | KeyboardEvent): void {
|
||||
onChooseSuggestion(item: MediaTypeModel, evt: MouseEvent | KeyboardEvent): void {
|
||||
this.onChoose(null, item);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
7
src/models/MediaTypeModel.ts
Normal file
7
src/models/MediaTypeModel.ts
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
export abstract class MediaTypeModel {
|
||||
type: string;
|
||||
title: string;
|
||||
dataSource: string;
|
||||
|
||||
abstract toMetaData(): string;
|
||||
}
|
||||
32
src/models/MovieModel.ts
Normal file
32
src/models/MovieModel.ts
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
import {MediaTypeModel} from './MediaTypeModel';
|
||||
|
||||
export class MovieModel extends MediaTypeModel {
|
||||
type: string;
|
||||
title: string;
|
||||
dataSource: string;
|
||||
|
||||
genres: string[];
|
||||
producer: string;
|
||||
duration: string;
|
||||
onlineRating: number;
|
||||
image: string;
|
||||
|
||||
released: boolean;
|
||||
premiere: string;
|
||||
|
||||
watched: boolean;
|
||||
lastWatched: string;
|
||||
personalRating: number;
|
||||
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.type = 'movie';
|
||||
}
|
||||
|
||||
toMetaData(): string {
|
||||
return '';
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
// Credits go to Liam's Periodic Notes Plugin: https://github.com/liamcain/obsidian-periodic-notes
|
||||
|
||||
import {TAbstractFile, TFolder} from 'obsidian';
|
||||
import {TextInputSuggest} from './suggest';
|
||||
import {TextInputSuggest} from './Suggest';
|
||||
|
||||
export class FolderSuggest extends TextInputSuggest<TFolder> {
|
||||
getSuggestions(inputStr: string): TFolder[] {
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
import {App, ISuggestOwner, Scope} from 'obsidian';
|
||||
import {createPopper, Instance as PopperInstance} from '@popperjs/core';
|
||||
import {wrapAround} from 'src/utils/utils';
|
||||
import {wrapAround} from 'src/utils/Utils';
|
||||
|
||||
export class Suggest<T> {
|
||||
private owner: ISuggestOwner<T>;
|
||||
Loading…
Add table
Add a link
Reference in a new issue