This commit is contained in:
koushaljain 2025-05-14 09:21:20 +01:00 committed by GitHub
commit 862fd22fbf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 166 additions and 0 deletions

View file

@ -163,3 +163,18 @@ For additional details, please refer to the blog post [Hello DCO, Goodbye CLA: S
## License
The Spring PetClinic sample application is released under version 2.0 of the [Apache License](https://www.apache.org/licenses/LICENSE-2.0).
## Assignment Enhancements
- Added endpoints to GET and POST pet attributes (Temperament, Length, Weight).
- Created REST API endpoints to **GET** and **POST** pet type data
- Developed model, service, repository, and controller layers
- Included SQL schema for new table
## Endpoints
- `POST /api/pettypes` — Create a new pet with attributes
- `GET /api/pettypes` — Retrieve all pet types with their attributes
Access the endpoints via Postman or curl at:
http://localhost:8080/api/pettypes

View file

@ -0,0 +1,31 @@
package org.springframework.samples.petclinic.controller;
import org.springframework.http.ResponseEntity;
import org.springframework.samples.petclinic.model.PetTypes;
import org.springframework.samples.petclinic.service.PetTypeService;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/api/pettypes")
public class PetTypeController {
private final PetTypeService petTypeService;
public PetTypeController(PetTypeService petTypeService) {
this.petTypeService = petTypeService;
}
@PostMapping
public ResponseEntity<PetTypes> createPetType(@RequestBody PetTypes petType) {
PetTypes savedPetType = petTypeService.save(petType);
return ResponseEntity.ok(savedPetType);
}
@GetMapping
public ResponseEntity<List<PetTypes>> getAllPetTypes() {
List<PetTypes> petTypes = petTypeService.findAll();
return ResponseEntity.ok(petTypes);
}
}

View file

@ -0,0 +1,61 @@
package org.springframework.samples.petclinic.model;
import jakarta.persistence.*;
@Entity
@Table(name = "pet_types")
public class PetTypes {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false)
private String name;
private String temperament;
private Double length;
private Double weight;
public Long getId() {
return id;
}
public void setId(Long 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;
}
}

View file

@ -0,0 +1,7 @@
package org.springframework.samples.petclinic.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.samples.petclinic.model.PetTypes;
public interface PetTypeRepository extends JpaRepository<PetTypes, Long> {
}

View file

@ -0,0 +1,10 @@
package org.springframework.samples.petclinic.service;
import org.springframework.samples.petclinic.model.PetTypes;
import java.util.List;
public interface PetTypeService {
PetTypes save(PetTypes petType);
List<PetTypes> findAll();
}

View file

@ -0,0 +1,27 @@
package org.springframework.samples.petclinic.service;
import org.springframework.samples.petclinic.model.PetTypes;
import org.springframework.samples.petclinic.repository.PetTypeRepository;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class PetTypeServiceImpl implements PetTypeService {
private final PetTypeRepository petTypeRepository;
public PetTypeServiceImpl(PetTypeRepository petTypeRepository) {
this.petTypeRepository = petTypeRepository;
}
@Override
public PetTypes save(PetTypes petType) {
return petTypeRepository.save(petType);
}
@Override
public List<PetTypes> findAll() {
return petTypeRepository.findAll();
}
}

View file

@ -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 pet_types (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
temperament VARCHAR(255),
length DOUBLE,
weight DOUBLE
);

View file

@ -0,0 +1,7 @@
CREATE TABLE pet_types (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
temperament VARCHAR(255),
length DOUBLE,
weight DOUBLE,
);