#4 nedredmond

Öppen
nedredmond vill sammanfoga 1 incheckningar från s[2]s in i master

+ 46
- 0
src/main/java/io/zipcoder/persistenceapp/Home.java Visa fil

@@ -0,0 +1,46 @@
1
+package io.zipcoder.persistenceapp;
2
+
3
+import javax.persistence.GeneratedValue;
4
+import javax.persistence.GenerationType;
5
+import javax.persistence.Id;
6
+
7
+public class Home {
8
+
9
+    @Id
10
+    @GeneratedValue(strategy = GenerationType.AUTO)
11
+    private Integer id;
12
+    private String address;
13
+    private String homeNumber;
14
+
15
+    public Home(String address, String homeNumber) {
16
+        this.address = address;
17
+        this.homeNumber = homeNumber;
18
+    }
19
+
20
+    public Home() {
21
+    }
22
+
23
+    public Integer getId() {
24
+        return id;
25
+    }
26
+
27
+    public void setId(Integer id) {
28
+        this.id = id;
29
+    }
30
+
31
+    public String getAddress() {
32
+        return address;
33
+    }
34
+
35
+    public void setAddress(String address) {
36
+        this.address = address;
37
+    }
38
+
39
+    public String getHomeNumber() {
40
+        return homeNumber;
41
+    }
42
+
43
+    public void setHomeNumber(String homeNumber) {
44
+        this.homeNumber = homeNumber;
45
+    }
46
+}

+ 45
- 0
src/main/java/io/zipcoder/persistenceapp/HomeController.java Visa fil

@@ -0,0 +1,45 @@
1
+package io.zipcoder.persistenceapp;
2
+
3
+import org.springframework.beans.factory.annotation.Autowired;
4
+import org.springframework.web.bind.annotation.*;
5
+
6
+import java.util.List;
7
+
8
+@RestController
9
+public class HomeController {
10
+
11
+    final HomeService homeService;
12
+
13
+    @Autowired
14
+    public HomeController(HomeService homeService) { this.homeService = homeService; }
15
+
16
+    @PostMapping("/home/{address}/{homeNumber}")
17
+    Home createHome(@PathVariable String address,
18
+                    @PathVariable String homeNumber) {
19
+        Home home = new Home(address, homeNumber);
20
+        homeService.addHome(home);
21
+        return home;
22
+    }
23
+
24
+    @GetMapping("/home")
25
+    List<Home> getHomes() {
26
+        return homeService.getAllHomes();
27
+    }
28
+
29
+    @GetMapping("/home/{id}")
30
+    Home getHomeById(@PathVariable Integer id) {
31
+        return homeService.getHomeById(id);
32
+    }
33
+
34
+    @PutMapping("/home/{id}/{address}/{homeNumber}")
35
+    Home updateHome(@ModelAttribute("home") Home h) {
36
+        homeService.updateHome(h);
37
+        return h;
38
+    }
39
+
40
+    @DeleteMapping("/home/{id}")
41
+    void deleteHome(@PathVariable Integer id) {
42
+        homeService.deleteHome(homeService.getHomeById(id));
43
+    }
44
+
45
+}

+ 17
- 0
src/main/java/io/zipcoder/persistenceapp/HomeMapper.java Visa fil

@@ -0,0 +1,17 @@
1
+package io.zipcoder.persistenceapp;
2
+
3
+import org.springframework.jdbc.core.RowMapper;
4
+
5
+import java.sql.ResultSet;
6
+import java.sql.SQLException;
7
+
8
+public class HomeMapper implements RowMapper<Home> {
9
+    @Override
10
+    public Home mapRow(ResultSet resultSet, int i) throws SQLException {
11
+        Home home = new Home();
12
+        home.setId(resultSet.getInt("id"));
13
+        home.setAddress(resultSet.getString("address"));
14
+        home.setHomeNumber(resultSet.getString("homenumber"));
15
+        return home;
16
+    }
17
+}

+ 41
- 0
src/main/java/io/zipcoder/persistenceapp/HomeService.java Visa fil

@@ -0,0 +1,41 @@
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.List;
8
+
9
+@Service
10
+public class HomeService {
11
+
12
+    @Autowired
13
+    JdbcTemplate jdbcTemplate;
14
+
15
+    private final String SQL_FIND_HOME = "select * from home where id = ?";
16
+    private final String SQL_DELETE_HOME = "delete from home where id = ?";
17
+    private final String SQL_UPDATE_HOME = "update home set address = ?, homenumber = ?, where id = ?";
18
+    private final String SQL_GET_ALL = "select * from home";
19
+    private final String SQL_INSERT_HOME = "insert into home(address, homenumber) values(?,?)";
20
+
21
+    public Home getHomeById(Integer id) {
22
+        return jdbcTemplate.queryForObject(SQL_FIND_HOME, new Object[] { id }, new HomeMapper());
23
+    }
24
+
25
+    public List<Home> getAllHomes() {
26
+        return jdbcTemplate.query(SQL_GET_ALL, new HomeMapper());
27
+    }
28
+
29
+    public boolean deleteHome(Home home) {
30
+        return jdbcTemplate.update(SQL_DELETE_HOME, home.getId()) > 0;
31
+    }
32
+
33
+    public boolean updateHome(Home home) {
34
+        return jdbcTemplate.update(SQL_UPDATE_HOME, home.getAddress(), home.getHomeNumber()) > 0;
35
+    }
36
+
37
+    public boolean addHome(Home home) {
38
+        return jdbcTemplate.update(SQL_INSERT_HOME, home.getAddress(), home.getHomeNumber()) > 0;
39
+    }
40
+
41
+}

+ 81
- 0
src/main/java/io/zipcoder/persistenceapp/Person.java Visa fil

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

+ 53
- 0
src/main/java/io/zipcoder/persistenceapp/PersonController.java Visa fil

@@ -0,0 +1,53 @@
1
+package io.zipcoder.persistenceapp;
2
+
3
+import io.zipcoder.persistenceapp.jdbc.JdbcPersonServiceImpl;
4
+import org.springframework.beans.factory.annotation.Autowired;
5
+import org.springframework.format.annotation.DateTimeFormat;
6
+import org.springframework.web.bind.annotation.*;
7
+
8
+import java.util.Date;
9
+import java.util.List;
10
+
11
+@RestController
12
+public class PersonController {
13
+
14
+    final JdbcPersonServiceImpl jdbcPersonServiceImpl;
15
+
16
+    @Autowired
17
+    public PersonController(JdbcPersonServiceImpl jdbcPersonServiceImpl) { this.jdbcPersonServiceImpl = jdbcPersonServiceImpl; }
18
+
19
+    @PostMapping("/people/{firstName}/{lastName}/{birthday}/{mobile}")
20
+    Person createPerson(@PathVariable String firstName, @PathVariable String lastName, @PathVariable @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date birthday, @PathVariable Long mobile) {
21
+        Person person = new Person(firstName, lastName, birthday, mobile);
22
+        jdbcPersonServiceImpl.addPerson(person);
23
+        return person;
24
+    }
25
+
26
+    @GetMapping("/people/{id}")
27
+    Person getPerson(@PathVariable Integer id) {
28
+        return jdbcPersonServiceImpl.getPersonById(id);
29
+    }
30
+
31
+    @GetMapping("/people")
32
+    List<Person> getPersonList() {
33
+        return jdbcPersonServiceImpl.getAllPersons();
34
+    }
35
+
36
+    @PutMapping("/people/{id}/{firstName}/{lastName}/{birthday}/{mobile}")
37
+    Person updatePerson(@PathVariable Integer id, @PathVariable String firstName, @PathVariable String lastName, @PathVariable @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date birthday, @PathVariable Long mobile) {
38
+        jdbcPersonServiceImpl.updatePerson(id, firstName, lastName, birthday, mobile);
39
+        return jdbcPersonServiceImpl.getPersonById(id);
40
+    }
41
+
42
+    @DeleteMapping("/people/{id}")
43
+    void deletePerson(@PathVariable Integer id) {
44
+        jdbcPersonServiceImpl.deletePerson(jdbcPersonServiceImpl.getPersonById(id));
45
+    }
46
+
47
+    @PutMapping("/people/{id}/{homeId}")
48
+    Person assignHome(@PathVariable Integer id, @PathVariable Integer homeId) {
49
+        jdbcPersonServiceImpl.assignHome(id, homeId);
50
+        return jdbcPersonServiceImpl.getPersonById(id);
51
+    }
52
+
53
+}

+ 22
- 0
src/main/java/io/zipcoder/persistenceapp/PersonMapper.java Visa fil

@@ -0,0 +1,22 @@
1
+package io.zipcoder.persistenceapp;
2
+
3
+import java.sql.ResultSet;
4
+import java.sql.SQLException;
5
+
6
+import org.springframework.jdbc.core.RowMapper;
7
+
8
+public class PersonMapper implements RowMapper<Person> {
9
+
10
+    public Person mapRow(ResultSet resultSet, int i) throws SQLException {
11
+
12
+        Person person = new Person();
13
+        person.setId(resultSet.getInt("id"));
14
+        person.setFirstName(resultSet.getString("first_name"));
15
+        person.setLastName(resultSet.getString("last_name"));
16
+        person.setBirthdate(resultSet.getDate("birthday"));
17
+        person.setMobile(resultSet.getLong("mobile"));
18
+        person.setHomeId(resultSet.getInt("home_id"));
19
+        return person;
20
+    }
21
+}
22
+

+ 4
- 0
src/main/java/io/zipcoder/persistenceapp/PersonService.java Visa fil

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

+ 51
- 0
src/main/java/io/zipcoder/persistenceapp/jdbc/JdbcPersonServiceImpl.java Visa fil

@@ -0,0 +1,51 @@
1
+package io.zipcoder.persistenceapp.jdbc;
2
+
3
+import io.zipcoder.persistenceapp.Person;
4
+import io.zipcoder.persistenceapp.PersonMapper;
5
+import io.zipcoder.persistenceapp.PersonService;
6
+import org.springframework.beans.factory.annotation.Autowired;
7
+import org.springframework.jdbc.core.JdbcTemplate;
8
+import org.springframework.stereotype.Service;
9
+
10
+import java.util.Date;
11
+import java.util.List;
12
+
13
+@Service
14
+public class JdbcPersonServiceImpl implements PersonService {
15
+
16
+    @Autowired
17
+    JdbcTemplate jdbcTemplate;
18
+
19
+    private final String SQL_FIND_PERSON = "select * from people where id = ?";
20
+    private final String SQL_DELETE_PERSON = "delete from people where id = ?";
21
+    private final String SQL_UPDATE_PERSON = "update people set first_name = ?, last_name = ?, mobile  = ?, birthday = ? where id = ?";
22
+    private final String SQL_GET_ALL = "select * from people";
23
+    private final String SQL_INSERT_PERSON = "insert into people(first_name, last_name, mobile, birthday) values(?,?,?,?)";
24
+    private final String SQL_ASSIGN_HOME = "update people set home_id = ? where id = ?";
25
+
26
+
27
+    public Person getPersonById(Integer id) {
28
+        return jdbcTemplate.queryForObject(SQL_FIND_PERSON, new Object[] { id }, new PersonMapper());
29
+    }
30
+
31
+    public List<Person> getAllPersons() {
32
+        return jdbcTemplate.query(SQL_GET_ALL, new PersonMapper());
33
+    }
34
+
35
+    public boolean deletePerson(Person person) {
36
+        return jdbcTemplate.update(SQL_DELETE_PERSON, person.getId()) > 0;
37
+    }
38
+
39
+    public boolean updatePerson(Integer id, String first_name, String last_name, Date birthday, Long mobile) {
40
+        return jdbcTemplate.update(SQL_UPDATE_PERSON, first_name, last_name, birthday, mobile, id) > 0;
41
+    }
42
+
43
+    public boolean addPerson(Person person) {
44
+        return jdbcTemplate.update(SQL_INSERT_PERSON, person.getFirstName(), person.getLastName(), person.getMobile(), person.getBirthdate()) > 0;
45
+    }
46
+
47
+    public boolean assignHome(Integer id, Integer home_id) {
48
+        return jdbcTemplate.update(SQL_ASSIGN_HOME, home_id, id) > 0;
49
+    }
50
+
51
+}

+ 65
- 0
src/main/java/io/zipcoder/persistenceapp/jpa/JpaPersonServiceImpl.java Visa fil

@@ -0,0 +1,65 @@
1
+package io.zipcoder.persistenceapp.jpa;
2
+
3
+import io.zipcoder.persistenceapp.HomeController;
4
+import io.zipcoder.persistenceapp.Person;
5
+import org.springframework.beans.factory.annotation.Autowired;
6
+import org.springframework.context.annotation.Primary;
7
+import org.springframework.stereotype.Service;
8
+
9
+import java.util.Date;
10
+import java.util.List;
11
+
12
+@Service
13
+@Primary
14
+public class JpaPersonServiceImpl {
15
+
16
+    private
17
+    PersonRepository personRepository;
18
+
19
+    @Autowired
20
+    public JpaPersonServiceImpl(PersonRepository personRepository) {
21
+        this.personRepository = personRepository;
22
+    }
23
+
24
+    public Person getPersonById(Integer id) {
25
+        return personRepository.findOne(id);
26
+    }
27
+
28
+    public List<Person> getAllPersons() {
29
+        return personRepository.findAll();
30
+    }
31
+
32
+    public boolean deletePerson(Person person) {
33
+        if (!personRepository.exists(person.getId())) return false;
34
+        else personRepository.delete(person);
35
+        return true;
36
+    }
37
+
38
+    public boolean updatePerson(Integer id, String first_name, String last_name, Date birthday, Long mobile) {
39
+        if (!personRepository.exists(id)) return false;
40
+        else {
41
+            Person p = getPersonById(id);
42
+            p.setFirstName(first_name);
43
+            p.setLastName(last_name);
44
+            p.setBirthdate(birthday);
45
+            p.setMobile(mobile);
46
+            personRepository.save(p);
47
+        }
48
+        return true;
49
+    }
50
+
51
+    public boolean addPerson(Person person) {
52
+        if (personRepository.exists(person.getId())) return false;
53
+        else personRepository.save(person);
54
+        return true;
55
+    }
56
+
57
+    public boolean assignHome(Integer id, Integer home_id) {
58
+        if (!personRepository.exists(id)) return false;
59
+        else {Person person = new Person();
60
+            person.setHomeId(id);
61
+            personRepository.save(person);}
62
+        return true;
63
+    }
64
+
65
+}

+ 9
- 0
src/main/java/io/zipcoder/persistenceapp/jpa/PersonRepository.java Visa fil

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

+ 3
- 2
src/main/resources/application-h2.properties Visa fil

@@ -1,4 +1,5 @@
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
-spring.datasource.continue-on-error=true
4
+spring.datasource.continue-on-error=true
5
+spring.datasource.initialization-mode=always

+ 3
- 1
src/main/resources/application.properties Visa fil

@@ -1,3 +1,5 @@
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
+spring.jpa.hibernate.ddl-auto=none
5
+spring.datasource.initialization-mode=always

src/main/resources/schema-h2.sql → src/main/resources/schema.sql Visa fil

@@ -1,6 +1,6 @@
1 1
 DROP TABLE IF EXISTS PERSON;
2 2
 
3
-CREATE TABLE PERSON (
3
+CREATE TABLE PEOPLE (
4 4
   ID INT NOT NULL AUTO_INCREMENT,
5 5
   FIRST_NAME VARCHAR2(255) NOT NULL DEFAULT '',
6 6
   LAST_NAME VARCHAR2(255) NOT NULL DEFAULT '',
@@ -9,10 +9,10 @@ CREATE TABLE PERSON (
9 9
   HOME_ID SMALLINT DEFAULT NULL,
10 10
   PRIMARY KEY (ID));
11 11
 
12
-  ALTER TABLE PERSON
13
-  ADD FOREIGN KEY (HOME_ID)
14
-  REFERENCES HOME(ID);
15
-
12
+  INSERT INTO people (FIRST_NAME, LAST_NAME, MOBILE, BIRTHDAY)
13
+VALUES ('Ned', 'Redmond', '3023023022', '1898-01-28'),
14
+  ('Nas', 'Pronina', '3023021234', '2000-01-01'),
15
+  ('Shep', 'Pronin', '1231231234', '2018-01-28');
16 16
 
17 17
 DROP TABLE IF EXISTS HOME;
18 18
 
@@ -23,6 +23,16 @@ CREATE TABLE HOME (
23 23
   PRIMARY KEY (ID)
24 24
 );
25 25
 
26
+INSERT INTO home (ADDRESS, HOMENUMBER)
27
+VALUES ('36 E. Bayberry Rd.Savannah, GA 31404', '565-6895'),
28
+  ('11 Essex Dr.Farmingdale, NY 11735', '454-4544'),
29
+  ('920 Arlington Street Clifton, NJ 07011', '985-4515'),
30
+  ('234 High Street, PA 19159', '267-3940');
31
+
32
+  ALTER TABLE PEOPLE
33
+  ADD FOREIGN KEY (HOME_ID)
34
+  REFERENCES HOME(ID);
35
+
26 36
 DROP TABLE IF EXISTS movies;
27 37
 
28 38
 CREATE TABLE movies (

+ 40
- 0
src/main/resources/script.sql Visa fil

@@ -0,0 +1,40 @@
1
+INSERT INTO movies (title, runtime, genre, imdb_score, rating)
2
+VALUES ('Howard the Duck', 110, 'Sci-Fi', 4.6, 'PG'),
3
+       ('Lavalantula',	83,	'Horror',	4.7,	'TV-14'),
4
+       ('Starship Troopers',	129,	'Sci-Fi',	7.2,	'PG-13'),
5
+       ('Waltz With Bashir',	90,	'Documentary',	8.0,	'R'),
6
+       ('Spaceballs',	96,	'Comedy',	7.1,	'PG'),
7
+       ('Monsters Inc.',	92,	'Animation',	8.1,	'G'),
8
+       ('Blue Velvet', 121, 'Drama/Mystery', 7.8, 'R'),
9
+       ('The Hunger', 100, 'Fantasy/Thriller', 6.7, 'R'),
10
+       ('The Color of Pomegranates', 75, 'Drama/Art film', 7.7, 'NR'),
11
+       ('Little Nemo: Adventures in Slumberland', 94, 'Fantasy/Adventure', 7.2, 'G'),
12
+       ('The Last Unicorn', 93, 'Drama/Fantasy', 7.5, 'G'),
13
+       ('The 7th Voyage of Sinbad', 88, 'Fantasy/Action', 7.1, 'G');
14
+
15
+-- Create a query to find all movies in the Sci-Fi genre.
16
+SELECT * FROM movies WHERE genre = 'Sci-Fi';
17
+
18
+--Create a query to find all films that scored at least a 6.5 on IMDB
19
+SELECT * FROM movies WHERE imdb_score >= 6.5;
20
+
21
+-- For parents who have young kids, but who don't want to sit through long children's movies, create a query to find all of the movies rated G or PG that are less than 100 minutes long.
22
+SELECT * FROM movies WHERE rating = 'G' OR rating = 'PG' AND runtime <= 100;
23
+
24
+-- Create a query to show the average runtimes of movies rated below a 7.5, grouped by their respective genres.
25
+SELECT genre, AVG(runtime) AS Average_Runtime FROM movies WHERE imdb_score < 7.5 GROUP BY genre;
26
+
27
+-- There's been a data entry mistake; Starship Troopers is actually rated R, not PG-13. Create a query that finds the movie by its title and changes its rating to R.
28
+UPDATE movies SET rating = 'R' WHERE title = 'Starship Troopers';
29
+
30
+-- Show the ID number and rating of all of the Horror and Documentary movies in the database. Do this in only one query.
31
+SELECT id, rating FROM movies WHERE genre = 'Horror' OR genre = 'Documentary';
32
+
33
+-- This time let's find the average, maximum, and minimum IMDB score for movies of each rating.
34
+SELECT rating, AVG(imdb_score), MAX(imdb_score), MIN(imdb_score) FROM movies GROUP BY rating;
35
+
36
+-- That last query isn't very informative for ratings that only have 1 entry. use a HAVING COUNT(*) > 1 clause to only show ratings with multiple movies showing.
37
+SELECT rating, AVG(imdb_score), MAX(imdb_score), MIN(imdb_score) FROM movies GROUP BY rating HAVING COUNT(*) > 1;
38
+
39
+-- Let's make our movie list more child-friendly. Delete all entries that have a rating of R. Remember to record your query in script.sql.
40
+DELETE FROM movies WHERE rating = 'R';