change users crud to items
All checks were successful
Build and Deploy Frontend / build-and-deploy (push) Successful in 8s
Build and Deploy API / build-and-deploy (push) Successful in 10s

This commit is contained in:
2026-03-21 23:44:26 -05:00
parent a9b4d136d5
commit 7ab03d8073
13 changed files with 172 additions and 151 deletions

View File

@@ -5,37 +5,38 @@
import { ref, onMounted } from "vue";
import { useRoute, useRouter } from "vue-router";
import { useUsersStore } from "../stores/UsersStore.ts";
import type { User } from "../models/User.ts";
import { useItemsStore } from "../stores/ItemsStore.ts";
import type { Item } from "../models/Item.ts";
const store = useUsersStore();
const store = useItemsStore();
const route = useRoute();
const router = useRouter();
const user = ref<User>({
const item = ref<Item>({
id: 0,
username: "",
email: "",
password: ""
name: "",
description: "",
createdAt: "",
lastEditedAt: ""
});
const id: string | undefined = route.params.id as string | undefined
onMounted(() => {
if(id) {
const existing = store.users.find(i => i.id == Number(id));
if (existing) user.value = { ...existing };
const existing = store.items.find(i => i.id == Number(id));
if (existing) item.value = { ...existing };
}
});
async function save(): Promise<void> {
if(id) {
await store.updateUser(Number(id), user.value);
await store.updateItem(Number(id), item.value);
} else {
await store.addUser(user.value);
await store.addItem(item.value);
}
router.push("/users"); // redirect
router.push("/items"); // redirect
}
</script>
@@ -43,10 +44,10 @@ async function save(): Promise<void> {
<template>
<div>
<h2>{{ id ? "Edit User" : "Create User" }}</h2> <!-- omg I love ternary operator :D -->
<h2>{{ id ? "Edit Item" : "Create Item" }}</h2> <!-- omg I love ternary operator :D -->
<form @submit.prevent="save">
<input v-model="user.username" placeholder="Name" />
<input v-model="item.name" placeholder="Name" />
<button type="submit">Save</button>
</form>

View File

@@ -3,14 +3,14 @@
import { onMounted } from "vue"
import { useRoute, useRouter } from "vue-router";
import { useUsersStore } from "../stores/UsersStore.ts"
import { useItemsStore } from "../stores/ItemsStore.ts"
import * as authApi from "../api/AuthApi";
const store = useUsersStore()
const store = useItemsStore()
const router = useRouter();
onMounted(() => {
store.fetchUsers()
store.fetchItems()
})
function logout() {
@@ -22,23 +22,23 @@ function logout() {
<template>
<div>
<h1>Users</h1>
<h1>Items</h1>
<router-link to="/user/new">Create User</router-link>
<router-link to="/user/new">Create Item</router-link>
<table>
<tr v-for="user in store.users" :key="user.id">
<td>{{ user.username }}</td>
<tr v-for="item in store.items" :key="item.id">
<td>{{ item.name }}</td>
<td>
<router-link :to="`/user/${user.id}`" custom v-slot="{ navigate }">
<router-link :to="`/item/${item.id}`" custom v-slot="{ navigate }">
<button @click="navigate" role="link">Edit</button>
</router-link>
<button @click="store.removeUser(user.id)">Delete</button>
<button @click="store.removeItem(item.id)">Delete</button>
</td>
</tr>
</table>
<button @click="logout()">Logout</button>
</div>
</template>
</template>