Skip to content

Commit

Permalink
Merge pull request #92 from MatsMoll/develop
Browse files Browse the repository at this point in the history
Better term support
  • Loading branch information
MatsMoll authored Jan 13, 2021
2 parents 7817921 + 3f06f23 commit 29d8a5b
Show file tree
Hide file tree
Showing 9 changed files with 289 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Public/assets/js/flash-card/submit-performance.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ function presentControlls() {
$(this).removeClass("d-none");
});
fetchSolutions();
fetchDiscussions($("#task-id").val())
// fetchDiscussions($("#task-id").val())
$("#knowledge-card").removeClass("d-none");
updateScoreButton()
}
Expand Down
2 changes: 1 addition & 1 deletion Public/assets/js/multiple-choise/task-submit.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ function presentControlls() {
$(this).removeClass("d-none");
});
fetchSolutions();
fetchDiscussions($("#task-id").val())
// fetchDiscussions($("#task-id").val())
}

$("input[name='choiseInput']").each(function () {
Expand Down
135 changes: 135 additions & 0 deletions Public/assets/js/resources/create.js
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);
})
}
51 changes: 51 additions & 0 deletions Public/assets/js/resources/fetch-html.js
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);
});
}
44 changes: 44 additions & 0 deletions Public/assets/js/term/create.js
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
})
}
26 changes: 26 additions & 0 deletions Public/assets/js/term/delete.js
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();
})
}
2 changes: 1 addition & 1 deletion Resources/Localization/nb.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"error.message" : "En Feil Oppstod - ",


"footer.copyright" : "2020 © Kognita - Mats Eikeland Mollestad",
"footer.copyright" : "2021 © Kognita - Mats Eikeland Mollestad",
"footer.about.us" : "Om oss",
"footer.help" : "Hjelp",
"footer.contact" : "Kontakt oss",
Expand Down
29 changes: 29 additions & 0 deletions Sources/App/Term/TermWebController.swift
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)
}
}
}
1 change: 1 addition & 0 deletions Sources/App/routes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ private func setupUserWeb(for app: Application) throws {
try redirectMiddle.register(collection: TestSessionWebController())
try redirectMiddle.register(collection: TaskDiscussionWebController())
try redirectMiddle.register(collection: LectureNoteRecapSessionWebController())
try redirectMiddle.register(collection: TermWebController())
}

extension HTTPCookies {
Expand Down

0 comments on commit 29d8a5b

Please sign in to comment.