Commit 1252104c authored by Linus's avatar Linus 🤔
Browse files

Put functions.go into multiple files, added welcome page

parent 370f131c
package main
import (
"net/http"
"strconv"
"strings"
)
func del(w http.ResponseWriter, r *http.Request) error {
args := map[string]interface{}{
"Title": "Daten löschen",
}
return render("templates/delete.html", args, w, r)
}
func doDel(w http.ResponseWriter, r *http.Request) error {
if r.Method != "POST" {
return renderError("Method falsch ("+r.Method+" statt POST)", w, r)
}
err := r.ParseForm()
if err != nil {
return err
}
email := r.FormValue("email")
if email == "" {
return renderError("Email leer", w, r)
}
listingsMutex.Lock()
defer listingsMutex.Unlock()
numDeleted := 0
for key, value := range listings {
i := 0
for _, listing := range value {
if strings.ToLower(listing.Email) != strings.ToLower(email) {
value[i] = listing
i++
}
}
l := len(value)
listings[key] = value[:i]
numDeleted += l - i
}
err = saveStruct(listings, listingsFile)
if err != nil {
return err
}
_, err = w.Write([]byte("Ok :)\nDeleted " + strconv.Itoa(numDeleted) + "\n"))
return err
}
......@@ -3,17 +3,15 @@ package main
import (
"html/template"
"net/http"
"strconv"
"strings"
"time"
)
var regFunctions = map[string]func(http.ResponseWriter, *http.Request) error{
"/": search,
"/": welcome,
"/search": search,
"/data": data,
"/delete": del,
"/dodelete": doDel,
"/search": doSearch,
"/dosearch": doSearch,
"/addlisting": addListing,
"/info": info,
}
......@@ -47,63 +45,15 @@ func renderError(err string, w http.ResponseWriter, r *http.Request) error {
"Error": err,
}, w, r)
}
func search(w http.ResponseWriter, r *http.Request) error {
func welcome(w http.ResponseWriter, r *http.Request) error {
if r.URL.Path != "/" {
http.NotFound(w, r)
return nil
}
args := map[string]interface{}{
"Subjects": subjects,
"Title": "Suche",
"Title": "Willkommen",
}
return render("templates/search.html", args, w, r)
}
func doSearch(w http.ResponseWriter, r *http.Request) error {
err := r.ParseForm()
if err != nil {
return err
}
subj := r.FormValue("subject")
tut := r.FormValue("tutorium")
if !hasSubject(subj) {
return renderError("Dieses Fach existiert nicht. Bitte nutze die Autocomplete-Funktion, "+
"oder falls du Javascript ablehnst die Liste von akzeptierten Fächern.\n", w, r)
}
var num string
if tut == "" {
num = ""
} else {
intNum, err := strconv.Atoi(tut)
num = strconv.Itoa(intNum)
if err != nil {
return renderError("Das Tutorium muss eine Integer-Zahl oder leer sein", w, r)
}
}
tutString := subj + "::" + num
listingsMutex.Lock()
defer listingsMutex.Unlock()
results := listings[tutString]
var resultTemplate string
if len(results) == 0 {
resultTemplate = "templates/result_none.html"
} else {
resultTemplate = "templates/result_some.html"
}
tmpl, err := template.ParseFiles("templates/template.html", "templates/found.html", resultTemplate)
if err != nil {
return err
}
return tmpl.ExecuteTemplate(w, "template", map[string]interface{}{
"FoundListings": results,
"Subject": tutString,
"Title": "Resultate",
})
return render("templates/welcome.html", args, w, r)
}
func data(w http.ResponseWriter, r *http.Request) error {
......@@ -112,96 +62,9 @@ func data(w http.ResponseWriter, r *http.Request) error {
}
return render("templates/data.html", args, w, r)
}
func addListing(w http.ResponseWriter, r *http.Request) error {
if r.Method != "POST" {
return renderError("Falsche Methode ("+r.Method+", nicht POST)", w, r)
}
err := r.ParseForm()
if err != nil {
return err
}
email := r.FormValue("email")
numStudentsStr := r.FormValue("num_students")
message := r.FormValue("message")
agreeStr := r.FormValue("agree")
subject := r.FormValue("subject")
if email == "" {
return renderError("Email leer", w, r)
}
numStudents, err := strconv.Atoi(numStudentsStr)
if err != nil {
return renderError("Anzahl Studenten muss eine Integer-Zahl sein", w, r)
}
if numStudents < 1 {
return renderError("Anzahl Studenten muss >= 1 sein", w, r)
}
if agreeStr != "on" {
return renderError("Du musst den Bedingungen zustimmen", w, r)
}
if !hasSubject(strings.Split(subject, "::")[0]) {
return renderError("Das angegebene Fach existiert nicht", w, r)
}
listing := Listing{email, numStudents, message, time.Now().Format("02.01.2006 um 15:04")}
listingsMutex.Lock()
defer listingsMutex.Unlock()
listings[subject] = append(listings[subject], listing)
err = saveStruct(listings, listingsFile)
if err != nil {
return err
}
http.Redirect(w, r, r.Referer(), http.StatusFound)
return nil
}
func info(w http.ResponseWriter, r *http.Request) error {
args := map[string]interface{}{
"Title": "Info",
}
return render("templates/info.html", args, w, r)
}
func del(w http.ResponseWriter, r *http.Request) error {
args := map[string]interface{}{
"Title": "Daten löschen",
}
return render("templates/delete.html", args, w, r)
}
func doDel(w http.ResponseWriter, r *http.Request) error {
if r.Method != "POST" {
return renderError("Method falsch ("+r.Method+" statt POST)", w, r)
}
err := r.ParseForm()
if err != nil {
return err
}
email := r.FormValue("email")
if email == "" {
return renderError("Email leer", w, r)
}
listingsMutex.Lock()
defer listingsMutex.Unlock()
numDeleted := 0
for key, value := range listings {
i := 0
for _, listing := range value {
if strings.ToLower(listing.Email) != strings.ToLower(email) {
value[i] = listing
i++
}
}
l := len(value)
listings[key] = value[:i]
numDeleted += l - i
}
err = saveStruct(listings, listingsFile)
if err != nil {
return err
}
_, err = w.Write([]byte("Ok :)\nDeleted " + strconv.Itoa(numDeleted) + "\n"))
return err
}
package main
import (
"html/template"
"net/http"
"strconv"
"strings"
"time"
)
func search(w http.ResponseWriter, r *http.Request) error {
args := map[string]interface{}{
"Subjects": subjects,
"Title": "Suche",
}
return render("templates/search.html", args, w, r)
}
func doSearch(w http.ResponseWriter, r *http.Request) error {
err := r.ParseForm()
if err != nil {
return err
}
subj := r.FormValue("subject")
tut := r.FormValue("tutorium")
if !hasSubject(subj) {
return renderError("Dieses Fach existiert nicht. Bitte nutze die Autocomplete-Funktion, "+
"oder falls du Javascript ablehnst die Liste von akzeptierten Fächern.\n", w, r)
}
var num string
if tut == "" {
num = ""
} else {
intNum, err := strconv.Atoi(tut)
num = strconv.Itoa(intNum)
if err != nil {
return renderError("Das Tutorium muss eine Integer-Zahl oder leer sein", w, r)
}
}
tutString := subj + "::" + num
listingsMutex.Lock()
defer listingsMutex.Unlock()
results := listings[tutString]
var resultTemplate string
if len(results) == 0 {
resultTemplate = "templates/result_none.html"
} else {
resultTemplate = "templates/result_some.html"
}
tmpl, err := template.ParseFiles("templates/template.html", "templates/found.html", resultTemplate)
if err != nil {
return err
}
return tmpl.ExecuteTemplate(w, "template", map[string]interface{}{
"FoundListings": results,
"Subject": tutString,
"Title": "Resultate",
})
}
func addListing(w http.ResponseWriter, r *http.Request) error {
if r.Method != "POST" {
return renderError("Falsche Methode ("+r.Method+", nicht POST)", w, r)
}
err := r.ParseForm()
if err != nil {
return err
}
email := r.FormValue("email")
numStudentsStr := r.FormValue("num_students")
message := r.FormValue("message")
agreeStr := r.FormValue("agree")
subject := r.FormValue("subject")
if email == "" {
return renderError("Email leer", w, r)
}
numStudents, err := strconv.Atoi(numStudentsStr)
if err != nil {
return renderError("Anzahl Studenten muss eine Integer-Zahl sein", w, r)
}
if numStudents < 1 {
return renderError("Anzahl Studenten muss >= 1 sein", w, r)
}
if agreeStr != "on" {
return renderError("Du musst den Bedingungen zustimmen", w, r)
}
if !hasSubject(strings.Split(subject, "::")[0]) {
return renderError("Das angegebene Fach existiert nicht", w, r)
}
listing := Listing{email, numStudents, message, time.Now().Format("02.01.2006 um 15:04")}
listingsMutex.Lock()
defer listingsMutex.Unlock()
listings[subject] = append(listings[subject], listing)
err = saveStruct(listings, listingsFile)
if err != nil {
return err
}
http.Redirect(w, r, r.Referer(), http.StatusFound)
return nil
}
......@@ -2,7 +2,7 @@
Auf dieser Seite kannst du nach Abgabepartnern an der RWTH
im Bachelor (vielleicht auch Master?) Informatik suchen, oder
dich selbst finden lassen.
<form method="get" action="/search">
<form method="get" action="/dosearch">
<br><br>
<label for="subject"><b>Dein Fach:</b></label>
<input name="subject" id="subject">
......
......@@ -7,7 +7,7 @@
</head>
<body>
<div class="container">
<div class="logo">
<div onclick="document.location = '/';" class="logo">
<h2>apb</h2>
<small>Finde den Abgabepartner deiner Träume</small>
</div>
......@@ -19,7 +19,13 @@
<div class="sidebar">
<b class="sidebar-title">Inhalte auf dieser Seite</b>
<div class="sidebar-element">
<a href="/">Suche</a>
<a href="/">Start</a>
</div>
<div class="sidebar-element">
<a href="/search">Abgabepartner suchen</a>
</div>
<div class="sidebar-element">
<a href="/tutswap">Tutorien tauschen</a>
</div>
<div class="sidebar-element">
<a href="/delete">Lösche deine Anzeige</a>
......@@ -28,7 +34,7 @@
<a href="/data">Datenschutzerklärung</a>
</div>
<div class="sidebar-element">
<a href="/info">Info</a>
<a href="/info">Info und Repositor</a>
</div>
</div>
<div class="main">
......
{{ define "content" }}
<p>Du hast die <b>Abgabepartnerbörse</b> für den Bachelor/Master Informatik gefunden.
<p>Auf dieser Website kannst du <a href="/search">Abgabepartner finden</a>
und <a href="/tutswap">Tutorien tauschen</a>.
<p>Möchtest du etwas ändern, ein Feature vorschlagen oder einen Bug melden?
Schau dir doch das <a href="https://git.rwth-aachen.de/h/apb">Repository</a> an.
<p>Viel Erfolg :)</p>
{{ end }}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment