Skip to content

Commit 4b21ba5

Browse files
committed
Create Tourn with image & fix + Tournament Service
1 parent 976bc59 commit 4b21ba5

File tree

6 files changed

+74
-56
lines changed

6 files changed

+74
-56
lines changed

backend/src/main/java/es/webapp6/padelante/controller/TournamentRestController.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@ public ResponseEntity<Tournament> createTournament(@RequestBody Tournament tourn
144144
if (principal != null) {
145145
// Data that must be default because tournament could be inconsistent
146146
tournament.setOwner(principal.getName());
147-
tournament.setNumParticipants(0);
148147
tournament.setNumSignedUp(0);
149148
tournament.setRounds(0);
150149

frontend/src/app/components/createTournament/createTournament.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ <h5 class="upper-text text-center pb-0 fw-bold">Crea un torneo nuevo</h5>
3333
<div class="col-12">
3434
<label for="tournamentImage" class="form-label">Imagen del torneo</label>
3535
<div class="tournament-img">
36-
<input type='file' name='imageField' accept=".jpg, .jpeg, .png" />
36+
<input #file type='file' name='imageField' accept=".jpg, .jpeg, .png" />
3737
</div>
3838
<div class="invalid-feedback">Introduce una imagen válida.</div>
3939
</div>
@@ -111,7 +111,7 @@ <h5 class="upper-text text-center pb-0 fw-bold">Crea un torneo nuevo</h5>
111111
</div>
112112

113113
<div class="col-12 mt-4">
114-
<button class="btn btn-login upper-text w-100" (click)="TournamentCreated($event,tournamentName.value,
114+
<button class="btn btn-login upper-text w-100" (click)="TournamentCreated(tournamentName.value,
115115
about.value,ruleset.value,location.value,numParticipants.value,inscriptionDate.value,startDate.value)" type="submit">Crear torneo</button>
116116
</div>
117117

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import { Router } from '@angular/router';
12
import { Tournament } from './../../models/tournament.model';
23
import { TournamentService } from './../../services/tournament.service';
3-
import { Component } from '@angular/core';
4+
import { Component, ViewChild } from '@angular/core';
45

56
import { LoginService } from 'src/app/services/login.service';
67

@@ -10,24 +11,54 @@ import { LoginService } from 'src/app/services/login.service';
1011
})
1112

1213
export class CreateTournamentComponent {
13-
Tournament :Tournament | undefined
14-
15-
constructor(private service: TournamentService,public loginService: LoginService){}
16-
17-
TournamentCreated(event:any,tournamentName:string,
18-
about:string,ruleset:string,location:string,numParticipants:string,inscriptionDate:string,startDate:string){
19-
20-
21-
this.Tournament={owner:"someone",tournamentName:tournamentName,numParticipants:Number(numParticipants),numSignedUp:0,rounds:0,about:about,
22-
ruleset:ruleset,location:location, inscriptionDate:inscriptionDate,startDate:startDate,started:false,image:false}
23-
24-
25-
this.service.createTournament(this.Tournament);
26-
27-
28-
2914

15+
@ViewChild("file")
16+
file: any;
17+
18+
constructor(private router: Router, private tournamentService: TournamentService,public loginService: LoginService){}
19+
20+
TournamentCreated(tournamentName:string,about:string,ruleset:string,location:string,numParticipants:string,
21+
inscriptionDate:string,startDate:string){
22+
23+
console.log(numParticipants)
24+
var numPart: number = +numParticipants
25+
console.log(numPart)
26+
27+
const tournament: Tournament ={
28+
owner:"someone",
29+
tournamentName:tournamentName,
30+
numParticipants: numPart,
31+
numSignedUp:0,
32+
rounds:0,
33+
about:about,
34+
ruleset:ruleset,
35+
location:location,
36+
inscriptionDate:inscriptionDate,
37+
startDate:startDate,
38+
started:false,
39+
image:false
40+
}
41+
42+
this.tournamentService.createTournament(tournament).subscribe(
43+
tournamentCreated => {
44+
this.uploadImage(tournamentCreated.id)
45+
}
46+
);
3047
}
3148

32-
49+
uploadImage(id: number): void {
50+
51+
const image = this.file.nativeElement.files[0];
52+
if(image){
53+
let formData = new FormData();
54+
formData.append("imageFile", image);
55+
this.tournamentService.setTournamentImage(id, formData).subscribe(
56+
_ => this.router.navigate(['']),
57+
_error => this.router.navigate([''])
58+
);
59+
}
60+
else {
61+
this.router.navigate([''])
62+
}
63+
}
3364
}

frontend/src/app/components/tournament/tournament.component.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ <h3 class="py-1"><em>{{tournament.startDate}}</em></h3>
4343
<a class="button btn-tournament upper-text my-1" href="#"><i class="ri-heart-3-fill pe-1"></i> Seguir</a>
4444
</div> -->
4545

46-
<div *ngIf="this.loginService.isLogged() && !userInTournament && !tournament.started" class="d-flex justify-content-end">
46+
<div *ngIf="this.loginService.isLogged() && !userInTournament && !tournament.started &&
47+
tournament.numParticipants > tournament.numSignedUp" class="d-flex justify-content-end">
4748
<a class="d-inline button btn-tournament upper-text my-1" href="#" data-bs-toggle="modal" (click)="getUsersForInscription()"
4849
data-bs-target="#staticBackdrop">
4950
<i class="ri-edit-fill pe-1"></i> Incribirse

frontend/src/app/components/tournament/tournament.component.ts

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -207,17 +207,11 @@ export class TournamentComponent{
207207
inscriptionDate: newinscriptionDate,
208208
startDate: newstartDate,
209209
started: false,
210-
//BE CAREFULL WITH THIS BOOLEAN WHEN UPDATED A IMG (NOT DONE YET)
211210
image: this.tournament.image
212211
}
213212

214213
this.tournamentService.updateTournament(updatedTournanent).subscribe(
215-
response => this.uploadImage(),
216-
error => {
217-
if (error.status != 400) {
218-
console.error('Unexpected Error on deleteUser')
219-
}
220-
}
214+
_ => this.uploadImage()
221215
);
222216
}
223217

@@ -226,12 +220,7 @@ export class TournamentComponent{
226220
const image = this.file.nativeElement.files[0];
227221
if (this.removeImage) {
228222
this.tournamentService.deleteTournamentImage(this.id).subscribe(
229-
_ => this.afterUploadImage(),
230-
error => {
231-
if (error.status != 400) {
232-
console.error('Error deleting user image')
233-
}
234-
}
223+
_ => this.afterUploadImage()
235224
);
236225
} else if(image){
237226
let formData = new FormData();
@@ -240,11 +229,9 @@ export class TournamentComponent{
240229
_ => {
241230
this.afterUploadImage()
242231
this.auxURL = this.auxURL +1
243-
},
244-
error => alert('Error uploading user image')
232+
}
245233
);
246234
} else {
247-
console.log("Entramos en else")
248235
this.afterUploadImage();
249236
}
250237
}

frontend/src/app/services/tournament.service.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ import { Team } from './../models/team.model';
44
import { Tournament } from './../models/tournament.model';
55
import { Injectable } from '@angular/core';
66
import { HttpClient } from '@angular/common/http';
7-
import { catchError, map, Observable, throwError } from 'rxjs';
8-
import { Router } from '@angular/router';
7+
import { map, throwError } from 'rxjs';
98

109
const BASE_URL = '/api/tournaments/';
1110

@@ -14,7 +13,7 @@ const BASE_URL = '/api/tournaments/';
1413
})
1514
export class TournamentService {
1615

17-
constructor(private http: HttpClient, private router: Router) {
16+
constructor(private http: HttpClient) {
1817
}
1918

2019
getTournament(id: number | string) {
@@ -53,17 +52,17 @@ export class TournamentService {
5352
}
5453

5554
createTournament(Tournament: Tournament) {
56-
return this.http.post(BASE_URL, Tournament, { withCredentials: true }).subscribe((_resp: any) => {
57-
console.log("Creation Tournament: Successfully");
58-
this.router.navigate(['']);
59-
});
60-
55+
return this.http.post(BASE_URL, Tournament, { withCredentials: true }).pipe(map(
56+
response => response as Tournament,
57+
error => errorIgnore(error, 403, "createTournament")
58+
));
6159
}
6260

6361
updateTournament(tournament: Tournament) {
64-
return this.http.put(BASE_URL + tournament.id, tournament).pipe(
65-
catchError(error => this.handleError(error))
66-
);
62+
return this.http.put(BASE_URL + tournament.id, tournament).pipe(map(
63+
response => response,
64+
error => errorIgnore(error, 400, "updateTournament")
65+
));
6766
}
6867

6968
deleteTeam(idTournament: number | string, idTeam: number | string) {
@@ -82,17 +81,18 @@ export class TournamentService {
8281
}
8382

8483
setTournamentImage(id:number, formData: FormData) {
85-
return this.http.post(BASE_URL +id+ '/image', formData)
86-
.pipe(
87-
catchError(error => this.handleError(error))
88-
);
84+
return this.http.post(BASE_URL +id+ '/image', formData).pipe(map(
85+
response => response,
86+
_error => console.error('Unexpected error in setTournamentImage')
87+
))
8988
}
9089

9190
deleteTournamentImage(id:number){
9291
return this.http.delete(BASE_URL +id+ '/image')
93-
.pipe(
94-
catchError(error => this.handleError(error))
95-
);
92+
.pipe(map(
93+
response => response,
94+
error => errorIgnore(error, 400, "deleteTournamentImage")
95+
));
9696
}
9797

9898
private handleError(error: any) {

0 commit comments

Comments
 (0)