diff --git a/backend/serverless.ts b/backend/serverless.ts index 51afe51..c5ea97c 100644 --- a/backend/serverless.ts +++ b/backend/serverless.ts @@ -6,7 +6,6 @@ const serverlessConfiguration: AwsConfig.Serverless = { service: 'dojo-serverless-backend', frameworkVersion: '>=1.83', plugins: ['serverless-webpack', 'serverless-step-functions'], - configValidationMode: 'error', provider: { name: 'aws', runtime: 'nodejs10.x', @@ -38,6 +37,35 @@ const serverlessConfiguration: AwsConfig.Serverless = { }, ], }, + getVirus: { + handler: 'src/handlers/virus/get.main', + events: [ + { + http: { + method: 'get', + path: 'virus', + cors: true, + }, + }, + ], + }, + createVirus: { + handler: 'src/handlers/virus/create.main', + events: [ + /* { + schedule: 'rate(1 minute)', + je l'ai commentée car sinon elle va tourner + tout le temps + }, */ + { + http: { + method: 'post', // pour l'instant elle ne poste rien dans la session 1 + path: 'virus', + cors: true, + }, + }, + ], + }, }, resources: { Resources: { diff --git a/backend/src/handlers/virus/create.ts b/backend/src/handlers/virus/create.ts new file mode 100644 index 0000000..6e9499b --- /dev/null +++ b/backend/src/handlers/virus/create.ts @@ -0,0 +1,8 @@ +import { APIGatewayProxyHandler } from 'aws-lambda'; + +import { success } from '@libs/response'; + +export const main: APIGatewayProxyHandler = async event => { + console.log(event); + return success({}); +}; diff --git a/backend/src/handlers/virus/get.ts b/backend/src/handlers/virus/get.ts new file mode 100644 index 0000000..e85d51f --- /dev/null +++ b/backend/src/handlers/virus/get.ts @@ -0,0 +1,29 @@ +import { APIGatewayProxyHandler } from 'aws-lambda'; +import uuid from 'uuid'; + +import { success } from '@libs/response'; + + +export const main: APIGatewayProxyHandler = async (event) => { + /* + La fonction doit générer 1 virus si un id est passé en query param + Sinon elle en génère 4 + */ + let viruses = [{}] + if (event.queryStringParameters) { + if (event.queryStringParameters.id) { + viruses = [ + { id: uuid() }, + ]; + } else { + viruses = [ + { id: uuid() }, + { id: uuid() }, + { id: uuid() }, + { id: uuid() } + ]; + } + } + + return success({ viruses }); +}; \ No newline at end of file diff --git a/frontend/src/pages/Home.tsx b/frontend/src/pages/Home.tsx index d04649d..38c2df3 100644 --- a/frontend/src/pages/Home.tsx +++ b/frontend/src/pages/Home.tsx @@ -3,7 +3,7 @@ import { Typography, Row, Button } from 'antd'; import { PlusOutlined } from '@ant-design/icons'; import styled from 'styled-components'; import { v4 } from 'uuid'; - +import { useAsync } from 'react-use'; import virus1 from 'assets/Virus1.png'; import virus2 from 'assets/Virus2.png'; import virus3 from 'assets/Virus3.png'; @@ -51,22 +51,27 @@ const Virus = styled.img` const getRandomPosition = (n: number) => Math.floor(Math.random() * n); -const getRandomVirus = () => ({ - id: v4(), +const getRandomVirus = (id: string) => ({ + id, positionX: getRandomPosition(100), positionY: getRandomPosition(100), src: VirusImgs[getRandomPosition(6)], }); export default () => { - const [viruses, setViruses] = useState([ - getRandomVirus(), - getRandomVirus(), - getRandomVirus(), - ]); + const [viruses, setViruses] = useState([]); + useAsync(async (id = 'TEST') => { + const response = await fetch( + `${process.env.REACT_APP_API_BASE_URL}/virus?id=${encodeURIComponent(id)}`, + { method: 'GET' }, + ); + const { viruses } = await response.json(); + setViruses(viruses.map(({ id }: { id: string }) => getRandomVirus(id))); + + }); const addVirus = () => - setViruses((prevViruses) => prevViruses.concat(getRandomVirus())); + setViruses((prevViruses) => prevViruses.concat(getRandomVirus(v4()))); const killVirus = (virusId: string) => setViruses((prevViruses) => prevViruses.filter(({ id }) => id !== virusId));