From 9868233f18060f13d9ab9775c5f539d6afa69d4d Mon Sep 17 00:00:00 2001 From: "github-classroom[bot]" <66690702+github-classroom[bot]@users.noreply.github.com> Date: Thu, 14 Nov 2024 11:32:55 +0000 Subject: [PATCH 1/4] Setting up GitHub Classroom Feedback From 16b6dfdaf787673009c4ae76f1fc50627d6d03b3 Mon Sep 17 00:00:00 2001 From: "github-classroom[bot]" <66690702+github-classroom[bot]@users.noreply.github.com> Date: Thu, 14 Nov 2024 11:32:57 +0000 Subject: [PATCH 2/4] add deadline --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 3a2112b..c68a9dc 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ +[![Review Assignment Due Date](https://classroom.github.com/assets/deadline-readme-button-22041afd0340ce965d47ae6ef1cefeee28c7c493a6346c4f15d667ab976d596c.svg)](https://classroom.github.com/a/um2scOsx) > **Note** > Si alguien quiere usar docker, docker-compose, adelante. > Pero ahora mismo no es nuestro objetivo. From 0051ed2c1fd0a3ae2b54f31a55077be0263e4afd Mon Sep 17 00:00:00 2001 From: marta Date: Thu, 14 Nov 2024 15:11:23 +0100 Subject: [PATCH 3/4] proyecto springboot,entidades,repos,servicios y controladores --- .idea/.gitignore | 8 ++ .mvn/wrapper/maven-wrapper.properties | 19 ++++ pom.xml | 96 ++++++++++++++++ .../daw2a/gestionbiblioteca/ApiCervezas.java | 15 +++ .../config/SecurityConfig.java | 14 +++ .../gestionbiblioteca/config/WebConfig.java | 26 +++++ .../controllers/BeerController.java | 53 +++++++++ .../controllers/BreweryController.java | 64 +++++++++++ .../controllers/CategoryController.java | 60 ++++++++++ .../controllers/StyleController.java | 56 +++++++++ .../gestionbiblioteca/entities/Beer.java | 57 ++++++++++ .../gestionbiblioteca/entities/Brewery.java | 42 +++++++ .../gestionbiblioteca/entities/Category.java | 31 +++++ .../gestionbiblioteca/entities/Style.java | 35 ++++++ .../repositories/BeerRepository.java | 13 +++ .../repositories/BreweryRepository.java | 15 +++ .../repositories/CategoryRepository.java | 12 ++ .../repositories/StyleRepository.java | 11 ++ .../services/BeerService.java | 40 +++++++ .../services/BreweryService.java | 64 +++++++++++ .../services/CategoryService.java | 48 ++++++++ .../services/StyleService.java | 49 ++++++++ src/main/resources/application.properties | 16 +++ src/main/resources/data.sql | 21 ++++ .../GestionBibliotecaApplicationTests.java | 13 +++ .../controllers/AutorControllerTest.java | 32 ++++++ .../controllers/LibroControllerTest.java | 107 ++++++++++++++++++ .../controllers/PrestamoControllerTest.java | 37 ++++++ .../controllers/UsuarioControllerTest.java | 37 ++++++ 29 files changed, 1091 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .mvn/wrapper/maven-wrapper.properties create mode 100644 pom.xml create mode 100644 src/main/java/daw2a/gestionbiblioteca/ApiCervezas.java create mode 100644 src/main/java/daw2a/gestionbiblioteca/config/SecurityConfig.java create mode 100644 src/main/java/daw2a/gestionbiblioteca/config/WebConfig.java create mode 100644 src/main/java/daw2a/gestionbiblioteca/controllers/BeerController.java create mode 100644 src/main/java/daw2a/gestionbiblioteca/controllers/BreweryController.java create mode 100644 src/main/java/daw2a/gestionbiblioteca/controllers/CategoryController.java create mode 100644 src/main/java/daw2a/gestionbiblioteca/controllers/StyleController.java create mode 100644 src/main/java/daw2a/gestionbiblioteca/entities/Beer.java create mode 100644 src/main/java/daw2a/gestionbiblioteca/entities/Brewery.java create mode 100644 src/main/java/daw2a/gestionbiblioteca/entities/Category.java create mode 100644 src/main/java/daw2a/gestionbiblioteca/entities/Style.java create mode 100644 src/main/java/daw2a/gestionbiblioteca/repositories/BeerRepository.java create mode 100644 src/main/java/daw2a/gestionbiblioteca/repositories/BreweryRepository.java create mode 100644 src/main/java/daw2a/gestionbiblioteca/repositories/CategoryRepository.java create mode 100644 src/main/java/daw2a/gestionbiblioteca/repositories/StyleRepository.java create mode 100644 src/main/java/daw2a/gestionbiblioteca/services/BeerService.java create mode 100644 src/main/java/daw2a/gestionbiblioteca/services/BreweryService.java create mode 100644 src/main/java/daw2a/gestionbiblioteca/services/CategoryService.java create mode 100644 src/main/java/daw2a/gestionbiblioteca/services/StyleService.java create mode 100644 src/main/resources/application.properties create mode 100644 src/main/resources/data.sql create mode 100644 src/test/java/daw2a/gestionbiblioteca/GestionBibliotecaApplicationTests.java create mode 100644 src/test/java/daw2a/gestionbiblioteca/controllers/AutorControllerTest.java create mode 100644 src/test/java/daw2a/gestionbiblioteca/controllers/LibroControllerTest.java create mode 100644 src/test/java/daw2a/gestionbiblioteca/controllers/PrestamoControllerTest.java create mode 100644 src/test/java/daw2a/gestionbiblioteca/controllers/UsuarioControllerTest.java diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.mvn/wrapper/maven-wrapper.properties b/.mvn/wrapper/maven-wrapper.properties new file mode 100644 index 0000000..d58dfb7 --- /dev/null +++ b/.mvn/wrapper/maven-wrapper.properties @@ -0,0 +1,19 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +wrapperVersion=3.3.2 +distributionType=only-script +distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.9/apache-maven-3.9.9-bin.zip diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..d6aedfa --- /dev/null +++ b/pom.xml @@ -0,0 +1,96 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 3.3.4 + + + daw2a + ApiCervezas + 0.0.1-SNAPSHOT + ApiCervezas + ApiCervezas + + + + + + + + + + + + + + + 17 + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-devtools + runtime + true + + + com.h2database + h2 + runtime + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework.boot + spring-boot + 3.3.4 + + + jakarta.validation + jakarta.validation-api + + + org.springframework.security + spring-security-crypto + 6.2.0 + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + org.projectlombok + lombok + + + + + + + + diff --git a/src/main/java/daw2a/gestionbiblioteca/ApiCervezas.java b/src/main/java/daw2a/gestionbiblioteca/ApiCervezas.java new file mode 100644 index 0000000..caa18be --- /dev/null +++ b/src/main/java/daw2a/gestionbiblioteca/ApiCervezas.java @@ -0,0 +1,15 @@ +package daw2a.gestionbiblioteca; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.data.web.config.EnableSpringDataWebSupport; + +@SpringBootApplication +@EnableSpringDataWebSupport(pageSerializationMode = EnableSpringDataWebSupport.PageSerializationMode.VIA_DTO) +public class ApiCervezas { + + public static void main(String[] args) { + SpringApplication.run(ApiCervezas.class, args); + } + +} diff --git a/src/main/java/daw2a/gestionbiblioteca/config/SecurityConfig.java b/src/main/java/daw2a/gestionbiblioteca/config/SecurityConfig.java new file mode 100644 index 0000000..5242512 --- /dev/null +++ b/src/main/java/daw2a/gestionbiblioteca/config/SecurityConfig.java @@ -0,0 +1,14 @@ +package daw2a.gestionbiblioteca.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; +import org.springframework.security.crypto.password.PasswordEncoder; + +@Configuration +public class SecurityConfig { + @Bean + public PasswordEncoder passwordEncoder() { + return new BCryptPasswordEncoder(); + } +} diff --git a/src/main/java/daw2a/gestionbiblioteca/config/WebConfig.java b/src/main/java/daw2a/gestionbiblioteca/config/WebConfig.java new file mode 100644 index 0000000..c4c9093 --- /dev/null +++ b/src/main/java/daw2a/gestionbiblioteca/config/WebConfig.java @@ -0,0 +1,26 @@ +package daw2a.gestionbiblioteca.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.cors.CorsConfiguration; +import org.springframework.web.cors.UrlBasedCorsConfigurationSource; +import org.springframework.web.filter.CorsFilter; + +@Configuration +public class WebConfig { + + @Bean + public CorsFilter corsFilter() { + CorsConfiguration config = new CorsConfiguration(); + config.addAllowedOrigin("http://localhost:3000"); // Dominio permitido (cambiar según el dominio de frontend) + config.addAllowedHeader("*"); // Permite todos los headers + config.addAllowedMethod("*"); // Permite todos los métodos (GET, POST, PUT, DELETE, etc.) + + UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); + source.registerCorsConfiguration("/**", config); + + return new CorsFilter(source); + } + + +} \ No newline at end of file diff --git a/src/main/java/daw2a/gestionbiblioteca/controllers/BeerController.java b/src/main/java/daw2a/gestionbiblioteca/controllers/BeerController.java new file mode 100644 index 0000000..dced8c2 --- /dev/null +++ b/src/main/java/daw2a/gestionbiblioteca/controllers/BeerController.java @@ -0,0 +1,53 @@ +package daw2a.gestionbiblioteca.controllers; + +import daw2a.gestionbiblioteca.entities.Beer; +import daw2a.gestionbiblioteca.services.BeerService; +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequestMapping("/api/beers") +@RequiredArgsConstructor +public class BeerController { + private final BeerService beerService; + + @GetMapping + public List getAllBeers() { + return beerService.getAllBeers(); + } + + @GetMapping("/{id}") + public Beer getBeerById(@PathVariable Long id) { + return beerService.getBeerById(id); + } + + @PostMapping + public Beer createBeer(@RequestBody Beer beer) { + return beerService.saveBeer(beer); + } + + @PutMapping("/{id}") + public Beer updateBeer(@PathVariable Long id, @RequestBody Beer beer) { + beer.setId(id); + return beerService.saveBeer(beer); + } + + @DeleteMapping("/{id}") + public void deleteBeer(@PathVariable Long id) { + beerService.deleteBeer(id); + } + + //segun volumen de alcohol + @GetMapping("/abv") + public List getBeersByAbv(@RequestParam Float minAbv) { + return beerService.findBeersWithAbvGreaterThan(minAbv); + } + + //segun nombre + @GetMapping("/search") + public List searchBeersByName(@RequestParam String name) { + return beerService.findBeersByNameContaining(name); + } +} \ No newline at end of file diff --git a/src/main/java/daw2a/gestionbiblioteca/controllers/BreweryController.java b/src/main/java/daw2a/gestionbiblioteca/controllers/BreweryController.java new file mode 100644 index 0000000..f903798 --- /dev/null +++ b/src/main/java/daw2a/gestionbiblioteca/controllers/BreweryController.java @@ -0,0 +1,64 @@ +package daw2a.gestionbiblioteca.controllers; + +import daw2a.gestionbiblioteca.entities.Brewery; +import daw2a.gestionbiblioteca.services.BreweryService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequestMapping("/api/breweries") +public class BreweryController { + + private final BreweryService breweryService; + + @Autowired + public BreweryController(BreweryService breweryService) { + this.breweryService = breweryService; + } + + // CRUD endpoints + @GetMapping + public List getAllBreweries() { + return breweryService.getAllBreweries(); + } + + @GetMapping("/{id}") + public ResponseEntity getBreweryById(@PathVariable Long id) { + Brewery brewery = breweryService.getBreweryById(id); + return ResponseEntity.ok(brewery); + } + + @PostMapping + public ResponseEntity createBrewery(@RequestBody Brewery brewery) { + Brewery newBrewery = breweryService.createBrewery(brewery); + return ResponseEntity.ok(newBrewery); + } + + @PutMapping("/{id}") + public ResponseEntity updateBrewery(@PathVariable Long id, @RequestBody Brewery breweryDetails) { + Brewery updatedBrewery = breweryService.updateBrewery(id, breweryDetails); + return ResponseEntity.ok(updatedBrewery); + } + + @DeleteMapping("/{id}") + public ResponseEntity deleteBrewery(@PathVariable Long id) { + breweryService.deleteBrewery(id); + return ResponseEntity.ok().build(); + } + + // por nombre y por ciudad + @GetMapping("/search") + public List searchBreweries(@RequestParam(required = false) String name, + @RequestParam(required = false) String city) { + if (name != null && !name.isEmpty()) { + return breweryService.findBreweriesByName(name); + } else if (city != null && !city.isEmpty()) { + return breweryService.findBreweriesByCity(city); + } else { + return breweryService.getAllBreweries(); + } + } +} diff --git a/src/main/java/daw2a/gestionbiblioteca/controllers/CategoryController.java b/src/main/java/daw2a/gestionbiblioteca/controllers/CategoryController.java new file mode 100644 index 0000000..2237bcb --- /dev/null +++ b/src/main/java/daw2a/gestionbiblioteca/controllers/CategoryController.java @@ -0,0 +1,60 @@ +package daw2a.gestionbiblioteca.controllers; + +import daw2a.gestionbiblioteca.entities.Category; +import daw2a.gestionbiblioteca.services.CategoryService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; +import java.util.List; + +@RestController +@RequestMapping("/api/categories") +public class CategoryController { + + private final CategoryService categoryService; + + @Autowired + public CategoryController(CategoryService categoryService) { + this.categoryService = categoryService; + } + + // CRUD endpoints + @GetMapping + public List getAllCategories() { + return categoryService.getAllCategories(); + } + + @GetMapping("/{id}") + public ResponseEntity getCategoryById(@PathVariable Long id) { + Category category = categoryService.getCategoryById(id); + return ResponseEntity.ok(category); + } + + @PostMapping + public ResponseEntity createCategory(@RequestBody Category category) { + Category newCategory = categoryService.createCategory(category); + return ResponseEntity.ok(newCategory); + } + + @PutMapping("/{id}") + public ResponseEntity updateCategory(@PathVariable Long id, @RequestBody Category categoryDetails) { + Category updatedCategory = categoryService.updateCategory(id, categoryDetails); + return ResponseEntity.ok(updatedCategory); + } + + @DeleteMapping("/{id}") + public ResponseEntity deleteCategory(@PathVariable Long id) { + categoryService.deleteCategory(id); + return ResponseEntity.ok().build(); + } + + // por nombre o todar las categorias + @GetMapping("/search") + public ResponseEntity searchCategories(@RequestParam(required = false) String name) { + if (name != null && !name.isEmpty()) { + return ResponseEntity.ok(categoryService.findCategoriesByName(name)); + } else { + return ResponseEntity.ok(categoryService.getAllCategories()); + } + } +} diff --git a/src/main/java/daw2a/gestionbiblioteca/controllers/StyleController.java b/src/main/java/daw2a/gestionbiblioteca/controllers/StyleController.java new file mode 100644 index 0000000..050e91e --- /dev/null +++ b/src/main/java/daw2a/gestionbiblioteca/controllers/StyleController.java @@ -0,0 +1,56 @@ +package daw2a.gestionbiblioteca.controllers; +import daw2a.gestionbiblioteca.entities.Style; +import daw2a.gestionbiblioteca.services.StyleService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequestMapping("/api/styles") +public class StyleController { + + private final StyleService styleService; + + @Autowired + public StyleController(StyleService styleService) { + this.styleService = styleService; + } + + // CRUD endpoints + @GetMapping + public List