4 Commits

Auteur SHA1 Bericht Datum
  mpierse 82b89069c3 completed person service and controller 5 jaren geleden
  mpierse 2f403e8662 started service layer 5 jaren geleden
  mpierse 19d9ad9ab9 person 5 jaren geleden
  mpierse 0d5baffff4 part one completed 5 jaren geleden

+ 75
- 0
src/main/java/io/zipcoder/persistenceapp/Person.java Bestand weergeven

@@ -0,0 +1,75 @@
1
+package io.zipcoder.persistenceapp;
2
+
3
+import javax.persistence.criteria.CriteriaBuilder;
4
+import java.util.Date;
5
+
6
+public class Person {
7
+
8
+    private Long id;
9
+    private String firstName;
10
+    private String lastName;
11
+    private String mobile;
12
+    private String birthDate;
13
+    private Short homeId;
14
+
15
+    public Person(){
16
+
17
+    }
18
+
19
+    public Person(Long id, String firstName, String lastName, String mobile, String date, Short homeId) {
20
+        this.id=id;
21
+        this.firstName = firstName;
22
+        this.lastName = lastName;
23
+        this.mobile=mobile;
24
+        this.birthDate=date;
25
+        this.homeId = homeId;
26
+    }
27
+
28
+    public String getMobile() {
29
+        return mobile;
30
+    }
31
+
32
+    public void setMobile(String mobile) {
33
+        this.mobile = mobile;
34
+    }
35
+
36
+    public Short getHomeId() {
37
+        return homeId;
38
+    }
39
+
40
+    public void setHomeId(Short homeId) {
41
+        this.homeId = homeId;
42
+    }
43
+
44
+    public Long getId() {
45
+        return id;
46
+    }
47
+
48
+    public void setId(Long id) {
49
+        this.id = id;
50
+    }
51
+
52
+    public String getFirstName() {
53
+        return firstName;
54
+    }
55
+
56
+    public void setFirstName(String firstName) {
57
+        this.firstName = firstName;
58
+    }
59
+
60
+    public String getLastName() {
61
+        return lastName;
62
+    }
63
+
64
+    public void setLastName(String lastName) {
65
+        this.lastName = lastName;
66
+    }
67
+
68
+    public String getBirthDate() {
69
+        return birthDate;
70
+    }
71
+
72
+    public void setBirthDate(String birthDate) {
73
+        this.birthDate = birthDate;
74
+    }
75
+}

+ 59
- 0
src/main/java/io/zipcoder/persistenceapp/PersonController.java Bestand weergeven

@@ -0,0 +1,59 @@
1
+package io.zipcoder.persistenceapp;
2
+
3
+import org.springframework.beans.factory.annotation.Autowired;
4
+import org.springframework.jdbc.core.JdbcOperations;
5
+import org.springframework.jdbc.core.RowMapper;
6
+import org.springframework.web.bind.annotation.PathVariable;
7
+import org.springframework.web.bind.annotation.RequestMapping;
8
+import org.springframework.web.bind.annotation.RequestMethod;
9
+import org.springframework.web.bind.annotation.RestController;
10
+
11
+import java.sql.ResultSet;
12
+import java.sql.SQLException;
13
+import java.util.Date;
14
+import java.util.List;
15
+
16
+@RestController
17
+public class PersonController {
18
+
19
+    @Autowired
20
+    private PersonService personService;
21
+
22
+    public PersonController() {
23
+    }
24
+
25
+    @RequestMapping(value = "/people", method = RequestMethod.GET)
26
+    public List<Person> getAllPeople(){
27
+        return personService.findAll();
28
+    }
29
+
30
+    @RequestMapping(value = "/people/{id}/{firstName}/{lastname}/{mobile}/{birthday}/{homeId}", method = RequestMethod.POST)
31
+    public Person createPerson(@PathVariable Long id, @PathVariable String firstName, @PathVariable String lastname, @PathVariable String mobile, @PathVariable String birthday, @PathVariable Short homeId) {
32
+        return personService.createPerson(id, firstName, lastname, mobile, birthday, homeId);
33
+    }
34
+
35
+    @RequestMapping(value = "/people/{id}/{firstName}/{lastname}/{mobile}/{birthday}/{homeId}", method = RequestMethod.PUT)
36
+    public Person updatePerson(@PathVariable Long id, @PathVariable String firstName, @PathVariable String lastname, @PathVariable String mobile, @PathVariable String birthday, @PathVariable Short homeId) {
37
+        return personService.updatePerson(id, firstName, lastname, mobile, birthday, homeId);
38
+    }
39
+
40
+    @RequestMapping(value = "/people/{id}", method = RequestMethod.GET)
41
+    public Person getPerson(@PathVariable Long id){
42
+        return personService.getPerson(id);
43
+    }
44
+
45
+    @RequestMapping(value = "/people/{lastName}", method = RequestMethod.GET)
46
+    public Person getPersonByLastname(@PathVariable String lastName){
47
+        return personService.getPerson(lastName);
48
+    }
49
+
50
+    @RequestMapping(value = "/people/{mobile}", method = RequestMethod.GET)
51
+    public Person getReverseLookup(@PathVariable String mobile){
52
+        return personService.getReverseLookup(mobile);
53
+    }
54
+
55
+    @RequestMapping(value = "/people/firstname/stats/{firstName}", method = RequestMethod.GET)
56
+    public int getFirstNameStats(@PathVariable String firstName){
57
+        return personService.getFirstnameStats(firstName);
58
+    }
59
+}

+ 86
- 0
src/main/java/io/zipcoder/persistenceapp/PersonService.java Bestand weergeven

@@ -0,0 +1,86 @@
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.RowMapper;
6
+import org.springframework.stereotype.Service;
7
+
8
+import java.text.DateFormat;
9
+import java.util.ArrayList;
10
+import java.util.Date;
11
+import java.util.List;
12
+import java.util.Map;
13
+
14
+@Service
15
+public class PersonService {
16
+
17
+    @Autowired
18
+    private JdbcTemplate jdbcTemplate;
19
+
20
+    public List<Person> findAll() {
21
+        String sql = "SELECT * FROM Person";
22
+        List<Person> people = new ArrayList<>();
23
+        List<Map<String, Object>> rows = jdbcTemplate.queryForList(sql);
24
+        for (Map row : rows) {
25
+            Person p = new Person();
26
+            p.setId(Long.parseLong(String.valueOf(row.get("id"))));
27
+            p.setFirstName((String) row.get("first_name"));
28
+            p.setLastName((String) row.get("last_name"));
29
+            p.setMobile((String) row.get("mobile"));
30
+            p.setBirthDate(String.valueOf(row.get("birthday")));
31
+            p.setHomeId(Short.parseShort(String.valueOf(row.get("home_id"))));
32
+            people.add(p);
33
+        }
34
+        return people;
35
+    }
36
+
37
+    public Person createPerson(Long id, String firstName, String lastName, String mobile, String birthdate, Short homeId){
38
+    this.jdbcTemplate.update("insert into person (id, first_name, last_name, mobile, birthday, home_id) values (?, ?, ?, ?, ?, ?)",
39
+            new Object[] {id, firstName, lastName, mobile, birthdate, homeId});
40
+    return new Person(id, firstName,lastName, mobile, birthdate, homeId);
41
+    }
42
+
43
+    public Person updatePerson(Long id, String firstName, String lastName, String mobile, String birthdate, Short homeId){
44
+         this.jdbcTemplate.update("update person set first_name=?, last_name=?, mobile=?, birthday=?, home_id=? where id= ?",
45
+                new Object[] {firstName, lastName, mobile, birthdate, homeId, id});
46
+        return new Person(id, firstName,lastName, mobile, birthdate, homeId);
47
+    }
48
+
49
+    public Person getPerson(Long id){
50
+        List<Person> personList = findAll();
51
+        for (Person p : personList) {
52
+            if(p.getId()==id)
53
+                return p;
54
+        }
55
+        return null;
56
+    }
57
+
58
+    public Person getPerson(String lastname){
59
+        List<Person> personList = findAll();
60
+        for (Person p : personList) {
61
+            if(p.getLastName()==lastname)
62
+                return p;
63
+        }
64
+        return null;
65
+    }
66
+
67
+    public Person getReverseLookup(String mobile){
68
+        List<Person> personList = findAll();
69
+        for (Person p : personList) {
70
+            if(p.getMobile()==mobile)
71
+                return p;
72
+        }
73
+        return null;
74
+    }
75
+
76
+    public int getFirstnameStats(String firstname){
77
+        List<Person> personList = findAll();
78
+        int stats = 0;
79
+        for (Person p : personList) {
80
+            if(p.getFirstName()==firstname)
81
+                stats++;
82
+        }
83
+        return stats;
84
+    }
85
+
86
+}

+ 1
- 1
src/main/resources/application-h2.properties Bestand weergeven

@@ -1,4 +1,4 @@
1
-spring.datasource.url=jdbc:h2:mem:testdb;Mode=Oracle
1
+spring.datasource.url=jdbc:h2:mem:test;Mode=Oracle
2 2
 spring.datasource.platform=h2
3 3
 spring.jpa.hibernate.ddl-auto=none
4 4
 spring.datasource.continue-on-error=true

+ 60
- 0
src/main/resources/script.sql Bestand weergeven

@@ -0,0 +1,60 @@
1
+CREATE TABLE MOVIE(ID INT PRIMARY KEY, TITLE VARCHAR(255),
2
+RUNTIME INT, GENRE VARCHAR(255), IMDB_SCORE DOUBLE, RATING VARCHAR(255));
3
+
4
+INSERT INTO MOVIE
5
+VALUES (1, 'Howard the Duck', 110, 'Sci-Fi', 4.6, 'PG');
6
+
7
+INSERT INTO MOVIE
8
+VALUES (2, 'Lavalantula', 83, 'Horror', 4.7, 'TV-14');
9
+
10
+INSERT INTO MOVIE
11
+VALUES (3, 'Starship Troopers', 129, 'Sci-Fi', 7.2, 'PG-13');
12
+
13
+INSERT INTO MOVIE
14
+VALUES (4, 'Waltz With Bashir', 90	, 'Documentary', 8.0, 'R');
15
+
16
+INSERT INTO MOVIE
17
+VALUES (5, 'Spaceballs', 96, 'Comedy', 7.1, 'PG');
18
+
19
+INSERT INTO MOVIE
20
+VALUES (6, 'Monsters Inc.', 92, 'Animation', 8.1, 'G');
21
+
22
+INSERT INTO MOVIE
23
+VALUES (7, 'The Princess Bride.', 100, 'Awesome', 10.0, 'PG');
24
+
25
+INSERT INTO MOVIE
26
+VALUES (8, 'Elf', 95, 'Christmas', 9.5, 'PG');
27
+
28
+Select * FROM MOVIE
29
+Where GENRE = 'Sci-Fi';
30
+
31
+Select * FROM MOVIE
32
+Where IMDB_SCORE > 6.5;
33
+
34
+Select * FROM MOVIE
35
+Where RUNTIME < 100
36
+AND RATING = 'PG'
37
+OR RATING = 'G';
38
+
39
+Select GENRE, AVG(RUNTIME) FROM MOVIE
40
+GROUP BY GENRE;
41
+
42
+UPDATE MOVIE
43
+SET Rating = 'R'
44
+WHERE TITLE = 'Starship Troopers';
45
+
46
+SELECT ID FROM MOVIE
47
+WHERE GENRE = 'Horror'
48
+OR GENRE = 'Documentary';
49
+
50
+Select RATING, AVG(IMDB_SCORE), MAX(IMDB_SCORE), MIN(IMDB_SCORE)
51
+FROM MOVIE
52
+GROUP BY RATING;
53
+
54
+Select RATING, AVG(IMDB_SCORE), MAX(IMDB_SCORE), MIN(IMDB_SCORE)
55
+FROM MOVIE
56
+GROUP BY RATING
57
+HAVING COUNT(*) >1;
58
+
59
+DELETE FROM MOVIE
60
+WHERE RATING = 'R';