2024-12-28 16:30:48 +01:00
|
|
|
package character
|
2024-12-21 07:34:38 +01:00
|
|
|
|
|
|
|
|
import (
|
2024-12-28 16:30:48 +01:00
|
|
|
"bamort/database"
|
|
|
|
|
|
2024-12-21 22:06:33 +01:00
|
|
|
"encoding/json"
|
2024-12-21 17:59:49 +01:00
|
|
|
"fmt"
|
2024-12-21 07:34:38 +01:00
|
|
|
"net/http"
|
2024-12-21 22:06:33 +01:00
|
|
|
"os"
|
2024-12-21 18:11:39 +01:00
|
|
|
"path/filepath"
|
|
|
|
|
"strings"
|
2024-12-21 07:34:38 +01:00
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
Character Handlers
|
|
|
|
|
|
|
|
|
|
Add CRUD operations for characters:
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
func GetCharacters(c *gin.Context) {
|
2024-12-30 15:58:05 +01:00
|
|
|
var characters []Char
|
2024-12-28 16:30:48 +01:00
|
|
|
if err := database.DB.Find(&characters).Error; err != nil {
|
2024-12-21 07:34:38 +01:00
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to retrieve characters"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
c.JSON(http.StatusOK, characters)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func CreateCharacter(c *gin.Context) {
|
2024-12-30 15:58:05 +01:00
|
|
|
var character Char
|
2024-12-21 07:34:38 +01:00
|
|
|
if err := c.ShouldBindJSON(&character); err != nil {
|
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-28 16:30:48 +01:00
|
|
|
if err := database.DB.Create(&character).Error; err != nil {
|
2024-12-21 07:34:38 +01:00
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create character"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.JSON(http.StatusCreated, character)
|
|
|
|
|
}
|
2024-12-21 07:42:20 +01:00
|
|
|
|
2024-12-21 17:59:49 +01:00
|
|
|
// Upload files
|
|
|
|
|
func UploadFiles(c *gin.Context) {
|
|
|
|
|
// Get files from the request
|
|
|
|
|
file_vtt, err1 := c.FormFile("file_vtt")
|
|
|
|
|
file_csv, err2 := c.FormFile("file_csv")
|
|
|
|
|
|
|
|
|
|
if err1 != nil {
|
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "file_vtt is required"})
|
|
|
|
|
return
|
|
|
|
|
}
|
2024-12-21 18:11:39 +01:00
|
|
|
if !isValidFileType(file_vtt.Filename) {
|
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "File1 must be a .csv or .json file"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
// Validate file2 if provided
|
|
|
|
|
if file_csv != nil && !isValidFileType(file_csv.Filename) {
|
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "File2 must be a .csv or .json file"})
|
|
|
|
|
return
|
|
|
|
|
}
|
2024-12-21 17:59:49 +01:00
|
|
|
|
|
|
|
|
// Save File 1
|
|
|
|
|
err := c.SaveUploadedFile(file_vtt, fmt.Sprintf("./uploads/%s", file_vtt.Filename))
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to save file_vtt"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Save File 2 if provided
|
|
|
|
|
if err2 == nil {
|
|
|
|
|
err := c.SaveUploadedFile(file_csv, fmt.Sprintf("./uploads/%s", file_csv.Filename))
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to save file_csv"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"message": "Files uploaded successfully"})
|
2024-12-21 22:06:33 +01:00
|
|
|
|
|
|
|
|
// Open and parse JSON
|
2024-12-30 15:58:05 +01:00
|
|
|
var character Char
|
2024-12-24 07:54:27 +01:00
|
|
|
filePath := fmt.Sprintf("./uploads/%s", file_vtt.Filename)
|
2024-12-21 22:06:33 +01:00
|
|
|
fileContent, err := os.ReadFile(filePath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to read file"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if err := json.Unmarshal(fileContent, &character); err != nil {
|
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid JSON structure"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Save character data to the database
|
2024-12-28 16:30:48 +01:00
|
|
|
if err := SaveCharacterToDB(&character); err != nil {
|
2024-12-21 22:06:33 +01:00
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to save character to database"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
"message": "Character imported successfully",
|
|
|
|
|
"character": character,
|
|
|
|
|
})
|
2024-12-21 17:59:49 +01:00
|
|
|
}
|
2024-12-21 18:11:39 +01:00
|
|
|
|
|
|
|
|
func isValidFileType(filename string) bool {
|
|
|
|
|
allowedExtensions := []string{".csv", ".json"}
|
|
|
|
|
ext := strings.ToLower(filepath.Ext(filename))
|
|
|
|
|
for _, allowedExt := range allowedExtensions {
|
|
|
|
|
if ext == allowedExt {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|