From a18559a77ad010973cf2bd06651f85396664c467 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sascha=20K=C3=BChl?= Date: Thu, 10 Apr 2025 16:49:54 +0200 Subject: [PATCH] save bullpen to backend --- app/src/services/BullpenSessionService.ts | 24 +++++--- app/src/store/bullpen.ts | 18 +++++- app/src/types/Pitch.ts | 13 ++-- app/src/views/BullpenStats.vue | 17 +++--- app/src/views/Home.vue | 12 ++-- app/src/views/Login.vue | 3 + app/src/views/PreparePitch.vue | 61 ++++++++++++------- backend/controllers/auth.controller.js | 2 - .../controllers/bullpenSession.controller.js | 2 +- 9 files changed, 96 insertions(+), 56 deletions(-) diff --git a/app/src/services/BullpenSessionService.ts b/app/src/services/BullpenSessionService.ts index ebb17ce..1da0c34 100644 --- a/app/src/services/BullpenSessionService.ts +++ b/app/src/services/BullpenSessionService.ts @@ -2,7 +2,7 @@ import Pitch from "@/types/Pitch"; import User from "@/types/User"; import Bullpen from '@/types/Bullpen'; import PitchTypeService from '@/services/PitchTypeService'; -// import api from './Api'; +import api from '@/services/Api'; export class BullpenSessionService { public create(user: User): Bullpen { @@ -15,16 +15,26 @@ export class BullpenSessionService { } } - public finish(): void {} + public save(bullpen: Bullpen): Promise { + console.log(JSON.stringify(bullpen, null, 2)); + return api + .post('/bullpen_session', { + bullpen + }) + .then(response => { + return response.data; + }, (error) => { + console.log(JSON.stringify(error, null, 2)); + }); + } public createPitch(): Pitch { return { id: 0, - date: new Date(), - pitchType: PitchTypeService.getLocalPitchTypes()[0], - plannedPitchArea: 0, - realPitchArea: 0, - realPitchSubArea: 0 + pitchTime: new Date(), + pitchTypeId: PitchTypeService.getLocalPitchTypes()[0].id, + aimedArea: 0, + hitArea: 0 } } } diff --git a/app/src/store/bullpen.ts b/app/src/store/bullpen.ts index 1dd0e61..adf8416 100644 --- a/app/src/store/bullpen.ts +++ b/app/src/store/bullpen.ts @@ -1,4 +1,4 @@ -import { Module } from 'vuex'; +import {ActionContext, Module} from 'vuex'; import { RootState } from './index'; import Bullpen from '@/types/Bullpen'; import User from '@/types/User'; @@ -9,12 +9,24 @@ export interface BullpenState { bullpen: Bullpen | null; } +type BullpenActionContext = ActionContext; + const bullpen: Module = { namespaced: true, state: { bullpen: null }, + actions: { + start({commit}: BullpenActionContext, user: User) { + const bullpen = BullpenSessionService.create(user); + commit('start', bullpen); + }, + async finish({commit}: BullpenActionContext, bullpen: Bullpen) { + await BullpenSessionService.save(bullpen); + commit('finish'); + } + }, mutations: { - start(state, user: User) { - state.bullpen = BullpenSessionService.create(user); + start(state, bullpen: Bullpen) { + state.bullpen = bullpen; }, addPitch(state, pitch: Pitch) { state.bullpen?.pitches.push({ ...pitch }); diff --git a/app/src/types/Pitch.ts b/app/src/types/Pitch.ts index 4834be6..e2d63fa 100644 --- a/app/src/types/Pitch.ts +++ b/app/src/types/Pitch.ts @@ -1,10 +1,7 @@ -import PitchType from "@/types/PitchType"; - export default interface Pitch { id: number, - date: Date, - pitchType: PitchType, - plannedPitchArea: number, - realPitchArea: number, - realPitchSubArea: number -} \ No newline at end of file + pitchTime: Date, + pitchTypeId: number, + aimedArea: number, + hitArea: number +} diff --git a/app/src/views/BullpenStats.vue b/app/src/views/BullpenStats.vue index 9695a4b..9bd32c2 100644 --- a/app/src/views/BullpenStats.vue +++ b/app/src/views/BullpenStats.vue @@ -17,11 +17,14 @@ import { IonToolbar } from '@ionic/vue'; import {useRouter} from 'vue-router'; -import BullpenSessionService from "@/services/BullpenSessionService"; -import {ref} from 'vue'; +import {computed} from 'vue'; +import {useStore} from 'vuex'; const router = useRouter(); -const bps = ref(BullpenSessionService); +const store = useStore(); + +const pitcher = computed(() => store.state.auth.user); +const bullpen = computed(() => store.state.bullpen.bullpen); const gotoHome = () => { router.push('/home'); @@ -32,11 +35,11 @@ const gotoHome = () => { - Bullpen Stats for {{ bps.getPitcher().firstName }} {{ bps.getPitcher().lastName }} + Bullpen Stats for {{ pitcher.firstName }} {{ pitcher.lastName }} - + Pitch {{index+1}} @@ -48,11 +51,11 @@ const gotoHome = () => { Planned Pitch Area - {{pitch.plannedPitchArea}} + {{pitch.aimedArea}} Real Pitch Area - {{pitch.realPitchArea}} + {{pitch.hitArea}} diff --git a/app/src/views/Home.vue b/app/src/views/Home.vue index 2c85ff0..78b0478 100644 --- a/app/src/views/Home.vue +++ b/app/src/views/Home.vue @@ -12,10 +12,11 @@ const store = useStore(); const userImage = ref(null); // const userImage = ref('../assets/groot.jpg'); -const user = computed(() => store.state.auth.user); +const pitcher = computed(() => store.state.auth.user); +const isAuthenticated = computed(() => store.state.auth.isAuthenticated); -console.log('user', user.value); -if (user.value === undefined || user.value === null || user.value === '') { +console.log('user', pitcher.value); +if (pitcher.value === undefined || pitcher.value === null || pitcher.value === '') { router.push({ path: '/login' }); } @@ -48,7 +49,8 @@ const logout = () => { - User Home + Home of {{ pitcher.firstName }} {{ pitcher.lastName }} + Home @@ -62,7 +64,7 @@ const logout = () => { -
{{ user.firstName }} {{ user.lastName }}
+
{{ pitcher.firstName }} {{ pitcher.lastName }}
diff --git a/app/src/views/Login.vue b/app/src/views/Login.vue index d689fb1..306a3da 100644 --- a/app/src/views/Login.vue +++ b/app/src/views/Login.vue @@ -16,6 +16,9 @@ import { IonInput, IonIcon, } from "@ionic/vue"; + +// Todo: https://github.com/alanmontgomery/ionic-react-login + import { lockClosedOutline, personOutline } from 'ionicons/icons'; import { useForm } from 'vee-validate'; import { useRouter } from 'vue-router'; diff --git a/app/src/views/PreparePitch.vue b/app/src/views/PreparePitch.vue index 488d447..2556ab5 100644 --- a/app/src/views/PreparePitch.vue +++ b/app/src/views/PreparePitch.vue @@ -28,14 +28,16 @@ const pitch = ref(BullpenSessionService.createPitch()); const currentStep = ref(BullpenStep.Prepare); const pitcher = computed(() => store.state.auth.user); +const isAuthenticated = computed(() => store.state.auth.isAuthenticated); const pitchTypes = computed(() => store.state.pitchTypes.pitchTypes); +const bullpen = computed(() => store.state.bullpen); onMounted(async () => { - store.commit("bullpen/start", pitcher); + await store.dispatch("bullpen/start", pitcher.value); }); const setPitchType = (pitchType: PitchType) => { - pitch.value.pitchType = pitchType; + pitch.value.pitchTypeId = pitchType.id; } const gotoFinalizePitch = () => { @@ -44,29 +46,35 @@ const gotoFinalizePitch = () => { const finalizeAndNextPitch = () => { store.commit("bullpen/addPitch", pitch.value); + pitch.value = BullpenSessionService.createPitch(); currentStep.value = BullpenStep.Prepare; } const finalizeAndEndBullpen = () => { store.commit("bullpen/addPitch", pitch.value); + pitch.value = BullpenSessionService.createPitch(); + currentStep.value = BullpenStep.Prepare; + const bp = bullpen.value.bullpen; + bp.finishedAt = new Date(); + store.dispatch("bullpen/finish", bp); router.push({ name: 'BullpenStats' }); } const setPlannedPitchArea = (hitArea: number) => { if (currentStep.value === BullpenStep.Prepare) { - pitch.value.plannedPitchArea = hitArea; + pitch.value.aimedArea = hitArea; } else { - pitch.value.realPitchArea = hitArea; + pitch.value.hitArea = hitArea; } }; const pitchAreaCssClasses = (area: number, name: string) => { const classes = []; - if (pitch.value.plannedPitchArea === area) { + if (pitch.value.aimedArea === area) { classes.push(name + '-aim-selected'); } - if (pitch.value.realPitchArea === area) { + if (pitch.value.hitArea === area) { classes.push(name + '-hit-selected'); } @@ -78,7 +86,8 @@ const pitchAreaCssClasses = (area: number, name: string) => { - Prepare Pitch for {{ pitcher.firstName }} {{ pitcher.lastName }} + Pitching {{ pitcher.firstName }} {{ pitcher.lastName }} + Pitching @@ -87,7 +96,7 @@ const pitchAreaCssClasses = (area: number, name: string) => { Select Pitch type - + {{pitchType.abbreviation}} @@ -158,25 +167,31 @@ const pitchAreaCssClasses = (area: number, name: string) => { - Pitch +
+ + + + Pitch + + + +
+
+ + + + Save Pitch
&
Next Pitch
+
+ + Save Pitch
&
End Session
+
+
+
+
- - - - - - - - - - - - - -