#1 JPA persistance lab

Open
jonathan-hinds wants to merge 3 commits from jonathan-hinds/CR-MacroLab-Spring-MovieDatabase.SQL.JDBC.JPA:master into master

+ 9
- 0
data-h2.sql View File

@@ -13,3 +13,12 @@ INSERT INTO PERSON ( LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID)VALUES ('S
13 13
 INSERT INTO PERSON ( LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY, HOME_ID)VALUES ('Brown', 'Doug', '466-6241', '1954-12-07', 3);
14 14
 
15 15
 
16
+INSERT INTO MOVIES (Title, Runtime, Genre, IMDB_Score, Rating) VALUES ('Howard the Duck',	110,	'Sci-Fi',	4.6,	'PG');
17
+INSERT INTO MOVIES (Title, Runtime, Genre, IMDB_Score, Rating) VALUES ('Lavalantula',	83,	'Horror',	4.7,	'TV-14');
18
+INSERT INTO MOVIES (Title, Runtime, Genre, IMDB_Score, Rating) VALUES ('Starship Troopers',	129,	'Sci-Fi',	7.2,	'PG-13');
19
+INSERT INTO MOVIES (Title, Runtime, Genre, IMDB_Score, Rating) VALUES ('Waltz With Bashir',	90,	'Documentary',	8.0,	'R');
20
+INSERT INTO MOVIES (Title, Runtime, Genre, IMDB_Score, Rating) VALUES ('Spaceballs',	96,	'Comedy',	7.1,	'PG');
21
+INSERT INTO MOVIES (Title, Runtime, Genre, IMDB_Score, Rating) VALUES ('Monsters Inc.',	92,	'Animation',	8.1,	'G');
22
+
23
+INSERT INTO MOVIES (Title, Runtime, Genre, IMDB_Score, Rating) VALUES ('Lords of Dogtown',	107,	'Sport',	7.1,	'PG-13');
24
+INSERT INTO MOVIES (Title, Runtime, Genre, IMDB_Score, Rating) VALUES ('A Beautiful Mind',	135,	'Drama',	8.2,	'PG-13');

+ 59
- 0
src/main/java/io/zipcoder/persistenceapp/Home.java View File

@@ -0,0 +1,59 @@
1
+package io.zipcoder.persistenceapp;
2
+
3
+import javax.persistence.Entity;
4
+import javax.persistence.GeneratedValue;
5
+import javax.persistence.GenerationType;
6
+import javax.persistence.Id;
7
+
8
+@Entity
9
+public class Home {
10
+
11
+    @Id
12
+    @GeneratedValue(strategy = GenerationType.IDENTITY)
13
+    private Integer id;
14
+    private String address;
15
+    private String homeNumber;
16
+
17
+    public Home() {
18
+        this.address = "address";
19
+        this.homeNumber = "home number";
20
+    }
21
+
22
+    public Home(String address, String homeNumber) {
23
+        this.address = address;
24
+        this.homeNumber = homeNumber;
25
+    }
26
+
27
+    public Integer getId() {
28
+        return id;
29
+    }
30
+
31
+    public void setId(Integer id) {
32
+        this.id = id;
33
+    }
34
+
35
+    public String getAddress() {
36
+        return address;
37
+    }
38
+
39
+    public void setAddress(String address) {
40
+        this.address = address;
41
+    }
42
+
43
+    public String getHomeNumber() {
44
+        return homeNumber;
45
+    }
46
+
47
+    public void setHomeNumber(String homeNumber) {
48
+        this.homeNumber = homeNumber;
49
+    }
50
+
51
+    @Override
52
+    public String toString() {
53
+        return "Home{" +
54
+                "id=" + id +
55
+                ", address='" + address + '\'' +
56
+                ", homeNumber='" + homeNumber + '\'' +
57
+                '}';
58
+    }
59
+}

+ 102
- 0
src/main/java/io/zipcoder/persistenceapp/HomeAndPersonService.java View File

@@ -0,0 +1,102 @@
1
+package io.zipcoder.persistenceapp;
2
+
3
+import org.springframework.beans.factory.annotation.Autowired;
4
+import org.springframework.jdbc.core.JdbcTemplate;
5
+import org.springframework.stereotype.Service;
6
+
7
+import java.util.ArrayList;
8
+import java.util.List;
9
+import java.util.Map;
10
+
11
+@Service
12
+public class HomeAndPersonService {
13
+
14
+    /**
15
+     // Find a home by person id
16
+     // Generate a list of people that live in a home
17
+     // List all of the homes that have more that one person that live there. Group people by the home that they live in
18
+     // List all of the people that live a the address 11 Essex Dr.Farmingdale, NY 11735
19
+     // Create a query to update a person's home
20
+     // Create a query to update a person's 'HOMENUMBER"
21
+     * Find the home number of John Smith
22
+     */
23
+
24
+    @Autowired
25
+    PersonServiceClass personService;
26
+
27
+    @Autowired
28
+    HomeService homeService;
29
+
30
+    @Autowired
31
+    JdbcTemplate jdbcTemplate;
32
+
33
+    public Home getHomeByPersonId(int personId){
34
+        Person person = personService.findById(personId);
35
+        Home home = homeService.findHomeById(person.getHomeId());
36
+        return home;
37
+    }
38
+
39
+    public List<Person> findPersonListByHomeId(int homeId){
40
+        List<Person> people = personService.getAllPeople();
41
+        List<Person> peopleWithHomeId = new ArrayList<>();
42
+        for(Person person : people){
43
+            if(person.getHomeId() == homeId){
44
+                peopleWithHomeId.add(person);
45
+            }
46
+        }
47
+        return peopleWithHomeId;
48
+    }
49
+
50
+    public List<Home> groupPeopleByHomeId(){
51
+        List<Map<String, Object>> list = jdbcTemplate.queryForList("select count(home_id) as count, address, homenumber from person join home on person.home_id = home.id group by address having count (home_id) > 1");
52
+        List<Home> homes = new ArrayList<>();
53
+        for (Map<String, Object> row : list) {
54
+            Home home = new Home(String.valueOf(row.get("ADDRESS")), String.valueOf(row.get("HOMENUMBER")));
55
+            home.setId((Integer)row.get("ID"));
56
+            homes.add(home);
57
+        }
58
+        return homes;
59
+    }
60
+
61
+    public List<Person> findHomesAtAddress() {
62
+        List<Home> homes = homeService.getAllHomes();
63
+        List<Person> peopleAtHome = new ArrayList<>();
64
+        for (Home home : homes) {
65
+            if (home.getAddress().equals("11 Essex Dr.Farmingdale, NY 11735")) {
66
+                Person person = personService.findByHomeId(home.getId());
67
+                if (person != null) {
68
+                    peopleAtHome.add(person);
69
+                }
70
+            }
71
+        }
72
+        return peopleAtHome;
73
+    }
74
+
75
+    public int updatePersonsHome(Person p, Home h){
76
+        Person person = personService.findById(p.getId());
77
+        if(person != null){
78
+            return homeService.updateHome(h);
79
+        }
80
+        return -1;
81
+    }
82
+
83
+    public int updatePersonHomeNumber(Person p, int homeNumber) {
84
+        Person person = personService.findById(p.getId());
85
+        if (person != null) {
86
+            person.setHomeId(homeNumber);
87
+            return personService.updatePerson(person);
88
+        }
89
+        return -1;
90
+    }
91
+
92
+    public String findHomeNumberOfJohnSmith(){
93
+        List<Person> people = personService.getAllPeople();
94
+        for(Person person : people){
95
+            if(person.getFirstName().equals("John") && person.getLastName().equals("Smith")){
96
+                Home home = homeService.findHomeById(person.getId());
97
+                return home.getHomeNumber();
98
+            }
99
+        }
100
+        return null;
101
+    }
102
+}

+ 51
- 0
src/main/java/io/zipcoder/persistenceapp/HomeController.java View File

@@ -0,0 +1,51 @@
1
+package io.zipcoder.persistenceapp;
2
+
3
+import org.springframework.beans.factory.annotation.Autowired;
4
+import org.springframework.http.HttpStatus;
5
+import org.springframework.http.ResponseEntity;
6
+import org.springframework.web.bind.annotation.*;
7
+
8
+import java.util.List;
9
+
10
+public class HomeController {
11
+
12
+    @Autowired
13
+    HomeService serv;
14
+
15
+    @PostMapping("/home")
16
+    public Home createHome(Home h){
17
+        int id = serv.saveHome(h);
18
+        h.setId(id);
19
+        return h;
20
+    }
21
+
22
+    @PutMapping("/home/{id}")
23
+    public Home updateHome(@PathVariable int id, @RequestBody Home updatedHome){
24
+        Home p = serv.findHomeById(id);
25
+        updatedHome.setId(p.getId());
26
+        if(p != null) {
27
+            serv.updateHome(updatedHome);
28
+        } else {
29
+            new ResponseEntity(HttpStatus.NOT_FOUND);
30
+        }
31
+        return updatedHome;
32
+    }
33
+
34
+    @GetMapping("/home")
35
+    public List<Home> getAllPeople(){
36
+        return serv.getAllHomes();
37
+    }
38
+
39
+
40
+    @GetMapping("/home/{id}")
41
+    public Home getPersonById(@PathVariable int id){
42
+        return serv.findHomeById(id);
43
+    }
44
+
45
+    @DeleteMapping("/home/{id}")
46
+    public Person deletePersonById(@PathVariable int id){
47
+        Home home = serv.findHomeById(id);
48
+        serv.deleteHome(home);
49
+        return null;
50
+    }
51
+}

+ 113
- 0
src/main/java/io/zipcoder/persistenceapp/HomeService.java View File

@@ -0,0 +1,113 @@
1
+package io.zipcoder.persistenceapp;
2
+
3
+import org.springframework.beans.factory.annotation.Autowired;
4
+import org.springframework.jdbc.core.JdbcTemplate;
5
+import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
6
+import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
7
+import org.springframework.jdbc.core.namedparam.SqlParameterSource;
8
+import org.springframework.jdbc.support.GeneratedKeyHolder;
9
+import org.springframework.jdbc.support.KeyHolder;
10
+import org.springframework.stereotype.Service;
11
+
12
+import java.util.ArrayList;
13
+import java.util.List;
14
+import java.util.Map;
15
+
16
+@Service
17
+public class HomeService {
18
+
19
+    @Autowired
20
+    private JdbcTemplate jdbcTemplate;
21
+
22
+    @Autowired
23
+    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
24
+
25
+    /**
26
+    // Add a "Home" to the database
27
+    // Add a person to a home
28
+    // Update an existing 'Home' in the database
29
+    // Remove a home from the database
30
+    // Remove a list of homes from the database
31
+    // Find a home by id
32
+    // Find a home by home number
33
+    //Find a home by address
34
+
35
+    * Find a home by person id
36
+    * Generate a list of people that live in a home
37
+    */
38
+
39
+    public List<Home> getAllHomes(){
40
+            List<Map<String, Object>> list = jdbcTemplate.queryForList("SELECT * FROM HOME");
41
+            List<Home> homes = new ArrayList<>();
42
+            for (Map<String, Object> row : list) {
43
+                Home home = new Home(String.valueOf(row.get("ADDRESS")), String.valueOf(row.get("HOMENUMBER")));
44
+                home.setId((Integer)row.get("ID"));
45
+                homes.add(home);
46
+            }
47
+            return homes;
48
+    }
49
+
50
+    public int saveHome(Home h){
51
+        KeyHolder holder = new GeneratedKeyHolder();
52
+        SqlParameterSource parameters = new MapSqlParameterSource()
53
+                .addValue("address", h.getAddress())
54
+                .addValue("homenumber", h.getHomeNumber());
55
+
56
+        namedParameterJdbcTemplate.update("INSERT INTO HOME (ADDRESS, HOMENUMBER) VALUES (:address, :homenumber);", parameters, holder);
57
+        return holder.getKey().intValue();
58
+    }
59
+
60
+    public void addPersonToHome(Person p, int homeID){
61
+        Home home = findHomeById(homeID);
62
+        if(home != null){
63
+            p.setHomeId(homeID);
64
+        }
65
+    }
66
+
67
+    public Home findHomeById(int homeID){
68
+        List<Home> allHomes = getAllHomes();
69
+        for(Home home : allHomes){
70
+            if(home.getId().equals(homeID)){
71
+                return home;
72
+            }
73
+        }
74
+        return null;
75
+    }
76
+
77
+    public int updateHome(Home h){
78
+        return jdbcTemplate.update("UPDATE PERSON SET ADDRESS = '" +
79
+                h.getAddress() + "', HOMENUMBER = '" +
80
+                h.getHomeNumber() + "' where id = " + h.getId() + ";");
81
+    }
82
+
83
+    public int deleteHome(Home h){
84
+        return jdbcTemplate.update("DELETE FROM PERSON where ID = " + h.getId() + ";");
85
+    }
86
+
87
+    public void deleteAllHome(List<Home> homes){
88
+        for(Home home : homes){
89
+            deleteHome(home);
90
+        }
91
+    }
92
+
93
+    public Home findByHomeNumber(Integer homeNumber){
94
+        List<Home> homes = getAllHomes();
95
+        for(Home home : homes){
96
+            if(home.getHomeNumber().equals(homeNumber)){
97
+                return home;
98
+            }
99
+        }
100
+        return null;
101
+    }
102
+
103
+    public Home findByAddress(String address){
104
+        List<Home> homes = getAllHomes();
105
+        for(Home home : homes){
106
+            if(home.getAddress().equals(address)){
107
+                return home;
108
+            }
109
+        }
110
+        return null;
111
+    }
112
+}
113
+

+ 7
- 0
src/main/java/io/zipcoder/persistenceapp/JdbcPersonServiceImpl.java View File

@@ -0,0 +1,7 @@
1
+package io.zipcoder.persistenceapp;
2
+
3
+import org.springframework.stereotype.Service;
4
+
5
+@Service
6
+public class JdbcPersonServiceImpl {
7
+}

+ 92
- 0
src/main/java/io/zipcoder/persistenceapp/Person.java View File

@@ -0,0 +1,92 @@
1
+package io.zipcoder.persistenceapp;
2
+
3
+import javax.persistence.*;
4
+
5
+@Entity
6
+public class Person {
7
+
8
+    @Id
9
+    @GeneratedValue(strategy = GenerationType.IDENTITY)
10
+    private Integer id;
11
+    private String firstName;
12
+    private String lastName;
13
+    private String mobile;
14
+    private String birthday;
15
+    private Integer homeId;
16
+
17
+    public Person() {
18
+        this.firstName = "firstName";
19
+        this.lastName = "lastName";
20
+        this.mobile = "mobile";
21
+        this.birthday = "2000-01-01";
22
+        this.homeId = 0;
23
+    }
24
+
25
+    public Person(String firstName, String lastName, String mobile, String birthday, int homeId) {
26
+        this.firstName = firstName;
27
+        this.lastName = lastName;
28
+        this.mobile = mobile;
29
+        this.birthday = birthday;
30
+        this.homeId = homeId;
31
+    }
32
+
33
+    public int getId() {
34
+        return id;
35
+    }
36
+
37
+    public void setId(int id) {
38
+        this.id = id;
39
+    }
40
+
41
+    public String getFirstName() {
42
+        return firstName;
43
+    }
44
+
45
+    public void setFirstName(String firstName) {
46
+        this.firstName = firstName;
47
+    }
48
+
49
+    public String getLastName() {
50
+        return lastName;
51
+    }
52
+
53
+    public void setLastName(String lastName) {
54
+        this.lastName = lastName;
55
+    }
56
+
57
+    public String getMobile() {
58
+        return mobile;
59
+    }
60
+
61
+    public void setMobile(String mobile) {
62
+        this.mobile = mobile;
63
+    }
64
+
65
+    public String getBirthday() {
66
+        return birthday;
67
+    }
68
+
69
+    public void setBirthday(String birthday) {
70
+        this.birthday = birthday;
71
+    }
72
+
73
+    public int getHomeId() {
74
+        return homeId;
75
+    }
76
+
77
+    public void setHomeId(int homeId) {
78
+        this.homeId = homeId;
79
+    }
80
+
81
+    @Override
82
+    public String toString() {
83
+        return "Person{" +
84
+                "id=" + id +
85
+                ", firstName='" + firstName + '\'' +
86
+                ", lastName='" + lastName + '\'' +
87
+                ", mobile='" + mobile + '\'' +
88
+                ", birthday='" + birthday + '\'' +
89
+                ", homeId=" + homeId +
90
+                '}';
91
+    }
92
+}

+ 99
- 0
src/main/java/io/zipcoder/persistenceapp/PersonController.java View File

@@ -0,0 +1,99 @@
1
+package io.zipcoder.persistenceapp;
2
+
3
+import org.springframework.beans.factory.annotation.Autowired;
4
+import org.springframework.http.HttpStatus;
5
+import org.springframework.http.ResponseEntity;
6
+import org.springframework.web.bind.annotation.*;
7
+
8
+import java.util.ArrayList;
9
+import java.util.List;
10
+import java.util.Map;
11
+
12
+@RestController
13
+public class PersonController {
14
+
15
+    @Autowired
16
+    PersonServiceClass serv;
17
+
18
+    @PostMapping("/people")
19
+    public Person createPerson(@RequestBody Person person){
20
+        int id = serv.savePerson(person);
21
+        person.setId(id);
22
+        return person;
23
+    }
24
+
25
+    @PutMapping("/people/{id}")
26
+    public Person updatePerson(@PathVariable int id, @RequestBody Person updatedPerson){
27
+        Person p = serv.findById(id);
28
+        updatedPerson.setId(p.getId());
29
+        if(p != null) {
30
+            serv.updatePerson(updatedPerson);
31
+        } else {
32
+            new ResponseEntity(HttpStatus.NOT_FOUND);
33
+        }
34
+        return updatedPerson;
35
+    }
36
+
37
+    @GetMapping("/people")
38
+    public List<Person> getAllPeople(){
39
+        return serv.getAllPeople();
40
+    }
41
+
42
+    @GetMapping("/people/{id}")
43
+    public Person getPersonById(@PathVariable int id){
44
+        return serv.findById(id);
45
+    }
46
+
47
+    @DeleteMapping("/people/{id}")
48
+    public Person deletePersonById(@PathVariable int id){
49
+        Person person = serv.findById(id);
50
+        serv.deletePerson(person);
51
+        return null;
52
+    }
53
+
54
+    @GetMapping("/people/reverselookup/{mobileNumber}")
55
+    public List<Person> getPeopleByNumber(@PathVariable String mobileNumber){
56
+        List<Person> people = serv.getAllPeople();
57
+        List<Person> peopleWithNumber = new ArrayList<>();
58
+        for(Person person : people){
59
+            if(person.getMobile().equals(mobileNumber)){
60
+                peopleWithNumber.add(person);
61
+            }
62
+        }
63
+       return peopleWithNumber;
64
+    }
65
+
66
+    @GetMapping("/people/surname/{lastName}")
67
+    public List<Person> getAllByLastName(@PathVariable String lastName){
68
+        Map<String, List<Person>> map = serv.surnameQuery();
69
+        for(String key : map.keySet()){
70
+            if(key.equals(lastName)){
71
+                return map.get(key);
72
+            }
73
+        }
74
+        return null;
75
+    }
76
+
77
+    @GetMapping("/people/surname")
78
+    public String getSurnameReport(){
79
+        String report = "";
80
+        Map<String, List<Person>> map = serv.surnameQuery();
81
+        for(String key : map.keySet()){
82
+            report += key + ": \n";
83
+            for(Person person : map.get(key)){
84
+                report += person.toString() + "\n";
85
+            }
86
+        }
87
+        return report;
88
+    }
89
+
90
+    @GetMapping("/people/firstname/stats")
91
+    public String getFirstNameCount(){
92
+        String report = "";
93
+        Map<String, Integer> map = serv.firstNameCount();
94
+        for(String name : map.keySet()){
95
+            report += name + ", amount: " + map.get(name);
96
+        }
97
+        return report;
98
+    }
99
+}

+ 6
- 0
src/main/java/io/zipcoder/persistenceapp/PersonRepository.java View File

@@ -0,0 +1,6 @@
1
+package io.zipcoder.persistenceapp;
2
+
3
+import org.springframework.data.jpa.repository.JpaRepository;
4
+
5
+public interface PersonRepository extends JpaRepository<Person, Integer> {
6
+}

+ 4
- 0
src/main/java/io/zipcoder/persistenceapp/PersonService.java View File

@@ -0,0 +1,4 @@
1
+package io.zipcoder.persistenceapp;
2
+
3
+public interface PersonService {
4
+}

+ 123
- 0
src/main/java/io/zipcoder/persistenceapp/PersonServiceClass.java View File

@@ -0,0 +1,123 @@
1
+package io.zipcoder.persistenceapp;
2
+
3
+import org.springframework.beans.factory.annotation.Autowired;
4
+import org.springframework.jdbc.core.JdbcTemplate;
5
+import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
6
+import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
7
+import org.springframework.jdbc.core.namedparam.SqlParameterSource;
8
+import org.springframework.jdbc.support.GeneratedKeyHolder;
9
+import org.springframework.jdbc.support.KeyHolder;
10
+import org.springframework.stereotype.Service;
11
+
12
+import java.util.ArrayList;
13
+import java.util.HashMap;
14
+import java.util.List;
15
+import java.util.Map;
16
+
17
+@Service
18
+public class PersonServiceClass {
19
+
20
+    @Autowired
21
+    private JdbcTemplate jdbcTemplate;
22
+
23
+    @Autowired
24
+    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
25
+
26
+
27
+    public List<Person> getAllPeople(){
28
+        List<Map<String, Object>> list = jdbcTemplate.queryForList("SELECT * FROM PERSON");
29
+        List<Person> people = new ArrayList<>();
30
+        for (Map<String, Object> row : list) {
31
+            Person person = new Person(String.valueOf(row.get("FIRST_NAME")), String.valueOf(row.get("LAST_NAME")), String.valueOf(row.get("MOBILE")), row.get("BIRTHDAY").toString(), (Short)row.get("HOME_ID"));
32
+            person.setId((Integer)row.get("ID"));
33
+            people.add(person);
34
+        }
35
+        return people;
36
+    }
37
+
38
+    public int savePerson(Person e){
39
+        System.out.println(e.toString());
40
+        KeyHolder holder = new GeneratedKeyHolder();
41
+        SqlParameterSource parameters = new MapSqlParameterSource()
42
+                .addValue("first_name", e.getFirstName())
43
+                .addValue("last_name", e.getLastName())
44
+                .addValue("mobile", e.getMobile())
45
+                .addValue("birthday", e.getBirthday())
46
+                .addValue("home_id", e.getHomeId());
47
+
48
+        namedParameterJdbcTemplate.update("INSERT INTO PERSON (FIRST_NAME, LAST_NAME, MOBILE, BIRTHDAY, HOME_ID) VALUES (:first_name, :last_name, :mobile, :birthday, :home_id);", parameters, holder);
49
+        return holder.getKey().intValue();
50
+    }
51
+
52
+    public int updatePerson(Person e){
53
+        return jdbcTemplate.update("UPDATE PERSON SET FIRST_NAME = '" +
54
+                e.getFirstName() + "', LAST_NAME = '" +
55
+                e.getLastName() + "', MOBILE = '" +
56
+                e.getMobile() + "', BIRTHDAY = '" +
57
+                e.getBirthday() + "', HOME_ID = " +
58
+                e.getHomeId() + " where id = " + e.getId() + ";");
59
+    }
60
+
61
+    public int deletePerson(Person e){
62
+        return jdbcTemplate.update("DELETE FROM PERSON where ID = " + e.getId() + ";");
63
+    }
64
+
65
+    public void removeList(Person... e){
66
+        for(Person person : e){
67
+            deletePerson(person);
68
+        }
69
+    }
70
+
71
+    public List<Person> findAllByFirstName(String firstName, Class objectType){
72
+        return jdbcTemplate.queryForList("SELECT * FROM PERSON WHERE FIRST_NAME = '" + firstName + "';", Person.class);
73
+    }
74
+
75
+    public Person findById(int ID){
76
+        List<Person> people = getAllPeople();
77
+        for(Person person : people){
78
+            if(person.getId() == ID){
79
+                return person;
80
+            }
81
+        }
82
+        return null;
83
+    }
84
+
85
+    public Person findByHomeId(int ID){
86
+        List<Person> people = getAllPeople();
87
+        for(Person person : people){
88
+            if(person.getHomeId() == ID){
89
+                return person;
90
+            }
91
+        }
92
+        return null;
93
+    }
94
+
95
+    public Map<String, List<Person>> surnameQuery(){
96
+        List<Person> people = getAllPeople();
97
+        Map<String, List<Person>> map = new HashMap<>();
98
+        for(Person person : people){
99
+            if(map.containsKey(person.getLastName())){
100
+                map.get(person.getLastName()).add(person);
101
+            } else {
102
+                map.put(person.getLastName(), new ArrayList<Person>());
103
+                map.get(person.getLastName()).add(person);
104
+            }
105
+        }
106
+        return map;
107
+    }
108
+
109
+    public Map<String, Integer> firstNameCount(){
110
+        List<Person> people = getAllPeople();
111
+        Map<String, Integer> map = new HashMap<>();
112
+        for(Person person : people){
113
+            if(map.containsKey(person.getFirstName())){
114
+                int amount = map.get(person.getFirstName());
115
+                amount = amount + 1;
116
+                map.put(person.getFirstName(), amount);
117
+            } else {
118
+                map.put(person.getFirstName(), 1);
119
+            }
120
+        }
121
+        return map;
122
+    }
123
+}

+ 26
- 0
src/main/resources/Qs View File

@@ -0,0 +1,26 @@
1
+INSERT INTO MOVIES (Title, Runtime, Genre, IMDB_Score, Rating) VALUES ('Howard the Duck',	110,	'Sci-Fi',	4.6,	'PG');
2
+INSERT INTO MOVIES (Title, Runtime, Genre, IMDB_Score, Rating) VALUES ('Lavalantula',	83,	'Horror',	4.7,	'TV-14');
3
+INSERT INTO MOVIES (Title, Runtime, Genre, IMDB_Score, Rating) VALUES ('Starship Troopers',	129,	'Sci-Fi',	7.2,	'PG-13');
4
+INSERT INTO MOVIES (Title, Runtime, Genre, IMDB_Score, Rating) VALUES ('Waltz With Bashir',	90,	'Documentary',	8.0,	'R');
5
+INSERT INTO MOVIES (Title, Runtime, Genre, IMDB_Score, Rating) VALUES ('Spaceballs',	96,	'Comedy',	7.1,	'PG');
6
+INSERT INTO MOVIES (Title, Runtime, Genre, IMDB_Score, Rating) VALUES ('Monsters Inc.',	92,	'Animation',	8.1,	'G');
7
+INSERT INTO MOVIES (Title, Runtime, Genre, IMDB_Score, Rating) VALUES ('Lords of Dogtown',	107,	'Sport',	7.1,	'PG-13');
8
+INSERT INTO MOVIES (Title, Runtime, Genre, IMDB_Score, Rating) VALUES ('A Beautiful Mind',	135,	'Drama',	8.2,	'PG-13');
9
+
10
+select * from movies where genre = 'Sci-Fi'
11
+
12
+select * from movies where IMDB_SCORE > 6.5
13
+
14
+select * from movies where rating = 'G' or rating = 'PG' and RUNTIME < 100
15
+
16
+select AVG(RUNTIME) from movies where IMDB_SCORE < 7.5 group by genre
17
+
18
+update movies set rating = 'R' where title = 'Starship Troopers'
19
+
20
+select rating, id from movies where genre = 'Horror' or genre = 'Documentary' order by rating
21
+
22
+select avg(IMDB_SCORE), rating from movies group by rating
23
+
24
+select rating from movies group by rating HAVING COUNT(*) > 1
25
+
26
+delete from movies where rating = 'R'

+ 7
- 2
src/main/resources/application-h2.properties View File

@@ -1,4 +1,9 @@
1
-spring.datasource.url=jdbc:h2:mem:testdb;Mode=Oracle
1
+spring.profiles.active=h2
2
+logging.level.org.springframework.boot.context.embedded=INFO
3
+spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect
4
+
5
+spring.datasource.url=jdbc:h2:mem:test;Mode=Oracle
2 6
 spring.datasource.platform=h2
3 7
 spring.jpa.hibernate.ddl-auto=none
4
-spring.datasource.continue-on-error=true
8
+spring.datasource.continue-on-error=true
9
+spring.h2.console.enabled=true

+ 2
- 1
src/main/resources/application.properties View File

@@ -1,3 +1,4 @@
1 1
 spring.profiles.active=h2
2 2
 logging.level.org.springframework.boot.context.embedded=INFO
3
-spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect
3
+spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect
4
+