mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-16 12:45:48 +00:00
Umesh Tayade - Code changes
This commit is contained in:
parent
30aab0ae76
commit
d38d89fce2
7 changed files with 157 additions and 0 deletions
|
@ -0,0 +1,34 @@
|
|||
package org.springframework.samples.petclinic.controller;
|
||||
|
||||
import org.springframework.samples.petclinic.model.ExtendedPetType;
|
||||
import org.springframework.samples.petclinic.service.PetService;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/extended-pet-types")
|
||||
public class PetTypeController {
|
||||
|
||||
private final PetService service;
|
||||
|
||||
public PetTypeController(PetService service) {
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public Iterable<ExtendedPetType> getAll() {
|
||||
return service.findAll();
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public ExtendedPetType add(@RequestBody ExtendedPetType petType) {
|
||||
return service.save(petType);
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public Optional<ExtendedPetType> getById(@PathVariable Integer id) {
|
||||
return service.findById(id);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
package org.springframework.samples.petclinic.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
@Entity
|
||||
@Table(name = "pet_type")
|
||||
public class ExtendedPetType {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Integer id;
|
||||
|
||||
private String name;
|
||||
|
||||
private String temperament;
|
||||
|
||||
private Double length;
|
||||
|
||||
private Double weight;
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getTemperament() {
|
||||
return temperament;
|
||||
}
|
||||
|
||||
public void setTemperament(String temperament) {
|
||||
this.temperament = temperament;
|
||||
}
|
||||
|
||||
public Double getLength() {
|
||||
return length;
|
||||
}
|
||||
|
||||
public void setLength(Double length) {
|
||||
this.length = length;
|
||||
}
|
||||
|
||||
public Double getWeight() {
|
||||
return weight;
|
||||
}
|
||||
|
||||
public void setWeight(Double weight) {
|
||||
this.weight = weight;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package org.springframework.samples.petclinic.repository;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.samples.petclinic.model.ExtendedPetType;
|
||||
|
||||
public interface PetRepository extends CrudRepository<ExtendedPetType, Integer> {
|
||||
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
package org.springframework.samples.petclinic.service;
|
||||
|
||||
import org.springframework.samples.petclinic.model.ExtendedPetType;
|
||||
import org.springframework.samples.petclinic.repository.PetRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class PetService {
|
||||
|
||||
private final PetRepository repository;
|
||||
|
||||
public PetService(PetRepository repository) {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
public Iterable<ExtendedPetType> findAll() {
|
||||
return repository.findAll();
|
||||
}
|
||||
|
||||
public Optional<ExtendedPetType> findById(Integer id) {
|
||||
return repository.findById(id);
|
||||
}
|
||||
|
||||
public ExtendedPetType save(ExtendedPetType petType) {
|
||||
return repository.save(petType);
|
||||
}
|
||||
|
||||
public void deleteById(Integer id) {
|
||||
repository.deleteById(id);
|
||||
}
|
||||
|
||||
}
|
|
@ -23,3 +23,12 @@ logging.level.org.springframework=INFO
|
|||
|
||||
# Maximum time static resources should be cached
|
||||
spring.web.resources.cache.cachecontrol.max-age=12h
|
||||
|
||||
spring.flyway.enabled=true
|
||||
|
||||
spring.jpa.hibernate.ddl-auto=update
|
||||
spring.datasource.url=jdbc:h2:mem:testdb
|
||||
spring.datasource.driver-class-name=org.h2.Driver
|
||||
spring.datasource.username=sa
|
||||
spring.datasource.password=
|
||||
spring.h2.console.enabled=true
|
||||
|
|
|
@ -51,3 +51,6 @@ INSERT INTO visits VALUES (default, 7, '2013-01-01', 'rabies shot');
|
|||
INSERT INTO visits VALUES (default, 8, '2013-01-02', 'rabies shot');
|
||||
INSERT INTO visits VALUES (default, 8, '2013-01-03', 'neutered');
|
||||
INSERT INTO visits VALUES (default, 7, '2013-01-04', 'spayed');
|
||||
|
||||
INSERT INTO extended_pet_type (name, temperament, length, weight)
|
||||
VALUES ('Golden Retriever', 'cool', 30.0, 60.0);
|
||||
|
|
|
@ -62,3 +62,11 @@ CREATE TABLE visits (
|
|||
);
|
||||
ALTER TABLE visits ADD CONSTRAINT fk_visits_pets FOREIGN KEY (pet_id) REFERENCES pets (id);
|
||||
CREATE INDEX visits_pet_id ON visits (pet_id);
|
||||
|
||||
CREATE TABLE extended_pet_type (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
name VARCHAR(255),
|
||||
temperament VARCHAR(255),
|
||||
length DOUBLE,
|
||||
weight DOUBLE
|
||||
);
|
||||
|
|
Loading…
Reference in a new issue