-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #92 from MatsMoll/develop
Better term support
- Loading branch information
Showing
9 changed files
with
289 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
|
||
function createResource() { | ||
let activeTab = document.querySelector("#create-resource-modal .tab-content .active") | ||
let resourceTabID = activeTab.getAttribute("id"); | ||
let title = document.getElementById("resource-title").value; | ||
|
||
let connectedID = document.getElementById("resource-connect-id").value; | ||
let connectionType = document.getElementById("resource-connect-type").value; | ||
|
||
var url = "/api/resources/" | ||
var jsonData; | ||
|
||
if (resourceTabID == "article-rec") { | ||
url += "article" | ||
jsonData = articleData(title) | ||
} else if (resourceTabID == "video-rec") { | ||
url += "video" | ||
jsonData = videoData(title) | ||
} else if (resourceTabID == "book-rec") { | ||
url += "book" | ||
jsonData = bookData(title) | ||
} else { | ||
return | ||
} | ||
|
||
fetch(url, { | ||
method: "POST", | ||
headers: { | ||
"Accept": "application/json, text/plain, */*", | ||
"Content-Type" : "application/json" | ||
}, | ||
body: jsonData | ||
}) | ||
.then(function (response) { | ||
if (response.ok) { | ||
return response.json(); | ||
} else if (response.status == 400) { | ||
throw new Error("Sjekk at all nødvendig info er fylt ut"); | ||
} else { | ||
throw new Error(response.statusText); | ||
} | ||
}) | ||
.then(function (json) { | ||
console.log(json); | ||
if (isNaN(json)) { | ||
console.log("Not a number"); | ||
return | ||
} | ||
if (connectionType == "term") { | ||
connectTerm(connectedID, json); | ||
} else if (connectionType == "subtopic") { | ||
connectSubtopic(connectedID, json); | ||
} | ||
}) | ||
} | ||
|
||
function videoData(title) { | ||
let url = document.getElementById("video-url").value; | ||
var duration = document.getElementById("video-duration").value; | ||
let creator = document.getElementById("video-creator").value; | ||
|
||
if (isNaN(duration)) { | ||
duration = null; | ||
} | ||
|
||
return JSON.stringify({ | ||
"url": url, | ||
"creator": creator, | ||
"duration": duration, | ||
"title": title | ||
}) | ||
} | ||
|
||
function bookData(title) { | ||
|
||
} | ||
|
||
function articleData(title) { | ||
let url = document.getElementById("article-url").value; | ||
let author = document.getElementById("article-author").value; | ||
|
||
return JSON.stringify({ | ||
"url": url, | ||
"author": author, | ||
"title": title | ||
}) | ||
} | ||
|
||
function connectTerm(termID, resourceID) { | ||
let url = "/api/terms/" + termID + "/resources/" + resourceID; | ||
|
||
fetch(url, { | ||
method: "POST", | ||
headers: { | ||
"Accept": "application/json, text/plain, */*", | ||
"Content-Type" : "application/json" | ||
} | ||
}) | ||
.then(function (response) { | ||
if (response.ok) { | ||
return | ||
} else if (response.status == 400) { | ||
throw new Error("Sjekk at all nødvendig info er fylt ut"); | ||
} else { | ||
throw new Error(response.statusText); | ||
} | ||
}) | ||
.then(function (json) { | ||
console.log(json); | ||
}) | ||
} | ||
|
||
function connectSubtopic(subtopicID, resourceID) { | ||
let url = "/api/subtopics/" + subtopicID + "/resources/" + resourceID; | ||
|
||
fetch(url, { | ||
method: "POST", | ||
headers: { | ||
"Accept": "application/json, text/plain, */*", | ||
"Content-Type" : "application/json" | ||
} | ||
}) | ||
.then(function (response) { | ||
if (response.ok) { | ||
return | ||
} else if (response.status == 400) { | ||
throw new Error("Sjekk at all nødvendig info er fylt ut"); | ||
} else { | ||
throw new Error(response.statusText); | ||
} | ||
}) | ||
.then(function (json) { | ||
console.log(json); | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
function fetchResources(termID) { | ||
let url = "/terms/" + termID + "/resources"; | ||
let resultID = "term-resources" | ||
$("#" + resultID).html('<div class="d-flex justify-content-center"><div class="spinner-border" role="status"></div></div>'); | ||
fetch(url, { | ||
method: "GET", | ||
headers: { | ||
"Accept": "application/html, text/plain, */*", | ||
} | ||
}) | ||
.then(function (response) { | ||
if (response.ok) { | ||
return response.text(); | ||
} else { | ||
throw new Error(response.statusText); | ||
} | ||
}) | ||
.then(function (html) { | ||
$("#" + resultID).html(html); | ||
renderMarkdownNodesIn($("#" + resultID)); | ||
}) | ||
.catch(function (error) { | ||
console.log(error); | ||
}); | ||
} | ||
|
||
function fetchTerm(termID) { | ||
let url = "/api/terms/" + termID; | ||
let resultID = "term-meaning" | ||
$("#" + resultID).html('<div class="d-flex justify-content-center"><div class="spinner-border" role="status"></div></div>'); | ||
fetch(url, { | ||
method: "GET", | ||
headers: { | ||
"Accept": "application/html, text/plain, */*", | ||
} | ||
}) | ||
.then(function (response) { | ||
if (response.ok) { | ||
return response.json(); | ||
} else { | ||
throw new Error(response.statusText); | ||
} | ||
}) | ||
.then(function (term) { | ||
$("#" + resultID).html(term["meaning"]); | ||
renderMarkdownNodesIn($("#term-detail")); | ||
}) | ||
.catch(function (error) { | ||
console.log(error); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
function createTerm() { | ||
let url = "/api/terms" | ||
fetch(url, { | ||
method: "POST", | ||
headers: { | ||
"Accept": "application/json, text/plain, */*", | ||
"Content-Type" : "application/json" | ||
}, | ||
body: termData() | ||
}) | ||
.then(function (response) { | ||
if (response.ok) { | ||
return | ||
} else if (response.status == 400) { | ||
throw new Error("Sjekk at all nødvendig info er fylt ut"); | ||
} else { | ||
throw new Error(response.statusText); | ||
} | ||
}) | ||
.then(function (json) { | ||
console.log(json); | ||
document.location.reload(); | ||
}) | ||
} | ||
|
||
function termData() { | ||
let term = document.getElementById("new-term").value; | ||
let meaning = newtermmeaning.value(); | ||
let subtopicID = parseInt(document.getElementById("new-term-subtopic-id").value); | ||
if (term.length == 0) { | ||
throw new Error("Ups! Må skrive inn et begrep"); | ||
} | ||
if (meaning.length == 0) { | ||
throw new Error("Ups! Må skrive hva begrepet betyr"); | ||
} | ||
if (isNaN(subtopicID)) { | ||
throw new Error("Ups! En feil oppstod"); | ||
} | ||
return JSON.stringify({ | ||
"term": term, | ||
"meaning": meaning, | ||
"subtopicID": subtopicID | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
function deleteTerm() { | ||
let termID = parseInt(document.getElementById("term-id").value) | ||
if (isNaN(termID)) { | ||
throw new Error("Ups! En feil oppstod"); | ||
} | ||
let url = "/api/terms/" + termID | ||
fetch(url, { | ||
method: "DELETE", | ||
headers: { | ||
"Accept": "application/json, text/plain, */*", | ||
"Content-Type" : "application/json" | ||
} | ||
}) | ||
.then(function (response) { | ||
if (response.ok) { | ||
return | ||
} else if (response.status == 400) { | ||
throw new Error("Sjekk at all nødvendig info er fylt ut"); | ||
} else { | ||
throw new Error(response.statusText); | ||
} | ||
}) | ||
.then(function (json) { | ||
document.location.reload(); | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// | ||
// TermWebController.swift | ||
// | ||
// | ||
// Created by Mats Mollestad on 12/01/2021. | ||
// | ||
|
||
import Vapor | ||
import KognitaModels | ||
import KognitaAPI | ||
import KognitaViews | ||
|
||
struct TermWebController: RouteCollection { | ||
|
||
func boot(routes: RoutesBuilder) throws { | ||
let termInstance = routes.grouped("terms", Term.parameter) | ||
termInstance.get("resources", use: resourcesForTerm(on:)) | ||
} | ||
|
||
func resourcesForTerm(on req: Request) throws -> EventLoopFuture<View> { | ||
let termID = try req.parameters.get(Term.self) | ||
return req.repositories { repo in | ||
repo.resourceRepository.resourcesFor(termIDs: [termID]) | ||
} | ||
.flatMap { resources in | ||
ResourceCardList().render(with: .init(resources: resources), for: req) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters