-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Murilo Guizelin
committed
Aug 2, 2023
1 parent
b7f5544
commit 6f6ad36
Showing
11 changed files
with
231 additions
and
171 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.testes.tags; | ||
|
||
import org.springframework.stereotype.Controller; | ||
import org.springframework.ui.Model; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.ModelAttribute; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
@Controller | ||
public class CounterTagController { | ||
|
||
private final CounterTagService tagCounterService; | ||
private final CounterTagRepository counterTagRepository; | ||
|
||
public CounterTagController(CounterTagService tagCounterService, CounterTagRepository counterTagRepository) { | ||
this.tagCounterService = tagCounterService; | ||
this.counterTagRepository = counterTagRepository; | ||
} | ||
|
||
@GetMapping("/") | ||
public String showForm(Model model) { | ||
model.addAttribute("urlsForm", new CounterTagForm()); | ||
|
||
List<CounterTagEntity> counterTagEntities = counterTagRepository.findAll(); | ||
model.addAttribute("counterTagEntities", counterTagEntities); | ||
|
||
return "index"; | ||
} | ||
|
||
@PostMapping("/countTags") | ||
public String countTags(@ModelAttribute CounterTagForm urlsForm, Model model) { | ||
List<String> urls = Arrays.asList(urlsForm.getUrls().split("\n")); | ||
List<CounterTagDTO> results = tagCounterService.countHtmlTags(urls); | ||
model.addAttribute("results", results); | ||
|
||
for (CounterTagDTO result : results) { | ||
List<CounterTagEntity> existingEntities = counterTagRepository.findByUrl(result.getUrl()); | ||
for (String tagName : result.getTagCountMap().keySet()) { | ||
int tagCount = result.getTagCountMap().get(tagName); | ||
if (!existingEntities.isEmpty()) { | ||
CounterTagEntity existingEntity = existingEntities.get(0); | ||
existingEntity.setTagCount(tagCount); | ||
counterTagRepository.save(existingEntity); | ||
} else { | ||
CounterTagEntity tagEntity = new CounterTagEntity(result.getUrl(), tagName, tagCount); | ||
counterTagRepository.save(tagEntity); | ||
} | ||
} | ||
} | ||
|
||
// Atualizar a lista de informações do banco de dados após salvar os dados | ||
List<CounterTagEntity> counterTagEntities = counterTagRepository.findAll(); | ||
model.addAttribute("counterTagEntities", counterTagEntities); | ||
|
||
return "results"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/main/java/com/testes/tags/TagForm.java → .../java/com/testes/tags/CounterTagForm.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.