Cleaned up dumbyt.ts a bit and fixed add channel
This commit is contained in:
218
src/dumbyt.ts
218
src/dumbyt.ts
@ -1,10 +1,102 @@
|
||||
import Axios from "axios";
|
||||
|
||||
// Base URL for API queries.
|
||||
const API_BASE_URL = "/api";
|
||||
export namespace API {
|
||||
// Base URL for API queries.
|
||||
export let Base_URL = "/api";
|
||||
|
||||
// How many chars at most for the description.
|
||||
const max_description_length = 100;
|
||||
// Types of modifications which can be applied to various models.
|
||||
export enum Modification {
|
||||
Add, Delete, Refresh
|
||||
}
|
||||
|
||||
// Change a channel state.
|
||||
export async function channel_modify(youtubeID : string, modify : Modification): Promise<void | string> {
|
||||
switch (modify){
|
||||
case Modification.Add: {
|
||||
let URL = API.Base_URL + '/Channels/' + youtubeID;
|
||||
let resp = await Axios.post(URL).catch((error) => {
|
||||
if (error.response)
|
||||
return error.response.data;
|
||||
else if (error.request)
|
||||
return error.request;
|
||||
else
|
||||
return "Axios request failed for unkown reason.";
|
||||
});
|
||||
if (typeof resp == "string")
|
||||
return resp;
|
||||
break;
|
||||
}
|
||||
case Modification.Delete: {
|
||||
let URL = API.Base_URL + '/Channels/' + youtubeID;
|
||||
let resp = await Axios.delete(URL).catch(e => console.log(e));
|
||||
if (resp != null){
|
||||
if (resp.status != 200)
|
||||
return resp.data();
|
||||
else
|
||||
return;
|
||||
} else {
|
||||
return "Response is null";
|
||||
}
|
||||
}
|
||||
case Modification.Refresh: {
|
||||
let URL = API.Base_URL + '/Channels/Update/' + youtubeID;
|
||||
let resp = await Axios.post(URL).catch(e => console.log(e));
|
||||
if (resp != null){
|
||||
if (resp.status != 200)
|
||||
return resp.data();
|
||||
else
|
||||
return;
|
||||
} else {
|
||||
return "Response is null";
|
||||
}
|
||||
}
|
||||
default: {
|
||||
console.log("Unknown request type, error ...");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Search for channels based on a search string.
|
||||
export async function search_channels(searchstr : string): Promise<Array<Channel>> {
|
||||
// Temporary holder for data.
|
||||
let Channels : Array<Channel> = [];
|
||||
|
||||
// Ask server for data.
|
||||
let resp = await Axios.get(API.Base_URL + '/Channels/' + searchstr).catch(e => console.log(e));
|
||||
|
||||
// Handle all our nulls.
|
||||
if (resp == null || resp.data == null)
|
||||
console.log("server /api/Channels/" + searchstr + " return is null");
|
||||
else {
|
||||
// Parse our videos and add it to the video output.
|
||||
resp.data.forEach((c: any) => Channels.push(new Channel(c)));
|
||||
}
|
||||
|
||||
// Send back the resulting videos.
|
||||
return Promise.resolve(Channels);
|
||||
}
|
||||
|
||||
// Search for videos based on a search string.
|
||||
export async function search_videos(searchstr : string): Promise<Array<Video>> {
|
||||
// Temporary holder for videos.
|
||||
let Videos : Array<Video> = [];
|
||||
|
||||
// Ask server for videos.
|
||||
let resp = await Axios.get(API.Base_URL + '/Videos/' + searchstr).catch(e => console.log(e));
|
||||
|
||||
// Handle all our nulls.
|
||||
if (resp == null || resp.data == null)
|
||||
console.log("server /videos/ return is null");
|
||||
else {
|
||||
// Parse our videos.
|
||||
resp.data.forEach((v: any) => Videos.push(new Video(v)));
|
||||
}
|
||||
|
||||
// Send back the resulting videos.
|
||||
return Promise.resolve(Videos);
|
||||
}
|
||||
}
|
||||
|
||||
// Wrapper for channels returned from our server.
|
||||
export class Channel {
|
||||
@ -90,121 +182,3 @@ export class Video {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Types of modifications which can be applied to various models.
|
||||
export enum Modification {
|
||||
Add, Delete, Refresh
|
||||
}
|
||||
|
||||
// Change a channel state.
|
||||
export async function channel_modify(youtubeID : string, modify : Modification): Promise<void | string> {
|
||||
switch (modify){
|
||||
case Modification.Add: {
|
||||
let URL = API_BASE_URL + '/Channels/' + youtubeID;
|
||||
let resp = await Axios.post(URL).catch((error) => {
|
||||
if (error.response)
|
||||
return error.response.data;
|
||||
else if (error.request)
|
||||
return error.request;
|
||||
else
|
||||
return "Axios request failed for unkown reason.";
|
||||
});
|
||||
if (typeof resp == "string")
|
||||
return resp;
|
||||
break;
|
||||
}
|
||||
case Modification.Delete: {
|
||||
let URL = API_BASE_URL + '/Channels/' + youtubeID;
|
||||
let resp = await Axios.delete(URL).catch(e => console.log(e));
|
||||
if (resp != null){
|
||||
if (resp.status != 200)
|
||||
return resp.data();
|
||||
else
|
||||
return;
|
||||
} else {
|
||||
return "Response is null";
|
||||
}
|
||||
}
|
||||
case Modification.Refresh: {
|
||||
let URL = API_BASE_URL + '/Channels/Update/' + youtubeID;
|
||||
let resp = await Axios.post(URL).catch(e => console.log(e));
|
||||
if (resp != null){
|
||||
if (resp.status != 200)
|
||||
return resp.data();
|
||||
else
|
||||
return;
|
||||
} else {
|
||||
return "Response is null";
|
||||
}
|
||||
}
|
||||
default: {
|
||||
console.log("Unknown request type, error ...");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Delete a video.
|
||||
export async function video_delete(youtubeID : string) : Promise<void> {
|
||||
let URL = API_BASE_URL + '/Videos/' + youtubeID;
|
||||
let resp = await Axios.delete(URL).catch(e => console.log(e));
|
||||
if ((resp == null) || (resp.data == null)){
|
||||
console.log("Video delete via " + URL + " FAIL");
|
||||
};
|
||||
}
|
||||
|
||||
// Search for channels based on a search string.
|
||||
export async function search_channels(searchstr : string): Promise<Array<Channel>> {
|
||||
// Temporary holder for data.
|
||||
let Channels : Array<Channel> = [];
|
||||
|
||||
// Ask server for data.
|
||||
let resp = await Axios.get(API_BASE_URL + '/Channels/' + searchstr).catch(e => console.log(e));
|
||||
|
||||
// Handle all our nulls.
|
||||
if (resp == null || resp.data == null)
|
||||
console.log("server /api/Channels/" + searchstr + " return is null");
|
||||
else {
|
||||
// Parse our videos.
|
||||
resp.data.forEach((c: any) => {
|
||||
// Trim description if needed.
|
||||
if (c.description.length > max_description_length) {
|
||||
c.description = c.description.substring(0, max_description_length) + " ...";
|
||||
}
|
||||
|
||||
// Add it to our array
|
||||
Channels.push(new Channel(c));
|
||||
});
|
||||
}
|
||||
|
||||
// Send back the resulting videos.
|
||||
return Promise.resolve(Channels);
|
||||
}
|
||||
|
||||
// Search for videos based on a search string.
|
||||
export async function search_videos(searchstr : string): Promise<Array<Video>> {
|
||||
// Temporary holder for videos.
|
||||
let Videos : Array<Video> = [];
|
||||
|
||||
// Ask server for videos.
|
||||
let resp = await Axios.get(API_BASE_URL + '/Videos/' + searchstr).catch(e => console.log(e));
|
||||
|
||||
// Handle all our nulls.
|
||||
if (resp == null || resp.data == null)
|
||||
console.log("server /videos/ return is null");
|
||||
else {
|
||||
// Parse our videos.
|
||||
resp.data.forEach((v: any) => {
|
||||
// Trim description if needed.
|
||||
if (v.description.length > max_description_length) {
|
||||
v.description = v.description.substring(0, max_description_length) + " ...";
|
||||
}
|
||||
|
||||
// Add it to our array
|
||||
Videos.push(new Video(v));
|
||||
});
|
||||
}
|
||||
|
||||
// Send back the resulting videos.
|
||||
return Promise.resolve(Videos);
|
||||
}
|
||||
|
Reference in New Issue
Block a user