Commit cf89d78a authored by Linus's avatar Linus 🤔
Browse files

Some refactoring

parent 8b2a5b4b
Pipeline #78476 passed with stages
in 25 seconds
......@@ -9,14 +9,16 @@ import (
"github.com/BurntSushi/toml"
)
type Listing struct {
// partnerListing represents a tutorium partner listing.
type partnerListing struct {
Email string
NumPeople int
Message string
PostTime string
}
type TutSwap struct {
// tutSwapListing represents a tutorium swap listing.
type tutSwapListing struct {
Email string
From string
To string
......@@ -24,13 +26,13 @@ type TutSwap struct {
PostTime string
}
type ListingsStruct map[string][]Listing
type SwapStruct map[string][]TutSwap
type partnerMap map[string][]partnerListing
type swapMap map[string][]tutSwapListing
var listings ListingsStruct
var listings partnerMap
var listingsMutex = &sync.Mutex{}
var swaps SwapStruct
var swaps swapMap
var swapMutex = &sync.Mutex{}
// loadListings tries to load the listings from listingsFile.
......@@ -39,7 +41,7 @@ var swapMutex = &sync.Mutex{}
func loadListings() error {
data, err := ioutil.ReadFile(listingsFile)
if err != nil {
listings = ListingsStruct{}
listings = partnerMap{}
err = saveStruct(listings, listingsFile)
fmt.Println("There was no listings file, so I created a new one.")
return err
......@@ -56,7 +58,7 @@ func loadListings() error {
func loadSwapListings() error {
data, err := ioutil.ReadFile(swapListingsFile)
if err != nil {
swaps = SwapStruct{}
swaps = swapMap{}
err = saveStruct(swaps, swapListingsFile)
fmt.Println("There was no swaps file, so I created a new one.")
return err
......
......@@ -28,7 +28,9 @@ func errorClosure(f func(http.ResponseWriter, *http.Request) error) func(http.Re
}
}
func RegisterAll() {
// the registerAll function registers all names in regFunctions to the
// error closure of their response functions
func registerAll() {
for key, value := range regFunctions {
http.HandleFunc(key, errorClosure(value))
}
......
......@@ -55,7 +55,7 @@ func main() {
panic(err)
}
RegisterAll()
registerAll()
fmt.Printf("Starting server at port %s and IP %s\n", string(*portPtr), string(*ipStrPtr))
log.Fatal(http.ListenAndServe(*ipStrPtr+":"+string(*portPtr), nil))
......
......@@ -18,7 +18,7 @@ func search(w http.ResponseWriter, r *http.Request) error {
type otherTut struct {
Name string
Id string
ID string
Subject string
}
......@@ -73,10 +73,10 @@ func doSearch(w http.ResponseWriter, r *http.Request) error {
toPush := otherTut{}
if splits[1] == "" {
toPush.Name = "Leeres Tutorium"
toPush.Id = ""
toPush.ID = ""
} else {
toPush.Name = "Tut. " + splits[1]
toPush.Id = splits[1]
toPush.ID = splits[1]
}
toPush.Subject = subj
otherTuts = append(otherTuts, toPush)
......@@ -84,11 +84,11 @@ func doSearch(w http.ResponseWriter, r *http.Request) error {
}
sort.Slice(otherTuts, func(i, j int) bool {
id1, err := strconv.Atoi(otherTuts[i].Id)
id1, err := strconv.Atoi(otherTuts[i].ID)
if err != nil {
id1 = 0
}
id2, err := strconv.Atoi(otherTuts[j].Id)
id2, err := strconv.Atoi(otherTuts[j].ID)
if err != nil {
id2 = 0
}
......@@ -143,7 +143,7 @@ func addListing(w http.ResponseWriter, r *http.Request) error {
if !hasSubject(strings.Split(subject, "::")[0]) {
return renderError("Das angegebene Fach existiert nicht", w, r)
}
listing := Listing{email, numStudents, message, getTime()}
listing := partnerListing{email, numStudents, message, getTime()}
listingsMutex.Lock()
defer listingsMutex.Unlock()
listings[subject] = append(listings[subject], listing)
......
......@@ -5,7 +5,7 @@
<div {{ .OtherTutsHidden }} style="color: green;">
In diesem Fach gibt es Anzeigen in den Tutorien:
{{ range .OtherTuts }}
<a href="/dosearch?subject={{ .Subject }}&tutorium={{ .Id }}">{{ .Name }}</a>
<a href="/dosearch?subject={{ .Subject }}&tutorium={{ .ID }}">{{ .Name }}</a>
{{ end }}
<br><br>
</div>
......
......@@ -74,7 +74,7 @@ func addSwap(w http.ResponseWriter, r *http.Request) error {
if !hasSubject(subject) {
return renderError("Das angegebene Fach existiert nicht", w, r)
}
swap := TutSwap{email, from, to, message, getTime()}
swap := tutSwapListing{email, from, to, message, getTime()}
swapMutex.Lock()
defer swapMutex.Unlock()
swaps[subject] = append(swaps[subject], swap)
......
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