Backend
package com.example.ecommerce.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
//WebConfiguration
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/api/**") // Apply CORS configuration to all endpoints under /api
.allowedOrigins("http://localhost:5173") // Allow requests from this origin
.allowedMethods("GET", "POST", "PUT", "DELETE"); // Allow these methods
}
}
Controllers
package com.example.ecommerce.controllers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.ecommerce.entities.Producto;
import com.example.ecommerce.services.ProductoService;
import java.util.List;
//Los controladores manejan las solicitudes HTTP y llaman a los servicios correspondientes.
@RestController
@RequestMapping("/api/productos")
public class ProductoController {
@Autowired
private ProductoService productoService;
@GetMapping
public List<Producto> obtenerTodos(){
return productoService.obtenerTodos();
}
@PostMapping
public Producto crearProducto(@RequestBody Producto producto){
return productoService.crearProducto(producto);
}
}
Entities
package com.example.ecommerce.entities;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
@Entity
public class Producto {
//ACA SE DEFINEN LAS ENTIDADES JPA PARA LA CLASE PRODUCTO
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String nombre;
private Double precio;
public Producto(){
}
public Producto(String nombre, Double precio){
this.nombre = nombre;
this.precio = precio;
}
public Long getId(){
return id;
}
public void setId(Long id){
this.id = id;
}
public String getNombre(){
return nombre;
}
public void setNombre(String nombre){
this.nombre = nombre;
}
public Double getPrecio(){
return precio;
}
public void setPrecio(Double precio){
this.precio = precio;
}
}
Repository
package com.example.ecommerce.repositories;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.example.ecommerce.entities.Producto;
@Repository
public interface ProductoRepository extends JpaRepository<Producto, Long>{
}
Repository by Lucas
package com.example.uade.tpo.ecommerce.repositories;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import com.example.uade.tpo.ecommerce.entities.Artwork;
@Repository
public interface ArtworkRepository extends JpaRepository<Artwork, Long> {
@Query(value = "select a from Artwork a where a.artist = ?1")
public List<Artwork> findAllByUserId(Long id);
}
Services
package com.example.ecommerce.services;
import java.util.List;
import java.util.Optional;
import com.example.ecommerce.entities.Ruedas;
public interface RuedasService {
List<Ruedas> findAll();
Optional<Ruedas> findById(Long id);
Ruedas save(Ruedas ruedas);
void deleteById(Long id);
}
Services Impl
@Service
public class RuedasServiceImpl implements RuedasService {
@Autowired
private RuedasRepository ruedasRepository;
@Override
public List<Ruedas> findAll(){
return ruedasRepository.findAll();
}
@Override
public Optional<Ruedas> findById(Long id){
return ruedasRepository.findById(id);
}
@Override
public Ruedas save(Ruedas ruedas){
return ruedasRepository.save(ruedas);
}
@Override
public void deleteById(Long id){
ruedasRepository.deleteById(id);
}
}
application properties
server.port=8080
spring.datasource.url=jdbc:mysql://localhost:3306/practicafinal
spring.datasource.username=root
spring.datasource.password=Facundo2554
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
im trying to aprove an examn in the college`
WebCrack is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.