lottery
This commit is contained in:
parent
ef105e1cac
commit
b918b92a4d
6 changed files with 702 additions and 0 deletions
|
|
@ -53,6 +53,12 @@ type DashboardData struct {
|
|||
Transactions []db.TransactionView
|
||||
}
|
||||
|
||||
type LotteryPageData struct {
|
||||
User *db.User
|
||||
Lottery *db.LotteryDashboard
|
||||
LastPayout *db.LotteryPayoutView
|
||||
}
|
||||
|
||||
type AdminDashboardData struct {
|
||||
Admin *db.AdminUser
|
||||
Dashboard *db.AdminDashboard
|
||||
|
|
@ -158,6 +164,8 @@ func (a *App) routes() http.Handler {
|
|||
protected.Get("/app", a.userDashboard)
|
||||
protected.Get("/app/transfer", a.showTransfer)
|
||||
protected.Post("/app/transfer", a.createTransfer)
|
||||
protected.Get("/app/lottery", a.lotteryPage)
|
||||
protected.Post("/app/lottery/deposit", a.createLotteryDeposit)
|
||||
protected.Get("/app/transactions", a.transactionsPage)
|
||||
})
|
||||
|
||||
|
|
@ -387,6 +395,94 @@ func (a *App) transactionsPage(w http.ResponseWriter, r *http.Request) {
|
|||
a.render(w, http.StatusOK, ViewData{Title: "Transactions", Page: "transactions", User: user, CSRFToken: a.csrfToken(r), Flash: a.readFlash(w, r), Data: DashboardData{User: user, Transactions: transactions}})
|
||||
}
|
||||
|
||||
func (a *App) lotteryPage(w http.ResponseWriter, r *http.Request) {
|
||||
user, _ := a.currentUser(r)
|
||||
lastPayout, err := a.store.SettleDueLotteryRound(r.Context())
|
||||
if err != nil {
|
||||
a.serverError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
updatedUser, err := a.store.GetUserDashboard(r.Context(), user.ID)
|
||||
if err != nil {
|
||||
a.serverError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
lottery, err := a.store.GetLotteryDashboard(r.Context(), user.ID)
|
||||
if err != nil {
|
||||
a.serverError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
a.renderLotteryPage(w, r, http.StatusOK, updatedUser, lottery, lastPayout, "")
|
||||
}
|
||||
|
||||
func (a *App) createLotteryDeposit(w http.ResponseWriter, r *http.Request) {
|
||||
user, _ := a.currentUser(r)
|
||||
if !a.verifyCSRF(r) {
|
||||
a.renderLotteryError(w, r, user, http.StatusBadRequest, "Invalid form token.")
|
||||
return
|
||||
}
|
||||
if err := r.ParseForm(); err != nil {
|
||||
a.serverError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
amountMinor, err := platform.ParseCZK(r.FormValue("amount"))
|
||||
if err != nil || amountMinor <= 0 {
|
||||
a.renderLotteryError(w, r, user, http.StatusBadRequest, "Enter a valid positive CZK amount.")
|
||||
return
|
||||
}
|
||||
|
||||
if err := a.store.CreateLotteryDeposit(r.Context(), user.ID, amountMinor); err != nil {
|
||||
switch {
|
||||
case errors.Is(err, db.ErrInsufficientFunds):
|
||||
a.renderLotteryError(w, r, user, http.StatusBadRequest, "Insufficient funds for this lottery deposit.")
|
||||
case errors.Is(err, db.ErrFrozen):
|
||||
a.renderLotteryError(w, r, user, http.StatusForbidden, "Lottery deposits are not available while the account is frozen.")
|
||||
default:
|
||||
a.renderLotteryError(w, r, user, http.StatusBadRequest, err.Error())
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
a.setFlash(w, "Lottery deposit added to the active pool.")
|
||||
http.Redirect(w, r, "/app/lottery", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
func (a *App) renderLotteryPage(w http.ResponseWriter, r *http.Request, status int, user *db.User, lottery *db.LotteryDashboard, lastPayout *db.LotteryPayoutView, errMsg string) {
|
||||
a.render(w, status, ViewData{
|
||||
Title: "Lottery",
|
||||
Page: "lottery",
|
||||
User: user,
|
||||
CSRFToken: a.csrfToken(r),
|
||||
Flash: a.readFlash(w, r),
|
||||
Error: errMsg,
|
||||
Data: LotteryPageData{
|
||||
User: user,
|
||||
Lottery: lottery,
|
||||
LastPayout: lastPayout,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func (a *App) renderLotteryError(w http.ResponseWriter, r *http.Request, user *db.User, status int, errMsg string) {
|
||||
lottery, err := a.store.GetLotteryDashboard(r.Context(), user.ID)
|
||||
if err != nil {
|
||||
a.serverError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
updatedUser, err := a.store.GetUserDashboard(r.Context(), user.ID)
|
||||
if err != nil {
|
||||
a.serverError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
a.renderLotteryPage(w, r, status, updatedUser, lottery, nil, errMsg)
|
||||
}
|
||||
|
||||
func (a *App) showAdminLogin(w http.ResponseWriter, r *http.Request) {
|
||||
a.render(w, http.StatusOK, ViewData{Title: "Admin login", Page: "admin_login", CSRFToken: a.csrfToken(r), Flash: a.readFlash(w, r)})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue