From b526b7964c7b77fd48550c0ac692371a26af6fb4 Mon Sep 17 00:00:00 2001 From: Jason Corchado Date: Thu, 15 Apr 2021 20:33:55 -0400 Subject: [PATCH 1/2] started endpoints --- .gitignore | 63 ++++++++------- README.md | 63 ++++++++------- data.sql | 58 +++++++------- .../zoos/config/H2ServerConfig.java | 44 +++++++++++ .../zoos/controllers/AnimalController.java | 24 ++++++ .../zoos/controllers/ZooController.java | 9 +++ .../java/lambdaschool/zoos/models/Animal.java | 75 ++++++++++++++++++ .../lambdaschool/zoos/models/Auditable.java | 32 ++++++++ .../lambdaschool/zoos/models/Telephone.java | 68 +++++++++++++++++ .../java/lambdaschool/zoos/models/Zoo.java | 75 ++++++++++++++++++ .../lambdaschool/zoos/models/ZooAnimals.java | 76 +++++++++++++++++++ .../zoos/models/ZooAnimalsId.java | 52 +++++++++++++ .../zoos/repositories/AnimalRepository.java | 19 +++++ .../zoos/repositories/ZooRepository.java | 9 +++ .../zoos/services/AnimalServices.java | 9 +++ .../zoos/services/AnimalServicesImpl.java | 20 +++++ .../zoos/services/ZooService.java | 12 +++ .../zoos/services/ZooServiceImpl.java | 31 ++++++++ .../lambdaschool/zoos/views/AnimalCount.java | 7 ++ 19 files changed, 659 insertions(+), 87 deletions(-) create mode 100644 src/main/java/lambdaschool/zoos/config/H2ServerConfig.java create mode 100644 src/main/java/lambdaschool/zoos/controllers/AnimalController.java create mode 100644 src/main/java/lambdaschool/zoos/controllers/ZooController.java create mode 100644 src/main/java/lambdaschool/zoos/models/Animal.java create mode 100644 src/main/java/lambdaschool/zoos/models/Auditable.java create mode 100644 src/main/java/lambdaschool/zoos/models/Telephone.java create mode 100644 src/main/java/lambdaschool/zoos/models/Zoo.java create mode 100644 src/main/java/lambdaschool/zoos/models/ZooAnimals.java create mode 100644 src/main/java/lambdaschool/zoos/models/ZooAnimalsId.java create mode 100644 src/main/java/lambdaschool/zoos/repositories/AnimalRepository.java create mode 100644 src/main/java/lambdaschool/zoos/repositories/ZooRepository.java create mode 100644 src/main/java/lambdaschool/zoos/services/AnimalServices.java create mode 100644 src/main/java/lambdaschool/zoos/services/AnimalServicesImpl.java create mode 100644 src/main/java/lambdaschool/zoos/services/ZooService.java create mode 100644 src/main/java/lambdaschool/zoos/services/ZooServiceImpl.java create mode 100644 src/main/java/lambdaschool/zoos/views/AnimalCount.java diff --git a/.gitignore b/.gitignore index 37e76631..549e00a2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,34 +1,33 @@ -# Jetbrains IntelliJ Idea +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### .idea +*.iws *.iml - -# Linux -# backup files -*~ - -# Windows -# thumbnails -Thumbs.db - -# Mac OS X -# metadata -.DS_Store -# thumbnails -._* - -# GIT -.git/ - -# Java -*.class - -# packages -*.jar -*.war -*.ear - -# Logging -*.log - -# jME (binaries) -*.so +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/README.md b/README.md index b303f4c2..40be1d5e 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,8 @@ A student that completes this project shows that they can: ## Introduction -This is a basic database scheme with zoos, animals, and telephones for the zoos. This Java Spring REST API application will provide endpoints for clients to read various data sets contained in the application's data. +This is a basic database scheme with zoos, animals, and telephones for the zoos. This Java Spring REST API application +will provide endpoints for clients to read various data sets contained in the application's data. ### Database layout @@ -20,35 +21,39 @@ You are creating a Java Spring REST API server that stores data in an H2 databas ![Zoo Database Layout](zoodb_mvp.png) * All tables contain the following auditing fields - * `createdby` - user name who created the row. Should default to **SYSTEM** - * `createddate` - date field when the row was created - * `lastmodifiedby` - user name who last changed data in the row. Should default to **SYSTEM** - * `lastmodifieddate` - date field when the data in the row was last changed + * `createdby` - user name who created the row. Should default to **SYSTEM** + * `createddate` - date field when the row was created + * `lastmodifiedby` - user name who last changed data in the row. Should default to **SYSTEM** + * `lastmodifieddate` - date field when the data in the row was last changed * Zoo - * `zooid` - long primary key - * `zooname` - String Name of the Zoo + * `zooid` - long primary key + * `zooname` - String Name of the Zoo * Telephone - * `phoneid` - long primary key - * `phonetype` - String - something like MAIN, EDUCATION, MEMBERSHIP, FAX, OPERATOR, OTHER - * `phonenumber` - String - a telephone number in any format - * `zooid` - foreign key to the Zoo table indicating the zoo who holds this telephone number - -There is a one to many relationship between zoos and telephones. One zoo can have multiple phone numbers but each phone number can only belong to one zoo. + * `phoneid` - long primary key + * `phonetype` - String - something like MAIN, EDUCATION, MEMBERSHIP, FAX, OPERATOR, OTHER + * `phonenumber` - String - a telephone number in any format + * `zooid` - foreign key to the Zoo table indicating the zoo who holds this telephone number + +There is a one to many relationship between zoos and telephones. One zoo can have multiple phone numbers but each phone +number can only belong to one zoo. * Animal - * `animalid` - long primary key - * `animaltype` - String - the type of animal such as lion or llama + * `animalid` - long primary key + * `animaltype` - String - the type of animal such as lion or llama -Zooaminals represents is a many to many relationship between zoos and animals. A zoo may have many animal types and an animal type may be at many zoos. +Zooaminals represents is a many to many relationship between zoos and animals. A zoo may have many animal types and an +animal type may be at many zoos. * Zooanimals - * `zooid` - long foreign key to zoo - * `animalid` - long foreign key to animal - * `incomingzoo` - String - the name of the zoo when the animal came from. The field can be left blank or null if the animal does not come from another zoo. + * `zooid` - long foreign key to zoo + * `animalid` - long foreign key to animal + * `incomingzoo` - String - the name of the zoo when the animal came from. The field can be left blank or null if the + animal does not come from another zoo. -Using the provided seed data, a successful application will return the follow data based on the given endpoint. Expand the section of the endpoint to see the data that is returned. +Using the provided seed data, a successful application will return the follow data based on the given endpoint. Expand +the section of the endpoint to see the data that is returned. ### MVP @@ -417,20 +422,26 @@ Status OK ## Instructions -* [ ] Please fork and clone this repository. This repository does not have a starter project, so create one inside of the cloned repository folder. Regularly commit and push your code as appropriate. +* [ ] Please fork and clone this repository. This repository does not have a starter project, so create one inside of + the cloned repository folder. Regularly commit and push your code as appropriate. * [ ] Create the entities needed to store this data. -* [ ] A data.sql file has been provided with seed data. You can use this class directly or modify it to fit your models. However, the data found in the file is the seed data to use! +* [ ] A data.sql file has been provided with seed data. You can use this class directly or modify it to fit your models. + However, the data found in the file is the seed data to use! * [ ] Add default Swagger Documentation Expose the following Endpoint -* [ ] GET /animals/count - that returns a JSON object list listing the animals and a count of how many zoos where they can be found. Use a custom query for this. +* [ ] GET /animals/count - that returns a JSON object list listing the animals and a count of how many zoos where they + can be found. Use a custom query for this. * [ ] GET /zoos/zoos - returns all zoos with their phone numbers and animals * [ ] GET /zoos/zoo/{id} - returns all information related to a zoo based on its id ## Stretch goals -* [ ] POST /zoos/zoo - adds a zoo including new telephone number and zoo animal combinations. The Animal Type must already exist. * In the header return as the location of the newly created zoo POST /zoos/zoo/{id} +* [ ] POST /zoos/zoo - adds a zoo including new telephone number and zoo animal combinations. The Animal Type must + already exist. * In the header return as the location of the newly created zoo POST /zoos/zoo/{id} * [ ] PUT /zoos/zoo/{id} - Completely replace the zoo record and all accompany records based off of the given zoo id. -* [ ] PATCH /zoos/zoo/{id} - Updates the zoo with new information. Only the new data is to be sent from the frontend client. -* [ ] DELETE /zoos/zoo/{id} - delete the zoo, associated phone numbers, and zoo animals combination associated with this zoo id +* [ ] PATCH /zoos/zoo/{id} - Updates the zoo with new information. Only the new data is to be sent from the frontend + client. +* [ ] DELETE /zoos/zoo/{id} - delete the zoo, associated phone numbers, and zoo animals combination associated with this + zoo id diff --git a/data.sql b/data.sql index d09840e3..55957d81 100644 --- a/data.sql +++ b/data.sql @@ -1,4 +1,3 @@ - DELETE FROM zooanimals; @@ -12,38 +11,39 @@ DELETE FROM zoos; INSERT INTO zoos (zooid, zooname, createdby, createddate, lastmodifiedby, lastmodifieddate) - VALUES (1, 'Gladys Porter Zoo', 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP), - (2, 'Point Defiance Zoo', 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP), - (3, 'San Diego Zoo', 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP), - (4, 'San Antonio Zoo', 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP), - (5, 'Smithsonian National Zoo', 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP); - -INSERT INTO telephones (phoneid, phonetype, phonenumber, zooid, createdby, createddate, lastmodifiedby, lastmodifieddate) - VALUES (1, 'MAIN', '555-555-5555', 1, 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP), - (2, 'EDUCATION', '555-555-1234', 1, 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP), - (3, 'MEMBERSHIP', '555-555-4321', 1, 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP), - (4, 'MAIN', '123-555-5555', 4, 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP), - (5, 'MAIN', '555-123-5555', 3, 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP); +VALUES (1, 'Gladys Porter Zoo', 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP), + (2, 'Point Defiance Zoo', 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP), + (3, 'San Diego Zoo', 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP), + (4, 'San Antonio Zoo', 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP), + (5, 'Smithsonian National Zoo', 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP); + +INSERT INTO telephones (phoneid, phonetype, phonenumber, zooid, createdby, createddate, lastmodifiedby, + lastmodifieddate) +VALUES (1, 'MAIN', '555-555-5555', 1, 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP), + (2, 'EDUCATION', '555-555-1234', 1, 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP), + (3, 'MEMBERSHIP', '555-555-4321', 1, 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP), + (4, 'MAIN', '123-555-5555', 4, 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP), + (5, 'MAIN', '555-123-5555', 3, 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP); INSERT INTO animals (animalid, animaltype, createdby, createddate, lastmodifiedby, lastmodifieddate) - VALUES (1, 'lion', 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP), - (2, 'bear', 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP), - (3, 'monkey', 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP), - (4, 'penguin', 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP), - (5, 'tiger', 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP), - (6, 'llama', 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP), - (7, 'turtle', 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP); +VALUES (1, 'lion', 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP), + (2, 'bear', 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP), + (3, 'monkey', 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP), + (4, 'penguin', 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP), + (5, 'tiger', 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP), + (6, 'llama', 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP), + (7, 'turtle', 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP); INSERT INTO zooanimals (zooid, animalid, incomingzoo, createdby, createddate, lastmodifiedby, lastmodifieddate) - VALUES (1, 1, 'Point Defiance Zoo', 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP), - (2, 2, 'Gladys Porter Zoo', 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP), - (1, 2, 'Point Defiance Zoo', 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP), - (5, 7, 'San Diego Zoo', 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP), - (5, 6, 'Gladys Porter Zoo', 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP), - (5, 5, 'Gladys Porter Zoo', 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP), - (5, 1, 'Gladys Porter Zoo', 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP), - (3, 1, 'Gladys Porter Zoo', 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP), - (3, 2, 'Point Defiance Zoo', 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP); +VALUES (1, 1, 'Point Defiance Zoo', 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP), + (2, 2, 'Gladys Porter Zoo', 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP), + (1, 2, 'Point Defiance Zoo', 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP), + (5, 7, 'San Diego Zoo', 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP), + (5, 6, 'Gladys Porter Zoo', 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP), + (5, 5, 'Gladys Porter Zoo', 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP), + (5, 1, 'Gladys Porter Zoo', 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP), + (3, 1, 'Gladys Porter Zoo', 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP), + (3, 2, 'Point Defiance Zoo', 'SYSTEM', CURRENT_TIMESTAMP, 'SYSTEM', CURRENT_TIMESTAMP); /* We must tell hibernate the ids that have already been used. diff --git a/src/main/java/lambdaschool/zoos/config/H2ServerConfig.java b/src/main/java/lambdaschool/zoos/config/H2ServerConfig.java new file mode 100644 index 00000000..b9e2a3e0 --- /dev/null +++ b/src/main/java/lambdaschool/zoos/config/H2ServerConfig.java @@ -0,0 +1,44 @@ +package lambdaschool.zoos.config; + +import org.h2.tools.Server; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import java.sql.SQLException; + +@Configuration +class H2ServerConfiguration +{ + + @Value("${h2.tcp.port:9092}") + private String h2TcpPort; + + @Value("${h2.web.port:8082}") + private String h2WebPort; + + @Bean + @ConditionalOnExpression("${h2.tcp.enabled:true}") + public Server h2TcpServer() throws + SQLException + { + return Server.createTcpServer("-tcp", + "-tcpAllowOthers", + "-tcpPort", + h2TcpPort) + .start(); + } + + @Bean + @ConditionalOnExpression("${h2.web.enabled:true}") + public Server h2WebServer() throws + SQLException + { + return Server.createWebServer("-web", + "-webAllowOthers", + "-webPort", + h2WebPort) + .start(); + } +} diff --git a/src/main/java/lambdaschool/zoos/controllers/AnimalController.java b/src/main/java/lambdaschool/zoos/controllers/AnimalController.java new file mode 100644 index 00000000..f3e67207 --- /dev/null +++ b/src/main/java/lambdaschool/zoos/controllers/AnimalController.java @@ -0,0 +1,24 @@ +package lambdaschool.zoos.controllers; + +import lambdaschool.zoos.services.AnimalServices; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping(value = "/animals") +public class AnimalController { + + @Autowired + private AnimalServices animalServices; + + + @GetMapping(value = "/count", produces = "application/json") + public ResponseEntity getAnimalCount(){ + return new ResponseEntity<>(animalServices.getAnimalCount(), HttpStatus.OK); + } + +} diff --git a/src/main/java/lambdaschool/zoos/controllers/ZooController.java b/src/main/java/lambdaschool/zoos/controllers/ZooController.java new file mode 100644 index 00000000..fdba8530 --- /dev/null +++ b/src/main/java/lambdaschool/zoos/controllers/ZooController.java @@ -0,0 +1,9 @@ +package lambdaschool.zoos.controllers; + +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping(value = "/zoos") +public class ZooController { +} diff --git a/src/main/java/lambdaschool/zoos/models/Animal.java b/src/main/java/lambdaschool/zoos/models/Animal.java new file mode 100644 index 00000000..09f265f7 --- /dev/null +++ b/src/main/java/lambdaschool/zoos/models/Animal.java @@ -0,0 +1,75 @@ +package lambdaschool.zoos.models; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; + +import javax.persistence.*; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +@Entity +@Table(name = "animals") +public class Animal extends Auditable{ + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private long animalid; + + @Column(unique = true, + nullable = false) + private String animaltype; + + @OneToMany(mappedBy = "zoo", + cascade = CascadeType.ALL) + @JsonIgnoreProperties(value = "zoo", + allowSetters = true) + private List telephones = new ArrayList<>(); + + @OneToMany(mappedBy = "animal", + cascade = CascadeType.ALL) + @JsonIgnoreProperties(value = "animal", + allowSetters = true) + private Set zoos = new HashSet<>(); + + public Animal() { + } + + public Animal(long animalid, String animaltype, Set zoos) { + this.animalid = animalid; + this.animaltype = animaltype; + this.zoos = zoos; + } + + public long getAnimalid() { + return animalid; + } + + public void setAnimalid(long animalid) { + this.animalid = animalid; + } + + public String getAnimaltype() { + return animaltype; + } + + public void setAnimaltype(String animaltype) { + this.animaltype = animaltype; + } + + public List getTelephones() { + return telephones; + } + + public void setTelephones(List telephones) { + this.telephones = telephones; + } + + public Set getZoos() { + return zoos; + } + + public void setZoos(Set zoos) { + this.zoos = zoos; + } +} diff --git a/src/main/java/lambdaschool/zoos/models/Auditable.java b/src/main/java/lambdaschool/zoos/models/Auditable.java new file mode 100644 index 00000000..a76392b7 --- /dev/null +++ b/src/main/java/lambdaschool/zoos/models/Auditable.java @@ -0,0 +1,32 @@ +package lambdaschool.zoos.models; + +import org.springframework.data.annotation.CreatedBy; +import org.springframework.data.annotation.CreatedDate; +import org.springframework.data.annotation.LastModifiedBy; +import org.springframework.data.annotation.LastModifiedDate; +import org.springframework.data.jpa.domain.support.AuditingEntityListener; + +import javax.persistence.EntityListeners; +import javax.persistence.MappedSuperclass; +import javax.persistence.Temporal; +import javax.persistence.TemporalType; +import java.util.Date; + +@MappedSuperclass +@EntityListeners(AuditingEntityListener.class) +abstract class Auditable { + + @CreatedBy + protected String createdBy; + + @CreatedDate + @Temporal(TemporalType.TIMESTAMP) + protected Date createdDate; + + @LastModifiedBy + protected String lastModifiedBy; + + @LastModifiedDate + @Temporal(TemporalType.TIMESTAMP) + protected Date lastModifiedDate; +} diff --git a/src/main/java/lambdaschool/zoos/models/Telephone.java b/src/main/java/lambdaschool/zoos/models/Telephone.java new file mode 100644 index 00000000..b600b9be --- /dev/null +++ b/src/main/java/lambdaschool/zoos/models/Telephone.java @@ -0,0 +1,68 @@ +package lambdaschool.zoos.models; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; + +import javax.persistence.*; + +@Entity +@Table(name = "telephones") +public class Telephone extends Auditable { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private long phoneid; + + @Column(nullable = false) + private String phonetype; + + @Column(nullable = false) + private String phonenumber; + + @ManyToOne + @JoinColumn(name = "zooid", + nullable = false) + @JsonIgnoreProperties(value = "telephones", + allowSetters = true) + private Zoo zoo; + + public Telephone() { + } + + public Telephone(String phonetype, String phonenumber, Zoo zoo) { + this.phonetype = phonetype; + this.phonenumber = phonenumber; + this.zoo = zoo; + } + + public long getPhoneid() { + return phoneid; + } + + public void setPhoneid(long phoneid) { + this.phoneid = phoneid; + } + + public String getPhonetype() { + return phonetype; + } + + public void setPhonetype(String phonetype) { + this.phonetype = phonetype; + } + + public String getPhonenumber() { + return phonenumber; + } + + public void setPhonenumber(String phonenumber) { + this.phonenumber = phonenumber; + } + + public Zoo getZoo() { + return zoo; + } + + public void setZoo(Zoo zoo) { + this.zoo = zoo; + } +} diff --git a/src/main/java/lambdaschool/zoos/models/Zoo.java b/src/main/java/lambdaschool/zoos/models/Zoo.java new file mode 100644 index 00000000..820ba73b --- /dev/null +++ b/src/main/java/lambdaschool/zoos/models/Zoo.java @@ -0,0 +1,75 @@ +package lambdaschool.zoos.models; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; + +import javax.persistence.*; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +@Entity +@Table(name = "zoos") +public class Zoo extends Auditable{ + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private long zooid; + + @Column(unique = true, + nullable = false) + private String zooname; + + @OneToMany(mappedBy = "zoo", + cascade = CascadeType.ALL, + orphanRemoval = true) + @JsonIgnoreProperties(value = "zoo") + private List telephones = new ArrayList<>(); + + @OneToMany(mappedBy = "zoo", + cascade = CascadeType.ALL) + @JsonIgnoreProperties(value = "zoo", + allowSetters = true) + private Set animals = new HashSet<>(); + + public Zoo() { + } + + public Zoo(String zooname, List telephones, Set animals) { + this.zooname = zooname; + this.telephones = telephones; + this.animals = animals; + } + + public long getZooid() { + return zooid; + } + + public void setZooid(long zooid) { + this.zooid = zooid; + } + + public String getZooname() { + return zooname; + } + + public void setZooname(String zooname) { + this.zooname = zooname; + } + + public List getTelephones() { + return telephones; + } + + public void setTelephones(List telephones) { + this.telephones = telephones; + } + + public Set getAnimals() { + return animals; + } + + public void setAnimals(Set animals) { + this.animals = animals; + } +} diff --git a/src/main/java/lambdaschool/zoos/models/ZooAnimals.java b/src/main/java/lambdaschool/zoos/models/ZooAnimals.java new file mode 100644 index 00000000..75f6ecce --- /dev/null +++ b/src/main/java/lambdaschool/zoos/models/ZooAnimals.java @@ -0,0 +1,76 @@ +package lambdaschool.zoos.models; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; + +import javax.persistence.*; +import java.io.Serializable; + +@Entity +@Table(name = "zooanimals") +@IdClass(ZooAnimalsId.class) +public class ZooAnimals extends Auditable implements Serializable { + + @Id + @ManyToOne + @JoinColumn(name = "zooid") + @JsonIgnoreProperties(value = "animals, allowSetters = true") + private Zoo zoo; + + @Id + @ManyToOne + @JoinColumn(name = "animalid") + @JsonIgnoreProperties(value = "zoos", allowSetters = true) + private Animal animal; + + String incomingzoo; + + public ZooAnimals() { + } + + public ZooAnimals(Zoo zoo, Animal animal, String incomingzoo) { + this.zoo = zoo; + this.animal = animal; + this.incomingzoo = incomingzoo; + } + + public Zoo getZoo() { + return zoo; + } + + public void setZoo(Zoo zoo) { + this.zoo = zoo; + } + + public Animal getAnimal() { + return animal; + } + + public void setAnimal(Animal animal) { + this.animal = animal; + } + + public String getIncomingzoo() { + return incomingzoo; + } + + public void setIncomingzoo(String incomingzoo) { + this.incomingzoo = incomingzoo; + } + + @Override + public boolean equals(Object o){ + if (this == o){ + return true; + }if (o == null || getClass() != o.getClass()){ + return false; + } + ZooAnimals that = (ZooAnimals) o; + return ((this.zoo == null) ? 0 : this.zoo.getZooid()) == ((that.zoo == null) ? 0 : that.zoo.getZooid()) && + ((this.animal == null) ? 0 : this.animal.getAnimalid()) == ((that.animal == null) ? 0 : that.animal.getAnimalid()); + } + + @Override + public int hashCode(){ + return 37; + } +} diff --git a/src/main/java/lambdaschool/zoos/models/ZooAnimalsId.java b/src/main/java/lambdaschool/zoos/models/ZooAnimalsId.java new file mode 100644 index 00000000..e5da82bb --- /dev/null +++ b/src/main/java/lambdaschool/zoos/models/ZooAnimalsId.java @@ -0,0 +1,52 @@ +package lambdaschool.zoos.models; + +import javax.persistence.Embeddable; +import java.io.Serializable; + +@Embeddable +public class ZooAnimalsId implements Serializable { + + private long zoo; + private long animal; + + public ZooAnimalsId() { + } + + public long getZoo() { + return zoo; + } + + public void setZoo(long zoo) { + this.zoo = zoo; + } + + public long getAnimal() { + return animal; + } + + public void setAnimal(long animal) { + this.animal = animal; + } + + @Override + public boolean equals(Object o) + { + if (this == o) + { + return true; + } + if (o == null || getClass() != o.getClass()) + { + return false; + } + ZooAnimalsId that = (ZooAnimalsId) o; + return zoo == that.zoo && + animal == that.animal; + } + + @Override + public int hashCode() + { + return 37; + } +} diff --git a/src/main/java/lambdaschool/zoos/repositories/AnimalRepository.java b/src/main/java/lambdaschool/zoos/repositories/AnimalRepository.java new file mode 100644 index 00000000..9144505c --- /dev/null +++ b/src/main/java/lambdaschool/zoos/repositories/AnimalRepository.java @@ -0,0 +1,19 @@ +package lambdaschool.zoos.repositories; + +import lambdaschool.zoos.models.Animal; +import lambdaschool.zoos.views.AnimalCount; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.CrudRepository; + +import java.util.ArrayList; + +public interface AnimalRepository extends CrudRepository { + + @Query(value = + "SELECT a.animalid, a.animaltype, count(z.zooid) as countzoos " + + "FROM zooanimals z RIGHT JOIN animals a ON z.animalid=a.animalid " + + "GROUP BY a.animalid, a.animaltype " + + "ORDER BY a.animaltype", nativeQuery = true) + ArrayList getAnimalCount(); + +} diff --git a/src/main/java/lambdaschool/zoos/repositories/ZooRepository.java b/src/main/java/lambdaschool/zoos/repositories/ZooRepository.java new file mode 100644 index 00000000..4cc5c61e --- /dev/null +++ b/src/main/java/lambdaschool/zoos/repositories/ZooRepository.java @@ -0,0 +1,9 @@ +package lambdaschool.zoos.repositories; + +import lambdaschool.zoos.models.Zoo; +import org.springframework.data.repository.CrudRepository; + +public interface ZooRepository extends CrudRepository { + + +} diff --git a/src/main/java/lambdaschool/zoos/services/AnimalServices.java b/src/main/java/lambdaschool/zoos/services/AnimalServices.java new file mode 100644 index 00000000..152e02e8 --- /dev/null +++ b/src/main/java/lambdaschool/zoos/services/AnimalServices.java @@ -0,0 +1,9 @@ +package lambdaschool.zoos.services; + +import lambdaschool.zoos.views.AnimalCount; + +import java.util.ArrayList; + +public interface AnimalServices { + ArrayList getAnimalCount(); +} diff --git a/src/main/java/lambdaschool/zoos/services/AnimalServicesImpl.java b/src/main/java/lambdaschool/zoos/services/AnimalServicesImpl.java new file mode 100644 index 00000000..fbb25f97 --- /dev/null +++ b/src/main/java/lambdaschool/zoos/services/AnimalServicesImpl.java @@ -0,0 +1,20 @@ +package lambdaschool.zoos.services; + +import lambdaschool.zoos.repositories.AnimalRepository; +import lambdaschool.zoos.views.AnimalCount; +import org.springframework.beans.factory.annotation.Autowired; + +import java.util.ArrayList; + +public class AnimalServicesImpl implements AnimalServices{ + + @Autowired + AnimalRepository animalRepository; + + @Override + public ArrayList getAnimalCount() { + + return animalRepository.getAnimalCount(); + } + +} diff --git a/src/main/java/lambdaschool/zoos/services/ZooService.java b/src/main/java/lambdaschool/zoos/services/ZooService.java new file mode 100644 index 00000000..bc9a586e --- /dev/null +++ b/src/main/java/lambdaschool/zoos/services/ZooService.java @@ -0,0 +1,12 @@ +package lambdaschool.zoos.services; + +import lambdaschool.zoos.models.Zoo; + +import java.util.ArrayList; +import java.util.List; + +public interface ZooService { + +List findZoos(); +Zoo findZooById(long id); +} diff --git a/src/main/java/lambdaschool/zoos/services/ZooServiceImpl.java b/src/main/java/lambdaschool/zoos/services/ZooServiceImpl.java new file mode 100644 index 00000000..3bc9ba33 --- /dev/null +++ b/src/main/java/lambdaschool/zoos/services/ZooServiceImpl.java @@ -0,0 +1,31 @@ +package lambdaschool.zoos.services; + + +import lambdaschool.zoos.models.Zoo; +import lambdaschool.zoos.repositories.ZooRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import javax.persistence.EntityNotFoundException; +import java.util.ArrayList; +import java.util.List; + +@Service(value = "zooService") +public class ZooServiceImpl implements ZooService { + + @Autowired + ZooRepository zooRepository; + + @Override + public List findZoos() { + List list = new ArrayList<>(); + zooRepository.findAll().iterator().forEachRemaining(list::add); + return list; + } + + @Override + public Zoo findZooById(long id) { + return zooRepository.findById(id) + .orElseThrow(() -> new EntityNotFoundException("Zoo " + id + " not found!")); + } +} diff --git a/src/main/java/lambdaschool/zoos/views/AnimalCount.java b/src/main/java/lambdaschool/zoos/views/AnimalCount.java new file mode 100644 index 00000000..a7dbcb28 --- /dev/null +++ b/src/main/java/lambdaschool/zoos/views/AnimalCount.java @@ -0,0 +1,7 @@ +package lambdaschool.zoos.views; + +public interface AnimalCount { + long getAnimalid(); + String getAnimaltype(); + int getCountzoos(); +} From 1e176793543d697f3203878bcc910351e972e91f Mon Sep 17 00:00:00 2001 From: Jason Corchado Date: Thu, 15 Apr 2021 20:58:52 -0400 Subject: [PATCH 2/2] started endpoints --- .../zoos/controllers/AnimalController.java | 16 ++++++++-------- .../zoos/controllers/ZooController.java | 13 +++++++++++++ 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/main/java/lambdaschool/zoos/controllers/AnimalController.java b/src/main/java/lambdaschool/zoos/controllers/AnimalController.java index f3e67207..74b6544d 100644 --- a/src/main/java/lambdaschool/zoos/controllers/AnimalController.java +++ b/src/main/java/lambdaschool/zoos/controllers/AnimalController.java @@ -12,13 +12,13 @@ @RequestMapping(value = "/animals") public class AnimalController { - @Autowired - private AnimalServices animalServices; - - - @GetMapping(value = "/count", produces = "application/json") - public ResponseEntity getAnimalCount(){ - return new ResponseEntity<>(animalServices.getAnimalCount(), HttpStatus.OK); - } +// @Autowired +// private AnimalServices animalServices; +// +// +// @GetMapping(value = "/count", produces = "application/json") +// public ResponseEntity getAnimalCount(){ +// return new ResponseEntity<>(animalServices.getAnimalCount(), HttpStatus.OK); +// } } diff --git a/src/main/java/lambdaschool/zoos/controllers/ZooController.java b/src/main/java/lambdaschool/zoos/controllers/ZooController.java index fdba8530..e9f2cb1c 100644 --- a/src/main/java/lambdaschool/zoos/controllers/ZooController.java +++ b/src/main/java/lambdaschool/zoos/controllers/ZooController.java @@ -1,9 +1,22 @@ package lambdaschool.zoos.controllers; +import lambdaschool.zoos.services.ZooService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping(value = "/zoos") public class ZooController { + + @Autowired + private ZooService zooService; + + @GetMapping(value = "/zoo", produces = "application/json") + public ResponseEntity findAllZoos(){ + return new ResponseEntity<>(zooService.findZoos(), HttpStatus.OK); + } }