Skip to content

Commit 3bc2f49

Browse files
committed
Services done and clean code
1 parent 34bd1fd commit 3bc2f49

File tree

9 files changed

+69
-100
lines changed

9 files changed

+69
-100
lines changed

backend/src/main/java/es/webapp6/padelante/ExamplesGenerator.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,21 @@ public void init() throws IOException,URISyntaxException{
8181
User user18 = new User("Almudena", passwordEncoder.encode("pass"), "[email protected]", "Almudena Arias", "USER");
8282

8383
ArrayList<Integer> array = user1.getHistoricalKarma();
84-
array.add(600);
85-
array.add(400);
86-
array.add(525);
87-
array.add(369);
84+
array.add(550);
85+
array.add(450);
86+
array.add(500);
87+
array.add(375);
8888
array.add(400);
8989
array.add(450);
9090
array.add(500);
91-
array.add(523);
92-
array.add(560);
91+
array.add(525);
92+
array.add(540);
9393
user1.setHistoricalKarma(array);
94+
user1.setNumMatchesPlayed(9);
95+
user1.setNumWins(7);
96+
user1.setNumLoses(2);
97+
user1.setLocation("Madrid");
98+
user1.setCountry("España");
9499

95100
userRepository.save(userNone);
96101
userRepository.save(user1);

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,7 @@ export class HeaderComponent {
1818

1919
getUserMatches(){
2020
this.userService.getUserMatches().subscribe(
21-
listMatches => this.userMatches = listMatches,
22-
error => {
23-
if (error.status != 403) {
24-
console.error('Unexpected Error on getUserMatches')
25-
}
26-
}
21+
listMatches => this.userMatches = listMatches
2722
)
2823
}
2924

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ export class MatchComponent {
2626
this.matchService.getMatch(id).subscribe(
2727
response => this.match = response,
2828
error => {
29-
if (error.status != 404) {
30-
console.error('Unexpected Error on getMatch')
31-
} else {
29+
if (error){
3230
this.router.navigate(['/error404'])
3331
}
3432
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ <h5 class="upper-text text-center pb-0 fw-bold">Crea un cuenta nueva</h5>
6767
</div>
6868
</div>
6969
<div class="col-12 mt-4">
70-
<button class="btn btn-login upper-text w-100" (click)="register($event,name.value,realName.value,email.value,password.value)" type="submit">Registrarse</button>
70+
<button class="btn btn-login upper-text w-100" (click)="register(name.value,realName.value,email.value,password.value)" type="submit">Registrarse</button>
7171
</div>
7272

7373
<!-- <input type="hidden" name="_csrf" value="{{token}}"/> -->
Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { User } from './../../models/user.model';
2-
import { Component, ViewChild } from '@angular/core';
2+
import { Component } from '@angular/core';
33
import { UserService } from 'src/app/services/user.service';
44

55

@@ -11,43 +11,21 @@ import { UserService } from 'src/app/services/user.service';
1111
export class RegisterComponent {
1212
newUser: boolean | undefined;
1313
User: User | undefined;
14-
// @ViewChild("file")
15-
// file: any;
1614

1715
removeImage:boolean | undefined;
1816

19-
constructor(private service: UserService)
20-
{
21-
// const id = activatedRoute.snapshot.params['id'];
22-
// if (id) {
23-
// service.getUser(id).subscribe(
24-
// User => this.User = User,
25-
// error => console.error(error)
26-
// );
27-
// this.newUser = false;
28-
// } else {
29-
// this.User = { name: '', realName: '',email:'', location:'',country:'', phone:'', numWins:0,numLoses:0,numMatchesPlayed:0,
30-
// historicalKarma:[],status:false,encodedPassword:'',roles:[], image: false };
31-
// this.newUser = true;
32-
// }
33-
}
17+
constructor(private service: UserService){}
3418

35-
register(event: any, name: string,realName:string,email:string,password:string){
19+
register(name: string,realName:string,email:string,password:string){
3620
if(!this.User){
3721

3822
this.User = { name: name, realName: realName,email:email, location:'',country:'', phone:'', numWins:0,numLoses:0,numMatchesPlayed:0,
3923
historicalKarma:[500],status:false,encodedPassword:password,roles:[], image: false };
4024

41-
42-
// if(this.User.image && this.removeImage){
43-
// this.User.image = false;
44-
// }
4525
this.service.registerUser(this.User)
46-
}else{
26+
}
27+
else {
4728
throw Error();
4829
}
4930
}
50-
51-
52-
5331
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<h2>{{loginService.currentUser().realName}}</h2>
2323
<h3 class="mt-1 color-green"><span class="fw-bold">@{{loginService.currentUser().name}}</span></h3>
2424
<h3 class="my-2">Karma: <span
25-
class="color-green fw-bold">{{calculatedKarma(loginService.currentUser().historicalKarma)}}
25+
class="color-green fw-bold">{{getKarma(loginService.currentUser().historicalKarma)}}
2626
puntos</span></h3>
2727
<h3 class="mb-3">{{loginService.currentUser().location}} {{loginService.currentUser().country}}</h3>
2828
</div>
@@ -89,7 +89,7 @@ <h2 class="mb-1">Tus parejas</h2>
8989
<h5 class="card-title pb-0 mb-0">Victorias - Derrotas</h5>
9090

9191
<!-- Donut Chart -->
92-
<div id="donutChart" style="min-height: 300px;"
92+
<div id="donutChart" style="min-height: 100px;"
9393
class="echart d-flex align-items-center justify-content-center">
9494
<doughnutChart> </doughnutChart>
9595
</div>
@@ -241,7 +241,7 @@ <h6 class="pt-1"><em>{{userTournament.startDate}}</em></h6>
241241

242242
<!-- <input type="hidden" name="_csrf" value="{{token}}" /> -->
243243
<div class="text-center">
244-
<button class="btn btn-green fw-bold" (click)="updateUserForm($event,fullName.value,location.value, country.value,
244+
<button class="btn btn-green fw-bold" (click)="updateUserForm(fullName.value,location.value, country.value,
245245
phone.value)" type="submit">Guardar cambios</button>
246246
</div>
247247

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

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { User } from '../../models/user.model';
22
import { Component, ViewChild } from '@angular/core';
33
import { UserService } from 'src/app/services/user.service';
44
import { LoginService } from 'src/app/services/login.service';
5-
import { Router } from '@angular/router';
65
import { Tournament } from 'src/app/models/tournament.model';
76

87

@@ -28,18 +27,14 @@ export class UserProfileComponent{
2827
removeImage:boolean;
2928
auxURL: number = 1;
3029

31-
constructor(private router: Router,public loginService: LoginService, public userService: UserService) {
30+
constructor(public loginService: LoginService, public userService: UserService) {
3231

3332
this.getUserTournaments();
3433
this.getUserPairs();
3534
}
3635

37-
calculatedKarma( array : number[]){
38-
let j = 0;
39-
for(let i = 0; i < array.length;i++){
40-
j+=array[i];
41-
}
42-
return Math.round(j/array.length);
36+
getKarma(array : number[]){
37+
return array[array.length-1];
4338
}
4439

4540
getUserPairs() {
@@ -62,20 +57,15 @@ export class UserProfileComponent{
6257
)
6358
}
6459

65-
updateUserForm(event:any,fullName:string,location:string, country:string, phone:string){
60+
updateUserForm(fullName:string,location:string, country:string, phone:string){
6661
let user = this.loginService.currentUser();
6762
user.realName=fullName;
6863
user.location = location;
6964
user.country=country;
7065
user.phone = phone;
7166

7267
this.userService.updateUser(user).subscribe(
73-
response => this.uploadImage(),
74-
error => {
75-
if (error.status != 400) {
76-
console.error('Unexpected Error on deleteUser')
77-
}
78-
}
68+
_ => this.uploadImage()
7969
)
8070
}
8171

@@ -88,12 +78,7 @@ export class UserProfileComponent{
8878
const image = this.file.nativeElement.files[0];
8979
if (this.removeImage) {
9080
this.userService.deleteUserImage().subscribe(
91-
_ => this.afterUploadImage(),
92-
error => {
93-
if (error.status != 400) {
94-
console.error('Error deleting user image')
95-
}
96-
}
81+
_ => this.afterUploadImage()
9782
);
9883
} else if(image){
9984
let formData = new FormData();
@@ -102,11 +87,9 @@ export class UserProfileComponent{
10287
_ => {
10388
this.afterUploadImage()
10489
this.auxURL = this.auxURL +1
105-
},
106-
error => alert('Error uploading user image')
90+
}
10791
);
10892
} else {
109-
console.log("Entramos en else")
11093
this.afterUploadImage();
11194
}
11295
}

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Injectable } from '@angular/core';
22
import { HttpClient } from '@angular/common/http';
3-
import { Observable } from 'rxjs';
3+
import { map } from 'rxjs';
44

55
import { Match } from '../models/match.model';
66
import { UserService } from './user.service';
@@ -18,7 +18,17 @@ export class MatchService {
1818
// THIS FUNCTIONS MAY NO WORK, TEST THEM WHEN DOING THE PAGES OF MATCH
1919

2020
getMatch(id: number | string) {
21-
return this.http.get(BASE_URL + id, { withCredentials: true }) as Observable<Match>;
21+
return this.http.get(BASE_URL + id, { withCredentials: true }).pipe(map(
22+
response => response as Match,
23+
error => {
24+
if (error.status != 404) {
25+
console.error('Unexpected Error on getMatch')
26+
return false
27+
} else {
28+
return true
29+
}
30+
}
31+
))
2232
}
2333

2434
resultMatch(id: number | string, result: number[]) {

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

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { User } from './../models/user.model';
22
import { Injectable } from '@angular/core';
33
import { HttpClient } from '@angular/common/http';
4-
import { Observable, throwError, map } from 'rxjs';
5-
import { catchError } from 'rxjs/operators';
4+
import { map } from 'rxjs';
65
import { Match } from '../models/match.model';
6+
import { Router } from '@angular/router';
77

88
const BASE_URL = '/api/users/';
99

@@ -13,34 +13,33 @@ const BASE_URL = '/api/users/';
1313
export class UserService {
1414

1515

16-
constructor(private http: HttpClient) { }
16+
constructor(private http: HttpClient, private router: Router) { }
1717

1818
//getuser by id
19-
getUser(id: number | string): Observable<User> {
20-
return this.http.get(BASE_URL + id) as Observable<User>;
19+
getUser(id: number | string) {
20+
return this.http.get(BASE_URL + id).pipe(map(
21+
response => response as User
22+
))
2123
}
2224

2325
registerUser(User: User) {
24-
25-
26-
return this.http.post(BASE_URL, User,{ withCredentials: true }).subscribe((resp: any) => {
27-
console.log("Register new User: Successfully");
28-
29-
});
26+
return this.http.post(BASE_URL, User,{ withCredentials: true }).subscribe(
27+
_ => this.router.navigate(['login'])
28+
);
3029
}
3130

3231
setUserImage(formData: FormData) {
33-
return this.http.post(BASE_URL + 'image', formData)
34-
.pipe(
35-
catchError(error => this.handleError(error))
36-
);
32+
return this.http.post(BASE_URL + 'image', formData).pipe(map(
33+
response => response,
34+
_error => console.error('Unexpected Error on setUserImage')
35+
))
3736
}
3837

3938
deleteUserImage(){
40-
return this.http.delete(BASE_URL + 'image')
41-
.pipe(
42-
catchError(error => this.handleError(error))
43-
);
39+
return this.http.delete(BASE_URL + 'image').pipe(map(
40+
response => response,
41+
error => errorIgnore(error, 400, "deleteUserImage")
42+
))
4443
}
4544

4645
deleteUser(id: number | string) {
@@ -51,7 +50,10 @@ constructor(private http: HttpClient) { }
5150
}
5251

5352
updateUser(user: User) {
54-
return this.http.put(BASE_URL , user,{ withCredentials: true })
53+
return this.http.put(BASE_URL , user,{ withCredentials: true }).pipe(map(
54+
response => response,
55+
error => errorIgnore(error, 400, "updateUser")
56+
));
5557
}
5658

5759
getAllUsers(page: number | string) {
@@ -69,8 +71,10 @@ constructor(private http: HttpClient) { }
6971
}
7072

7173
getUserMatches() {
72-
return this.http.get(BASE_URL + 'me/matches', { withCredentials: true }) as Observable<Match[]>;
73-
// return this.http.get(BASE_URL + id, { withCredentials: true }).pipe(catchError(error => this.router.navigate(['/error_404'])));
74+
return this.http.get(BASE_URL + 'me/matches', { withCredentials: true }).pipe(map(
75+
response => response as Match[],
76+
error => errorIgnore(error, 403, "getUserMatches")
77+
));
7478
}
7579

7680
getUserTournaments(page: number | string) {
@@ -79,13 +83,9 @@ constructor(private http: HttpClient) { }
7983
error => errorIgnore(error, 403, "getUserTournaments")
8084
));
8185
}
82-
83-
private handleError(error: any) {
84-
console.log("ERROR:");
85-
console.error(error);
86-
return throwError("Server error (" + error.status + "): ");
87-
}
8886
}
87+
88+
8989
function getInfoPageable(response: any): [any[], boolean] {
9090
if (response.content != undefined) {
9191
return [response.content,!response.last]

0 commit comments

Comments
 (0)