-
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.
Adição de estrutura web e possibilidade de várias urls
- Loading branch information
Murilo Guizelin
committed
Aug 1, 2023
1 parent
ba769ba
commit 566bcd9
Showing
16 changed files
with
349 additions
and
148 deletions.
There are no files selected for viewing
27 changes: 0 additions & 27 deletions
27
src/main/java/com/testes/tags/Controladores/TagControlador.java
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,49 @@ | ||
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 CounterController { | ||
|
||
private final CounterService tagCounterService; | ||
private final CounterTagRepository counterTagRepository; | ||
|
||
public CounterController(CounterService tagCounterService, CounterTagRepository counterTagRepository) { | ||
this.tagCounterService = tagCounterService; | ||
this.counterTagRepository = counterTagRepository; | ||
} | ||
|
||
@GetMapping("/") | ||
public String showForm(Model model) { | ||
model.addAttribute("urlsForm", new TagForm()); | ||
return "index"; | ||
} | ||
|
||
@PostMapping("/countTags") | ||
public String countTags(@ModelAttribute TagForm urlsForm, Model model) { | ||
List<String> urls = Arrays.asList(urlsForm.getUrls().split("\n")); | ||
List<TagDTO> results = tagCounterService.countHtmlTags(urls); | ||
model.addAttribute("results", results); | ||
|
||
// Save the tag counts to the database | ||
for (TagDTO result : results) { | ||
for (String tagName : result.getTagCountMap().keySet()) { | ||
int tagCount = result.getTagCountMap().get(tagName); | ||
CounterTagEntity tagEntity = new CounterTagEntity(); | ||
tagEntity.setTagName(tagName); | ||
tagEntity.setTagCount(tagCount); | ||
counterTagRepository.save(tagEntity); | ||
} | ||
} | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.testes.tags; | ||
|
||
import org.jsoup.Jsoup; | ||
import org.jsoup.nodes.Document; | ||
import org.jsoup.nodes.Element; | ||
import org.jsoup.parser.Parser; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@Service | ||
public class CounterService { | ||
|
||
public List<TagDTO> countHtmlTags(List<String> urls) { | ||
List<TagDTO> urlTagInfoList = new ArrayList<>(); | ||
|
||
for (String url : urls) { | ||
try { | ||
Map<String, Integer> tagCountMap = new HashMap<>(); | ||
Document doc = Jsoup.connect(url).parser(Parser.htmlParser()).get(); | ||
|
||
// Obtém todas as tags do HTML e conta a ocorrência de cada uma | ||
for (Element element : doc.getAllElements()) { | ||
String tagName = element.tagName(); | ||
tagCountMap.put(tagName, tagCountMap.getOrDefault(tagName, 0) + 1); | ||
} | ||
|
||
urlTagInfoList.add(new TagDTO(url, tagCountMap)); | ||
} catch (IOException e) { | ||
// Em caso de erro ao fazer a requisição ou processar a URL, podemos adicionar uma entrada vazia | ||
urlTagInfoList.add(new TagDTO(url, new HashMap<>())); | ||
} | ||
} | ||
|
||
return urlTagInfoList; | ||
} | ||
} |
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,50 @@ | ||
package com.testes.tags; | ||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
|
||
@Entity | ||
public class CounterTagEntity { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
private String tagName; | ||
|
||
private int tagCount; | ||
|
||
public CounterTagEntity() { | ||
} | ||
|
||
public CounterTagEntity(String tagName, int tagCount) { | ||
this.tagName = tagName; | ||
this.tagCount = tagCount; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public String getTagName() { | ||
return tagName; | ||
} | ||
|
||
public void setTagName(String tagName) { | ||
this.tagName = tagName; | ||
} | ||
|
||
public int getTagCount() { | ||
return tagCount; | ||
} | ||
|
||
public void setTagCount(int tagCount) { | ||
this.tagCount = tagCount; | ||
} | ||
} |
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,6 @@ | ||
package com.testes.tags; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface CounterTagRepository extends JpaRepository<CounterTagEntity, String> { | ||
} |
This file was deleted.
Oops, something went wrong.
9 changes: 0 additions & 9 deletions
9
src/main/java/com/testes/tags/Repositorios/TagRepositorio.java
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
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,32 @@ | ||
package com.testes.tags; | ||
|
||
import java.util.Map; | ||
|
||
public class TagDTO { | ||
private String url; | ||
private Map<String, Integer> tagCountMap; | ||
|
||
public TagDTO() { | ||
} | ||
|
||
public TagDTO(String url, Map<String, Integer> tagCountMap) { | ||
this.url = url; | ||
this.tagCountMap = tagCountMap; | ||
} | ||
|
||
public String getUrl() { | ||
return url; | ||
} | ||
|
||
public void setUrl(String url) { | ||
this.url = url; | ||
} | ||
|
||
public Map<String, Integer> getTagCountMap() { | ||
return tagCountMap; | ||
} | ||
|
||
public void setTagCountMap(Map<String, Integer> tagCountMap) { | ||
this.tagCountMap = tagCountMap; | ||
} | ||
} |
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,13 @@ | ||
package com.testes.tags; | ||
|
||
public class TagForm { | ||
private String urls; | ||
|
||
public String getUrls() { | ||
return urls; | ||
} | ||
|
||
public void setUrls(String urls) { | ||
this.urls = urls; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,3 +1,6 @@ | ||
spring.datasource.url=jdbc:postgresql://localhost:5432/tag | ||
spring.datasource.username=postgres | ||
spring.datasource.password=123 | ||
|
||
spring.jpa.hibernate.ddl-auto=create | ||
|
Oops, something went wrong.