diff --git a/pages/api/loteria/index.js b/pages/api/loteria/index.js new file mode 100644 index 00000000..b3cdf092 --- /dev/null +++ b/pages/api/loteria/index.js @@ -0,0 +1,28 @@ + +import app from '@/app'; +import { getLoteriaResults } from '@/services/loteria'; + +const action = async (request, response) => { + try { + + const allLoteriaData = await getLoteriaResults(); + + + const premios = allLoteriaData.listaRateioPremio.map(premio => ({ + descricaoFaixa: premio.descricaoFaixa, + valorPremio: premio.valorPremio + })); + + + response.status(200).json(premios); + } catch (error) { + + console.error("Error:", error); + response.status(500).json({ + message: 'Error: 500', + type: 'INTERNAL_SERVER_ERROR', + }); + } +}; + +export default app().get(action); diff --git a/pages/docs/doc/loteria.json b/pages/docs/doc/loteria.json new file mode 100644 index 00000000..a306853b --- /dev/null +++ b/pages/docs/doc/loteria.json @@ -0,0 +1,147 @@ +{ + "tags": [ + { + "name": "LOTERIA", + "description": "Resultados e dados das loterias" + } + ], + "paths": { + "/loterias/v1": { + "get": { + "tags": [ + "LOTERIA" + ], + "summary": "Retorna os últimos resultados das loterias", + "description": "Fornece informações detalhadas sobre os últimos resultados das loterias, incluindo números sorteados, prêmios e mais.", + "responses": { + "200": { + "description": "Operação bem-sucedida", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ResultadoLoteria" + } + } + } + }, + "404": { + "description": "Resultado não encontrado", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ErrorMessage" + } + } + } + } + } + } + } + }, + "components": { + "schemas": { + "ResultadoLoteria": { + "title": "ResultadoLoteria", + "type": "object", + "properties": { + "acumulado": { + "type": "boolean" + }, + "dataApuracao": { + "type": "string", + "format": "date" + }, + "dataProximoConcurso": { + "type": "string", + "format": "date" + }, + "dezenasSorteadasOrdemSorteio": { + "type": "array", + "items": { + "type": "string" + } + }, + "listaDezenas": { + "type": "array", + "items": { + "type": "string" + } + }, + "valorArrecadado": { + "type": "number", + "format": "float" + }, + "valorEstimadoProximoConcurso": { + "type": "number", + "format": "float" + }, + "listaRateioPremio": { + "type": "array", + "items": { + "$ref": "#/components/schemas/RateioPremio" + } + } + + }, + "example": { + "acumulado": true, + "dataApuracao": "29/11/2023", + "dezenasSorteadasOrdemSorteio": [ + "29", "89", "85", "46", "93", "54", "25", "07", "17", "87" + ], + "listaDezenas": [ + "07", "17", "25", "29", "30", "35", "40", "41", "43", "46" + ], + "valorArrecadado": 4690338.0, + "valorEstimadoProximoConcurso": 5000000.0 + } + + }, + "RateioPremio": { + "title": "RateioPremio", + "type": "object", + "properties": { + "descricaoFaixa": { + "type": "string" + }, + "faixa": { + "type": "integer" + }, + "numeroDeGanhadores": { + "type": "integer" + }, + "valorPremio": { + "type": "number", + "format": "float" + } + }, + "example": { + "descricaoFaixa": "20 acertos", + "faixa": 1, + "numeroDeGanhadores": 0, + "valorPremio": 0.0 + } + }, + "ErrorMessage": { + "title": "ErrorMessage", + "required": [ + "name", + "message" + ], + "type": "object", + "properties": { + "name": { + "type": "string" + }, + "message": { + "type": "string" + } + }, + "example": { + "name": "NotFoundError", + "message": "Resultado não encontrado" + } + } + } + } +} diff --git a/services/dados-loteria/loteria.js b/services/dados-loteria/loteria.js new file mode 100644 index 00000000..3048f28e --- /dev/null +++ b/services/dados-loteria/loteria.js @@ -0,0 +1,20 @@ +//recuperação de dados da última loteria +import axios from 'axios'; +let tipoDeLoteria; +//Escolher tipo de loteria +//Exemplo de tipo de loteria - passar como parâmetro para a API + +tipoDeLoteria = 'lotomania' + +const URL_LOTERIAS = 'https://servicebus2.caixa.gov.br/portaldeloterias/api/${tipoDeLoteria}/'; + +export const getLoteriaResults = async () => { + try { + const response = await axios.get(URL_LOTERIAS); + return response.data; + } catch (error) { + console.error("Error ao coletar dados da API", error); + throw error; + } +}; +