Skip to content

Commit

Permalink
Usage of API functions must check if User is authenticated via OAuth2…
Browse files Browse the repository at this point in the history
… & User is able to delete existing records in all tables that references player id.
  • Loading branch information
augustinecyr committed Apr 17, 2023
1 parent ba9ee3f commit ae4d310
Show file tree
Hide file tree
Showing 12 changed files with 244 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
.authorizeRequests()
.antMatchers("/login", "/error", "/auth/google/callback",
"/login/oauth2/code/github", "/login/oauth/access_token", "/login/oauth/access_token?code=",
"/token", "/contact", "/clubs/squad", "/players", "/players/stats", "/googlemap")
"/token", "/contact", "/clubs/squad", "/players", "/players/stats", "/googlemap" ,"/players/delete")
.permitAll()
.anyRequest()
.authenticated()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sg.backend.models.PlayerSQL;
Expand All @@ -29,4 +32,11 @@ public List<PlayerSQL> getPlayerList(HttpSession sess){
List<PlayerSQL> players = clubRepo.getPlayerList();
return players;
}
// removes player record from both tables
@DeleteMapping(path="/players/delete")
@CrossOrigin(origins = "*", allowedHeaders = "*")
public ResponseEntity<Void> deletePlayerById(HttpSession sess, @RequestParam String id){
clubRepo.deletePlayerById(id);
return ResponseEntity.noContent().build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,23 @@ public class ClubRepository {

public void insert(Club player) {
/*
// check for existing entries
// shifted this logic into a new method and @ClubService
List<String> ids = template.queryForList(Queries.SQL_ID_CLUB_PLAYER, String.class);
if (ids.contains(player.getId())) {
System.out.println("Player ID already exists in database.");
return;
}
*/
* // check for existing entries
* // shifted this logic into a new method and @ClubService
* List<String> ids = template.queryForList(Queries.SQL_ID_CLUB_PLAYER,
* String.class);
* if (ids.contains(player.getId())) {
* System.out.println("Player ID already exists in database.");
* return;
* }
*/

Object[] params = new Object[] {
player.getId(),
player.getName()
};
template.update(Queries.SQL_INSERT_CLUB_PLAYER, params);
}

// return list of IDs in mySQL
public List<String> getPlayerIds() {

Expand All @@ -54,4 +55,11 @@ public PlayerSQL mapRow(ResultSet rs, int rowNum) throws SQLException {
});
return players;
}

// delete by records by id
public void deletePlayerById(String id) {
// must delete any record that references PK first
template.update(Queries.SQL_DELETE_PLAYER_STATS_BY_PLAYERID, id);
template.update(Queries.SQL_DELETE_CLUB_PLAYER_BY_ID, id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ public class Queries {
public static String SQL_VIEW_ALL_CLUB_PLAYER = "select * from club_player order by name";
public static String SQL_INSERT_PLAYER_STATS = "insert into player_stats(club_player_id, goals, assists, yellowCards, redCards, cleanSheets, concededGoals, isGoalkeeper) values (?, ?, ?, ?, ?, ?, ?, ?)";
public static String SQL_CLUB_PLAYER_ID_STATS = "select club_player_id from player_stats";
public static String SQL_DELETE_CLUB_PLAYER_BY_ID = "delete from club_player where id = ?";
public static String SQL_DELETE_PLAYER_STATS_BY_PLAYERID = "delete from player_stats where club_player_id = ?";
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public List<Club> getSquad(String id) {
System.out.println("-------------------------------");
System.out.println("Loading the squad");
// return squads so it will not be blank on view
return squads;
continue;
}
// insert only if id doesnt exist
clubRepo.insert(player);
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/app/components/aboutus.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component } from '@angular/core';
import { Component, OnInit } from '@angular/core';
import { MapService } from '../map.service';
import { DomSanitizer, SafeResourceUrl } from '@angular/platform-browser';

Expand All @@ -7,7 +7,7 @@ import { DomSanitizer, SafeResourceUrl } from '@angular/platform-browser';
templateUrl: './aboutus.component.html',
styleUrls: ['./aboutus.component.css'],
})
export class AboutusComponent {
export class AboutusComponent implements OnInit {
mapsUrl: SafeResourceUrl | undefined;

constructor(
Expand Down
174 changes: 147 additions & 27 deletions frontend/src/app/components/clubs.component.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,164 @@
import { Component } from '@angular/core';
import { Component, OnInit } from '@angular/core';
import { MatIconRegistry } from '@angular/material/icon';
import { DomSanitizer } from '@angular/platform-browser';
import { ClubService } from '../clubs.service';
import { Observable } from 'rxjs';
import { Club } from '../models';
import { Club, GoogleData, UserData } from '../models';
import { UserService } from '../user.service';
import { Router } from '@angular/router';

@Component({
selector: 'app-clubs',
templateUrl: './clubs.component.html',
styleUrls: ['./clubs.component.css']
styleUrls: ['./clubs.component.css'],
})
export class ClubsComponent {
export class ClubsComponent implements OnInit {
squads: Observable<Club[]> | undefined;
userData: UserData | undefined;
googleUserData: GoogleData | undefined;

constructor(private matIconRegistry: MatIconRegistry, private domSanitizer: DomSanitizer, private clubService: ClubService) {
this.matIconRegistry.addSvgIcon('manchester-united', this.domSanitizer.bypassSecurityTrustResourceUrl('../../../assets/manchester-united.svg'));
this.matIconRegistry.addSvgIcon('manchester-city', this.domSanitizer.bypassSecurityTrustResourceUrl('../../../assets/manchester-city.svg'));
this.matIconRegistry.addSvgIcon('arsenal', this.domSanitizer.bypassSecurityTrustResourceUrl('../../../assets/arsenal.svg'));
this.matIconRegistry.addSvgIcon('chelsea', this.domSanitizer.bypassSecurityTrustResourceUrl('../../../assets/chelsea.svg'));
this.matIconRegistry.addSvgIcon('liverpool', this.domSanitizer.bypassSecurityTrustResourceUrl('../../../assets/liverpool.svg'));
this.matIconRegistry.addSvgIcon('tottenham', this.domSanitizer.bypassSecurityTrustResourceUrl('../../../assets/tottenham.svg'));
this.matIconRegistry.addSvgIcon('newcastle-united', this.domSanitizer.bypassSecurityTrustResourceUrl('../../../assets/newcastle-united.svg'));
this.matIconRegistry.addSvgIcon('westham', this.domSanitizer.bypassSecurityTrustResourceUrl('../../../assets/westham.svg'));
this.matIconRegistry.addSvgIcon('leicester-city', this.domSanitizer.bypassSecurityTrustResourceUrl('../../../assets/leicester-city.svg'));
this.matIconRegistry.addSvgIcon('astonvilla', this.domSanitizer.bypassSecurityTrustResourceUrl('../../../assets/astonvilla.svg'));
this.matIconRegistry.addSvgIcon('wolves', this.domSanitizer.bypassSecurityTrustResourceUrl('../../../assets/wolves.svg'));
this.matIconRegistry.addSvgIcon('southampton', this.domSanitizer.bypassSecurityTrustResourceUrl('../../../assets/southampton.svg'));
this.matIconRegistry.addSvgIcon('brighton', this.domSanitizer.bypassSecurityTrustResourceUrl('../../../assets/brighton.svg'));
this.matIconRegistry.addSvgIcon('everton', this.domSanitizer.bypassSecurityTrustResourceUrl('../../../assets/everton.svg'));
this.matIconRegistry.addSvgIcon('nottinghamforest', this.domSanitizer.bypassSecurityTrustResourceUrl('../../../assets/nottinghamforest.svg'));
this.matIconRegistry.addSvgIcon('brentford', this.domSanitizer.bypassSecurityTrustResourceUrl('../../../assets/brentford.svg'));
this.matIconRegistry.addSvgIcon('leedsunited', this.domSanitizer.bypassSecurityTrustResourceUrl('../../../assets/leedsunited.svg'));
this.matIconRegistry.addSvgIcon('crystalpalace', this.domSanitizer.bypassSecurityTrustResourceUrl('../../../assets/crystalpalace.svg'));
this.matIconRegistry.addSvgIcon('fulham', this.domSanitizer.bypassSecurityTrustResourceUrl('../../../assets/fulham.svg'));
this.matIconRegistry.addSvgIcon('bournemouth', this.domSanitizer.bypassSecurityTrustResourceUrl('../../../assets/bournemouth.svg'));
constructor(
private matIconRegistry: MatIconRegistry,
private domSanitizer: DomSanitizer,
private clubService: ClubService,
private userService: UserService,
private router: Router
) {
this.matIconRegistry.addSvgIcon(
'manchester-united',
this.domSanitizer.bypassSecurityTrustResourceUrl(
'../../../assets/manchester-united.svg'
)
);
this.matIconRegistry.addSvgIcon(
'manchester-city',
this.domSanitizer.bypassSecurityTrustResourceUrl(
'../../../assets/manchester-city.svg'
)
);
this.matIconRegistry.addSvgIcon(
'arsenal',
this.domSanitizer.bypassSecurityTrustResourceUrl(
'../../../assets/arsenal.svg'
)
);
this.matIconRegistry.addSvgIcon(
'chelsea',
this.domSanitizer.bypassSecurityTrustResourceUrl(
'../../../assets/chelsea.svg'
)
);
this.matIconRegistry.addSvgIcon(
'liverpool',
this.domSanitizer.bypassSecurityTrustResourceUrl(
'../../../assets/liverpool.svg'
)
);
this.matIconRegistry.addSvgIcon(
'tottenham',
this.domSanitizer.bypassSecurityTrustResourceUrl(
'../../../assets/tottenham.svg'
)
);
this.matIconRegistry.addSvgIcon(
'newcastle-united',
this.domSanitizer.bypassSecurityTrustResourceUrl(
'../../../assets/newcastle-united.svg'
)
);
this.matIconRegistry.addSvgIcon(
'westham',
this.domSanitizer.bypassSecurityTrustResourceUrl(
'../../../assets/westham.svg'
)
);
this.matIconRegistry.addSvgIcon(
'leicester-city',
this.domSanitizer.bypassSecurityTrustResourceUrl(
'../../../assets/leicester-city.svg'
)
);
this.matIconRegistry.addSvgIcon(
'astonvilla',
this.domSanitizer.bypassSecurityTrustResourceUrl(
'../../../assets/astonvilla.svg'
)
);
this.matIconRegistry.addSvgIcon(
'wolves',
this.domSanitizer.bypassSecurityTrustResourceUrl(
'../../../assets/wolves.svg'
)
);
this.matIconRegistry.addSvgIcon(
'southampton',
this.domSanitizer.bypassSecurityTrustResourceUrl(
'../../../assets/southampton.svg'
)
);
this.matIconRegistry.addSvgIcon(
'brighton',
this.domSanitizer.bypassSecurityTrustResourceUrl(
'../../../assets/brighton.svg'
)
);
this.matIconRegistry.addSvgIcon(
'everton',
this.domSanitizer.bypassSecurityTrustResourceUrl(
'../../../assets/everton.svg'
)
);
this.matIconRegistry.addSvgIcon(
'nottinghamforest',
this.domSanitizer.bypassSecurityTrustResourceUrl(
'../../../assets/nottinghamforest.svg'
)
);
this.matIconRegistry.addSvgIcon(
'brentford',
this.domSanitizer.bypassSecurityTrustResourceUrl(
'../../../assets/brentford.svg'
)
);
this.matIconRegistry.addSvgIcon(
'leedsunited',
this.domSanitizer.bypassSecurityTrustResourceUrl(
'../../../assets/leedsunited.svg'
)
);
this.matIconRegistry.addSvgIcon(
'crystalpalace',
this.domSanitizer.bypassSecurityTrustResourceUrl(
'../../../assets/crystalpalace.svg'
)
);
this.matIconRegistry.addSvgIcon(
'fulham',
this.domSanitizer.bypassSecurityTrustResourceUrl(
'../../../assets/fulham.svg'
)
);
this.matIconRegistry.addSvgIcon(
'bournemouth',
this.domSanitizer.bypassSecurityTrustResourceUrl(
'../../../assets/bournemouth.svg'
)
);
}

ngOnInit() {
this.userData = this.userService.userData;
this.googleUserData = this.userService.googleData;
if (!this.userData && !this.googleUserData) {
console.log('No account information exists. Please login');
this.router.navigate(['/login']);
} else {
console.log("User is authenticated");
}
}

getSquad(id: string) {
console.log("club id (referred on transfermarkt.com):", id)
console.log('club id (referred on transfermarkt.com):', id);
this.squads = this.clubService.getSquad(id);
}

}
1 change: 1 addition & 0 deletions frontend/src/app/components/contact.component.css
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
border-radius: 8px;
max-width: 600px;
margin: auto;
opacity: 0.95;
}

/* Title styles */
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/app/components/contact.component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Component, ElementRef, ViewChild } from '@angular/core';
import { Form, FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
import { Component, ElementRef, OnInit, ViewChild } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { ContactService } from '../contact.service';
import { Contact } from '../models';
import { MatSnackBar } from '@angular/material/snack-bar';
Expand All @@ -11,7 +11,7 @@ import { MatSnackBar } from '@angular/material/snack-bar';
styleUrls: ['./contact.component.css']
})

export class ContactComponent {
export class ContactComponent implements OnInit{

@ViewChild('attachment')
attachment!: ElementRef
Expand Down
11 changes: 11 additions & 0 deletions frontend/src/app/components/stats.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<th>ID</th>
<th>Name</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
Expand All @@ -57,6 +58,16 @@
View Stats
</button>
</td>
<td>
<button
mat-raised-button
routerLink="/players/stats"
color="warn"
(click)="deletePlayerById(p.id)"
>
Delete Record
</button>
</td>
</tr>
</tbody>
</table>
Expand Down
Loading

0 comments on commit ae4d310

Please sign in to comment.