From 0b70427eb7c633b8cd2651cc716e209ee5019303 Mon Sep 17 00:00:00 2001 From: Luan de Luna Date: Mon, 27 Apr 2020 11:56:37 -0300 Subject: [PATCH 1/6] Criando projeto --- .gitignore | 6 +++ selecaojava/.gitignore | 31 ++++++++++++ selecaojava/pom.xml | 49 +++++++++++++++++++ .../selecaojava/SelecaojavaApplication.java | 13 +++++ .../src/main/resources/application.properties | 1 + .../SelecaojavaApplicationTests.java | 13 +++++ 6 files changed, 113 insertions(+) create mode 100644 .gitignore create mode 100644 selecaojava/.gitignore create mode 100644 selecaojava/pom.xml create mode 100644 selecaojava/src/main/java/com/indra/selecao/selecaojava/SelecaojavaApplication.java create mode 100644 selecaojava/src/main/resources/application.properties create mode 100644 selecaojava/src/test/java/com/indra/selecao/selecaojava/SelecaojavaApplicationTests.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..aeef487 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ + +selecaojava/.mvn + +selecaojava/mvnw.cmd + +selecaojava/mvnw diff --git a/selecaojava/.gitignore b/selecaojava/.gitignore new file mode 100644 index 0000000..a2a3040 --- /dev/null +++ b/selecaojava/.gitignore @@ -0,0 +1,31 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/** +!**/src/test/** + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ + +### VS Code ### +.vscode/ diff --git a/selecaojava/pom.xml b/selecaojava/pom.xml new file mode 100644 index 0000000..847224e --- /dev/null +++ b/selecaojava/pom.xml @@ -0,0 +1,49 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.2.6.RELEASE + + + com.indra.selecao + selecaojava + 0.0.1-SNAPSHOT + selecaojava + Teste de seleção para vaga na Indra + + + 1.8 + + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.vintage + junit-vintage-engine + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/selecaojava/src/main/java/com/indra/selecao/selecaojava/SelecaojavaApplication.java b/selecaojava/src/main/java/com/indra/selecao/selecaojava/SelecaojavaApplication.java new file mode 100644 index 0000000..6d98fc9 --- /dev/null +++ b/selecaojava/src/main/java/com/indra/selecao/selecaojava/SelecaojavaApplication.java @@ -0,0 +1,13 @@ +package com.indra.selecao.selecaojava; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SelecaojavaApplication { + + public static void main(String[] args) { + SpringApplication.run(SelecaojavaApplication.class, args); + } + +} diff --git a/selecaojava/src/main/resources/application.properties b/selecaojava/src/main/resources/application.properties new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/selecaojava/src/main/resources/application.properties @@ -0,0 +1 @@ + diff --git a/selecaojava/src/test/java/com/indra/selecao/selecaojava/SelecaojavaApplicationTests.java b/selecaojava/src/test/java/com/indra/selecao/selecaojava/SelecaojavaApplicationTests.java new file mode 100644 index 0000000..d7518f1 --- /dev/null +++ b/selecaojava/src/test/java/com/indra/selecao/selecaojava/SelecaojavaApplicationTests.java @@ -0,0 +1,13 @@ +package com.indra.selecao.selecaojava; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class SelecaojavaApplicationTests { + + @Test + void contextLoads() { + } + +} From 096217924e2311d21dd3a6a49ca910cd9d03136a Mon Sep 17 00:00:00 2001 From: Luan de Luna Date: Tue, 28 Apr 2020 19:13:41 -0300 Subject: [PATCH 2/6] Cruds completos --- selecaojava/pom.xml | 30 ++- .../selecao/selecaojava/SwaggerConfig.java | 44 ++++ .../controller/CityController.java | 146 +++++++++++ .../controller/LabelController.java | 148 +++++++++++ .../controller/MerchantController.java | 149 +++++++++++ .../controller/PriceHistoryController.java | 234 ++++++++++++++++++ .../controller/ProductController.java | 147 +++++++++++ .../controller/RegionController.java | 146 +++++++++++ .../controller/StateController.java | 147 +++++++++++ .../controller/UserController.java | 144 +++++++++++ .../selecao/selecaojava/entity/City.java | 71 ++++++ .../selecao/selecaojava/entity/Label.java | 41 +++ .../selecao/selecaojava/entity/Merchant.java | 56 +++++ .../selecaojava/entity/PriceHistory.java | 173 +++++++++++++ .../selecao/selecaojava/entity/Product.java | 53 ++++ .../selecao/selecaojava/entity/Region.java | 54 ++++ .../selecao/selecaojava/entity/State.java | 71 ++++++ .../selecao/selecaojava/entity/User.java | 64 +++++ .../repository/CityRepository.java | 9 + .../repository/LabelRepository.java | 9 + .../repository/MerchantRepository.java | 9 + .../repository/PriceHistoryRepository.java | 9 + .../repository/ProductRepository.java | 9 + .../repository/RegionRepository.java | 9 + .../repository/StateRepository.java | 9 + .../repository/UserRepository.java | 9 + .../src/main/resources/application.properties | 13 + 27 files changed, 2001 insertions(+), 2 deletions(-) create mode 100644 selecaojava/src/main/java/com/indra/selecao/selecaojava/SwaggerConfig.java create mode 100644 selecaojava/src/main/java/com/indra/selecao/selecaojava/controller/CityController.java create mode 100644 selecaojava/src/main/java/com/indra/selecao/selecaojava/controller/LabelController.java create mode 100644 selecaojava/src/main/java/com/indra/selecao/selecaojava/controller/MerchantController.java create mode 100644 selecaojava/src/main/java/com/indra/selecao/selecaojava/controller/PriceHistoryController.java create mode 100644 selecaojava/src/main/java/com/indra/selecao/selecaojava/controller/ProductController.java create mode 100644 selecaojava/src/main/java/com/indra/selecao/selecaojava/controller/RegionController.java create mode 100644 selecaojava/src/main/java/com/indra/selecao/selecaojava/controller/StateController.java create mode 100644 selecaojava/src/main/java/com/indra/selecao/selecaojava/controller/UserController.java create mode 100644 selecaojava/src/main/java/com/indra/selecao/selecaojava/entity/City.java create mode 100644 selecaojava/src/main/java/com/indra/selecao/selecaojava/entity/Label.java create mode 100644 selecaojava/src/main/java/com/indra/selecao/selecaojava/entity/Merchant.java create mode 100644 selecaojava/src/main/java/com/indra/selecao/selecaojava/entity/PriceHistory.java create mode 100644 selecaojava/src/main/java/com/indra/selecao/selecaojava/entity/Product.java create mode 100644 selecaojava/src/main/java/com/indra/selecao/selecaojava/entity/Region.java create mode 100644 selecaojava/src/main/java/com/indra/selecao/selecaojava/entity/State.java create mode 100644 selecaojava/src/main/java/com/indra/selecao/selecaojava/entity/User.java create mode 100644 selecaojava/src/main/java/com/indra/selecao/selecaojava/repository/CityRepository.java create mode 100644 selecaojava/src/main/java/com/indra/selecao/selecaojava/repository/LabelRepository.java create mode 100644 selecaojava/src/main/java/com/indra/selecao/selecaojava/repository/MerchantRepository.java create mode 100644 selecaojava/src/main/java/com/indra/selecao/selecaojava/repository/PriceHistoryRepository.java create mode 100644 selecaojava/src/main/java/com/indra/selecao/selecaojava/repository/ProductRepository.java create mode 100644 selecaojava/src/main/java/com/indra/selecao/selecaojava/repository/RegionRepository.java create mode 100644 selecaojava/src/main/java/com/indra/selecao/selecaojava/repository/StateRepository.java create mode 100644 selecaojava/src/main/java/com/indra/selecao/selecaojava/repository/UserRepository.java diff --git a/selecaojava/pom.xml b/selecaojava/pom.xml index 847224e..a6dffb0 100644 --- a/selecaojava/pom.xml +++ b/selecaojava/pom.xml @@ -1,12 +1,13 @@ - 4.0.0 org.springframework.boot spring-boot-starter-parent 2.2.6.RELEASE - + com.indra.selecao selecaojava @@ -35,6 +36,31 @@ + + org.springframework.boot + spring-boot-starter-web + + + + io.springfox + springfox-swagger2 + 2.9.2 + + + io.springfox + springfox-swagger-ui + 2.9.2 + compile + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + com.h2database + h2 + diff --git a/selecaojava/src/main/java/com/indra/selecao/selecaojava/SwaggerConfig.java b/selecaojava/src/main/java/com/indra/selecao/selecaojava/SwaggerConfig.java new file mode 100644 index 0000000..ab4d659 --- /dev/null +++ b/selecaojava/src/main/java/com/indra/selecao/selecaojava/SwaggerConfig.java @@ -0,0 +1,44 @@ +package com.indra.selecao.selecaojava; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; +import springfox.documentation.builders.ApiInfoBuilder; +import springfox.documentation.builders.RequestHandlerSelectors; +import springfox.documentation.service.ApiInfo; +import springfox.documentation.spi.DocumentationType; +import springfox.documentation.spring.web.plugins.Docket; +import springfox.documentation.swagger2.annotations.EnableSwagger2; + +@Configuration +@EnableSwagger2 +public class SwaggerConfig extends WebMvcConfigurationSupport { + + @Bean + public Docket productApi() { + return new Docket(DocumentationType.SWAGGER_2) + .select() + .apis(RequestHandlerSelectors.basePackage("com.indra.selecao.selecaojava")) + .build() + .apiInfo(metaData()); + } + + private ApiInfo metaData() { + return new ApiInfoBuilder() + .title("Spring Boot REST API") + .description("\"Spring Boot REST API\"") + .version("1.0.0") + .license("Apache License Version 2.0") + .licenseUrl("https://www.apache.org/licenses/LICENSE-2.0\"") + .build(); + } + + @Override + protected void addResourceHandlers(ResourceHandlerRegistry registry) { + registry.addResourceHandler("swagger-ui.html") + .addResourceLocations("classpath:/META-INF/resources/"); + registry.addResourceHandler("/webjars/**") + .addResourceLocations("classpath:/META-INF/resources/webjars/"); + } +} \ No newline at end of file diff --git a/selecaojava/src/main/java/com/indra/selecao/selecaojava/controller/CityController.java b/selecaojava/src/main/java/com/indra/selecao/selecaojava/controller/CityController.java new file mode 100644 index 0000000..729b99c --- /dev/null +++ b/selecaojava/src/main/java/com/indra/selecao/selecaojava/controller/CityController.java @@ -0,0 +1,146 @@ +package com.indra.selecao.selecaojava.controller; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +import javax.validation.Valid; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +import com.indra.selecao.selecaojava.entity.City; +import com.indra.selecao.selecaojava.repository.CityRepository; + +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; + +@RestController +@Api(value = "City") +@RequestMapping(value = "/city") +public class CityController { + @Autowired + private CityRepository cityRepository; + + Logger logger = LoggerFactory.getLogger(CityController.class); + + + + @RequestMapping(method = RequestMethod.GET) + @ApiOperation(value = "Retorna todas as cidades") + public List getAll(){ + List city = new ArrayList(); + + try { + city = cityRepository.findAll(); + + } catch (Exception e) { + logger.error("Erro ao se conectar com a base de dados!"); + } + + return city; + + } + + @RequestMapping(value = "/{id}", method = RequestMethod.GET) + @ApiOperation(value = "Retorna a cidade referente ao id passado como parâmetro") + public ResponseEntity getById(@PathVariable(value = "id") Long cityId) { + Optional city; + + try { + city = cityRepository.findById(cityId); + } catch (Exception e) { + logger.error("Erro ao se conectar com a base de dados!"); + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } + + + if (city.isPresent()) { + return new ResponseEntity(city.get(), HttpStatus.OK); + } + + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + + @RequestMapping(method = RequestMethod.POST) + @ApiOperation(value = "Insere um nova cidade") + public ResponseEntity post(@Valid @RequestBody City city) { + + if (city != null) { + try { + cityRepository.save(city); + return new ResponseEntity(HttpStatus.OK); + + } catch (Exception e) { + logger.error("Erro ao tentar salvar cidade!"); + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } + + } + + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + } + + @RequestMapping(value = "/{id}", method = RequestMethod.PUT) + @ApiOperation(value = "Altera os dados da cidade") + public ResponseEntity put(@PathVariable(value = "id") Long CityId, @Valid @RequestBody City newCity) { + Optional storedCity; + + try { + storedCity = cityRepository.findById(CityId); + } catch (Exception e) { + logger.error("Erro ao tentar buscar a cidade id(" + CityId + ")!"); + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } + + if (storedCity.isPresent()) { + newCity.setId(storedCity.get().getId()); + + try { + newCity = cityRepository.save(newCity); + return new ResponseEntity(newCity, HttpStatus.OK); + + } catch (Exception e) { + logger.error("Erro ao tentar alterar a cidade id(" + CityId + ")!"); + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } + } + + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + + @RequestMapping(value = "/{id}", method = RequestMethod.DELETE) + @ApiOperation(value = "Remove um cidade do sistema") + public ResponseEntity delete(@PathVariable(value = "id") Long CityId) { + Optional City; + + try { + City = cityRepository.findById(CityId); + + } catch (Exception e) { + logger.error("Erro ao tentar buscar a cidade id(" + CityId + ")!"); + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } + + if (City.isPresent()) { + try { + cityRepository.delete(City.get()); + return new ResponseEntity<>(HttpStatus.OK); + + } catch (Exception e) { + logger.error("Erro ao tentar deletar a cidade id(" + CityId + ")!"); + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } + } + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + + } +} diff --git a/selecaojava/src/main/java/com/indra/selecao/selecaojava/controller/LabelController.java b/selecaojava/src/main/java/com/indra/selecao/selecaojava/controller/LabelController.java new file mode 100644 index 0000000..62c0caf --- /dev/null +++ b/selecaojava/src/main/java/com/indra/selecao/selecaojava/controller/LabelController.java @@ -0,0 +1,148 @@ +package com.indra.selecao.selecaojava.controller; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +import javax.validation.Valid; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +import com.indra.selecao.selecaojava.entity.Label; +import com.indra.selecao.selecaojava.repository.LabelRepository; + +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; + +@RestController +@Api(value = "Label") +@RequestMapping(value = "/label") +public class LabelController { + + @Autowired + private LabelRepository labelRepository; + + Logger logger = LoggerFactory.getLogger(LabelController.class); + + + + @RequestMapping(method = RequestMethod.GET) + @ApiOperation(value = "Retorna todas as Labels") + public List