Tasks: able to get and print tasks

This commit is contained in:
Filip Rojek 2023-12-31 01:39:42 +01:00
parent cf20935e01
commit 4c9765b4a3
10 changed files with 103 additions and 24 deletions

View File

@ -4,7 +4,7 @@ import env from './environment'
const db = new Sequelize(env.DB_DATABASE, env.DB_USERNAME, env.DB_PASSWORD, { const db = new Sequelize(env.DB_DATABASE, env.DB_USERNAME, env.DB_PASSWORD, {
host: env.DB_HOST, host: env.DB_HOST,
dialect: 'mariadb', dialect: 'mariadb',
logging: false logging: true
}) })
export default db export default db

View File

@ -0,0 +1,19 @@
import { Request, Response } from 'express'
import Task from '../models/Task'
export async function get(req: Request, res: Response) {
try {
const data = await Task.findAll()
res.json(data)
} catch (error) {
res.status(500).send(error)
}
}
export async function add(req: Request, res: Response) {
try {
const task = await Task.create(req.body)
res.json(task)
} catch (error) {
res.status(500).send(error)
}
}

43
api/src/models/Task.ts Normal file
View File

@ -0,0 +1,43 @@
import { DataTypes, Model } from 'sequelize'
import path from 'path'
import db from '../config/sequelize.config'
class Instance extends Model {}
Instance.init(
{
_id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
allowNull: false,
unique: true
},
title: {
type: DataTypes.STRING,
allowNull: false
},
client: {
type: DataTypes.STRING,
allowNull: true
},
project: {
type: DataTypes.STRING,
allowNull: true
},
timeStart: {
type: DataTypes.BIGINT,
allowNull: false
},
timeEnd: {
type: DataTypes.BIGINT,
allowNull: true
}
},
{
sequelize: db,
tableName: path.basename(__filename).split('.')[0].toLowerCase()
}
)
export default Instance

View File

@ -1,6 +1,6 @@
import { DataTypes, Model } from 'sequelize' import { DataTypes, Model } from 'sequelize'
import path from 'path' import path from 'path'
import db from '../config/sequelize.config.ts' import db from '../config/sequelize.config'
class Instance extends Model {} class Instance extends Model {}

View File

@ -0,0 +1,8 @@
import { Router } from 'express'
import * as taskController from "../controllers/taskController"
export const router = Router()
//const mws = [handleValidation.handleValidationError]
router.get("/task/get", taskController.get)
router.post("/task/add", taskController.add)

View File

@ -1,9 +1,11 @@
import { Request, Response, Router } from 'express' import { Request, Response, Router } from 'express'
import { router as rootRoutes } from './rootRoutes' import { router as rootRoutes } from './rootRoutes'
import { router as apiRoutes } from './apiRoutes'
export const router = Router() export const router = Router()
router.use(rootRoutes) router.use(rootRoutes)
router.use("/api/v1", apiRoutes)
// 404 // 404
router.use((req: Request, res: Response) => { router.use((req: Request, res: Response) => {

View File

@ -27,6 +27,8 @@ export default {
return { return {
username: AppStore.data.user.username username: AppStore.data.user.username
} }
} },
methods: {},
async created() {}
} }
</script> </script>

View File

@ -29,7 +29,6 @@ const durationInSeconds = computed(() =>
Math.floor((props.task.timeEnd - props.task.timeStart) / 1000) Math.floor((props.task.timeEnd - props.task.timeStart) / 1000)
) )
const normalizedDuration = computed(() => formatDuration(durationInSeconds.value)) const normalizedDuration = computed(() => formatDuration(durationInSeconds.value))
console.log(props.task.title)
</script> </script>
<template> <template>
@ -40,7 +39,6 @@ console.log(props.task.title)
class="title" class="title"
placeholder="What are you working on?" placeholder="What are you working on?"
:value="props.task.title" :value="props.task.title"
:size="props.task.title.length + 1"
/> />
</span> </span>
<span> <span>

View File

@ -1,4 +1,5 @@
export default { export default {
api_url: 'http://localhost:6060/api/v1',
data: { data: {
user: { user: {
_id: '1234', _id: '1234',
@ -7,26 +8,28 @@ export default {
}, },
newTask: {}, newTask: {},
fetchedTasks: [ fetchedTasks: [
{ //{
_id: 1, // _id: 1,
title: 'frontend', // title: 'Loading',
project: 'TiM', // project: 'Loading',
client: 'Filip Rojek', // client: 'Loading',
timeStart: 1703960133061, // timeStart: 1703960133061,
timeEnd: 1703960141414 // timeEnd: 1703960141414
}, //}
{
_id: 2,
title: 'setting up',
project: 'l2tp vpn',
client: 'IS Media',
timeStart: 1703960133061,
timeEnd: 1703960141414
}
] ]
}, },
// Methods that you need, for e.g fetching data from server etc. async fetchData() {
fetchData() { try {
// fetch logic const response = await fetch(this.api_url + '/task/get')
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`)
}
const data = await response.json()
this.data.fetchedTasks = data
console.log('Received data:', data)
} catch (error) {
console.error('Error fetching data:', error)
}
} }
} }

View File

@ -25,6 +25,10 @@ export default {
return { return {
tasks: AppStore.data.fetchedTasks tasks: AppStore.data.fetchedTasks
} }
},
async mounted() {
await AppStore.fetchData()
this.tasks = AppStore.data.fetchedTasks
} }
} }
</script> </script>