From b7dd4e221fbf0ff9eb17fea69773920973f5a2ad Mon Sep 17 00:00:00 2001 From: Nils Jakobi Date: Sun, 22 Nov 2020 18:33:27 +0100 Subject: [PATCH] WIP: rearranged stuff --- cake.go | 69 ----------------------------------- kraken.go | 75 ++++++++++++++++++++++++++++++++++++++ main.go | 4 +-- rewards.go | 80 +++++++++++++++++++++++++++++++++++++++++ templates/includes.html | 4 ++- templates/rewards.html | 6 +++- 6 files changed, 165 insertions(+), 73 deletions(-) create mode 100644 kraken.go create mode 100644 rewards.go diff --git a/cake.go b/cake.go index 1edc3a6..af379d8 100644 --- a/cake.go +++ b/cake.go @@ -35,75 +35,6 @@ type rewards struct { Swapped map[time.Month]string } -// CakeRewards needs a comment -func CakeRewards(w http.ResponseWriter, r *http.Request) { - type data struct { - Title string - Uploaded bool - Success bool - Rewards rewards - CumulativeRewards map[time.Month]string - Colors []string - Currency string - ErrorText string - Lending bool - } - - // set common attributes - d := data{ - Title: "Pool by Cake Rewards", - } - - // check the method used to access this site (GET/POST) - switch r.Method { - - case http.MethodGet: - // display upload site - d.Uploaded = false - d.Success = false - render(w, "rewards.html", d) - - case http.MethodPost: - // upload the file that was posted here - var success bool = false - - lines, err := uploadFile(w, r) - if err == nil { - success = true - } else { - // set ErrorText - d.ErrorText = err.Error() - } - - // prepare data for usage - var color string - var currency string - var precision int - var rewards rewards - if success == true { - currency = lines[1].Cryptocurrency - color, precision, d.Lending = getCurrencyOpts(currency) - rewards = monthlyRewardOverview(lines) - d.CumulativeRewards = getCumulative(rewards, precision) - d.Success = success - d.Rewards.Staking = rewards.Staking - d.Rewards.Confectionery = rewards.Confectionery - d.Rewards.Lapis = rewards.Lapis - d.Rewards.LapisDFI = rewards.LapisDFI - d.Rewards.Referral = rewards.Referral - d.Rewards.Airdrop = rewards.Airdrop - d.Rewards.Swapped = rewards.Swapped - d.Colors = getOtherColors(color, 8) - d.Currency = currency - } - d.Uploaded = true - - // prettify.Print(rewards) - render(w, "rewards.html", d) - } - // create a new line instance -} - func getCumulative(r rewards, precision int) map[time.Month]string { // create map to calculate cumulative rewards res := make(map[time.Month]string) diff --git a/kraken.go b/kraken.go new file mode 100644 index 0000000..c24b110 --- /dev/null +++ b/kraken.go @@ -0,0 +1,75 @@ +package main + +import ( + "net/http" + "time" +) + +// KrakenRewards needs a comment +func KrakenRewards(w http.ResponseWriter, r *http.Request) { + type data struct { + Title string + Uploaded bool + Success bool + Rewards rewards + CumulativeRewards map[time.Month]string + Colors []string + Currency string + ErrorText string + Lending bool + } + + // set common attributes + d := data{ + Title: "Kraken Rewards", + } + + // check the method used to access this site (GET/POST) + switch r.Method { + + case http.MethodGet: + // display upload site + d.Uploaded = false + d.Success = false + render(w, "rewards.html", d) + + case http.MethodPost: + // upload the file that was posted here + var success bool = false + + lines, err := uploadFile(w, r) + if err == nil { + success = true + } else { + // set ErrorText + d.ErrorText = err.Error() + } + + // prepare data for usage + var color string + var currency string + var precision int + var rewards rewards + if success == true { + currency = lines[1].Cryptocurrency + color, precision, d.Lending = getCurrencyOpts(currency) + rewards = monthlyRewardOverview(lines) + d.CumulativeRewards = getCumulative(rewards, precision) + d.Success = success + d.Rewards.Staking = rewards.Staking + d.Rewards.Confectionery = rewards.Confectionery + d.Rewards.Lapis = rewards.Lapis + d.Rewards.LapisDFI = rewards.LapisDFI + d.Rewards.Referral = rewards.Referral + d.Rewards.Airdrop = rewards.Airdrop + d.Rewards.Swapped = rewards.Swapped + d.Colors = getOtherColors(color, 8) + d.Currency = currency + } + d.Uploaded = true + + // prettify.Print(rewards) + render(w, "rewards.html", d) + } + // create a new line instance +} diff --git a/main.go b/main.go index 3981f89..d6b339d 100644 --- a/main.go +++ b/main.go @@ -10,8 +10,8 @@ func main() { // when navigating to /home it should serve the home page http.HandleFunc("/", Home) - http.HandleFunc("/rewards", CakeRewards) - http.HandleFunc("/table", CakeRewards) + http.HandleFunc("/rewards", Rewards) + http.HandleFunc("/kraken", Rewards) http.ListenAndServe(":8080", nil) } diff --git a/rewards.go b/rewards.go new file mode 100644 index 0000000..7915cd2 --- /dev/null +++ b/rewards.go @@ -0,0 +1,80 @@ +package main + +import ( + "net/http" + "time" +) + +// Rewards needs a comment +func Rewards(w http.ResponseWriter, r *http.Request) { + type data struct { + Title string + Uploaded bool + Success bool + Rewards rewards + CumulativeRewards map[time.Month]string + Colors []string + Currency string + ErrorText string + Lending bool + } + + // set common attributes + d := data{} + + // set title according to site that called this function + switch r.URL.Path { + case "/rewards": + d.Title = "Cake" + case "/kraken": + d.Title = "Kraken" + } + + // check the method used to access this site (GET/POST) + switch r.Method { + + case http.MethodGet: + // display upload site + d.Uploaded = false + d.Success = false + render(w, "rewards.html", d) + + case http.MethodPost: + // upload the file that was posted here + var success bool = false + + lines, err := uploadFile(w, r) + if err == nil { + success = true + } else { + // set ErrorText + d.ErrorText = err.Error() + } + + // prepare data for usage + var color string + var currency string + var precision int + var rewards rewards + if success == true { + currency = lines[1].Cryptocurrency + color, precision, d.Lending = getCurrencyOpts(currency) + rewards = monthlyRewardOverview(lines) + d.CumulativeRewards = getCumulative(rewards, precision) + d.Success = success + d.Rewards.Staking = rewards.Staking + d.Rewards.Confectionery = rewards.Confectionery + d.Rewards.Lapis = rewards.Lapis + d.Rewards.LapisDFI = rewards.LapisDFI + d.Rewards.Referral = rewards.Referral + d.Rewards.Airdrop = rewards.Airdrop + d.Rewards.Swapped = rewards.Swapped + d.Colors = getOtherColors(color, 8) + d.Currency = currency + } + d.Uploaded = true + + render(w, "rewards.html", d) + } + // create a new line instance +} diff --git a/templates/includes.html b/templates/includes.html index 7ec874f..5c6be64 100644 --- a/templates/includes.html +++ b/templates/includes.html @@ -22,10 +22,12 @@
  • Home
  • -
  • Pool by Cake Rewards
  • +
  • Pool by Cake Rewards
  • +
  • Kraken Rewards
  • {{ end }} + {{end}} {{define "footer"}} diff --git a/templates/rewards.html b/templates/rewards.html index 0bda38c..9f402ad 100644 --- a/templates/rewards.html +++ b/templates/rewards.html @@ -2,9 +2,13 @@
    {{if not .Success}} -

    If you upload your CSV file from Cake here, we will display some graphs here

    +

    If you upload your CSV file from {{.Title}} here, we will display some graphs here

    + {{ if eq .Title "Cake" }}
    + {{ else }} + + {{ end }}