parent
0f6ebe1e58
commit
6af8b87dd4
@ -0,0 +1,42 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
type Link struct {
|
||||
ID primitive.ObjectID `bson:"_id" json:"id"`
|
||||
Url string `bson:"url"`
|
||||
Description string `bson:"description"`
|
||||
}
|
||||
|
||||
func addLinkToDatabase(l Link) bool {
|
||||
_, err := colLinks.InsertOne(mongoCtx, l)
|
||||
if err != nil {
|
||||
log.Println("Could not add new link to database: ", err)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func deleteLinkFromDatabase(id primitive.ObjectID) bool {
|
||||
_, err := colLinks.DeleteOne(mongoCtx, bson.M{"_id": id})
|
||||
if err != nil {
|
||||
log.Println("Could not remove link from database: ", err)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func getLinksFromDatabase() (links []Link) {
|
||||
cursor, err := colLinks.Find(mongoCtx, bson.M{})
|
||||
if err != nil {
|
||||
log.Println("Could not remove link from database: ", err)
|
||||
return nil
|
||||
}
|
||||
cursor.All(mongoCtx, &links)
|
||||
return
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
let ll = document.getElementById("linklist");
|
||||
let infoBox = document.getElementById("general-info");
|
||||
|
||||
ll.addEventListener("click", e => {
|
||||
|
||||
let rbtn = e.target;
|
||||
|
||||
if (!rbtn.classList.contains("btn-delete")) {
|
||||
return;
|
||||
}
|
||||
|
||||
e.preventDefault();
|
||||
|
||||
let row = e.target.parentNode.parentNode;
|
||||
let link_id = row.children[0].dataset.id;
|
||||
|
||||
toggleDisabled(rbtn, "on");
|
||||
|
||||
fetch("/linklist", {
|
||||
method: "DELETE",
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
body: JSON.stringify({id: link_id})
|
||||
}).then(res => {
|
||||
if (res.status == 200) {
|
||||
row.remove();
|
||||
} else {
|
||||
setInfo(infoBox, "Failure", "red", "grey1");
|
||||
toggleHidden(infoBox, "off");
|
||||
setTimeout(() => {
|
||||
toggleHidden(infoBox, "on");
|
||||
}, 2000);
|
||||
}
|
||||
toggleDisabled(rbtn, "off");
|
||||
});
|
||||
});
|
@ -0,0 +1,33 @@
|
||||
{{define "body"}}
|
||||
<section id="wrapper">
|
||||
<table id="linklist">
|
||||
{{range .Links}}
|
||||
<tr>
|
||||
<td data-id="{{UnwrapOID .ID}}">
|
||||
<a target="_blank" href="{{.Url}}">{{.Description}}</a>
|
||||
</td>
|
||||
{{- if eq $.User.Role "admin"}}
|
||||
<td>
|
||||
<button class="btn-delete">✖</button>
|
||||
</td>
|
||||
{{end}}
|
||||
</tr>
|
||||
{{end}}
|
||||
</table>
|
||||
{{- if eq .User.Role "admin"}}
|
||||
<form action="/linklist" method="post">
|
||||
<label for="inp-url">URL</label>
|
||||
<input id="inp-url" name="url" type="text" value=""/>
|
||||
<label for="inp-desc">Description</label>
|
||||
<input id="inp-desc" name="description" type="text" value=""/>
|
||||
<button>Create</button>
|
||||
</form>
|
||||
<div class="hidden" id="general-info"></div>
|
||||
{{end}}
|
||||
</section>
|
||||
{{end}}
|
||||
|
||||
{{define "scripts"}}
|
||||
<script src="/static/js/helper.js"></script>
|
||||
<script src="/static/js/linklist.js"></script>
|
||||
{{end}}
|
Loading…
Reference in new issue