Springs Database Manager and H2 works
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
package com.voyage.workspace_api;
|
||||
package com.voyage.workspace;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.voyage.workspace;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class DebugController {
|
||||
@GetMapping("/ping")
|
||||
public String ping() {
|
||||
return "pong";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.voyage.workspace.products;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import java.time.Instant;
|
||||
|
||||
@Entity
|
||||
@Table(name = "products")
|
||||
public class Product {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@Column(nullable=false)
|
||||
private String name;
|
||||
|
||||
private String category;
|
||||
|
||||
@Column(nullable=false)
|
||||
private String status = "ACTIVE";
|
||||
|
||||
@Column(nullable=false, updatable=false)
|
||||
private Instant createdAt = Instant.now();
|
||||
|
||||
public Product() {}
|
||||
|
||||
public Product(String name, String category) {
|
||||
this.name = name;
|
||||
this.category = category;
|
||||
}
|
||||
|
||||
public Long getId() { return id; }
|
||||
public String getName() { return name; }
|
||||
public String getCategory() { return category; }
|
||||
public String getStatus() { return status; }
|
||||
public Instant getCreatedAt() { return createdAt; }
|
||||
|
||||
public void setName(String name) { this.name = name; }
|
||||
public void setCategory(String category) { this.category = category; }
|
||||
public void setStatus(String status) { this.status = status; }
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.voyage.workspace.products;
|
||||
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/products")
|
||||
public class ProductController {
|
||||
|
||||
private final ProductRepository repo;
|
||||
|
||||
public ProductController(ProductRepository repo) {
|
||||
this.repo = repo;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public List<Product> list() {
|
||||
return repo.findAll();
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public Product create(@RequestBody ProductCreateRequest req) {
|
||||
Product p = new Product(req.name(), req.category());
|
||||
return repo.save(p);
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public ResponseEntity<Product> get(@PathVariable Long id) {
|
||||
return repo.findById(id).map(ResponseEntity::ok)
|
||||
.orElse(ResponseEntity.notFound().build());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
package com.voyage.workspace.products;
|
||||
|
||||
public record ProductCreateRequest(String name, String category) {}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.voyage.workspace.products;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface ProductRepository extends JpaRepository<Product, Long> {}
|
||||
Reference in New Issue
Block a user