ソースを参照

initial commit

Yesoda Sanka 5 年 前
コミット
6c2fbbd1ba

+ 1
- 1
README.md ファイルの表示

@@ -5,7 +5,7 @@
5 5
 The following lab is to be completed using the H2 Console. Once you have figured out the correct queries for each step, save a copy in a file called `src/main/resources/script.sql`. This will be how you submit this assignment. If at any time you need to reset the database, you can restart your Spring Boot server.
6 6
 
7 7
 ### Mini Movie Database
8
-
8
+_**`****__`**_
9 9
 Add the following movies to the `movies` table using an insert statement:
10 10
 
11 11
 | Title | Runtime | Genre | IMDB Score | Rating |

+ 36
- 1
src/main/java/io/zipcoder/persistenceapp/PersistenceStarterApplication.java ファイルの表示

@@ -1,13 +1,26 @@
1 1
 package io.zipcoder.persistenceapp;
2 2
 
3
+import com.apple.eawt.Application;
3 4
 import org.h2.server.web.WebServlet;
5
+import org.slf4j.Logger;
6
+import org.slf4j.LoggerFactory;
7
+import org.springframework.beans.factory.annotation.Autowired;
8
+import org.springframework.boot.CommandLineRunner;
4 9
 import org.springframework.boot.SpringApplication;
5 10
 import org.springframework.boot.autoconfigure.SpringBootApplication;
6 11
 import org.springframework.boot.web.servlet.ServletRegistrationBean;
7 12
 import org.springframework.context.annotation.Bean;
13
+import org.springframework.jdbc.core.JdbcTemplate;
14
+
15
+import java.util.Arrays;
16
+import java.util.List;
17
+import java.util.stream.Collectors;
8 18
 
9 19
 @SpringBootApplication
10
-public class PersistenceStarterApplication {
20
+public class PersistenceStarterApplication implements CommandLineRunner  {
21
+
22
+	private static final Logger log = LoggerFactory.getLogger(Application.class);
23
+
11 24
 
12 25
 	public static void main(String[] args) {
13 26
 		SpringApplication.run(PersistenceStarterApplication.class, args);
@@ -19,4 +32,26 @@ public class PersistenceStarterApplication {
19 32
 		registrationBean.addUrlMappings("/console/*");
20 33
 		return registrationBean;
21 34
 	}
35
+     @Autowired
36
+
37
+	 JdbcTemplate jdbcTemplate;
38
+
39
+	@Override
40
+
41
+	public void run(String... strings) throws Exception {
42
+		log.info("Creating tables");
43
+		jdbcTemplate.execute("DROP TABLE Person IF EXISTS");
44
+		jdbcTemplate.execute("CREATE TABLE Person(" +
45
+				"id Integer(10), first_name VARCHAR(255), last_name VARCHAR(255),Mobile VARCHAR(255),Birthday DATE,Home_Id SMALLINT(5)");
46
+
47
+		jdbcTemplate.batchUpdate("INSERT INTO Person(id,first_name, last_name,Mobile,Birthday,Home_Id) VALUES ('1','hello','world','111-222-3333','01-2-1980','2')");
48
+	    jdbcTemplate.batchUpdate("INSERT INTO Person(id,first_name, last_name,Mobile,Birthday,Home_Id) VALUES ('2','hai','world1','222-222-3333','02-3-1985','3')");
49
+		jdbcTemplate.batchUpdate("INSERT INTO Person(id,first_name, last_name,Mobile,Birthday,Home_Id) VALUES ('3','welcome','world','333-222-3333','03-5-1989','4')");
50
+		jdbcTemplate.batchUpdate("INSERT INTO Person(id,first_name, last_name,Mobile,Birthday,Home_Id) VALUES ('4','hai','','111-222-3333','01-2-1980','2')");
51
+
52
+//		List<Object[]> splitUpNames = Arrays.asList("John Woo", "Jeff Dean", "Josh Bloch", "Josh Long").stream()
53
+//				.map(name -> name.split(" "))
54
+//				.collect(Collectors.toList());
55
+		//jdbcTemplate.query("select * from Person where id='1'");
56
+	}
22 57
 }

+ 61
- 0
src/main/java/io/zipcoder/persistenceapp/PersonController.java ファイルの表示

@@ -0,0 +1,61 @@
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
+import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
8
+
9
+import java.net.URI;
10
+import java.util.stream.Collectors;
11
+import java.util.stream.StreamSupport;
12
+
13
+@RestController
14
+public class PersonController  {
15
+
16
+    @Autowired
17
+    PersonRepository personRepository;
18
+
19
+    @RequestMapping(value = "/person", method = RequestMethod.POST )
20
+
21
+    public ResponseEntity<?> createPerson(@RequestBody PersonService p){
22
+
23
+        URI newPollUri = ServletUriComponentsBuilder
24
+                .fromCurrentRequest()
25
+                .path("/{id}")
26
+                .buildAndExpand(p.getID() )
27
+                .toUri();
28
+        return new ResponseEntity<> (null, HttpStatus.CREATED) ;
29
+
30
+    }
31
+
32
+    @RequestMapping(value = "/person{id}", method = RequestMethod.GET)
33
+
34
+    public  ResponseEntity<?> getPerson(@PathVariable Integer id){
35
+
36
+        PersonService person=personRepository.findOne(id);
37
+
38
+        return new ResponseEntity<>(person ,HttpStatus.OK ) ;
39
+    }
40
+
41
+    @RequestMapping(value = "/person", method = RequestMethod.GET)
42
+    public ResponseEntity<Iterable  <PersonService > >getPersonList( ){
43
+        Iterable<PersonService> person = personRepository .findAll();
44
+
45
+        Iterable<PersonService > personIterable = StreamSupport.stream(person .spliterator(),false)
46
+
47
+                .collect(Collectors.toList() );
48
+        return new ResponseEntity<>(personIterable  , HttpStatus.OK ) ;
49
+    }
50
+
51
+//    @RequestMapping(value = "/person/{id}", method = RequestMethod.PUT)
52
+//    public  ResponseEntity<?> updatePerson(@RequestBody PersonService person,@PathVariable Integer id){
53
+//        PersonService  p=personRepository.save(person ) ;
54
+//        return new ResponseEntity<>(HttpStatus.OK);
55
+//    }
56
+    @RequestMapping(value = "/person{id}", method = RequestMethod.DELETE )
57
+    public void deletePerson( @PathVariable Integer id){
58
+        personRepository .delete(id);
59
+
60
+    }
61
+}

+ 8
- 0
src/main/java/io/zipcoder/persistenceapp/PersonRepository.java ファイルの表示

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

+ 81
- 0
src/main/java/io/zipcoder/persistenceapp/PersonService.java ファイルの表示

@@ -0,0 +1,81 @@
1
+package io.zipcoder.persistenceapp;
2
+
3
+import java.util.Date;
4
+
5
+public class PersonService {
6
+    Integer  ID;
7
+    String FIRST_NAME;
8
+    String LAST_NAME;
9
+    String MOBILE;
10
+    Date BIRTHDAY;
11
+    int HOME_ID;
12
+
13
+    public PersonService(Integer ID, String FIRST_NAME, String LAST_NAME, String MOBILE, Date BIRTHDAY, int HOME_ID) {
14
+        this.ID = ID;
15
+        this.FIRST_NAME = FIRST_NAME;
16
+        this.LAST_NAME = LAST_NAME;
17
+        this.MOBILE = MOBILE;
18
+        this.BIRTHDAY = BIRTHDAY;
19
+        this.HOME_ID = HOME_ID;
20
+    }
21
+
22
+    public Integer getID() {
23
+        return ID;
24
+    }
25
+
26
+    public void setID(Integer ID) {
27
+        this.ID = ID;
28
+    }
29
+
30
+    public String getFIRST_NAME() {
31
+        return FIRST_NAME;
32
+    }
33
+
34
+    public void setFIRST_NAME(String FIRST_NAME) {
35
+        this.FIRST_NAME = FIRST_NAME;
36
+    }
37
+
38
+    public String getLAST_NAME() {
39
+        return LAST_NAME;
40
+    }
41
+
42
+    public void setLAST_NAME(String LAST_NAME) {
43
+        this.LAST_NAME = LAST_NAME;
44
+    }
45
+
46
+    public String getMOBILE() {
47
+        return MOBILE;
48
+    }
49
+
50
+    public void setMOBILE(String MOBILE) {
51
+        this.MOBILE = MOBILE;
52
+    }
53
+
54
+    public Date getBIRTHDAY() {
55
+        return BIRTHDAY;
56
+    }
57
+
58
+    public void setBIRTHDAY(Date BIRTHDAY) {
59
+        this.BIRTHDAY = BIRTHDAY;
60
+    }
61
+
62
+    public int getHOME_ID() {
63
+        return HOME_ID;
64
+    }
65
+
66
+    public void setHOME_ID(int HOME_ID) {
67
+        this.HOME_ID = HOME_ID;
68
+    }
69
+
70
+    @Override
71
+    public String toString() {
72
+        return "PersonService{" +
73
+                "ID=" + ID +
74
+                ", FIRST_NAME='" + FIRST_NAME + '\'' +
75
+                ", LAST_NAME='" + LAST_NAME + '\'' +
76
+                ", MOBILE='" + MOBILE + '\'' +
77
+                ", BIRTHDAY=" + BIRTHDAY +
78
+                ", HOME_ID=" + HOME_ID +
79
+                '}';
80
+    }
81
+}

+ 6
- 0
src/main/resources/schema-h2.sql ファイルの表示

@@ -60,6 +60,12 @@ CREATE TABLE auto_prices (
60 60
 
61 61
 );
62 62
 
63
+--INSERT INTO movies(id,title,runtime,genre ,imdb_score ,rating )VALUES    (Howard the Duck , 110 , Sci-Fi , 4.6 , PG );
64
+--INSERT INTO movies(id,title,runtime,genre ,imdb_score ,rating )VALUES   ( Lavalantula , 83 , Horror , 4.7 , TV-14  );
65
+--INSERT INTO movies(id,title,runtime,genre ,imdb_score ,rating )VALUES   (Starship Troopers , 129 , Sci-Fi , 7.2 , PG-13);
66
+--INSERT INTO movies(id,title,runtime,genre ,imdb_score ,rating )VALUES   (Waltz With Bashir , 90 , Documentary , 8.0 , R);
67
+--INSERT INTO movies(id,title,runtime,genre ,imdb_score ,rating )VALUES   (Spaceballs , 96 , Comedy , 7.1 , PG );
68
+--INSERT INTO movies(id,title,runtime,genre ,imdb_score ,rating )VALUES   ( Monsters Inc. , 92 , Animation , 8.1 , G);
63 69
 
64 70
 DROP SEQUENCE hibernate_sequence;
65 71
 

+ 29
- 0
src/main/resources/script.sql ファイルの表示

@@ -0,0 +1,29 @@
1
+SELECT * FROM MOVIES;
2
+
3
+SELECT  * FROM  MOVIES WHERE GENRE='SCI-FI';
4
+
5
+SELECT * FROM MOVIES WHERE IMDB_SCORE>7.0;
6
+
7
+SELECT * FROM MOVIES WHERE RATING= ('G' || 'PG')>100;
8
+
9
+SELECT AVG(RUNTIME) FROM MOVIES WHERE  RUNTIME<7.5 GROUP BY RATING;
10
+
11
+SELECT  * FROM  MOVIES ORDER BY  TITLE WHERE RATING='R';
12
+
13
+SELECT ID,RATING FROM MOVIES WHERE GENRE='HORROR' OR GENRE= 'DOCUMENTARY';
14
+
15
+SELECT MAX(IMDB_SCORE) AS LargestIMDB_SCORE FROM MOVIES ;
16
+
17
+SELECT MIN(IMDB_SCORE)FROM MOVIES;
18
+
19
+SELECT AVG(IMDB_SCORE) FROM MOVIES;
20
+
21
+SELECT DISTINCT (RATING), ID, TITLE, RUNTIME, GENRE FROM MOVIES;
22
+
23
+SELECT * FROM MOVIES;
24
+SELECT  DISTINCT (RATING), MAX(IMDB_SCORE), MIN (IMDB_SCORE), AVG(IMDB_SCORE) FROM MOVIES GROUP BY RATING
25
+
26
+SELECT  DISTINCT (RATING), MAX(IMDB_SCORE), MIN (IMDB_SCORE), AVG(IMDB_SCORE) FROM MOVIES GROUP BY RATING HAVING COUNT(RATING) >1;
27
+
28
+
29
+SELECT DELETE FROM MOVIES WHERE RATING='R';