Fix non complete cast of query

pull/73/head
MillenniumEarl 2021-03-03 20:29:09 +01:00
parent 3711d8ae3e
commit bd23956dc4
1 changed files with 29 additions and 25 deletions

View File

@ -32,7 +32,9 @@ type THandiworkOrder = "date" | "likes" | "relevance" | "replies" | "title" | "v
export default class HandiworkSearchQuery implements IQuery { export default class HandiworkSearchQuery implements IQuery {
//#region Private fields //#region Private fields
static MIN_PAGE = 1; static MIN_PAGE = 1;
//#endregion Private fields //#endregion Private fields
//#region Properties //#region Properties
@ -53,8 +55,8 @@ export default class HandiworkSearchQuery implements IQuery {
* Tags to exclude from the search. * Tags to exclude from the search.
*/ */
public excludedTags: string[] = []; public excludedTags: string[] = [];
public includedPrefixes: string[]; public includedPrefixes: string[] = [];
public category: TCategory; public category: TCategory = null;
/** /**
* Results presentation order. * Results presentation order.
*/ */
@ -94,7 +96,7 @@ export default class HandiworkSearchQuery implements IQuery {
public createURL(): URL { public createURL(): URL {
// Local variables // Local variables
let query: LatestSearchQuery | ThreadSearchQuery = null; let url = null;
// Check if the query is valid // Check if the query is valid
if (!this.validate()) { if (!this.validate()) {
@ -102,22 +104,19 @@ export default class HandiworkSearchQuery implements IQuery {
} }
// Convert the query // Convert the query
if (this.selectSearchType() === "latest") query = this.cast<LatestSearchQuery>(); if (this.selectSearchType() === "latest") url = this.cast<LatestSearchQuery>("LatestSearchQuery").createURL();
else query = this.cast<ThreadSearchQuery>(); else url = this.cast<ThreadSearchQuery>("ThreadSearchQuery").createURL();
return query.createURL(); return url;
} }
public cast<T extends IQuery>(): T { public cast<T extends IQuery>(type: TQueryInterface): T {
// Local variables // Local variables
let returnValue = null; let returnValue = null;
// Cast the query
const query:T = {} as IQuery as T;
// Convert the query // Convert the query
if (query.itype === "LatestSearchQuery") returnValue = this.castToLatest(); if (type === "LatestSearchQuery") returnValue = this.castToLatest();
else if (query.itype === "ThreadSearchQuery") returnValue = this.castToThread(); else if (type === "ThreadSearchQuery") returnValue = this.castToThread();
else returnValue = this as HandiworkSearchQuery; else returnValue = this as HandiworkSearchQuery;
// Cast the result to T // Cast the result to T
@ -127,12 +126,16 @@ export default class HandiworkSearchQuery implements IQuery {
//#region Private methods //#region Private methods
private castToLatest(): LatestSearchQuery { private castToLatest(): LatestSearchQuery {
// Cast the basic query object // Cast the basic query object and copy common values
const query: LatestSearchQuery = this as IQuery as LatestSearchQuery; const query: LatestSearchQuery = new LatestSearchQuery;
let orderFilter = this.order as string; Object.keys(this).forEach(key => {
query.itype = "LatestSearchQuery"; if (query.hasOwnProperty(key)) {
query[key] = this[key];
}
});
// Adapt order filter // Adapt order filter
let orderFilter = this.order as string;
if (orderFilter === "relevance") orderFilter = "rating"; if (orderFilter === "relevance") orderFilter = "rating";
else if (orderFilter === "replies") orderFilter = "views"; else if (orderFilter === "replies") orderFilter = "views";
query.order = orderFilter as TLatestOrder; query.order = orderFilter as TLatestOrder;
@ -144,18 +147,19 @@ export default class HandiworkSearchQuery implements IQuery {
} }
private castToThread(): ThreadSearchQuery { private castToThread(): ThreadSearchQuery {
// Cast the basic query object // Cast the basic query object and copy common values
const query: ThreadSearchQuery = this as IQuery as ThreadSearchQuery; const query: ThreadSearchQuery = new ThreadSearchQuery;
let orderFilter = this.order as string; Object.keys(this).forEach(key => {
if (query.hasOwnProperty(key)) {
query[key] = this[key];
}
});
// Set common values // Set uncommon values
query.excludedTags = this.excludedTags;
query.newerThan = this.newerThan;
query.olderThan = this.olderThan;
query.onlyTitles = true; query.onlyTitles = true;
query.keywords = this.keywords;
// Adapt order filter // Adapt order filter
let orderFilter = this.order as string;
if (orderFilter === "likes" || orderFilter === "title") orderFilter = "relevance"; if (orderFilter === "likes" || orderFilter === "title") orderFilter = "relevance";
query.order = orderFilter as TThreadOrder; query.order = orderFilter as TThreadOrder;