added file upload for import
This commit is contained in:
@@ -6,6 +6,7 @@ Add handlers for user registration and login:
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -166,3 +167,34 @@ func DeleteAusruestung(c *gin.Context) {
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"message": "Ausruestung deleted successfully"})
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
// 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"})
|
||||
}
|
||||
|
||||
@@ -30,6 +30,7 @@ func main() {
|
||||
r.GET("/ausruestung/:character_id", AuthMiddleware(), GetAusruestung)
|
||||
r.PUT("/ausruestung/:ausruestung_id", AuthMiddleware(), UpdateAusruestung)
|
||||
r.DELETE("/ausruestung/:ausruestung_id", AuthMiddleware(), DeleteAusruestung)
|
||||
r.POST("/upload", AuthMiddleware(), UploadFiles)
|
||||
|
||||
r.Run(":8180") // Start server on port 8080
|
||||
}
|
||||
|
||||
@@ -4,12 +4,15 @@
|
||||
<li>
|
||||
<router-link to="/dashboard" active-class="active">Dashboard</router-link>
|
||||
</li>
|
||||
<li v-if="!isLoggedIn">
|
||||
<router-link to="/register" active-class="active">Register</router-link>
|
||||
<li>
|
||||
<router-link to="/upload" active-class="active">Import Data</router-link>
|
||||
</li>
|
||||
<li v-if="isLoggedIn">
|
||||
<button @click="logout">Logout</button>
|
||||
</li>
|
||||
<li v-if="!isLoggedIn">
|
||||
<router-link to="/register" active-class="active">Register</router-link>
|
||||
</li>
|
||||
</ul>
|
||||
</nav>
|
||||
</template>
|
||||
|
||||
@@ -4,12 +4,17 @@ import LoginView from "../views/LoginView.vue";
|
||||
import RegisterView from "../views/RegisterView.vue";
|
||||
import DashboardView from "../views/DashboardView.vue";
|
||||
import AusruestungView from "../views/AusruestungView.vue";
|
||||
import FileUploadPage from "../views/FileUploadPage.vue";
|
||||
|
||||
|
||||
|
||||
|
||||
const routes = [
|
||||
{ path: "/", name: "Login", component: LoginView },
|
||||
{ path: "/register", name: "Register", component: RegisterView },
|
||||
{ path: "/dashboard", name: "Dashboard", component: DashboardView, meta: { requiresAuth: true } },
|
||||
{ path: "/ausruestung/:characterId", name: "Ausruestung", component: AusruestungView, meta: { requiresAuth: true } },
|
||||
{ path: "/upload", name: "FileUpload", component: FileUploadPage },
|
||||
];
|
||||
|
||||
const router = createRouter({
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
<template>
|
||||
<div class="upload-page">
|
||||
<h2>Import Data</h2>
|
||||
<form @submit.prevent="handleUpload">
|
||||
<div>
|
||||
<label for="file_vtt">Char VTT:</label>
|
||||
<input type="file" id="file_vtt" @change="onFileChange($event, 1)" />
|
||||
</div>
|
||||
<div>
|
||||
<label for="file_csv">Char CSS (optional):</label>
|
||||
<input type="file" id="file_csv" @change="onFileChange($event, 2)" />
|
||||
</div>
|
||||
<button type="submit" :disabled="!file_vtt">Upload</button>
|
||||
</form>
|
||||
<p v-if="error" class="error">{{ error }}</p>
|
||||
<p v-if="success" class="success">{{ success }}</p>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import API from "../utils/api";
|
||||
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
file_vtt: null,
|
||||
file_csv: null,
|
||||
error: "",
|
||||
success: "",
|
||||
};
|
||||
},
|
||||
methods: {
|
||||
onFileChange(event, fileNumber) {
|
||||
const file = event.target.files[0];
|
||||
if (fileNumber === 1) this.file_vtt = file;
|
||||
if (fileNumber === 2) this.file_csv = file;
|
||||
},
|
||||
async handleUpload() {
|
||||
try {
|
||||
const formData = new FormData();
|
||||
formData.append("file_vtt", this.file_vtt);
|
||||
if (this.file_csv) formData.append("file_csv", this.file_csv);
|
||||
|
||||
const response = await API.post("/upload", formData, {
|
||||
headers: {
|
||||
"Content-Type": "multipart/form-data",
|
||||
},
|
||||
});
|
||||
|
||||
this.success = "Files uploaded successfully!";
|
||||
this.error = "";
|
||||
} catch (err) {
|
||||
this.error = err.response?.data?.error || "Failed to upload files.";
|
||||
this.success = "";
|
||||
}
|
||||
},
|
||||
},
|
||||
};
|
||||
</script>
|
||||
|
||||
<style>
|
||||
.upload-page {
|
||||
padding: 1rem;
|
||||
}
|
||||
|
||||
.error {
|
||||
color: red;
|
||||
}
|
||||
|
||||
.success {
|
||||
color: green;
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user