Skip to content
This repository has been archived by the owner on Nov 15, 2022. It is now read-only.

Commit

Permalink
Changed /api/province endpoint, updated data, increased performance
Browse files Browse the repository at this point in the history
  • Loading branch information
Samurai016 committed Apr 24, 2020
1 parent 778dcd7 commit 2e027ad
Show file tree
Hide file tree
Showing 27 changed files with 8,129 additions and 7,985 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/.*
/node_modules
package-lock.json
regionediv.js
regionidiv.js
15,830 changes: 7,915 additions & 7,915 deletions comuni.json

Large diffs are not rendered by default.

54 changes: 32 additions & 22 deletions docs/assets/comuni-ita.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ info:
Semplice API che permette di ottenere in formato JSON i comuni italiani.
*Logo ispired by: [Castle by Jasfart from the Noun Project](https://thenounproject.com/omataloon/)*
version: 2.0.0
version: 2.1.0
x-logo:
url: "./assets/logo.png"
altText: "Comuni ITA Logo"
Expand All @@ -18,7 +18,7 @@ info:
url: https://opensource.org/licenses/MIT
externalDocs:
description: Github
url: https://github.com/Samurai016/comuni-ita
url: https://github.com/Samurai016/Comuni-ITA
servers:
- url: https://comuni-ita.herokuapp.com
- url: http://comuni-ita.herokuapp.com
Expand Down Expand Up @@ -97,6 +97,8 @@ paths:
5XX:
description: Errore del server. **Contattare lo sviluppatore.**
/api/province:
parameters:
- $ref: '#/components/parameters/onlyname'
get:
summary: Ottieni la lista di tutte le province italiane.
operationId: province
Expand All @@ -110,12 +112,12 @@ paths:
schema:
type: array
items:
type: string
example: "roma"
$ref: '#/components/schemas/Provincia'
5XX:
description: Errore del server. **Contattare lo sviluppatore.**
/api/province/{regione}:
parameters:
- $ref: '#/components/parameters/onlyname'
- $ref: '#/components/parameters/regione'
get:
summary: Ottieni la lista di tutte le province della regione indicata.
Expand All @@ -130,8 +132,7 @@ paths:
schema:
type: array
items:
type: string
example: "roma"
$ref: '#/components/schemas/Provincia'
400:
description: Richiesta errata. Probabilmente uno dei parametri è errato.
5XX:
Expand Down Expand Up @@ -162,22 +163,16 @@ components:
required: true
schema:
type: string
description: |-
Nome della provincia di cui si vogliono i dati.
Eventuali spazi o apostrofi vanno convertiti in notazione URL ([Link a W3Schools](https://www.w3schools.com/tags/ref_urlencode.ASP)).
[Qui per maggiori informazioni](https://github.com/Samurai016/Comuni-ITA/blob/master/README.md#province)
example: "l%27aquila"
description: Nome della provincia di cui si vogliono i dati.
example: "l'aquila"
regione:
in: path
name: regione
required: true
schema:
type: string
description: |-
Nome della regione di cui si vogliono i dati.
Eventuali spazi o apostrofi vanno convertiti in notazione URL ([Link a W3Schools](https://www.w3schools.com/tags/ref_urlencode.ASP)).
[Qui per maggiori informazioni](https://github.com/Samurai016/Comuni-ITA/blob/master/README.md#regioni)
example: "valle%20d%27aosta"
description: Nome della regione di cui si vogliono i dati.
example: "valle d'aosta"
onlyname:
in: query
name: onlyname
Expand Down Expand Up @@ -207,11 +202,6 @@ components:
type: string
description: Provincia del comune (in minuscolo, per esteso).
example: "roma"
sigla:
type: string
description: Sigla della provincia del comune (in maiuscolo).
example: "RM"
pattern: "[A-Z]{2}"
codiceCatastale:
type: string
description: Codice catastale del comune.
Expand All @@ -232,4 +222,24 @@ components:
lng:
type: number
description: Longitudine
example: 12.4823243
example: 12.4823243
Provincia:
type: object
properties:
nome:
type: string
description: Nome della provincia.
example: "roma"
sigla:
type: string
description: Sigla della provincia (in minuscolo).
example: "rm"
pattern: "[a-z]{2}"
codice:
type: string
description: Codice ISTAT della provincia.
example: "058"
regione:
type: string
description: Regione del provincia (in minuscolo).
example: "lazio"
58 changes: 42 additions & 16 deletions docs/comuni-ita.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"info": {
"title": "Comuni ITA",
"description": "Semplice API che permette di ottenere in formato JSON i comuni italiani.\n\n*Logo ispired by: [Castle by Jasfart from the Noun Project](https://thenounproject.com/omataloon/)*",
"version": "2.0.0",
"version": "2.1.0",
"x-logo": {
"url": "./assets/logo.png",
"altText": "Comuni ITA Logo"
Expand All @@ -20,7 +20,7 @@
},
"externalDocs": {
"description": "Github",
"url": "https://github.com/Samurai016/comuni-ita"
"url": "https://github.com/Samurai016/Comuni-ITA"
},
"servers": [
{
Expand Down Expand Up @@ -155,6 +155,11 @@
}
},
"/api/province": {
"parameters": [
{
"$ref": "#/components/parameters/onlyname"
}
],
"get": {
"summary": "Ottieni la lista di tutte le province italiane.",
"operationId": "province",
Expand All @@ -169,8 +174,7 @@
"schema": {
"type": "array",
"items": {
"type": "string",
"example": "roma"
"$ref": "#/components/schemas/Provincia"
}
}
}
Expand All @@ -184,6 +188,9 @@
},
"/api/province/{regione}": {
"parameters": [
{
"$ref": "#/components/parameters/onlyname"
},
{
"$ref": "#/components/parameters/regione"
}
Expand All @@ -202,8 +209,7 @@
"schema": {
"type": "array",
"items": {
"type": "string",
"example": "roma"
"$ref": "#/components/schemas/Provincia"
}
}
}
Expand Down Expand Up @@ -256,8 +262,8 @@
"schema": {
"type": "string"
},
"description": "Nome della provincia di cui si vogliono i dati. \nEventuali spazi o apostrofi vanno convertiti in notazione URL ([Link a W3Schools](https://www.w3schools.com/tags/ref_urlencode.ASP)). \n[Qui per maggiori informazioni](https://github.com/Samurai016/Comuni-ITA/blob/master/README.md#province)",
"example": "l%27aquila"
"description": "Nome della provincia di cui si vogliono i dati.",
"example": "l'aquila"
},
"regione": {
"in": "path",
Expand All @@ -266,8 +272,8 @@
"schema": {
"type": "string"
},
"description": "Nome della regione di cui si vogliono i dati. \nEventuali spazi o apostrofi vanno convertiti in notazione URL ([Link a W3Schools](https://www.w3schools.com/tags/ref_urlencode.ASP)). \n[Qui per maggiori informazioni](https://github.com/Samurai016/Comuni-ITA/blob/master/README.md#regioni)",
"example": "valle%20d%27aosta"
"description": "Nome della regione di cui si vogliono i dati.",
"example": "valle d'aosta"
},
"onlyname": {
"in": "query",
Expand Down Expand Up @@ -303,12 +309,6 @@
"description": "Provincia del comune (in minuscolo, per esteso).",
"example": "roma"
},
"sigla": {
"type": "string",
"description": "Sigla della provincia del comune (in maiuscolo).",
"example": "RM",
"pattern": "[A-Z]{2}"
},
"codiceCatastale": {
"type": "string",
"description": "Codice catastale del comune.",
Expand Down Expand Up @@ -337,6 +337,32 @@
}
}
}
},
"Provincia": {
"type": "object",
"properties": {
"nome": {
"type": "string",
"description": "Nome della provincia.",
"example": "roma"
},
"sigla": {
"type": "string",
"description": "Sigla della provincia (in minuscolo).",
"example": "rm",
"pattern": "[a-z]{2}"
},
"codice": {
"type": "string",
"description": "Codice ISTAT della provincia.",
"example": "058"
},
"regione": {
"type": "string",
"description": "Regione del provincia (in minuscolo).",
"example": "lazio"
}
}
}
}
}
Expand Down
17 changes: 8 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ const province = {
"lecce":"puglia",
"ravenna":"emilia romagna",
"palermo":"sicilia",
"valle d'aosta":"valle d'aosta",
"aosta":"valle d'aosta",
"fermo":"marche",
"chieti":"abruzzo",
"treviso":"veneto",
Expand Down Expand Up @@ -166,24 +166,23 @@ function getComuni(params) {
}

if (params["onlyname"]) {
comuni.forEach((c, index) => comuni[index] = c.nome);
comuni = comuni.map((c) => c.nome);
}

return new ApiResponse(200, comuni);
}
function getProvince(params) {
var output = [];
var output = JSON.parse(fs.readFileSync(`./province.json`, 'utf8'));

if (params["regione"]) { // Handle regione
if (!regioni.includes(params["regione"])) {
return new ApiResponse(400, "Regione inesistente");
}
Object.keys(province).forEach((provincia) => {
if (province[provincia]==params["regione"])
output.push(provincia);
})
} else {
output = Object.keys(province);
output = output.filter((p) => p.regione == params["regione"]);
}

if (params["onlyname"]) {
output = output.map((p) => p.nome);
}

return new ApiResponse(200, output);
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "comuni-ita",
"version": "2.0.0",
"version": "2.1.0",
"description": "API to fetch data about italian municipalities",
"keywords": [
"api",
Expand All @@ -21,7 +21,7 @@
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/Samurai016/comuni-ita.git"
"url": "https://github.com/Samurai016/Comuni-ITA.git"
},
"dependencies": {
"cors": "^2.8.5",
Expand Down
Loading

0 comments on commit 2e027ad

Please sign in to comment.