From 09aa302363e793d8eb6be946c651666fac1a5377 Mon Sep 17 00:00:00 2001 From: JoeSeff Date: Thu, 15 Oct 2020 21:43:11 +0300 Subject: [PATCH 1/6] Adding ReadMe --- ReadMe.md | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 ReadMe.md diff --git a/ReadMe.md b/ReadMe.md new file mode 100644 index 0000000..2047777 --- /dev/null +++ b/ReadMe.md @@ -0,0 +1,8 @@ +# Rest API App +This is a project to showcase the rest API using Spring boot. It allows you to add, view, update and remove film titles. + +## Sub heading level 1 + +### Sub heading level 2 + + \ No newline at end of file From 207dd762087f0da8aa34e19fb86817e86c10251e Mon Sep 17 00:00:00 2001 From: JoeSeff Date: Fri, 16 Oct 2020 02:20:10 +0300 Subject: [PATCH 2/6] Test added to ensure that data layer is working --- pom.xml | 6 +++ .../birichani/code/restapi/model/Video.java | 7 +++ src/main/resources/application.properties | 8 ++-- .../repository/VideoRepositoryTest.java | 44 +++++++++++++++++++ src/test/resources/application.properties | 10 +++++ 5 files changed, 70 insertions(+), 5 deletions(-) create mode 100644 src/test/java/com/birichani/code/restapi/repository/VideoRepositoryTest.java create mode 100644 src/test/resources/application.properties diff --git a/pom.xml b/pom.xml index 8f9fb82..807f047 100644 --- a/pom.xml +++ b/pom.xml @@ -60,6 +60,12 @@ + + + com.github.javafaker + javafaker + 1.0.2 + diff --git a/src/main/java/com/birichani/code/restapi/model/Video.java b/src/main/java/com/birichani/code/restapi/model/Video.java index 65ace79..3076688 100644 --- a/src/main/java/com/birichani/code/restapi/model/Video.java +++ b/src/main/java/com/birichani/code/restapi/model/Video.java @@ -1,6 +1,7 @@ package com.birichani.code.restapi.model; import lombok.Data; +import lombok.NoArgsConstructor; import javax.persistence.Entity; import javax.persistence.GeneratedValue; @@ -8,6 +9,7 @@ import javax.persistence.Id; @Data +@NoArgsConstructor @Entity public class Video { @Id @@ -15,4 +17,9 @@ public class Video { private Long vid; private String title; private String description; + + public Video(String title, String description) { + this.title = title; + this.description = description; + } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index ebba310..eb0b3f5 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,9 +1,7 @@ -server.port=8084 - # Datasource properties -spring.datasource.url=jdbc:mysql://localhost/dynamiccontents?useSSL=false -spring.datasource.username=root -spring.datasource.password=123456 +spring.datasource.url=jdbc:mysql://localhost:3306/video_db?useSSL=false +spring.datasource.username=test +spring.datasource.password=test123* spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver # JPA properties diff --git a/src/test/java/com/birichani/code/restapi/repository/VideoRepositoryTest.java b/src/test/java/com/birichani/code/restapi/repository/VideoRepositoryTest.java new file mode 100644 index 0000000..b2af782 --- /dev/null +++ b/src/test/java/com/birichani/code/restapi/repository/VideoRepositoryTest.java @@ -0,0 +1,44 @@ +package com.birichani.code.restapi.repository; + +import com.birichani.code.restapi.model.Video; +import com.github.javafaker.Faker; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +import java.util.Locale; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +/** + * @author - JoeSeff + * @created - 16/10/2020 02:02 + */ + +@ExtendWith(SpringExtension.class) +@DataJpaTest +@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) +@TestPropertySource(properties = { + "spring.jpa.hibernate.ddl-auto=create-drop" +}) +class VideoRepositoryTest { + + @Autowired private VideoRepository videoRepository; + private final Faker faker = new Faker(Locale.ENGLISH); + + @Test + void testThatRecordsAreInserted() { + assertNotNull(videoRepository); + + for (int i = 0; i < 5; i++) { + videoRepository.save( new Video(faker.name().title(), faker.lorem().paragraph()) ); + } + + assertEquals(5, videoRepository.count()); + } +} diff --git a/src/test/resources/application.properties b/src/test/resources/application.properties new file mode 100644 index 0000000..eb0b3f5 --- /dev/null +++ b/src/test/resources/application.properties @@ -0,0 +1,10 @@ +# Datasource properties +spring.datasource.url=jdbc:mysql://localhost:3306/video_db?useSSL=false +spring.datasource.username=test +spring.datasource.password=test123* +spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver + +# JPA properties +spring.jpa.show-sql=true +spring.jpa.properties.hibernate.format_sql=true +spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect \ No newline at end of file From 57dd63f6ea1990ea868956b16cd7060747fc8d99 Mon Sep 17 00:00:00 2001 From: JoeSeff Date: Fri, 16 Oct 2020 02:25:33 +0300 Subject: [PATCH 3/6] Returning the server.port property --- src/main/resources/application.properties | 2 ++ src/test/resources/application.properties | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index eb0b3f5..5a2e0cb 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,3 +1,5 @@ +server.port=8084 + # Datasource properties spring.datasource.url=jdbc:mysql://localhost:3306/video_db?useSSL=false spring.datasource.username=test diff --git a/src/test/resources/application.properties b/src/test/resources/application.properties index eb0b3f5..5a2e0cb 100644 --- a/src/test/resources/application.properties +++ b/src/test/resources/application.properties @@ -1,3 +1,5 @@ +server.port=8084 + # Datasource properties spring.datasource.url=jdbc:mysql://localhost:3306/video_db?useSSL=false spring.datasource.username=test From a93b78d4fff7f666e7c369e212cb18313352e134 Mon Sep 17 00:00:00 2001 From: birichani Date: Mon, 19 Oct 2020 09:42:43 +0300 Subject: [PATCH 4/6] Initial Commit:a demo on REST-API using springBoot . --- ReadMe.md | 13 ++++++------- .../com/birichani/code/restapi/model/Video.java | 4 +++- .../restapi/repository/VideoRepositoryTest.java | 2 +- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/ReadMe.md b/ReadMe.md index 2047777..34d4623 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -1,8 +1,7 @@ -# Rest API App +# REST-API APP This is a project to showcase the rest API using Spring boot. It allows you to add, view, update and remove film titles. - -## Sub heading level 1 - -### Sub heading level 2 - - \ No newline at end of file +## spring Boot +The use of spring data jpa, hibernate as one of the technologies used. +### MYSQL +database used to store data. +#### maven. diff --git a/src/main/java/com/birichani/code/restapi/model/Video.java b/src/main/java/com/birichani/code/restapi/model/Video.java index 3076688..d09a93f 100644 --- a/src/main/java/com/birichani/code/restapi/model/Video.java +++ b/src/main/java/com/birichani/code/restapi/model/Video.java @@ -17,9 +17,11 @@ public class Video { private Long vid; private String title; private String description; + private String link; - public Video(String title, String description) { + public Video(String title, String description, String link) { this.title = title; this.description = description; + this.link = link; } } diff --git a/src/test/java/com/birichani/code/restapi/repository/VideoRepositoryTest.java b/src/test/java/com/birichani/code/restapi/repository/VideoRepositoryTest.java index b2af782..97344c1 100644 --- a/src/test/java/com/birichani/code/restapi/repository/VideoRepositoryTest.java +++ b/src/test/java/com/birichani/code/restapi/repository/VideoRepositoryTest.java @@ -36,7 +36,7 @@ void testThatRecordsAreInserted() { assertNotNull(videoRepository); for (int i = 0; i < 5; i++) { - videoRepository.save( new Video(faker.name().title(), faker.lorem().paragraph()) ); + videoRepository.save( new Video(faker.name().title(), faker.lorem().paragraph(), link) ); } assertEquals(5, videoRepository.count()); From 312e6cd5deb0861eb384170db80ec9102b78eba5 Mon Sep 17 00:00:00 2001 From: birichani Date: Tue, 20 Oct 2020 16:59:20 +0300 Subject: [PATCH 5/6] Initial Commit:a demo on REST-API using springBoot . --- src/main/java/com/birichani/code/restapi/model/Video.java | 2 ++ src/main/resources/application.properties | 6 +++--- .../code/restapi/repository/VideoRepositoryTest.java | 3 ++- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/birichani/code/restapi/model/Video.java b/src/main/java/com/birichani/code/restapi/model/Video.java index d09a93f..4d0daa1 100644 --- a/src/main/java/com/birichani/code/restapi/model/Video.java +++ b/src/main/java/com/birichani/code/restapi/model/Video.java @@ -1,5 +1,6 @@ package com.birichani.code.restapi.model; +import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @@ -10,6 +11,7 @@ @Data @NoArgsConstructor +@AllArgsConstructor @Entity public class Video { @Id diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 5a2e0cb..8740892 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,9 +1,9 @@ server.port=8084 # Datasource properties -spring.datasource.url=jdbc:mysql://localhost:3306/video_db?useSSL=false -spring.datasource.username=test -spring.datasource.password=test123* +spring.datasource.url=jdbc:mysql://localhost:3306/video_db?allowPublicKeyRetrieval=true&useSSL=false +spring.datasource.username=root +spring.datasource.password=Sb34106209 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver # JPA properties diff --git a/src/test/java/com/birichani/code/restapi/repository/VideoRepositoryTest.java b/src/test/java/com/birichani/code/restapi/repository/VideoRepositoryTest.java index 97344c1..3a70612 100644 --- a/src/test/java/com/birichani/code/restapi/repository/VideoRepositoryTest.java +++ b/src/test/java/com/birichani/code/restapi/repository/VideoRepositoryTest.java @@ -30,7 +30,8 @@ class VideoRepositoryTest { @Autowired private VideoRepository videoRepository; private final Faker faker = new Faker(Locale.ENGLISH); - + private String link; + @Test void testThatRecordsAreInserted() { assertNotNull(videoRepository); From dfec8e41f427f2d99dbed964ebf1aff34d4afda7 Mon Sep 17 00:00:00 2001 From: birichani Date: Thu, 22 Oct 2020 18:55:12 +0300 Subject: [PATCH 6/6] Initial Commit:a demo on REST-API using springBoot . --- .../code/restapi/api/VideoApiController.java | 39 +++++++++++-------- .../code/restapi/constant/ErrorMessage.java | 5 +++ .../code/restapi/constant/InfoMessage.java | 12 ++++++ src/main/resources/application.properties | 2 +- .../code/restapi/VideoControllerUnitTest.java | 26 +++++++++++++ 5 files changed, 67 insertions(+), 17 deletions(-) create mode 100644 src/main/java/com/birichani/code/restapi/constant/ErrorMessage.java create mode 100644 src/main/java/com/birichani/code/restapi/constant/InfoMessage.java create mode 100644 src/test/java/com/birichani/code/restapi/VideoControllerUnitTest.java diff --git a/src/main/java/com/birichani/code/restapi/api/VideoApiController.java b/src/main/java/com/birichani/code/restapi/api/VideoApiController.java index 9739c7d..bfbd514 100644 --- a/src/main/java/com/birichani/code/restapi/api/VideoApiController.java +++ b/src/main/java/com/birichani/code/restapi/api/VideoApiController.java @@ -1,39 +1,46 @@ package com.birichani.code.restapi.api; +import com.birichani.code.restapi.constant.ErrorMessage; +import com.birichani.code.restapi.constant.InfoMessage; import com.birichani.code.restapi.model.Video; import com.birichani.code.restapi.repository.VideoRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.RequestMapping; import java.util.List; +@RequestMapping("/videos") @RestController public class VideoApiController { - private VideoRepository videoRepository; - + private final VideoRepository videoRepository; @Autowired public VideoApiController(VideoRepository videoRepository) { this.videoRepository = videoRepository; } - - @GetMapping("/Getvideos/{videoTopic}") - public String video(@PathVariable String videoTopic) { - - switch(videoTopic) { - case"python": - - return String.format("This is our python contents") ; - case "java": - return String.format("This is our java contents") ; + + @GetMapping("/{videoTopic}") + public String video (@PathVariable String videoTopic) { + String message; + + switch (videoTopic) { + case "python": + message = InfoMessage.PYTHON_RESPONSE_MESSAGE; + break; + case "java": + message = InfoMessage.JAVA_RESPONSE_MESSAGE; + break; default: - return String.format("sorry,Incorrect input") ; - + + + message = ErrorMessage.VIDEO_TOPIC_SELECTION_MESSAGE; } - + + return message; } - @GetMapping("/GetAll") + @GetMapping() public List