Skip to content

Commit c1a6ebd

Browse files
Merge pull request #18 from HE-Arc/12-add-character-element
add basic actions on characters (CRUD)
2 parents c41b18d + 82e49f8 commit c1a6ebd

File tree

6 files changed

+151
-30
lines changed

6 files changed

+151
-30
lines changed

api/fantasyforge/settings.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
DEBUG = True
3131

3232
ALLOWED_HOSTS = [
33-
"127.0.0.1",
33+
"localhost",
3434
"api-fantasy-forge.k8s.ing.he-arc.ch",
3535
]
3636

api/fantasyforgeapp/forms.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from django import forms
2+
from .models import Character
3+
4+
class CharacterForm(forms.ModelForm):
5+
class Meta:
6+
model = Character
7+
fields = ['name']

frontend/src/router/index.js

+14-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,20 @@ const router = createRouter({
2121
{
2222
path: '/characters',
2323
name: 'characters',
24-
component: () => import('../views/CharachtersView.vue'),
25-
}
24+
component: () => import('../views/CharactersView.vue'),
25+
},
26+
27+
{
28+
path: '/characters/:id',
29+
name: 'character-edit',
30+
component: () => import('../views/CharacterEdition.vue'),
31+
},
32+
33+
{
34+
path: '/characters/new',
35+
name: 'character-new',
36+
component: () => import('../views/CharacterEdition.vue'),
37+
},
2638
],
2739
})
2840

frontend/src/views/CharachtersView.vue

-27
This file was deleted.
+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<script setup>
2+
import { ref, onMounted, watch } from 'vue';
3+
import { useRoute, useRouter } from 'vue-router';
4+
import axios from 'axios';
5+
6+
// State variables
7+
const name = ref('');
8+
const message = ref('');
9+
const isEdit = ref(false); // Flag to check if we are in edit mode
10+
const characterId = ref(null);
11+
12+
// Fetch character data for editing
13+
const route = useRoute();
14+
const router = useRouter();
15+
16+
// On mounted, check if we're editing (via URL param)
17+
onMounted(async () => {
18+
if (route.params.id) {
19+
isEdit.value = true;
20+
characterId.value = route.params.id;
21+
await fetchCharacter();
22+
}
23+
});
24+
25+
// Fetch the character data by ID (for editing)
26+
const fetchCharacter = async () => {
27+
try {
28+
const response = await axios.get(`/api/characters/${characterId.value}/`);
29+
name.value = response.data.name; // Pre-fill the form with the existing data
30+
} catch (error) {
31+
message.value = 'Error fetching character data!';
32+
console.error(error);
33+
}
34+
};
35+
36+
// Handle form submission (POST for create, PUT for edit)
37+
const submitForm = async () => {
38+
try {
39+
if (isEdit.value) {
40+
// PUT request for editing an existing character
41+
const response = await axios.put(`/api/characters/${characterId.value}/`, { name: name.value });
42+
message.value = `Character "${response.data.name}" updated successfully!`;
43+
} else {
44+
// POST request for creating a new character
45+
const response = await axios.post('/api/characters/', { name: name.value });
46+
message.value = `Character "${response.data.name}" created successfully!`;
47+
}
48+
49+
router.push('/characters'); // Redirect to the list page after submission
50+
} catch (error) {
51+
message.value = 'Error submitting the form!';
52+
console.error(error);
53+
}
54+
};
55+
</script>
56+
57+
<template>
58+
<div>
59+
<h2>{{ isEdit ? 'Edit' : 'Create' }} Character</h2>
60+
<form @submit.prevent="submitForm">
61+
<input v-model="name" type="text" placeholder="Character Name" required />
62+
<button type="submit">{{ isEdit ? 'Update' : 'Create' }} Character</button>
63+
</form>
64+
65+
<p v-if="message">{{ message }}</p>
66+
</div>
67+
</template>
68+
69+
<style scoped>
70+
input {
71+
padding: 8px;
72+
margin-right: 10px;
73+
border: 1px solid #ccc;
74+
border-radius: 4px;
75+
}
76+
button {
77+
padding: 8px 12px;
78+
background-color: blue;
79+
color: white;
80+
border: none;
81+
cursor: pointer;
82+
}
83+
</style>

frontend/src/views/CharactersView.vue

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<script setup>
2+
import axios from "axios";
3+
import { ref, onMounted } from "vue";
4+
const characters = ref([]);
5+
6+
const fetchCharacters = async () => {
7+
try {
8+
const res = await axios.get("/api/characters/");
9+
characters.value = res.data;
10+
} catch (error) {
11+
console.error("Error fetching characters:", error);
12+
}
13+
};
14+
15+
onMounted(() => {
16+
fetchCharacters();
17+
});
18+
const deleteCharacter = async (id) => {
19+
try {
20+
await axios.delete(`/api/characters/${id}/`);
21+
characters.value = characters.value.filter(character => character.id !== id);
22+
} catch (error) {
23+
console.error("Error deleting character:", error);
24+
}
25+
};
26+
</script>
27+
<template>
28+
<div>
29+
<div class="flex justify-end mb-4">
30+
<router-link :to="{ name: 'character-new' }" class="bg-red-500 text-white px-4 py-2 rounded">Add Character</router-link>
31+
</div>
32+
<ul class="list-none p-0">
33+
<li v-for="character in characters" :key="character.id" class="p-2 border-b border-gray-300 flex justify-between items-center">
34+
<span>{{ character.name }}</span>
35+
<div>
36+
<router-link :to="{ name: 'character-edit', params: { id: character.id } }">
37+
✏️
38+
</router-link>
39+
<button @click="deleteCharacter(character.id)">
40+
<router-link :to="{ name: 'characters' }">🗑️</router-link>
41+
</button>
42+
</div>
43+
</li>
44+
</ul>
45+
</div>
46+
</template>

0 commit comments

Comments
 (0)