Skip to content

Commit d9212a1

Browse files
Merge pull request #15 from HE-Arc/5-database
5 database
2 parents bad3906 + 7dc66bc commit d9212a1

17 files changed

+241
-15
lines changed

api/Pipfile

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@ name = "pypi"
66
[packages]
77
django = "*"
88
djangorestframework = "*"
9+
django-cors-headers = "*"
10+
psycopg2-binary = "*"
911

10-
[dev-packages]
12+
[dev-packages]

api/Pipfile.lock

+85-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/fantasyforge/settings.py

+22-6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"""
1212

1313
from pathlib import Path
14+
import os
1415

1516
# Build paths inside the project like this: BASE_DIR / 'subdir'.
1617
BASE_DIR = Path(__file__).resolve().parent.parent
@@ -25,7 +26,10 @@
2526
# SECURITY WARNING: don't run with debug turned on in production!
2627
DEBUG = True
2728

28-
ALLOWED_HOSTS = []
29+
ALLOWED_HOSTS = [
30+
"127.0.0.1",
31+
"https://fantasy-forge.k8s.ing.he-arc.ch",
32+
]
2933

3034

3135
# Application definition
@@ -39,9 +43,10 @@
3943
'django.contrib.staticfiles',
4044
'fantasyforgeapp',
4145
'rest_framework',
46+
'corsheaders',
4247
]
43-
4448
MIDDLEWARE = [
49+
'corsheaders.middleware.CorsMiddleware',
4550
'django.middleware.security.SecurityMiddleware',
4651
'django.contrib.sessions.middleware.SessionMiddleware',
4752
'django.middleware.common.CommonMiddleware',
@@ -77,12 +82,15 @@
7782

7883
DATABASES = {
7984
'default': {
80-
'ENGINE': 'django.db.backends.sqlite3',
81-
'NAME': BASE_DIR / 'db.sqlite3',
85+
'ENGINE': 'django.db.backends.postgresql',
86+
'NAME': os.getenv('DB_NAME'),
87+
'USER': os.getenv('DB_USER'),
88+
'PASSWORD': os.getenv('DB_PASSWORD'),
89+
'HOST': os.getenv('DB_HOST', 'localhost'), # Default to localhost
90+
'PORT': os.getenv('DB_PORT', '5432'), # Default to 5432
8291
}
8392
}
8493

85-
8694
# Password validation
8795
# https://docs.djangoproject.com/en/5.1/ref/settings/#auth-password-validators
8896

@@ -117,9 +125,17 @@
117125
# Static files (CSS, JavaScript, Images)
118126
# https://docs.djangoproject.com/en/5.1/howto/static-files/
119127

120-
STATIC_URL = 'static/'
128+
STATIC_URL = '/static/'
121129

122130
# Default primary key field type
123131
# https://docs.djangoproject.com/en/5.1/ref/settings/#default-auto-field
124132

125133
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
134+
135+
# CORS settings
136+
CORS_ALLOWED_ORIGINS = [
137+
'http://localhost:5173',
138+
'https://fantasy-forge.k8s.ing.he-arc.ch',
139+
]
140+
141+
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

api/fantasyforge/urls.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@
2525

2626
urlpatterns = [
2727
path('admin/', admin.site.urls),
28-
path("", include(router.urls)),
28+
path("api/", include("fantasyforgeapp.urls")),
2929
]

api/fantasyforgeapp/admin.py

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
from django.contrib import admin
22

33
# Register your models here.
4+
from .models import *
5+
6+
admin.site.register(Character)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[
2+
{"model": "fantasyforgeapp.character", "pk": 1, "fields": {"name": "Place Holder"}}
3+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Generated by Django 5.1.6 on 2025-02-28 13:44
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
initial = True
9+
10+
dependencies = [
11+
]
12+
13+
operations = [
14+
migrations.CreateModel(
15+
name='Character',
16+
fields=[
17+
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
18+
('name', models.CharField(max_length=100)),
19+
],
20+
),
21+
]

api/fantasyforgeapp/serializers.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from rest_framework import serializers
2+
from .models import Character
3+
4+
class CharacterSerializer(serializers.ModelSerializer):
5+
class Meta:
6+
model = Character
7+
fields = [
8+
"id",
9+
"name",
10+
]
11+
12+
def create(self, validated_data):
13+
return Character.objects.create(**validated_data)
14+
15+
def update(self, instance, validated_data):
16+
instance.name = validated_data.get("name", instance.name)
17+
instance.save()
18+
return instance

api/fantasyforgeapp/urls.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from . import views
2+
from django.urls import path
3+
from rest_framework.routers import DefaultRouter
4+
from django.urls import include
5+
6+
router = DefaultRouter()
7+
router.register(r"characters", views.CharacterViewSet, basename="characters")
8+
9+
urlpatterns = [
10+
path("", include(router.urls)),
11+
]

api/fantasyforgeapp/views.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1-
from django.shortcuts import render
1+
2+
from .models import Character
3+
from .serializers import CharacterSerializer
4+
from rest_framework import viewsets
25

3-
# Create your views here.
6+
class CharacterViewSet(viewsets.ModelViewSet):
7+
queryset = Character.objects.all()
8+
serializer_class = CharacterSerializer

api/requirements.txt

780 Bytes
Binary file not shown.

frontend/package-lock.json

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frontend/src/components/NavBar.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { ref } from 'vue'
1515
</router-link>
1616
</li>
1717
<li>
18-
<router-link :to="{ name: 'home' }" class="text-gray-900 dark:text-gray-100 inline-block py-2 px-4 text-sm font-medium">
18+
<router-link :to="{ name: 'characters' }" class="text-gray-900 dark:text-gray-100 inline-block py-2 px-4 text-sm font-medium">
1919
Characters
2020
</router-link>
2121
</li>

frontend/src/main.js

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import './assets/main.css'
33
import { createApp } from 'vue'
44
import App from './App.vue'
55
import router from './router'
6+
import axios from "axios";
7+
axios.defaults.baseURL = import.meta.env.VITE_API_URL
68

79
const app = createApp(App)
810

frontend/src/router/index.js

+6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ const router = createRouter({
1717
// which is lazy-loaded when the route is visited.
1818
component: () => import('../views/AboutView.vue'),
1919
},
20+
21+
{
22+
path: '/characters',
23+
name: 'characters',
24+
component: () => import('../views/CharachtersView.vue'),
25+
}
2026
],
2127
})
2228

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
</script>
19+
<template>
20+
<div>
21+
<ul>
22+
<li v-for="character in characters" :key="character.id">
23+
{{ character.name }}
24+
</li>
25+
</ul>
26+
</div>
27+
</template>

frontend/src/views/Test.vue

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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("http://127.0.0.1:8000/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+
</script>
19+
<template>
20+
<div>
21+
<h1>Characters</h1>
22+
<ul>
23+
<li v-for="character in characters" :key="character.id">
24+
{{ character.name }}
25+
</li>
26+
</ul>
27+
</div>
28+
</template>

0 commit comments

Comments
 (0)