59 lines
1.2 KiB
Go
59 lines
1.2 KiB
Go
package http
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/google/uuid"
|
|
"github.com/pressly/goose/v3"
|
|
)
|
|
|
|
type AdminData struct {
|
|
}
|
|
|
|
func (h *Handler) admin(c *gin.Context) {
|
|
d := AdminData{}
|
|
|
|
c.HTML(http.StatusOK, "admin.html", d)
|
|
}
|
|
|
|
func (h *Handler) clearDatabase(c *gin.Context) {
|
|
d := AdminData{}
|
|
|
|
if err := goose.Down(h.Service.LegacyDB, "schema"); err != nil {
|
|
c.AbortWithError(http.StatusInternalServerError, err)
|
|
}
|
|
|
|
if err := goose.Up(h.Service.LegacyDB, "schema"); err != nil {
|
|
c.AbortWithError(http.StatusInternalServerError, err)
|
|
}
|
|
|
|
c.HTML(http.StatusOK, "admin.html", d)
|
|
}
|
|
|
|
func (h *Handler) clearBudget(c *gin.Context) {
|
|
budgetID := c.Param("budgetid")
|
|
budgetUUID, err := uuid.Parse(budgetID)
|
|
if err != nil {
|
|
c.Redirect(http.StatusTemporaryRedirect, "/login")
|
|
return
|
|
}
|
|
|
|
rows, err := h.Service.DB.DeleteAllAssignments(c.Request.Context(), budgetUUID)
|
|
if err != nil {
|
|
c.AbortWithError(http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
|
|
fmt.Printf("Deleted %d assignments\n", rows)
|
|
|
|
rows, err = h.Service.DB.DeleteAllTransactions(c.Request.Context(), budgetUUID)
|
|
if err != nil {
|
|
c.AbortWithError(http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
|
|
fmt.Printf("Deleted %d transactions\n", rows)
|
|
}
|