Skip to content

Commit

Permalink
Players are stored individually in club_player with existing ID checks
Browse files Browse the repository at this point in the history
  • Loading branch information
augustinecyr committed Apr 7, 2023
1 parent ea9bb2f commit e51756f
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.sg.backend.repositories;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.sg.backend.models.Club;

@Repository
public class ClubRepository {

@Autowired
private JdbcTemplate template;

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;
}
*/

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() {

List<String> ids = template.queryForList(Queries.SQL_ID_CLUB_PLAYER, String.class);
return ids;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@

public class Queries {
public static String SQL_INSERT_CONTACT = "insert into contact(id, email, title, text, attachment) values (?, ?, ?, ?, ?)";
public static String SQL_VIEW_ALL_CONTACT = "select * from contact";
public static String SQL_INSERT_CLUB_PLAYER = "insert into club_player(id, name) values (?, ?)";
public static String SQL_ID_CLUB_PLAYER = "select id from club_player";
}
27 changes: 27 additions & 0 deletions backend/src/main/java/com/sg/backend/service/ClubService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.LinkedList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
Expand All @@ -13,6 +14,7 @@
import org.springframework.web.util.UriComponentsBuilder;

import com.sg.backend.models.Club;
import com.sg.backend.repositories.ClubRepository;

import jakarta.json.Json;
import jakarta.json.JsonArray;
Expand All @@ -23,6 +25,9 @@
@Service
public class ClubService {

@Autowired
private ClubRepository clubRepo;

private static final String URL = "https://transfermarket.p.rapidapi.com/clubs/get-squad";

@Value("${X_RapidAPI_Key}")
Expand Down Expand Up @@ -86,6 +91,28 @@ public List<Club> getSquad(String id) {
}
}
}
// list down all the individual player names and ids respectively
for (Club club : squads) {
System.out.println(club.getName() + ": " + club.getId());
// to store into mySQL
String playerId = club.getId();
String playerName = club.getName();
Club player = new Club();
player.setId(playerId);
player.setName(playerName);
System.out.println(player);
// check the mySQL db for existing IDs before inserting
List<String> ids = clubRepo.getPlayerIds();
if (ids.contains(player.getId())) {
System.out.println("Player already exists in database.");
System.out.println("-------------------------------");
System.out.println("Loading the squad");
// return squads so it will not be blank on view
return squads;
}
// insert only if id doesnt exist
clubRepo.insert(player);
}
}
return squads;
}
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/app/contact.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { HttpClient } from "@angular/common/http"
import { Injectable } from "@angular/core"
import { firstValueFrom } from "rxjs"
import { Contact, FormResponse } from "./models"
import { Contact } from "./models"

@Injectable()
export class ContactService {
Expand Down
4 changes: 0 additions & 4 deletions frontend/src/app/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ export interface Contact {
attachment: File;
}

export interface FormResponse {
formId: string;
}

export interface Club {
name: string;
id: string;
Expand Down
7 changes: 7 additions & 0 deletions schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,10 @@ CREATE TABLE `contact` (
UNIQUE(`id`),
PRIMARY KEY (`id`)
);

CREATE TABLE `club_player` (
`id` CHAR(8) NOT NULL,
`name` varchar(255) NOT NULL,
UNIQUE(`id`),
PRIMARY KEY (`id`)
);

0 comments on commit e51756f

Please sign in to comment.