From 4b2d8cf7b692b7406bdbc8e40f8c316d2b3ecd3d Mon Sep 17 00:00:00 2001
From: onsaliyo <gonzalo.mdvc@gmail.com>
Date: Sat, 20 Mar 2021 13:21:50 +0100
Subject: [PATCH] Se suben eventos a la BD, asociados a la discoteca del
 usuario

---
 src/app/discoteca.ts                          |  7 +++
 src/app/evento.ts                             | 10 ++--
 src/app/interfaces/discoteca-i.ts             |  2 +-
 src/app/interfaces/eventoi.ts                 | 12 ++++
 src/app/prompt-evento/prompt-evento.page.html | 13 ++++-
 src/app/prompt-evento/prompt-evento.page.ts   | 56 ++++++++++++++-----
 src/app/services/api.service.ts               |  7 +++
 src/app/tab1/tab1.service.ts                  | 14 +++++
 8 files changed, 102 insertions(+), 19 deletions(-)

diff --git a/src/app/discoteca.ts b/src/app/discoteca.ts
index 8114e04..8438b79 100644
--- a/src/app/discoteca.ts
+++ b/src/app/discoteca.ts
@@ -10,6 +10,13 @@ export class Discoteca {
     private eventos: Evento[];
     private descripcion: string;
 
+    setId(id: number): void{
+        this.id = id;
+    }
+
+    getId(): number{
+        return this.id;
+    }
 
     setNombre(nombre: string): void{
         this.nombre = nombre;
diff --git a/src/app/evento.ts b/src/app/evento.ts
index 0471ab1..4ce12b3 100644
--- a/src/app/evento.ts
+++ b/src/app/evento.ts
@@ -1,10 +1,12 @@
+import { Time } from "@angular/common";
+
 export class Evento {
     private id: number;
     nombre: string;
     localizacion: string;
     fecha: Date;
     dia: string;
-    hora: string;
+    hora: Time;
     descripcion: string;
     precio1: number;
     precio2: number;
@@ -49,11 +51,11 @@ export class Evento {
         return this.dia;
     }
 
-    setHora(fecha: Date): void{
-        this.hora = fecha.getHours()+":"+fecha.getMinutes();
+    setHora(time: Time): void{
+        this.hora = time;
     }
 
-    getHora(): string{
+    getHora(): Time{
         return this.hora;
     }
 
diff --git a/src/app/interfaces/discoteca-i.ts b/src/app/interfaces/discoteca-i.ts
index a3274f1..c5e5c93 100644
--- a/src/app/interfaces/discoteca-i.ts
+++ b/src/app/interfaces/discoteca-i.ts
@@ -1,5 +1,5 @@
 export interface DiscotecaI {
-    discotecaId: number,
+    discotecaID: number,
     nombre: string,
     telefono: number,
     localizacion: string
diff --git a/src/app/interfaces/eventoi.ts b/src/app/interfaces/eventoi.ts
index 961d4e7..8926fb3 100644
--- a/src/app/interfaces/eventoi.ts
+++ b/src/app/interfaces/eventoi.ts
@@ -1,2 +1,14 @@
+import { Time } from "@angular/common";
+
 export interface Eventoi {
+
+    discotecaID: number,
+    nombre: string,
+    localizacion: string, 
+    fecha: Date, 
+    hora: Time,
+    descripcion: string,
+    precio1: number,
+    precio2: number,
+
 }
diff --git a/src/app/prompt-evento/prompt-evento.page.html b/src/app/prompt-evento/prompt-evento.page.html
index 94845bd..8e2cd63 100644
--- a/src/app/prompt-evento/prompt-evento.page.html
+++ b/src/app/prompt-evento/prompt-evento.page.html
@@ -21,7 +21,7 @@
   <ion-item>
     <label>
       Fecha:
-      <ion-datetime displayFormat="DD/MM, HH:mm"placeholder="Elegir Fecha" formControlName="fecha"></ion-datetime>
+      <ion-datetime displayFormat="YYYY/DD/MM"placeholder="Elegir Fecha" formControlName="fecha"></ion-datetime>
     </label>
     <span
       *ngIf="fecha.errors.required && submitted">
@@ -29,6 +29,17 @@
     </span>
   </ion-item>
 
+  <ion-item>
+    <label>
+      Hora
+      <ion-datetime displayFormat="HH:MM"placeholder="Elegir Hora" formControlName="hora"></ion-datetime>
+    </label>
+    <span
+      *ngIf="hora.errors.required && submitted">
+      Tu evento necesita una hora
+    </span>
+  </ion-item>
+
 
   <ion-item>
     <label>
diff --git a/src/app/prompt-evento/prompt-evento.page.ts b/src/app/prompt-evento/prompt-evento.page.ts
index 7711c91..0e44ac1 100644
--- a/src/app/prompt-evento/prompt-evento.page.ts
+++ b/src/app/prompt-evento/prompt-evento.page.ts
@@ -3,6 +3,9 @@ import { FormControl, FormGroup, Validators } from '@angular/forms';
 import { Tab1Service } from '../tab1/tab1.service';
 import { Evento } from '../evento';
 import { Router } from '@angular/router';
+import { Eventoi } from '../interfaces/eventoi';
+import { DatePipe } from '@angular/common'
+import { ViewEventoPageRoutingModule } from '../view-evento/view-evento-routing.module';
 
 @Component({
   selector: 'app-prompt-evento',
@@ -10,17 +13,17 @@ import { Router } from '@angular/router';
   styleUrls: ['./prompt-evento.page.scss'],
 })
 export class PromptEventoPage implements OnInit{
-
   submitted = false;
   eventoForm = new FormGroup({
-      nombre: new FormControl('', Validators.required),
-      fecha: new FormControl('', Validators.required),
-      precio1: new FormControl('', Validators.required),
-      precio2: new FormControl('', Validators.required),
-      descripcion: new FormControl('', Validators.required),
+      nombre: new FormControl(null, Validators.required),
+      fecha: new FormControl(null, Validators.required),
+      hora: new FormControl(null, Validators.required),
+      precio1: new FormControl(null, Validators.required),
+      precio2: new FormControl(null, Validators.required),
+      descripcion: new FormControl(null, Validators.required),
   });
 
-  eventos: Evento[];
+  eventos: Eventoi[];
 
   constructor(private tab1Service: Tab1Service, private router: Router) {
 
@@ -36,28 +39,38 @@ export class PromptEventoPage implements OnInit{
     if (this.eventoForm.valid){
       let evento = new Evento();
       this.asignarEvento(evento);
-      this.router.navigate(['/tabs']);
+
     }
   }
   asignarEvento(evento: Evento){
     evento.setNombre(this.eventoForm.get('nombre').value);
     evento.setDesc(this.eventoForm.get('descripcion').value);
-    evento.setFecha(this.eventoForm.get('fecha').value);
+    var fecha = this.eventoForm.get('fecha').value;
+    fecha = fecha.split("T")[0];
+    evento.setFecha(fecha);
+    var hora = this.eventoForm.get('hora').value;
+    hora = hora.split("T")[1];
+    hora = hora.split(":")[0]+(":")+hora.split(":")[1];
+    evento.setHora(hora);
     evento.setPrecio1(this.eventoForm.get('precio1').value);
     evento.setPrecio2(this.eventoForm.get('precio2').value);
     //evento.setDia(evento.getFecha()); el datetime de Ion devuelve un String, no se puede 
-    //evento.setHora(evento.getFecha()); transformar en dia y hora
     if (!this.tab1Service.eventos){
       this.tab1Service.initEventos();}
     if(!this.tab1Service.eventoForms){
       this.tab1Service.initEventoForms();}
+    let eventoInterface = this.interfaceEvento(evento);
     if(this.tab1Service.editarEvento==true){
-      this.tab1Service.eventos[this.tab1Service.eventoIndex] = evento;
-      this.tab1Service.eventoForms[this.tab1Service.eventoIndex] = this.eventoForm;
+      this.tab1Service.updateEvento(eventoInterface);
+      //this.tab1Service.eventos[this.tab1Service.eventoIndex] = evento;
+      //this.tab1Service.eventoForms[this.tab1Service.eventoIndex] = this.eventoForm;
     }
     else{
       this.tab1Service.eventos.push(evento);
-      this.tab1Service.eventoForms.push(this.eventoForm);}
+      this.tab1Service.postEvento(eventoInterface);
+      //this.tab1Service.eventos.push(evento);
+      //this.tab1Service.eventoForms.push(this.eventoForm);
+    }
     
   }
 
@@ -84,6 +97,23 @@ export class PromptEventoPage implements OnInit{
   get descripcion(){
     return this.eventoForm.get('descripcion');
   }
+
+  interfaceEvento(evento: Evento): Eventoi{
+    let eventoInterface: Eventoi;
+    eventoInterface = {
+      discotecaID: this.tab1Service.discotecaI.discotecaID,
+      nombre: evento.nombre,
+      localizacion: this.tab1Service.discotecaI.localizacion,
+      fecha: evento.fecha,
+      hora: evento.hora,
+      descripcion: evento.descripcion,
+      precio1: evento.precio1,
+      precio2: evento.precio2,
+    }
+
+    return eventoInterface;
+  }
+
   
 
 }
diff --git a/src/app/services/api.service.ts b/src/app/services/api.service.ts
index cf30afc..9c3bd9a 100644
--- a/src/app/services/api.service.ts
+++ b/src/app/services/api.service.ts
@@ -6,6 +6,7 @@ import { User } from '../interfaces/user';
 import { Discoteca } from '../discoteca';
 import { UserLogin } from '../interfaces/user-login';
 import { DiscotecaI } from '../interfaces/discoteca-i';
+import { Eventoi } from '../interfaces/eventoi';
 
 
 @Injectable({
@@ -32,6 +33,12 @@ export class ApiService {
 
   }
 
+  postEvento(eventoInterface: Eventoi): Observable<Eventoi>{
+
+    return this.http.post<Eventoi>(this.baseUrl+"/evento", eventoInterface);
+    
+  }
+
 
 
 
diff --git a/src/app/tab1/tab1.service.ts b/src/app/tab1/tab1.service.ts
index 7df4ad2..aa1db15 100644
--- a/src/app/tab1/tab1.service.ts
+++ b/src/app/tab1/tab1.service.ts
@@ -8,6 +8,7 @@ import { NumericValueAccessor } from '@ionic/angular';
 import { DiscotecaI } from '../interfaces/discoteca-i';
 import { ApiService } from '../services/api.service';
 import { Router } from '@angular/router';
+import { Eventoi } from '../interfaces/eventoi';
 
 
 @Injectable({
@@ -33,6 +34,7 @@ export class Tab1Service implements OnInit{
 
   initValues(): void{
     this.discoteca = new Discoteca();
+    this.discoteca.setId(this.discotecaI.discotecaID);
     this.discoteca.setNombre(this.discotecaI.nombre);
     this.discoteca.setTelefono(this.discotecaI.telefono);
     this.discoteca.setLocalizacion(this.discotecaI.localizacion);
@@ -89,4 +91,16 @@ export class Tab1Service implements OnInit{
   }
   }
 
+  postEvento(evento: Eventoi){
+    this.apiService.postEvento(evento)
+    .subscribe(evento => {
+      this.router.navigate(['/tabs']);
+    })
+
+  }
+
+  updateEvento(evento: Eventoi){
+
+  }
+
 }