From 5fa5cda7f2c1a825d8f6189b86267d41c1a0bd9e Mon Sep 17 00:00:00 2001 From: Pablo Bonilla Date: Sun, 18 Jul 2021 14:21:45 -0600 Subject: [PATCH] Add survey questions with their options respectively --- .../datasurvey/web/rest/EncuestaResource.java | 76 ++++- .../encuesta/service/encuesta.service.ts | 12 + .../update/encuesta-update.component.html | 115 +++++++- .../update/encuesta-update.component.ts | 279 +++++++++++------- .../paper-dashboard/_datasurvey-form.scss | 2 + 5 files changed, 363 insertions(+), 121 deletions(-) diff --git a/src/main/java/org/datasurvey/web/rest/EncuestaResource.java b/src/main/java/org/datasurvey/web/rest/EncuestaResource.java index bcf74c8..2d79429 100644 --- a/src/main/java/org/datasurvey/web/rest/EncuestaResource.java +++ b/src/main/java/org/datasurvey/web/rest/EncuestaResource.java @@ -2,15 +2,20 @@ package org.datasurvey.web.rest; import java.net.URI; import java.net.URISyntaxException; +import java.util.ArrayList; import java.util.List; import java.util.Objects; import java.util.Optional; +import java.util.stream.Collectors; +import java.util.stream.Stream; import javax.validation.Valid; import javax.validation.constraints.NotNull; +import org.datasurvey.domain.EPreguntaAbierta; +import org.datasurvey.domain.EPreguntaCerrada; +import org.datasurvey.domain.EPreguntaCerradaOpcion; import org.datasurvey.domain.Encuesta; import org.datasurvey.repository.EncuestaRepository; -import org.datasurvey.service.EncuestaQueryService; -import org.datasurvey.service.EncuestaService; +import org.datasurvey.service.*; import org.datasurvey.service.criteria.EncuestaCriteria; import org.datasurvey.web.rest.errors.BadRequestAlertException; import org.slf4j.Logger; @@ -41,14 +46,26 @@ public class EncuestaResource { private final EncuestaQueryService encuestaQueryService; + private final EPreguntaCerradaService ePreguntaCerradaService; + + private final EPreguntaAbiertaService ePreguntaAbiertaService; + + private final EPreguntaCerradaOpcionService ePreguntaCerradaOpcionService; + public EncuestaResource( EncuestaService encuestaService, EncuestaRepository encuestaRepository, - EncuestaQueryService encuestaQueryService + EncuestaQueryService encuestaQueryService, + EPreguntaCerradaService ePreguntaCerradaService, + EPreguntaAbiertaService ePreguntaAbiertaService, + EPreguntaCerradaOpcionService ePreguntaCerradaOpcionService ) { this.encuestaService = encuestaService; this.encuestaRepository = encuestaRepository; this.encuestaQueryService = encuestaQueryService; + this.ePreguntaCerradaService = ePreguntaCerradaService; + this.ePreguntaAbiertaService = ePreguntaAbiertaService; + this.ePreguntaCerradaOpcionService = ePreguntaCerradaOpcionService; } /** @@ -74,7 +91,7 @@ public class EncuestaResource { /** * {@code PUT /encuestas/:id} : Updates an existing encuesta. * - * @param id the id of the encuesta to save. + * @param id the id of the encuesta to save. * @param encuesta the encuesta to update. * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the updated encuesta, * or with status {@code 400 (Bad Request)} if the encuesta is not valid, @@ -108,7 +125,7 @@ public class EncuestaResource { /** * {@code PATCH /encuestas/:id} : Partial updates given fields of an existing encuesta, field will ignore if it is null * - * @param id the id of the encuesta to save. + * @param id the id of the encuesta to save. * @param encuesta the encuesta to update. * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the updated encuesta, * or with status {@code 400 (Bad Request)} if the encuesta is not valid, @@ -154,6 +171,55 @@ public class EncuestaResource { return ResponseEntity.ok().body(entityList); } + @GetMapping("/encuestas/preguntas/{id}") + public ResponseEntity> getPreguntasByIdEncuesta(@PathVariable Long id) { + List preguntasCerradas = ePreguntaCerradaService.findAll(); + List preguntasAbiertas = ePreguntaAbiertaService.findAll(); + List preguntas = Stream.concat(preguntasCerradas.stream(), preguntasAbiertas.stream()).collect(Collectors.toList()); + List preguntasFiltered = new ArrayList<>(); + + for (Object obj : preguntas) { + if (obj.getClass() == EPreguntaCerrada.class) { + if (((EPreguntaCerrada) obj).getEncuesta() != null) { + if (((EPreguntaCerrada) obj).getEncuesta().getId().equals(id)) { + preguntasFiltered.add(obj); + } + } + } else if (obj.getClass() == EPreguntaAbierta.class) { + if (((EPreguntaAbierta) obj).getEncuesta() != null) { + if (((EPreguntaAbierta) obj).getEncuesta().getId().equals(id)) { + preguntasFiltered.add(obj); + } + } + } + } + return ResponseEntity.ok().body(preguntasFiltered); + } + + @GetMapping("/encuestas/preguntas-opciones/{id}") + public ResponseEntity>> getPreguntaCerradaOpcionByIdEncuesta(@PathVariable Long id) { + List> res = new ArrayList<>(); + List preguntasCerradas = ePreguntaCerradaService.findAll(); + List preguntasCerradasFiltered = preguntasCerradas + .stream() + .filter(p -> Objects.nonNull(p.getEncuesta())) + .filter(p -> p.getEncuesta().getId().equals(id)) + .collect(Collectors.toList()); + List opciones = ePreguntaCerradaOpcionService.findAll(); + + for (EPreguntaCerrada ePreguntaCerrada : preguntasCerradasFiltered) { + long preguntaCerradaId = ePreguntaCerrada.getId(); + List opcionesFiltered = opciones + .stream() + .filter(o -> Objects.nonNull(o.getEPreguntaCerrada())) + .filter(o -> o.getEPreguntaCerrada().getId().equals(preguntaCerradaId)) + .collect(Collectors.toList()); + res.add(opcionesFiltered); + } + + return ResponseEntity.ok().body(res); + } + /** * {@code GET /encuestas/count} : count all the encuestas. * diff --git a/src/main/webapp/app/entities/encuesta/service/encuesta.service.ts b/src/main/webapp/app/entities/encuesta/service/encuesta.service.ts index ef95403..bb1d3a4 100644 --- a/src/main/webapp/app/entities/encuesta/service/encuesta.service.ts +++ b/src/main/webapp/app/entities/encuesta/service/encuesta.service.ts @@ -45,6 +45,18 @@ export class EncuestaService { .pipe(map((res: EntityResponseType) => this.convertDateFromServer(res))); } + findQuestions(id: number): Observable { + return this.http + .get(`${this.resourceUrl}/preguntas/${id}`, { observe: 'response' }) + .pipe(map((res: EntityResponseType) => this.convertDateFromServer(res))); + } + + findQuestionsOptions(id: number): Observable { + return this.http + .get(`${this.resourceUrl}/preguntas-opciones/${id}`, { observe: 'response' }) + .pipe(map((res: EntityResponseType) => this.convertDateFromServer(res))); + } + query(req?: any): Observable { const options = createRequestOption(req); return this.http diff --git a/src/main/webapp/app/entities/encuesta/update/encuesta-update.component.html b/src/main/webapp/app/entities/encuesta/update/encuesta-update.component.html index f097553..30ec5b8 100644 --- a/src/main/webapp/app/entities/encuesta/update/encuesta-update.component.html +++ b/src/main/webapp/app/entities/encuesta/update/encuesta-update.component.html @@ -1,4 +1,115 @@ -
+
+

+ {{ encuesta!.nombre }} + +
+ + + +
+

+ + + + + +
+ No se encontraron preguntas +
+ +
+
+
+ {{ ePregunta.nombre }} | + {{ ePregunta.tipo }} | + {{ ePregunta.opcional }} + + + + +
+ --------- {{ ePreguntaOpcion2.nombre }} +
+
+
+
+ +
+
+ +
+
+ + diff --git a/src/main/webapp/app/entities/encuesta/update/encuesta-update.component.ts b/src/main/webapp/app/entities/encuesta/update/encuesta-update.component.ts index 5ac48c3..115c995 100644 --- a/src/main/webapp/app/entities/encuesta/update/encuesta-update.component.ts +++ b/src/main/webapp/app/entities/encuesta/update/encuesta-update.component.ts @@ -15,6 +15,11 @@ import { CategoriaService } from 'app/entities/categoria/service/categoria.servi import { IUsuarioExtra } from 'app/entities/usuario-extra/usuario-extra.model'; import { UsuarioExtraService } from 'app/entities/usuario-extra/service/usuario-extra.service'; +import { NgbModal } from '@ng-bootstrap/ng-bootstrap'; +import { IEPreguntaCerrada } from 'app/entities/e-pregunta-cerrada/e-pregunta-cerrada.model'; +import { EPreguntaCerradaService } from 'app/entities/e-pregunta-cerrada/service/e-pregunta-cerrada.service'; +import { EPreguntaCerradaDeleteDialogComponent } from 'app/entities/e-pregunta-cerrada/delete/e-pregunta-cerrada-delete-dialog.component'; + @Component({ selector: 'jhi-encuesta-update', templateUrl: './encuesta-update.component.html', @@ -41,147 +46,193 @@ export class EncuestaUpdateComponent implements OnInit { usuarioExtra: [], }); + ePreguntas?: any[]; + ePreguntasOpciones?: any[]; + encuesta: Encuesta | null = null; + + isLoading = false; + constructor( protected encuestaService: EncuestaService, protected categoriaService: CategoriaService, protected usuarioExtraService: UsuarioExtraService, protected activatedRoute: ActivatedRoute, - protected fb: FormBuilder + protected fb: FormBuilder, + protected modalService: NgbModal ) {} + loadAll(): void { + this.isLoading = true; + + this.encuestaService.findQuestions(this.encuesta?.id!).subscribe( + (res: any) => { + this.isLoading = false; + this.ePreguntas = res.body ?? []; + }, + () => { + this.isLoading = false; + } + ); + + this.encuestaService.findQuestionsOptions(this.encuesta?.id!).subscribe( + (res: any) => { + this.isLoading = false; + this.ePreguntasOpciones = res.body ?? []; + }, + () => { + this.isLoading = false; + } + ); + } + ngOnInit(): void { this.activatedRoute.data.subscribe(({ encuesta }) => { - console.log(this.activatedRoute.data); - console.log(encuesta); - if (encuesta.id === undefined) { const today = dayjs().startOf('day'); encuesta.fechaCreacion = today; encuesta.fechaPublicacion = today; encuesta.fechaFinalizar = today; encuesta.fechaFinalizada = today; + } else { + this.encuesta = encuesta; + this.loadAll(); } - this.updateForm(encuesta); + // this.updateForm(encuesta); - this.loadRelationshipsOptions(); + // this.loadRelationshipsOptions(); }); } - previousState(): void { - window.history.back(); - } - - save(): void { - this.isSaving = true; - const encuesta = this.createFromForm(); - if (encuesta.id !== undefined) { - this.subscribeToSaveResponse(this.encuestaService.update(encuesta)); - } else { - this.subscribeToSaveResponse(this.encuestaService.create(encuesta)); - } - } - - trackCategoriaById(index: number, item: ICategoria): number { + trackId(index: number, item: IEPreguntaCerrada): number { return item.id!; } - trackUsuarioExtraById(index: number, item: IUsuarioExtra): number { - return item.id!; - } - - protected subscribeToSaveResponse(result: Observable>): void { - result.pipe(finalize(() => this.onSaveFinalize())).subscribe( - () => this.onSaveSuccess(), - () => this.onSaveError() - ); - } - - protected onSaveSuccess(): void { - this.previousState(); - } - - protected onSaveError(): void { - // Api for inheritance. - } - - protected onSaveFinalize(): void { - this.isSaving = false; - } - - protected updateForm(encuesta: IEncuesta): void { - this.editForm.patchValue({ - id: encuesta.id, - nombre: encuesta.nombre, - descripcion: encuesta.descripcion, - fechaCreacion: encuesta.fechaCreacion ? encuesta.fechaCreacion.format(DATE_TIME_FORMAT) : null, - fechaPublicacion: encuesta.fechaPublicacion ? encuesta.fechaPublicacion.format(DATE_TIME_FORMAT) : null, - fechaFinalizar: encuesta.fechaFinalizar ? encuesta.fechaFinalizar.format(DATE_TIME_FORMAT) : null, - fechaFinalizada: encuesta.fechaFinalizada ? encuesta.fechaFinalizada.format(DATE_TIME_FORMAT) : null, - calificacion: encuesta.calificacion, - acceso: encuesta.acceso, - contrasenna: encuesta.contrasenna, - estado: encuesta.estado, - categoria: encuesta.categoria, - usuarioExtra: encuesta.usuarioExtra, + delete(ePreguntaCerrada: IEPreguntaCerrada): void { + const modalRef = this.modalService.open(EPreguntaCerradaDeleteDialogComponent, { size: 'lg', backdrop: 'static' }); + modalRef.componentInstance.ePreguntaCerrada = ePreguntaCerrada; + // unsubscribe not needed because closed completes on modal close + modalRef.closed.subscribe(reason => { + if (reason === 'deleted') { + this.loadAll(); + } }); - - this.categoriasSharedCollection = this.categoriaService.addCategoriaToCollectionIfMissing( - this.categoriasSharedCollection, - encuesta.categoria - ); - this.usuarioExtrasSharedCollection = this.usuarioExtraService.addUsuarioExtraToCollectionIfMissing( - this.usuarioExtrasSharedCollection, - encuesta.usuarioExtra - ); } - protected loadRelationshipsOptions(): void { - this.categoriaService - .query() - .pipe(map((res: HttpResponse) => res.body ?? [])) - .pipe( - map((categorias: ICategoria[]) => - this.categoriaService.addCategoriaToCollectionIfMissing(categorias, this.editForm.get('categoria')!.value) - ) - ) - .subscribe((categorias: ICategoria[]) => (this.categoriasSharedCollection = categorias)); + // previousState(): void { + // window.history.back(); + // } - this.usuarioExtraService - .query() - .pipe(map((res: HttpResponse) => res.body ?? [])) - .pipe( - map((usuarioExtras: IUsuarioExtra[]) => - this.usuarioExtraService.addUsuarioExtraToCollectionIfMissing(usuarioExtras, this.editForm.get('usuarioExtra')!.value) - ) - ) - .subscribe((usuarioExtras: IUsuarioExtra[]) => (this.usuarioExtrasSharedCollection = usuarioExtras)); - } + // save(): void { + // this.isSaving = true; + // const encuesta = this.createFromForm(); + // if (encuesta.id !== undefined) { + // this.subscribeToSaveResponse(this.encuestaService.update(encuesta)); + // } else { + // this.subscribeToSaveResponse(this.encuestaService.create(encuesta)); + // } + // } - protected createFromForm(): IEncuesta { - return { - ...new Encuesta(), - id: this.editForm.get(['id'])!.value, - nombre: this.editForm.get(['nombre'])!.value, - descripcion: this.editForm.get(['descripcion'])!.value, - fechaCreacion: this.editForm.get(['fechaCreacion'])!.value - ? dayjs(this.editForm.get(['fechaCreacion'])!.value, DATE_TIME_FORMAT) - : undefined, - fechaPublicacion: this.editForm.get(['fechaPublicacion'])!.value - ? dayjs(this.editForm.get(['fechaPublicacion'])!.value, DATE_TIME_FORMAT) - : undefined, - fechaFinalizar: this.editForm.get(['fechaFinalizar'])!.value - ? dayjs(this.editForm.get(['fechaFinalizar'])!.value, DATE_TIME_FORMAT) - : undefined, - fechaFinalizada: this.editForm.get(['fechaFinalizada'])!.value - ? dayjs(this.editForm.get(['fechaFinalizada'])!.value, DATE_TIME_FORMAT) - : undefined, - calificacion: this.editForm.get(['calificacion'])!.value, - acceso: this.editForm.get(['acceso'])!.value, - contrasenna: this.editForm.get(['contrasenna'])!.value, - estado: this.editForm.get(['estado'])!.value, - categoria: this.editForm.get(['categoria'])!.value, - usuarioExtra: this.editForm.get(['usuarioExtra'])!.value, - }; - } + // trackCategoriaById(index: number, item: ICategoria): number { + // return item.id!; + // } + + // trackUsuarioExtraById(index: number, item: IUsuarioExtra): number { + // return item.id!; + // } + + // protected subscribeToSaveResponse(result: Observable>): void { + // result.pipe(finalize(() => this.onSaveFinalize())).subscribe( + // () => this.onSaveSuccess(), + // () => this.onSaveError() + // ); + // } + + // protected onSaveSuccess(): void { + // this.previousState(); + // } + + // protected onSaveError(): void { + // // Api for inheritance. + // } + + // protected onSaveFinalize(): void { + // this.isSaving = false; + // } + + // protected updateForm(encuesta: IEncuesta): void { + // this.editForm.patchValue({ + // id: encuesta.id, + // nombre: encuesta.nombre, + // descripcion: encuesta.descripcion, + // fechaCreacion: encuesta.fechaCreacion ? encuesta.fechaCreacion.format(DATE_TIME_FORMAT) : null, + // fechaPublicacion: encuesta.fechaPublicacion ? encuesta.fechaPublicacion.format(DATE_TIME_FORMAT) : null, + // fechaFinalizar: encuesta.fechaFinalizar ? encuesta.fechaFinalizar.format(DATE_TIME_FORMAT) : null, + // fechaFinalizada: encuesta.fechaFinalizada ? encuesta.fechaFinalizada.format(DATE_TIME_FORMAT) : null, + // calificacion: encuesta.calificacion, + // acceso: encuesta.acceso, + // contrasenna: encuesta.contrasenna, + // estado: encuesta.estado, + // categoria: encuesta.categoria, + // usuarioExtra: encuesta.usuarioExtra, + // }); + + // this.categoriasSharedCollection = this.categoriaService.addCategoriaToCollectionIfMissing( + // this.categoriasSharedCollection, + // encuesta.categoria + // ); + // this.usuarioExtrasSharedCollection = this.usuarioExtraService.addUsuarioExtraToCollectionIfMissing( + // this.usuarioExtrasSharedCollection, + // encuesta.usuarioExtra + // ); + // } + + // protected loadRelationshipsOptions(): void { + // this.categoriaService + // .query() + // .pipe(map((res: HttpResponse) => res.body ?? [])) + // .pipe( + // map((categorias: ICategoria[]) => + // this.categoriaService.addCategoriaToCollectionIfMissing(categorias, this.editForm.get('categoria')!.value) + // ) + // ) + // .subscribe((categorias: ICategoria[]) => (this.categoriasSharedCollection = categorias)); + + // this.usuarioExtraService + // .query() + // .pipe(map((res: HttpResponse) => res.body ?? [])) + // .pipe( + // map((usuarioExtras: IUsuarioExtra[]) => + // this.usuarioExtraService.addUsuarioExtraToCollectionIfMissing(usuarioExtras, this.editForm.get('usuarioExtra')!.value) + // ) + // ) + // .subscribe((usuarioExtras: IUsuarioExtra[]) => (this.usuarioExtrasSharedCollection = usuarioExtras)); + // } + + // protected createFromForm(): IEncuesta { + // return { + // ...new Encuesta(), + // id: this.editForm.get(['id'])!.value, + // nombre: this.editForm.get(['nombre'])!.value, + // descripcion: this.editForm.get(['descripcion'])!.value, + // fechaCreacion: this.editForm.get(['fechaCreacion'])!.value + // ? dayjs(this.editForm.get(['fechaCreacion'])!.value, DATE_TIME_FORMAT) + // : undefined, + // fechaPublicacion: this.editForm.get(['fechaPublicacion'])!.value + // ? dayjs(this.editForm.get(['fechaPublicacion'])!.value, DATE_TIME_FORMAT) + // : undefined, + // fechaFinalizar: this.editForm.get(['fechaFinalizar'])!.value + // ? dayjs(this.editForm.get(['fechaFinalizar'])!.value, DATE_TIME_FORMAT) + // : undefined, + // fechaFinalizada: this.editForm.get(['fechaFinalizada'])!.value + // ? dayjs(this.editForm.get(['fechaFinalizada'])!.value, DATE_TIME_FORMAT) + // : undefined, + // calificacion: this.editForm.get(['calificacion'])!.value, + // acceso: this.editForm.get(['acceso'])!.value, + // contrasenna: this.editForm.get(['contrasenna'])!.value, + // estado: this.editForm.get(['estado'])!.value, + // categoria: this.editForm.get(['categoria'])!.value, + // usuarioExtra: this.editForm.get(['usuarioExtra'])!.value, + // }; + // } } diff --git a/src/main/webapp/content/scss/paper-dashboard/_datasurvey-form.scss b/src/main/webapp/content/scss/paper-dashboard/_datasurvey-form.scss index bbfb5d4..33e0a35 100644 --- a/src/main/webapp/content/scss/paper-dashboard/_datasurvey-form.scss +++ b/src/main/webapp/content/scss/paper-dashboard/_datasurvey-form.scss @@ -75,7 +75,9 @@ $form-background: #f1f5f9; } label { + font-size: 0.8rem; color: #757d94; + margin-bottom: 0.5rem; } }