From 3c92b4ede8cc7235ccb43c8e09ca24cf4a65f44f Mon Sep 17 00:00:00 2001 From: Stanleyowskiki Date: Fri, 10 May 2024 15:51:21 +0200 Subject: [PATCH] Adding/Refactor services --- src/lists/TournamentsList.js | 58 ++++---- src/services/Api.js | 30 +--- src/services/BotService.js | 65 ++++++++- src/services/GameService.js | 87 ++++++++++- src/services/PointsService.js | 28 +++- src/services/TournamentService.js | 132 ++++++++++++++--- src/services/UserService.js | 129 ++++++++++++++++- src/services/UserSettingsService.js | 26 ++++ src/services/client.config.json | 215 ---------------------------- 9 files changed, 467 insertions(+), 303 deletions(-) create mode 100644 src/services/UserSettingsService.js delete mode 100644 src/services/client.config.json diff --git a/src/lists/TournamentsList.js b/src/lists/TournamentsList.js index 9e0c9ea..6bb0d67 100644 --- a/src/lists/TournamentsList.js +++ b/src/lists/TournamentsList.js @@ -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 }) => (
handleTournamentClick(tournament.id)}> -
{tournament.tournamentsTitle}
+
{tournament.tournamentTitle}
{tournament.author}
{tournament.tournamentsDate}
{tournament.playersLimit}
); - const handleTournamentClick = (tournamentId) => { - navigate(`/tournaments/details/${tournamentId}`); - }; - return (
@@ -67,13 +67,15 @@ function TournamentsList({ tournaments, isAuthenticated }) { Action
- {upcomingTournaments.map(tournament => ( + {isAuthenticated ? + upcomingTournaments.map(tournament => (
- +
- ))} - {upcomingTournaments.map(tournament => ( + )) + : + upcomingTournaments.map(tournament => ( ))}
diff --git a/src/services/Api.js b/src/services/Api.js index 3733431..5bc8826 100644 --- a/src/services/Api.js +++ b/src/services/Api.js @@ -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, } \ No newline at end of file diff --git a/src/services/BotService.js b/src/services/BotService.js index 0b28b7b..8b48b2a 100644 --- a/src/services/BotService.js +++ b/src/services/BotService.js @@ -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) } diff --git a/src/services/GameService.js b/src/services/GameService.js index 062c693..6f40929 100644 --- a/src/services/GameService.js +++ b/src/services/GameService.js @@ -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) { @@ -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, @@ -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) + } + }, } \ No newline at end of file diff --git a/src/services/PointsService.js b/src/services/PointsService.js index c4c35a4..4503ed3 100644 --- a/src/services/PointsService.js +++ b/src/services/PointsService.js @@ -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) } diff --git a/src/services/TournamentService.js b/src/services/TournamentService.js index 2d47d55..b6773d3 100644 --- a/src/services/TournamentService.js +++ b/src/services/TournamentService.js @@ -1,5 +1,4 @@ import { Api } from './Api' - const getCurrentTime = () => { const now = new Date(); const year = now.getFullYear(); @@ -16,42 +15,137 @@ const getCurrentTime = () => { const currentTime = getCurrentTime(); export const TournamentService = { - - getListOfTournaments: async function () { + getFilteredTournaments: async function (page, pageSize, tournamentTitle, minPlayOutDate, maxPlayOutDate, creator, userParticipation) { + // Type GET + // Parameters: + //// page - integer - int32 + //// pagesize - integer - int32 + // Body: + //// tournamentTitle - string + //// minPlayOutDate - date (2024-05-10T13:13:15.534Z) + //// maxPlayOutDate - date (2024-05-10T13:13:15.534Z) + //// creator - string + //// userParticipation - string try { - return await Api.get('Tournament/getAll') + return await Api.get(`Tournament/getFiltered?page=${page}&pageSize=${pageSize}`,{ + tournamentTitle: tournamentTitle, + minPlayOutDate: minPlayOutDate, + maxPlayOutDate: maxPlayOutDate, + creator: creator, + userParticipation: userParticipation}) } catch(e) { return Api.processError(e) } }, + getListOfTournaments: async function (page, pageSize) { + // Type GET + // Parameters: + //// page - integer - int32 + //// pagesize - integer - int32 + try { + return await Api.get(`Tournament/getFiltered?page=${page}&pageSize=${pageSize}`) + } catch(e) { + return Api.processError(e) + } + }, deleteTournament: async function (id) { + // Type DELETE + // Parameters: + //// id - integer - int64 + try { + return await Api.delete(`Tournament/delete?id=${id}`) + } catch(e) { + return Api.processError(e) + } + }, + addTournament: async function (tournamentTitle, description, gameId, playersLimit, tournamentsDate, constraints, image, memoryLimit, timeLimit) { + // Type POST + // Body: + //// tournamentTitle - string + //// description - string + //// gameId - number + //// playersLimit - number + //// tournamentsDate - string (e.g. "2024-05-10T13:19:46.277Z") + //// constraints - string + //// image - string + //// memoryLimit - number + //// timeLimit - number + try { + return await Api.post(`Tournament/add`,{ + tournamentTitle: tournamentTitle, + description: description, + gameId: gameId, + playersLimit: playersLimit, + tournamentsDate: tournamentsDate, + constraints: constraints, + image: image, + memoryLimit: memoryLimit, + timeLimit: timeLimit}) + } catch(e) { + return Api.processError(e) + } + }, + getTournament: async function (id) { + // Type GET + // Parameters: + //// id - integer - int64 + try { + return await Api.get(`Tournament/getOne?id=${id}`) + } catch(e) { + return Api.processError(e) + } + }, + registerBot: async function (tournamentId, botId) { + // Type POST + // Parameters: + //// tournamentId - integer - int64 + //// botId - integer - int64 try { - return await Api.delete(`Tournament/delete?id=${id}`); + return await Api.post(`Tournament/registerBot?tournamentId=${tournamentId}&botId=${botId}`) } catch(e) { return Api.processError(e) } }, - addTournament: async function (numOfPlayers, gameFile, gameInstructions, interfaceDefinition, isAvaiableForPlay) { + unregisterBot: async function (tournamentId, botId) { + // Type DELETE + // Parameters: + //// tournamentId - integer - int64 + //// botId - integer - int64 try { - return await Api.post('Tournament/add',{ - numbersOfPlayer: numOfPlayers, - lastModification: currentTime, - gameFile: gameFile, - gameInstructions: gameInstructions, - interfaceDefinition: interfaceDefinition, - isAvaiableForPlay: isAvaiableForPlay - }) + return await Api.delete(`Tournament/unregisterBot?tournamentId=${tournamentId}&botId=${botId}`) } catch(e) { return Api.processError(e) } }, - getForPlayer: async function (login) { + updateTournament: async function (tournamentId, tournamentTitle, description, gameId, playersLimit, tournamentsDate, constraints, image, memoryLimit, timeLimit) { + // Type PUT + // Parameters: + //// id - integer - int64 + // Body: + //// tournamentId - number + //// tournamentTitle - string + //// description - string + //// gameId - number + //// playersLimit - number + //// tournamentsDate - string (e.g. "2024-05-10T13:19:46.277Z") + //// constraints - string + //// image - string + //// memoryLimit - number + //// timeLimit - number try { - return await Api.post(`Tournament/getFiltered`,{ - creator:login - }) + return await Api.put(`Tournament/update`,{ + tournamentId: tournamentId, + tournamentTitle: tournamentTitle, + description: description, + gameId: gameId, + playersLimit: playersLimit, + tournamentsDate: tournamentsDate, + constraints: constraints, + image: image, + memoryLimit: memoryLimit, + timeLimit: timeLimit}) } catch(e) { return Api.processError(e) } }, -} \ No newline at end of file +} diff --git a/src/services/UserService.js b/src/services/UserService.js index d8c61d0..3deefee 100644 --- a/src/services/UserService.js +++ b/src/services/UserService.js @@ -5,20 +5,42 @@ import store from '../User/store' export const UserService = { registerUser: async function (username, email, password, roleId) { + // Type POST + // Body: + //// email - string + //// login - string + //// password - string try { - return await Api.post('Player/register', { + return await Api.post('Player/registerPlayer', { + email: email, + login: username, + password: password, + }) + } catch(e) { + return Api.processError(e) + } + }, + registerAdmin: async function (username, email, password, roleId) { + // Type POST + // Body: + //// email - string + //// login - string + //// password - string + try { + return await Api.post('Player/registerAdmin', { email: email, login: username, password: password, - roleId: roleId, - isBanned: false, - points: 0 }) } catch(e) { return Api.processError(e) } }, loginUser: async function (email, password) { + // Type POST + // Body: + //// email - string + //// password - string try { const response = await Api.post('Player/login', { email: email, @@ -34,5 +56,102 @@ export const UserService = { } catch(e) { return Api.processError(e) } - } + }, + getPlayerInfo: async function (idOrName) { + + // Type GET + // Parameters: + //// idOrName - integer - int64 + //// - string + try { + if (typeof idOrName === 'number') { + return await Api.get(`Player/getPlayerInfo?id=${idOrName}`) + } else if (typeof idOrName === 'string') { + return await Api.get(`Player/getPlayerInfoByName?playerName=${idOrName}`) + } + } catch(e) { + return Api.processError(e) + } + }, + changePassword: async function (oldPassword, newPassword) { + // Type POST + // Body: + //// password - string + //// changePassword - string + + try { + return await Api.post('Player/changePassword', { + password: oldPassword, + changePassword: newPassword, + }) + } catch(e) { + return Api.processError(e) + } + }, + changeLogin: async function (login, newLogin) { + // Type POST + // Body: + //// login - string + //// newLogin - string + try { + return await Api.post('Player/changeLogin', { + login: login, + newLogin: newLogin, + }) + } catch(e) { + return Api.processError(e) + } + }, + getGamesForPlayer: async function (id) { + // Type GET + // Parameters: + //// id - integer - int64 + try { + return await Api.get(`Player/getGamesForPlayer?id=${id}`) + } catch(e) { + return Api.processError(e) + } + }, + getImageForPlayer: async function (id) { + // Type GET + // Parameters: + //// playerId - integer - int64 + try { + return await Api.get(`Player/getImageForPlayer?id=${id}`) + } catch(e) { + return Api.processError(e) + } + }, + changeMyImage: async function (image) { + // Type POST + // Body: + //// image - string + try { + return await Api.post('Player/changeMyImage', { + image: image, + }) + } catch(e) { + return Api.processError(e) + } + }, + deleteAccount: async function (password) { + // Type DELETE + // Parameters: + //// password - string + try { + return await Api.delete(`Player/deleteAccount`) + } catch(e) { + return Api.processError(e) + } + }, + getBotsForPlayer: async function (id) { + // Type GET + // Parameters: + //// playerId - integer - int64 + try { + return await Api.get(`Player/getBotsForPlayer?playerId=${id}`) + } catch(e) { + return Api.processError(e) + } + }, } \ No newline at end of file diff --git a/src/services/UserSettingsService.js b/src/services/UserSettingsService.js new file mode 100644 index 0000000..0f95812 --- /dev/null +++ b/src/services/UserSettingsService.js @@ -0,0 +1,26 @@ +import { Api } from './Api' + +export const UserSettings = { + getUserSettings: async function () { + // Type GET + try { + return await Api.get(`UserSettings/get`) + } catch(e) { + return Api.processError(e) + } + }, + updateUserSettings: async function (isDarkTheme, language) { + // Type PUT + // Body: + //// isDarkTheme - boolean + //// language - string + try { + return await Api.put(`UserSettings/update`, { + isDarkTheme: isDarkTheme, + language: language + }) + } catch(e) { + return Api.processError(e) + } + }, +}; \ No newline at end of file diff --git a/src/services/client.config.json b/src/services/client.config.json deleted file mode 100644 index 8b139cb..0000000 --- a/src/services/client.config.json +++ /dev/null @@ -1,215 +0,0 @@ -{ - "protocol": "http", - "host": "localhost", - "port": 3000, - "path": "api/", - "version": "v1", - "urls": { - "achievements": { - "path": "/Achievement", - "functions":{ - "GetAchievementsForPlayer": { - "path": "/getAchievementsForPlayer", - "method": "GET", - "params": ["playerId"] - }, - "GetAchievementTypes": { - "path": "/getAchievementTypes", - "method": "GET" - } - } - }, - "administrative": { - "path": "/Administrative", - "functions": { - "BanPlayer": { - "path": "/banPlayer", - "method": "PUT", - "params": ["playerId"] - }, - "UnbanPlayer": { - "path": "/unbanPlayer", - "method": "PUT", - "params": ["playerId"] - } - } - }, - "bot": { - "path": "/Bot", - "functions": { - "GetAll": { - "path": "/getAll", - "method": "GET" - }, - "GetOne": { - "path": "/getOne", - "method": "GET", - "params": ["id"] - }, - "Add": { - "path": "/add", - "method": "POST", - "body": ["name","description","gameId","botFile","isAvailableForPlay"] - }, - "Delete" : { - "path": "/delete", - "method": "DELETE", - "params": ["id"] - } - } - }, - "database" : { - "path": "/Database", - "functions": { - "ResetDatabase" : - { - "path": "/reset", - "method": "GET" - }, - "DeleteDatabase" : - { - "path": "/delete", - "method": "DELETE" - } - } - }, - "gametype" : { - "path": "/GameType", - "functions": { - "GetGameType": { - "path": "/getOne", - "method": "GET", - "params": ["id"] - }, - "GetGameTypes": { - "path": "/getAll", - "method": "GET" - }, - "GetAvailableGameTypes": { - "path": "/getAvailable", - "method": "GET" - }, - "AddGameType": { - "path": "/add", - "method": "POST", - "body": ["numbersOfPlayer","lastModification","gameFile","gameInstructions","interfaceDefinition","isAvaiableForPlay"] - }, - "UpdateGameType": { - "path": "/update", - "method": "PUT", - "body": ["id","numbersOfPlayer","lastModification","gameFile","gameInstructions","interfaceDefinition","isAvaiableForPlay"] - }, - "DeleteGameType": { - "path": "/delete", - "method": "DELETE", - "params": ["id"] - } - } - }, - "player" : { - "path": "/Player", - "functions": { - "RegisterPlayer": { - "path": "/register", - "method": "POST", - "body": ["name","email","password", "roleId", "isBanned", "points"] - }, - "LoginPlayer": { - "path": "/login", - "method": "POST", - "body": ["email","password"] - }, - "GetPlayer": { - "path": "/getPlayerInfo", - "method": "GET", - "params": ["playerId"] - } - } - }, - "points" : { - "path": "/Points", - "functions": { - "GetPointsForPlayer": { - "path": "/getPointsForPlayer", - "method": "GET", - "params": ["playerId"] - }, - "GetLeaderboard": { - "path": "/getLeaderboards", - "method": "GET" - }, - "GetPointsHistoryForPlayer": { - "path": "/getPointsHistoryForPlayer", - "method": "GET", - "params": ["playerId"] - } - } - }, - "tournament": { - "path": "/Tournament", - "functions": { - "AddTournament": { - "path": "/add", - "method": "POST", - "body": ["id","tournamentsTitle","description","gameId","playersLimit","tournamentsDate","postedDate","wasPlayedOut","constrains","image"] - }, - "DeleteTournament": { - "path": "/delete", - "method": "DELETE", - "params": ["id"] - }, - "GetAllTournaments": { - "path": "/getAll", - "method": "GET" - }, - "GetFilteredTournaments": { - "path": "/getFiltered", - "method": "GET", - "params": ["gameId","date"] - }, - "GetTournament": { - "path": "/getOne", - "method": "GET", - "params": ["id"] - }, - "RegisterBot": { - "path": "/registerBot", - "method": "PUT", - "body": ["tournamentsId","botId"] - }, - "UnregisterBot": { - "path": "/unregisterBot", - "method": "PUT", - "params": ["tournamentsId","botId"] - }, - "UpdateTournament": { - "path": "/update", - "method": "PUT", - "body": ["id","tournamentsTitle","description","gameId","playersLimit","tournamentsDate","postedDate","wasPlayedOut","constrains","image"] - } - } - }, - "userSettings": { - "path": "/UserSettings", - "functions": { - "AddUserSettings": { - "path": "/createForPlayer", - "method": "POST", - "params": ["id"], - "body": ["playerId","isDarkTheme","language"] - }, - "GetUserSettings": { - "path": "/getForPlayer", - "method": "GET", - "params": ["id"] - }, - "UpdateUserSettings": { - "path": "/update", - "method": "PUT", - "params": ["id"], - "body": ["playerId","isDarkTheme","language"] - } - } - } - } -} \ No newline at end of file