package webserver

import (
	"fmt"
	"net/http"
	"os"

	"git.ctdo.de/henne/blitzer-v2/db"
	"github.com/gin-gonic/gin"
	"github.com/sirupsen/logrus"
)

func HandleDelete(ctx *gin.Context) {
	filename := fmt.Sprintf("images/original/ticket-%s.jpg", ctx.Param("id"))
	os.Remove(filename)
	if err := db.DB.Where("id = ?", ctx.Param("id")).Delete(&db.SpeedingTicket{}).Error; err != nil {
		logrus.Error(err)
		ctx.String(500, "internal server error")
		return
	}
	ctx.Redirect(http.StatusTemporaryRedirect, "/")
}
