Skip to content

Commit

Permalink
Adding/Refactor services
Browse files Browse the repository at this point in the history
  • Loading branch information
StanislawMalinski committed May 10, 2024
1 parent c10f056 commit 3c92b4e
Show file tree
Hide file tree
Showing 9 changed files with 467 additions and 303 deletions.
58 changes: 30 additions & 28 deletions src/lists/TournamentsList.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,45 +10,45 @@ import React, { useState, useEffect } from "react";
function TournamentsList({ tournaments, isAuthenticated }) {
const navigate = useNavigate();
const currentDate = new Date();
const [tournamentList, setTournamentList] = useState([]);

const [upcomingTournaments, setUpcomingTournaments] = useState([]);
const [pastTournaments, setPastTournaments] = useState([]);
const [message, setMessage] = useState('');

useEffect(() => {
const fetchTournamentData = async () => {
try {
const gl = await TournamentService.getListOfTournaments();
setTournamentList(gl.data.data);
} catch (e) {
console.log(e);
setMessage('Sorry, there was a problem with fetching tournament data.Try again later');
}
};
TournamentService.getListOfTournaments(
{"page":0, "pagesize":10, "maxPlayOutDate": currentDate,})
.then((response) => {
setPastTournaments(response.data);
}).catch((error) => {
console.log(error);
setMessage('Sorry, there was a problem with fetching tournament data.Try again later');
});

fetchTournamentData();
TournamentService.getListOfTournaments(
{"page":0, "pagesize":10, "minPlayOutDate": currentDate,})
.then((response) => {
setUpcomingTournaments(response.data);
}).catch((error) => {
console.log(error);
setMessage('Sorry, there was a problem with fetching tournament data.Try again later');
});
}, []);

const upcomingTournaments = tournamentList.filter(t => {
const tournamentDate = new Date(t.tournamentsDate);
return tournamentDate > currentDate;
});
const pastTournaments = tournamentList.filter(t => {
const tournamentDate = new Date(t.tournamentsDate);
return tournamentDate <= currentDate;
});;
const handleTournamentClick = (tournamentId) => {
navigate(`/tournaments/details/${tournamentId}`);
};


const TournamentItem = ({ tournament }) => (
<div className="tournament-item" onClick={() => handleTournamentClick(tournament.id)}>
<div className="tournament-detail">{tournament.tournamentsTitle}</div>
<div className="tournament-detail">{tournament.tournamentTitle}</div>
<div className="tournament-detail">{tournament.author}</div>
<div className="tournament-detail">{tournament.tournamentsDate}</div>
<div className="tournament-detail">{tournament.playersLimit}</div>
</div>
);

const handleTournamentClick = (tournamentId) => {
navigate(`/tournaments/details/${tournamentId}`);
};

return (
<div className="tournaments-container">
<UserButtons/>
Expand All @@ -67,13 +67,15 @@ function TournamentsList({ tournaments, isAuthenticated }) {
<span className="header-detail">Action</span>
</div>
<div className="tournaments-content">
{upcomingTournaments.map(tournament => (
{isAuthenticated ?
upcomingTournaments.map(tournament => (
<div>
<TournamentItem key={tournament.id} tournament={tournament} />
<DeleteTournamentButton key={tournament.id} />
<DeleteTournamentButton tournamentId={tournament.id} />
</div>
))}
{upcomingTournaments.map(tournament => (
))
:
upcomingTournaments.map(tournament => (
<TournamentItem key={tournament.id} tournament={tournament} />
))}
</div>
Expand Down
30 changes: 1 addition & 29 deletions src/services/Api.js
Original file line number Diff line number Diff line change
Expand Up @@ -496,33 +496,5 @@ function ApiTest() {
}

export {
GetAchievementsTypes,
BanPlayer,
UnbanPlayer,
ResetDatabase,
DeleteDatabase,
GetGameTypes,
GetAvailableGameTypes,
GetGameType,
AddGameType,
UpdateGameType,
DeleteGameType,
RegisterPlayer,
LoginPlayer,
GetUser,
GetPointsForPlayer,
GetLeaderboard,
GetPointsHistoryForPlayer,
GetTournaments,
GetAllTournaments,
GetFilteredTournaments,
AddTournament,
UpdateTournament,
DeleteTournament,
RegisterBotForTournament,
UnregisterBotForTournament,
GetUserSettings,
UpdateUserSettings,
DeleteUserSettings,
ApiTest,
Request,
}
65 changes: 62 additions & 3 deletions src/services/BotService.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,68 @@
import { Api } from './Api'
export const BotService = {

getForPlayer: async function (login) {
getBotsForPlayer: async function (id) {
// Type GET
// Parameters:
//// playerId - integer - int64
try {
return await Api.get(`Bot/getForPlayer?playerId=${login}`)
return await Api.get(`Player/getBotsForPlayer?playerId=${id}`)
} catch(e) {
return Api.processError(e)
}
},
addBot: async function (GameId, Language, BotFile) {
// Type POST
// Paramters:
//// GameId - integer - int64
//// Language - integer - int32
// Body:
//// BotFile - string - binary

try {
return await Api.post(`Bot/add?GameId=${GameId}&Language=${Language}`, {
BotFile: BotFile
})
} catch(e) {
return Api.processError(e)
}
},
deleteBot: async function (botId) {
// Type DELETE
// Parameters:
//// botId - integer - int64
try {
return await Api.delete(`Bot/delete?botId=${botId}`)
} catch(e) {
return Api.processError(e)
}
},
getBot: async function (botId) {
// Type GET
// Parameters:
//// botId - integer - int64
try {
return await Api.get(`Bot/getOne?botId=${botId}`)
} catch(e) {
return Api.processError(e)
}
},
getBotsForPlayer: async function (playerId) {
// Type GET
// Parameters:
//// playerId - integer - int64
try {
return await Api.get(`Player/getBotsForPlayer?playerId=${playerId}`)
} catch(e) {
return Api.processError(e)
}
},
getBotFile: async function (botId, playerId) {
// Type GET
// Parameters:
//// botId - integer - int64
//// playerId - integer - int64
try {
return await Api.get(`Bot/getBotFile?botId=${botId}&playerId=${playerId}`)
} catch(e) {
return Api.processError(e)
}
Expand Down
87 changes: 85 additions & 2 deletions src/services/GameService.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,21 @@ const currentTime = getCurrentTime();

export const GameService = {

getListOfGames: async function () {
getListOfGames: async function (page, pagesize) {
// Type GET
// Parameters:
//// page - integer - int32
//// pagesize - integer - int32
try {
return await Api.get('GameType/getAll')
return await Api.get(`GameType/getAll?page=${page}&pagesize=${pagesize}`)
} catch(e) {
return Api.processError(e)
}
},
deleteGame: async function (id) {
// Type DELETE
// Parameters:
//// id - integer - int32
try {
return await Api.delete(`GameType/delete?id=${id}`);
} catch(e) {
Expand All @@ -39,6 +46,14 @@ export const GameService = {
}
},
addGameType: async function (numOfPlayers, gameFile, gameInstructions, interfaceDefinition, isAvaiableForPlay) {
// Type POST
// Body:
//// NumberOfPlayer - integer - int32
//// GameFile - string - binary
//// GameInstructions - string
//// InterfaceDefinition - string
//// Language - integer (typ)
//// IsAvaiableForPlay - boolean
try {
return await Api.post('GameType/add',{
numbersOfPlayer: numOfPlayers,
Expand All @@ -53,4 +68,72 @@ export const GameService = {
return Api.processError(e)
}
},
getAvailableGames: async function (page, pagesize) {
// Type GET
// Parameters:
//// page - integer - int32
//// pagesize - integer - int32
try {
return await Api.get(`GameType/getAvailable?page=${page}&pagesize=${pagesize}`)
} catch(e) {
return Api.processError(e)
}
},
getGame: async function (id) {
// Type GET
// Parameters:
//// id - integer - int32
try {
return await Api.get(`GameType/get?id=${id}`)
} catch(e) {
return Api.processError(e)
}
},
getByName: async function (name, page, pagesize) {
// Type GET
// Parameters:
//// name - string
//// page - integer - int32
//// pagesize - integer - int32
try {
return await Api.get(`GameType/getByName?name=${name}&page=${page}&pagesize=${pagesize}`)
} catch(e) {
return Api.processError(e)
}
},
getAllForPlayer: async function (name) {
// Type GET
// Parameters:
//// name - string
try {
return await Api.get(`GameType/getAllForPlayer?name=${name}`)
} catch(e) {
return Api.processError(e)
}
},
updateGameType: async function (numOfPlayers, gameFile, gameInstructions, interfaceDefinition, isAvaiableForPlay) {
// Type PUT
// Parameters:
//// id - integer - int32
// Body:
//// NumberOfPlayer - integer - int32
//// GameFile - string - binary
//// GameInstructions - string
//// InterfaceDefinition - string
//// Language - integer (typ)
//// IsAvaiableForPlay - boolean
try {
return await Api.put('GameType/update',{
numbersOfPlayer: numOfPlayers,
lastModification: currentTime,
gameFile: gameFile,
gameInstructions: gameInstructions,
interfaceDefinition: interfaceDefinition,
isAvaiableForPlay: isAvaiableForPlay
})

} catch(e) {
return Api.processError(e)
}
},
}
28 changes: 26 additions & 2 deletions src/services/PointsService.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,33 @@
import { Api } from './Api'
export const PointsService = {

getLeaderboards: async function () {
getLeaderboards: async function (page, pageSize) {
// Type GET
// Parameters:
//// page - integer - int32
//// pagesize - integer - int32
try {
return await Api.get('Points/getLeaderboards')
return await Api.get(`Points/getLeaderboards?page=${page}&pageSize=${pageSize}`)
} catch(e) {
return Api.processError(e)
}
},
getPointsForPlayer: async function (id) {
// Type GET
// Parameters:
//// id - integer - int32
try {
return await Api.get(`Points/getPointsForPlayer?id=${id}`)
} catch(e) {
return Api.processError(e)
}
},
getPointsHistoryForPlayer: async function (id) {
// Type GET
// Parameters:
//// id - integer - int32
try {
return await Api.get(`Points/getPointsHistoryForPlayer?id=${id}`)
} catch(e) {
return Api.processError(e)
}
Expand Down
Loading

0 comments on commit 3c92b4e

Please sign in to comment.