Skip to content

Commit

Permalink
Merge pull request #29 from AlexcastroDev/chore/avoid-multiple-reques…
Browse files Browse the repository at this point in the history
…t-count

chore: avoid multiple request
  • Loading branch information
alexcastrodev authored Oct 19, 2023
2 parents f9a594a + 4e72b8f commit 4dd8828
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
21 changes: 18 additions & 3 deletions ibge/Controllers/BulkController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,27 @@ private async Task<BulkProcessResults> ProcessExcel(Stream stream, List<StatesRo
{
var i = 0;
var values = new List<Dictionary<string, object>>();
var ids = _locationService.GetIds();

foreach (CityRow row in stream.Query<CityRow>(sheetName: "MUNICIPIOS"))
{
i++;
if (ids.Contains(row.Codigo_Municipio))
{
values.Add(
new Dictionary<string, object> { { "Reference", i }, { "ID", row.Codigo_Municipio }, { "City", row.Nome_Municipio }, { "Error", "Localização já existe" } }
);
continue;
}

StatesRow? state = states.Find(s => s.Code == row.Codigo_UF);
if (state == null) continue;
if (state == null)
{
values.Add(
new Dictionary<string, object> { { "Reference", i }, { "ID", row.Codigo_Municipio }, { "City", row.Nome_Municipio }, { "Error", "Estado não encontrado" } }
);
continue;
};

try
{
Expand All @@ -102,12 +117,12 @@ private async Task<BulkProcessResults> ProcessExcel(Stream stream, List<StatesRo
if (ex is ConflictException)
{
values.Add(
new Dictionary<string, object> { { "Reference", i }, { "ID", row.Codigo_Municipio }, { "State", state.Name }, { "City", row.Nome_Municipio }, { "Error", "Localização já existe" } }
new Dictionary<string, object> { { "Reference", i }, { "ID", row.Codigo_Municipio }, { "City", row.Nome_Municipio }, { "Error", "Localização já existe" } }
);
continue;
}
values.Add(
new Dictionary<string, object> { { "Reference", i }, { "ID", row.Codigo_Municipio }, { "State", state.Name }, { "City", row.Nome_Municipio }, { "Error", "Linha inválida" } }
new Dictionary<string, object> { { "Reference", i }, { "ID", row.Codigo_Municipio }, { "City", row.Nome_Municipio }, { "Error", "Linha inválida" } }
);
}
}
Expand Down
3 changes: 2 additions & 1 deletion ibge/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"AZURE_DB_URL": "localhost",
"AZURE_DB_USER": "sa",
"AZURE_DB_PASSWORD": "Admin123@@",
"AZURE_DB_DB": "ibge"
"AZURE_DB_DB": "ibge",
"AZURE_BLOB_TOKEN": "DMELxSryBXiYP4L3X1KnosTPNIL03EmX9QpY/YkkI7MI4m+N8ObYH9sEKPBozbavH/LwVnobR2F8+AStj7jEww=="
},
"dotnetRunMessages": true
},
Expand Down
1 change: 1 addition & 0 deletions ibge/Repositories/ILocationRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ public interface ILocationRepository
Task<ActionResult<int>> Delete(int id);
Task<ActionResult<Location?>> Update(int id, LocationUpdate model);
Task<ActionResult<Location?>> Patch(int id, LocationPatch model);
List<int> GetIds();
}
9 changes: 9 additions & 0 deletions ibge/Services/LocationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ public LocationService(AppDbContext context)
_context = context;
}

public List<int> GetIds()
{
var locationsIds = _context.Locations
.Select(l => l.Id)
.ToList();

return locationsIds;
}

public async Task<ActionResult<List<Location>>> Get(LocationSearchCriteria searchCriteria)
{
var query = _context.Locations.AsQueryable();
Expand Down

0 comments on commit 4dd8828

Please sign in to comment.