67 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			67 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package server
 | |
| 
 | |
| import (
 | |
| 	"fmt"
 | |
| 	"net/http"
 | |
| 
 | |
| 	"github.com/gin-gonic/gin"
 | |
| 	"github.com/google/uuid"
 | |
| 	"github.com/pressly/goose/v3"
 | |
| )
 | |
| 
 | |
| func (h *Handler) clearDatabase(c *gin.Context) {
 | |
| 	if err := goose.Reset(h.Service.DB, "schema"); err != nil {
 | |
| 		c.AbortWithError(http.StatusInternalServerError, err)
 | |
| 	}
 | |
| 
 | |
| 	if err := goose.Up(h.Service.DB, "schema"); err != nil {
 | |
| 		c.AbortWithError(http.StatusInternalServerError, err)
 | |
| 	}
 | |
| }
 | |
| 
 | |
| func (h *Handler) deleteBudget(c *gin.Context) {
 | |
| 	budgetID := c.Param("budgetid")
 | |
| 	budgetUUID, err := uuid.Parse(budgetID)
 | |
| 	if err != nil {
 | |
| 		c.AbortWithStatus(http.StatusBadRequest)
 | |
| 		return
 | |
| 	}
 | |
| 
 | |
| 	h.clearBudgetData(c, budgetUUID)
 | |
| 
 | |
| 	err = h.Service.DeleteBudget(c.Request.Context(), budgetUUID)
 | |
| 	if err != nil {
 | |
| 		c.AbortWithError(http.StatusInternalServerError, err)
 | |
| 		return
 | |
| 	}
 | |
| }
 | |
| 
 | |
| func (h *Handler) clearBudgetData(c *gin.Context, budgetUUID uuid.UUID) {
 | |
| 	rows, err := h.Service.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.DeleteAllTransactions(c.Request.Context(), budgetUUID)
 | |
| 	if err != nil {
 | |
| 		c.AbortWithError(http.StatusInternalServerError, err)
 | |
| 		return
 | |
| 	}
 | |
| 
 | |
| 	fmt.Printf("Deleted %d transactions\n", rows)
 | |
| }
 | |
| 
 | |
| func (h *Handler) clearBudget(c *gin.Context) {
 | |
| 	budgetID := c.Param("budgetid")
 | |
| 	budgetUUID, err := uuid.Parse(budgetID)
 | |
| 	if err != nil {
 | |
| 		c.AbortWithStatus(http.StatusBadRequest)
 | |
| 		return
 | |
| 	}
 | |
| 
 | |
| 	h.clearBudgetData(c, budgetUUID)
 | |
| }
 |