From 0ecde8f3ea05c6b6e56644e72b572d6c72ea81d4 Mon Sep 17 00:00:00 2001 From: snackk Date: Wed, 24 Oct 2018 00:40:32 +0100 Subject: [PATCH] refactor domain model to support vet available hours --- .../samples/petclinic/vet/AvailableHour.java | 59 +++++++++++++++++++ .../samples/petclinic/vet/VetRepository.java | 1 + src/main/resources/db/hsqldb/data.sql | 15 +++-- src/main/resources/db/hsqldb/schema.sql | 12 +++- .../templates/fragments/inputField.html | 3 + .../templates/owners/ownersList.html | 2 + .../resources/templates/vets/vetList.html | 3 + 7 files changed, 90 insertions(+), 5 deletions(-) create mode 100644 src/main/java/org/springframework/samples/petclinic/vet/AvailableHour.java diff --git a/src/main/java/org/springframework/samples/petclinic/vet/AvailableHour.java b/src/main/java/org/springframework/samples/petclinic/vet/AvailableHour.java new file mode 100644 index 000000000..5abdd207e --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/vet/AvailableHour.java @@ -0,0 +1,59 @@ +/* + * Copyright 2002-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.samples.petclinic.vet; + +import org.springframework.format.annotation.DateTimeFormat; +import org.springframework.samples.petclinic.model.BaseEntity; + +import javax.persistence.*; +import java.io.Serializable; +import java.time.LocalDateTime; + +/** + * Models a {@link AvailableHour AvailableHour} for each {@link Vet Vet} + * + * @author Diogo Santos + */ +@Entity +@Table(name = "available_hour") +public class AvailableHour extends BaseEntity implements Serializable { + + @Column(name = "time_date") + @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm") + private LocalDateTime timeDate; + + @ManyToOne + @JoinColumn(name = "vet_id") + private Vet vet; + + public AvailableHour() {} + + public Vet getVet() { + return vet; + } + + public void setVet(Vet vet) { + this.vet = vet; + } + + public LocalDateTime getTimeDate() { + return timeDate; + } + + public void setTimeDate(LocalDateTime timeDate) { + this.timeDate = timeDate; + } +} diff --git a/src/main/java/org/springframework/samples/petclinic/vet/VetRepository.java b/src/main/java/org/springframework/samples/petclinic/vet/VetRepository.java index 549b1c229..abd41e3c5 100644 --- a/src/main/java/org/springframework/samples/petclinic/vet/VetRepository.java +++ b/src/main/java/org/springframework/samples/petclinic/vet/VetRepository.java @@ -19,6 +19,7 @@ import java.util.Collection; import org.springframework.cache.annotation.Cacheable; import org.springframework.dao.DataAccessException; +import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.Repository; import org.springframework.transaction.annotation.Transactional; diff --git a/src/main/resources/db/hsqldb/data.sql b/src/main/resources/db/hsqldb/data.sql index 16dda3e84..f713b3e57 100644 --- a/src/main/resources/db/hsqldb/data.sql +++ b/src/main/resources/db/hsqldb/data.sql @@ -5,6 +5,13 @@ INSERT INTO vets VALUES (4, 'Rafael', 'Ortega'); INSERT INTO vets VALUES (5, 'Henry', 'Stevens'); INSERT INTO vets VALUES (6, 'Sharon', 'Jenkins'); +INSERT INTO available_hour VALUES (1, 1, '2018-10-23 14:00:00'); +INSERT INTO available_hour VALUES (2, 1, '2018-10-23 15:00:00'); +INSERT INTO available_hour VALUES (3, 1, '2018-10-23 16:00:00'); +INSERT INTO available_hour VALUES (4, 2, '2018-10-23 14:00:00'); +INSERT INTO available_hour VALUES (5, 2, '2018-10-23 15:00:00'); +INSERT INTO available_hour VALUES (6, 2, '2018-10-23 16:00:00'); + INSERT INTO specialties VALUES (1, 'radiology'); INSERT INTO specialties VALUES (2, 'surgery'); INSERT INTO specialties VALUES (3, 'dentistry'); @@ -47,7 +54,7 @@ INSERT INTO pets VALUES (11, 'Freddy', '2010-03-09', 5, 9); INSERT INTO pets VALUES (12, 'Lucky', '2010-06-24', 2, 10); INSERT INTO pets VALUES (13, 'Sly', '2012-06-08', 1, 10); -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'); +INSERT INTO visits VALUES (1, 1, 1, '2018-10-23 14:00:00', 'rabies shot'); +INSERT INTO visits VALUES (2, 1, 1, '2018-10-23 15:00:00', 'rabies shot'); +INSERT INTO visits VALUES (3, 2, 2, '2018-10-23 14:00:00', 'neutered'); +INSERT INTO visits VALUES (4, 4, 2, '2018-10-23 16:00:00', 'spayed'); diff --git a/src/main/resources/db/hsqldb/schema.sql b/src/main/resources/db/hsqldb/schema.sql index f3c6947b7..782df6123 100644 --- a/src/main/resources/db/hsqldb/schema.sql +++ b/src/main/resources/db/hsqldb/schema.sql @@ -57,8 +57,18 @@ CREATE INDEX pets_name ON pets (name); CREATE TABLE visits ( id INTEGER IDENTITY PRIMARY KEY, pet_id INTEGER NOT NULL, - visit_date DATE, + vet_id INTEGER NOT NULL, + visit_date TIMESTAMP, description VARCHAR(255) ); ALTER TABLE visits ADD CONSTRAINT fk_visits_pets FOREIGN KEY (pet_id) REFERENCES pets (id); +ALTER TABLE visits ADD CONSTRAINT fk_vet_visits FOREIGN KEY (vet_id) REFERENCES vets (id); CREATE INDEX visits_pet_id ON visits (pet_id); + +CREATE TABLE available_hour ( + id INTEGER IDENTITY PRIMARY KEY, + vet_id INTEGER NOT NULL, + time_date TIMESTAMP +); +ALTER TABLE available_hour ADD CONSTRAINT fk_vet_available_hour FOREIGN KEY (vet_id) REFERENCES vets (id); +CREATE INDEX available_hour_vet_id ON available_hour (vet_id); diff --git a/src/main/resources/templates/fragments/inputField.html b/src/main/resources/templates/fragments/inputField.html index c3373bea0..5c0b8e7c2 100644 --- a/src/main/resources/templates/fragments/inputField.html +++ b/src/main/resources/templates/fragments/inputField.html @@ -12,6 +12,9 @@ + + id Name Address City @@ -18,6 +19,7 @@ + diff --git a/src/main/resources/templates/vets/vetList.html b/src/main/resources/templates/vets/vetList.html index 4fc961793..97d8bc08b 100644 --- a/src/main/resources/templates/vets/vetList.html +++ b/src/main/resources/templates/vets/vetList.html @@ -12,6 +12,7 @@ Name Specialties + Hours @@ -20,6 +21,8 @@ none +