Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package org.springframework.samples.petclinic.goods;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Table;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Positive;
import org.springframework.samples.petclinic.model.NamedEntity;

/**
* Simple JavaBean domain object representing a dog good/product.
*/
@Entity
@Table(name = "dog_goods")
public class DogGood extends NamedEntity {

@Column(name = "description")
@NotBlank
private String description;

@Column(name = "price")
@NotNull
@Positive
private Double price;

@Column(name = "category")
@NotBlank
private String category;

@Column(name = "stock_quantity")
@NotNull
@Positive
private Integer stockQuantity;

public String getDescription() {
return this.description;
}

public void setDescription(String description) {
this.description = description;
}

public Double getPrice() {
return this.price;
}

public void setPrice(Double price) {
this.price = price;
}

public String getCategory() {
return this.category;
}

public void setCategory(String category) {
this.category = category;
}

public Integer getStockQuantity() {
return this.stockQuantity;
}

public void setStockQuantity(Integer stockQuantity) {
this.stockQuantity = stockQuantity;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package org.springframework.samples.petclinic.goods;

import java.util.List;
import java.util.Optional;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import jakarta.validation.Valid;

/**
* REST Controller for managing dog goods.
*/
@RestController
@RequestMapping("/api/dog-goods")
public class DogGoodController {

private final DogGoodRepository dogGoodRepository;

public DogGoodController(DogGoodRepository dogGoodRepository) {
this.dogGoodRepository = dogGoodRepository;
}

/**
* GET /api/dog-goods : Get all dog goods.
*
* @param page the pagination information
* @param size the pagination size
* @return the ResponseEntity with status 200 (OK) and the list of dog goods in body
*/
@GetMapping
public ResponseEntity<Page<DogGood>> getAllDogGoods(
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "10") int size) {
Pageable pageable = PageRequest.of(page, size);
Page<DogGood> result = dogGoodRepository.findAll(pageable);
return new ResponseEntity<>(result, HttpStatus.OK);
}

/**
* GET /api/dog-goods/:id : Get the "id" dog good.
*
* @param id the id of the dog good to retrieve
* @return the ResponseEntity with status 200 (OK) and with body the dog good, or with status 404 (Not Found)
*/
@GetMapping("/{id}")
public ResponseEntity<DogGood> getDogGood(@PathVariable Integer id) {
Optional<DogGood> dogGood = dogGoodRepository.findById(id);
return dogGood.map(response -> ResponseEntity.ok().body(response))
.orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
}

/**
* GET /api/dog-goods/category/:category : Get dog goods by category.
*
* @param category the category of the dog goods to retrieve
* @return the ResponseEntity with status 200 (OK) and the list of dog goods in body
*/
@GetMapping("/category/{category}")
public ResponseEntity<List<DogGood>> getDogGoodsByCategory(@PathVariable String category) {
List<DogGood> dogGoods = dogGoodRepository.findByCategory(category);
return new ResponseEntity<>(dogGoods, HttpStatus.OK);
}

/**
* GET /api/dog-goods/search : Search dog goods by name.
*
* @param name the name to search for
* @return the ResponseEntity with status 200 (OK) and the list of dog goods in body
*/
@GetMapping("/search")
public ResponseEntity<List<DogGood>> searchDogGoods(@RequestParam String name) {
List<DogGood> dogGoods = dogGoodRepository.findByNameContaining(name);
return new ResponseEntity<>(dogGoods, HttpStatus.OK);
}

/**
* POST /api/dog-goods : Create a new dog good.
*
* @param dogGood the dog good to create
* @return the ResponseEntity with status 201 (Created) and with body the new dog good
*/
@PostMapping
public ResponseEntity<DogGood> createDogGood(@Valid @RequestBody DogGood dogGood) {
if (dogGood.getId() != null) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
DogGood result = dogGoodRepository.save(dogGood);
return new ResponseEntity<>(result, HttpStatus.CREATED);
}

/**
* PUT /api/dog-goods/:id : Updates an existing dog good.
*
* @param id the id of the dog good to update
* @param dogGood the dog good to update
* @return the ResponseEntity with status 200 (OK) and with body the updated dog good,
* or with status 400 (Bad Request) if the dog good is not valid,
* or with status 500 (Internal Server Error) if the dog good couldn't be updated
*/
@PutMapping("/{id}")
public ResponseEntity<DogGood> updateDogGood(
@PathVariable Integer id,
@Valid @RequestBody DogGood dogGood) {
if (dogGood.getId() == null) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
if (!id.equals(dogGood.getId())) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
if (!dogGoodRepository.existsById(id)) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
DogGood result = dogGoodRepository.save(dogGood);
return new ResponseEntity<>(result, HttpStatus.OK);
}

/**
* DELETE /api/dog-goods/:id : Delete the "id" dog good.
*
* @param id the id of the dog good to delete
* @return the ResponseEntity with status 204 (NO_CONTENT)
*/
@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteDogGood(@PathVariable Integer id) {
if (!dogGoodRepository.existsById(id)) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
dogGoodRepository.deleteById(id);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.springframework.samples.petclinic.goods;

import java.util.List;
import java.util.Optional;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

/**
* Repository class for <code>DogGood</code> domain objects
*/
public interface DogGoodRepository extends JpaRepository<DogGood, Integer> {

/**
* Retrieve all {@link DogGood}s from the data store.
* @return a Collection of {@link DogGood}s.
*/
List<DogGood> findAll();

/**
* Retrieve all {@link DogGood}s from the data store with pagination.
* @param pageable pagination information
* @return a Page of {@link DogGood}s.
*/
Page<DogGood> findAll(Pageable pageable);

/**
* Retrieve {@link DogGood}s from the data store by category.
* @param category to search for
* @return a Collection of matching {@link DogGood}s
*/
List<DogGood> findByCategory(String category);

/**
* Retrieve {@link DogGood}s from the data store by name containing the given string.
* @param name Value to search for
* @return a Collection of matching {@link DogGood}s
*/
List<DogGood> findByNameContaining(String name);

/**
* Retrieve a {@link DogGood} from the data store by id.
* @param id the id to search for
* @return the {@link DogGood} if found
*/
Optional<DogGood> findById(Integer id);
}
12 changes: 12 additions & 0 deletions src/main/resources/db/h2/data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,15 @@ 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');

-- Dog goods data
INSERT INTO dog_goods VALUES (default, 'Premium Dog Food', 'High-quality dog food for all breeds', 29.99, 'Food', 100);
INSERT INTO dog_goods VALUES (default, 'Chew Toy', 'Durable chew toy for dogs', 12.99, 'Toys', 50);
INSERT INTO dog_goods VALUES (default, 'Dog Bed', 'Comfortable bed for small to medium dogs', 49.99, 'Bedding', 30);
INSERT INTO dog_goods VALUES (default, 'Dog Leash', 'Strong and durable leash', 15.99, 'Accessories', 75);
INSERT INTO dog_goods VALUES (default, 'Dog Collar', 'Adjustable collar for all sizes', 9.99, 'Accessories', 80);
INSERT INTO dog_goods VALUES (default, 'Dog Shampoo', 'Gentle shampoo for sensitive skin', 14.99, 'Grooming', 60);
INSERT INTO dog_goods VALUES (default, 'Dog Brush', 'Effective brush for all coat types', 19.99, 'Grooming', 45);
INSERT INTO dog_goods VALUES (default, 'Dog Treats', 'Delicious treats for training', 8.99, 'Food', 120);
INSERT INTO dog_goods VALUES (default, 'Dog Bowl', 'Non-slip food and water bowl', 16.99, 'Accessories', 65);
INSERT INTO dog_goods VALUES (default, 'Dog Coat', 'Warm coat for cold weather', 34.99, 'Clothing', 25);
12 changes: 12 additions & 0 deletions src/main/resources/db/h2/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ DROP TABLE visits IF EXISTS;
DROP TABLE pets IF EXISTS;
DROP TABLE types IF EXISTS;
DROP TABLE owners IF EXISTS;
DROP TABLE dog_goods IF EXISTS;


CREATE TABLE vets (
Expand Down Expand Up @@ -62,3 +63,14 @@ 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 dog_goods (
id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
name VARCHAR(80),
description VARCHAR(255),
price DOUBLE,
category VARCHAR(80),
stock_quantity INTEGER
);
CREATE INDEX dog_goods_name ON dog_goods (name);
CREATE INDEX dog_goods_category ON dog_goods (category);
12 changes: 12 additions & 0 deletions src/main/resources/db/hsqldb/data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,15 @@ INSERT INTO visits VALUES (1, 7, '2013-01-01', 'rabies shot');
INSERT INTO visits VALUES (2, 8, '2013-01-02', 'rabies shot');
INSERT INTO visits VALUES (3, 8, '2013-01-03', 'neutered');
INSERT INTO visits VALUES (4, 7, '2013-01-04', 'spayed');

-- Dog goods data
INSERT INTO dog_goods VALUES (1, 'Premium Dog Food', 'High-quality dog food for all breeds', 29.99, 'Food', 100);
INSERT INTO dog_goods VALUES (2, 'Chew Toy', 'Durable chew toy for dogs', 12.99, 'Toys', 50);
INSERT INTO dog_goods VALUES (3, 'Dog Bed', 'Comfortable bed for small to medium dogs', 49.99, 'Bedding', 30);
INSERT INTO dog_goods VALUES (4, 'Dog Leash', 'Strong and durable leash', 15.99, 'Accessories', 75);
INSERT INTO dog_goods VALUES (5, 'Dog Collar', 'Adjustable collar for all sizes', 9.99, 'Accessories', 80);
INSERT INTO dog_goods VALUES (6, 'Dog Shampoo', 'Gentle shampoo for sensitive skin', 14.99, 'Grooming', 60);
INSERT INTO dog_goods VALUES (7, 'Dog Brush', 'Effective brush for all coat types', 19.99, 'Grooming', 45);
INSERT INTO dog_goods VALUES (8, 'Dog Treats', 'Delicious treats for training', 8.99, 'Food', 120);
INSERT INTO dog_goods VALUES (9, 'Dog Bowl', 'Non-slip food and water bowl', 16.99, 'Accessories', 65);
INSERT INTO dog_goods VALUES (10, 'Dog Coat', 'Warm coat for cold weather', 34.99, 'Clothing', 25);
12 changes: 12 additions & 0 deletions src/main/resources/db/hsqldb/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ DROP TABLE visits IF EXISTS;
DROP TABLE pets IF EXISTS;
DROP TABLE types IF EXISTS;
DROP TABLE owners IF EXISTS;
DROP TABLE dog_goods IF EXISTS;


CREATE TABLE vets (
Expand Down Expand Up @@ -62,3 +63,14 @@ 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 dog_goods (
id INTEGER IDENTITY PRIMARY KEY,
name VARCHAR(80),
description VARCHAR(255),
price DOUBLE,
category VARCHAR(80),
stock_quantity INTEGER
);
CREATE INDEX dog_goods_name ON dog_goods (name);
CREATE INDEX dog_goods_category ON dog_goods (category);
12 changes: 12 additions & 0 deletions src/main/resources/db/mysql/data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,15 @@ INSERT IGNORE INTO visits VALUES (1, 7, '2010-03-04', 'rabies shot');
INSERT IGNORE INTO visits VALUES (2, 8, '2011-03-04', 'rabies shot');
INSERT IGNORE INTO visits VALUES (3, 8, '2009-06-04', 'neutered');
INSERT IGNORE INTO visits VALUES (4, 7, '2008-09-04', 'spayed');

-- Dog goods data
INSERT IGNORE INTO dog_goods VALUES (1, 'Premium Dog Food', 'High-quality dog food for all breeds', 29.99, 'Food', 100);
INSERT IGNORE INTO dog_goods VALUES (2, 'Chew Toy', 'Durable chew toy for dogs', 12.99, 'Toys', 50);
INSERT IGNORE INTO dog_goods VALUES (3, 'Dog Bed', 'Comfortable bed for small to medium dogs', 49.99, 'Bedding', 30);
INSERT IGNORE INTO dog_goods VALUES (4, 'Dog Leash', 'Strong and durable leash', 15.99, 'Accessories', 75);
INSERT IGNORE INTO dog_goods VALUES (5, 'Dog Collar', 'Adjustable collar for all sizes', 9.99, 'Accessories', 80);
INSERT IGNORE INTO dog_goods VALUES (6, 'Dog Shampoo', 'Gentle shampoo for sensitive skin', 14.99, 'Grooming', 60);
INSERT IGNORE INTO dog_goods VALUES (7, 'Dog Brush', 'Effective brush for all coat types', 19.99, 'Grooming', 45);
INSERT IGNORE INTO dog_goods VALUES (8, 'Dog Treats', 'Delicious treats for training', 8.99, 'Food', 120);
INSERT IGNORE INTO dog_goods VALUES (9, 'Dog Bowl', 'Non-slip food and water bowl', 16.99, 'Accessories', 65);
INSERT IGNORE INTO dog_goods VALUES (10, 'Dog Coat', 'Warm coat for cold weather', 34.99, 'Clothing', 25);
11 changes: 11 additions & 0 deletions src/main/resources/db/mysql/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,14 @@ CREATE TABLE IF NOT EXISTS visits (
description VARCHAR(255),
FOREIGN KEY (pet_id) REFERENCES pets(id)
) engine=InnoDB;

CREATE TABLE IF NOT EXISTS dog_goods (
id INT(4) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(80),
description VARCHAR(255),
price DOUBLE,
category VARCHAR(80),
stock_quantity INT(4) UNSIGNED,
INDEX(name),
INDEX(category)
) engine=InnoDB;
12 changes: 12 additions & 0 deletions src/main/resources/db/postgres/data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,15 @@ INSERT INTO visits (pet_id, visit_date, description) SELECT 7, '2010-03-04', 'ra
INSERT INTO visits (pet_id, visit_date, description) SELECT 8, '2011-03-04', 'rabies shot' WHERE NOT EXISTS (SELECT * FROM visits WHERE id=2);
INSERT INTO visits (pet_id, visit_date, description) SELECT 8, '2009-06-04', 'neutered' WHERE NOT EXISTS (SELECT * FROM visits WHERE id=3);
INSERT INTO visits (pet_id, visit_date, description) SELECT 7, '2008-09-04', 'spayed' WHERE NOT EXISTS (SELECT * FROM visits WHERE id=4);

-- Dog goods data
INSERT INTO dog_goods (name, description, price, category, stock_quantity) SELECT 'Premium Dog Food', 'High-quality dog food for all breeds', 29.99, 'Food', 100 WHERE NOT EXISTS (SELECT * FROM dog_goods WHERE id=1);
INSERT INTO dog_goods (name, description, price, category, stock_quantity) SELECT 'Chew Toy', 'Durable chew toy for dogs', 12.99, 'Toys', 50 WHERE NOT EXISTS (SELECT * FROM dog_goods WHERE id=2);
INSERT INTO dog_goods (name, description, price, category, stock_quantity) SELECT 'Dog Bed', 'Comfortable bed for small to medium dogs', 49.99, 'Bedding', 30 WHERE NOT EXISTS (SELECT * FROM dog_goods WHERE id=3);
INSERT INTO dog_goods (name, description, price, category, stock_quantity) SELECT 'Dog Leash', 'Strong and durable leash', 15.99, 'Accessories', 75 WHERE NOT EXISTS (SELECT * FROM dog_goods WHERE id=4);
INSERT INTO dog_goods (name, description, price, category, stock_quantity) SELECT 'Dog Collar', 'Adjustable collar for all sizes', 9.99, 'Accessories', 80 WHERE NOT EXISTS (SELECT * FROM dog_goods WHERE id=5);
INSERT INTO dog_goods (name, description, price, category, stock_quantity) SELECT 'Dog Shampoo', 'Gentle shampoo for sensitive skin', 14.99, 'Grooming', 60 WHERE NOT EXISTS (SELECT * FROM dog_goods WHERE id=6);
INSERT INTO dog_goods (name, description, price, category, stock_quantity) SELECT 'Dog Brush', 'Effective brush for all coat types', 19.99, 'Grooming', 45 WHERE NOT EXISTS (SELECT * FROM dog_goods WHERE id=7);
INSERT INTO dog_goods (name, description, price, category, stock_quantity) SELECT 'Dog Treats', 'Delicious treats for training', 8.99, 'Food', 120 WHERE NOT EXISTS (SELECT * FROM dog_goods WHERE id=8);
INSERT INTO dog_goods (name, description, price, category, stock_quantity) SELECT 'Dog Bowl', 'Non-slip food and water bowl', 16.99, 'Accessories', 65 WHERE NOT EXISTS (SELECT * FROM dog_goods WHERE id=9);
INSERT INTO dog_goods (name, description, price, category, stock_quantity) SELECT 'Dog Coat', 'Warm coat for cold weather', 34.99, 'Clothing', 25 WHERE NOT EXISTS (SELECT * FROM dog_goods WHERE id=10);
Loading
Loading