Skip to content

Commit 2c7cd46

Browse files
authored
Merge pull request #1 from vimitdhawan/Spring-Boot-Application-initials
This is initial check in branch
2 parents c88c4b0 + 45b61aa commit 2c7cd46

File tree

11 files changed

+316
-0
lines changed

11 files changed

+316
-0
lines changed

springbootapp/pom.xml

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>org.newgen.vimit</groupId>
4+
<artifactId>springbootapp</artifactId>
5+
<version>0.0.1-SNAPSHOT</version>
6+
<name>spring boot application</name>
7+
8+
<parent>
9+
<groupId>org.springframework.boot</groupId>
10+
<artifactId>spring-boot-starter-parent</artifactId>
11+
<version>1.4.3.RELEASE</version>
12+
</parent>
13+
<dependencies>
14+
<dependency>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-web</artifactId>
17+
</dependency>
18+
<dependency>
19+
<groupId>org.apache.derby</groupId>
20+
<artifactId>derby</artifactId>
21+
<scope>runtime</scope>
22+
</dependency>
23+
<dependency>
24+
<groupId>org.springframework.boot</groupId>
25+
<artifactId>spring-boot-starter-data-jpa</artifactId>
26+
</dependency>
27+
</dependencies>
28+
<properties>
29+
<java.version>1.8</java.version>
30+
</properties>
31+
32+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package io.newgen.springbootstarter;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class CourseApiApp {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(CourseApiApp.class, args);
11+
12+
13+
}
14+
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package io.newgen.springbootstarter.course;
2+
3+
4+
import io.newgen.springbootstarter.topic.Topic;
5+
6+
import javax.persistence.Entity;
7+
import javax.persistence.Id;
8+
import javax.persistence.ManyToOne;
9+
10+
@Entity
11+
public class Course {
12+
@Id
13+
private String id;
14+
private String name;
15+
private String description;
16+
17+
@ManyToOne
18+
private Topic topic;
19+
20+
public String getId() {
21+
return id;
22+
}
23+
public void setId(String id) {
24+
this.id = id;
25+
}
26+
public String getName() {
27+
return name;
28+
}
29+
public void setName(String name) {
30+
this.name = name;
31+
}
32+
public String getDescription() {
33+
return description;
34+
}
35+
public void setDescription(String description) {
36+
this.description = description;
37+
}
38+
public Topic getTopic() {
39+
return topic;
40+
}
41+
public void setTopic(Topic topic) {
42+
this.topic = topic;
43+
}
44+
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package io.newgen.springbootstarter.course;
2+
3+
import java.util.List;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.web.bind.annotation.PathVariable;
7+
import org.springframework.web.bind.annotation.RequestBody;
8+
import org.springframework.web.bind.annotation.RequestMapping;
9+
import org.springframework.web.bind.annotation.RequestMethod;
10+
import org.springframework.web.bind.annotation.RestController;
11+
12+
@RestController
13+
public class CourseController {
14+
15+
@Autowired
16+
private CourseService courseService;
17+
18+
@RequestMapping("/topics/{topicId}/courses")
19+
public List<Course> getAllCourses(@PathVariable String topicId){
20+
return courseService.getAllCourses(topicId);
21+
}
22+
23+
@RequestMapping("/topics/{topicId}/course/{id}")
24+
public Course getCourse(@PathVariable String topicId, @PathVariable String id){
25+
return courseService.getCourse(topicId, id);
26+
27+
}
28+
29+
@RequestMapping(method = RequestMethod.POST, value="/topics/{topicId}/course")
30+
public void addCourse(@RequestBody Course course, @PathVariable String topicId){
31+
courseService.addCourse(course, topicId);
32+
33+
}
34+
35+
/*@RequestMapping(method = RequestMethod.PUT, value="/topic/{topicId}/course")
36+
public void updateTopic(@RequestBody Course course, @PathVariable String topicId){
37+
courseService.updateCourse(course, topicId);
38+
}
39+
*/
40+
41+
42+
/* @RequestMapping(method = RequestMethod.DELETE, value="/course")
43+
public void deleteTopic(@RequestBody Course course){
44+
courseService.deleteCourse(course);
45+
}*/
46+
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package io.newgen.springbootstarter.course;
2+
3+
import java.util.List;
4+
5+
import org.springframework.data.repository.CrudRepository;
6+
7+
public interface CourseRepository extends CrudRepository<Course, String>{
8+
9+
public List<Course> findByTopicId(String topicId);
10+
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package io.newgen.springbootstarter.course;
2+
3+
import io.newgen.springbootstarter.topic.Topic;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.stereotype.Service;
10+
11+
@Service
12+
public class CourseService {
13+
14+
@Autowired
15+
private CourseRepository courseRepository;
16+
17+
public List<Course> getAllCourses(String topicId){
18+
List<Course> courses = new ArrayList<>();
19+
courseRepository.findByTopicId(topicId).forEach(courses::add);
20+
return courses;
21+
}
22+
23+
public Course getCourse(String topicId, String id){
24+
return (Course) courseRepository.findByTopicId(topicId).stream().filter(c -> c.getId().equals(id));
25+
}
26+
27+
public void addCourse(Course course, String topicId) {
28+
Topic topic = new Topic();
29+
topic.setId(topicId);
30+
course.setTopic(topic);
31+
courseRepository.save(course);
32+
}
33+
34+
/*public void updateCourse(Course course, String topicId) {
35+
course.setTopicId(topicId);
36+
courseRepository.save(course);
37+
}*/
38+
39+
/*public void deleteCourse(Course course) {
40+
courseRepository.delete(course);
41+
}*/
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package io.newgen.springbootstarter.topic;
2+
3+
4+
import javax.persistence.Entity;
5+
import javax.persistence.Id;
6+
7+
8+
@Entity
9+
public class Topic {
10+
@Id
11+
private String id;
12+
private String name;
13+
private String description;
14+
15+
16+
public String getId() {
17+
return id;
18+
}
19+
public void setId(String id) {
20+
this.id = id;
21+
}
22+
public String getName() {
23+
return name;
24+
}
25+
public void setName(String name) {
26+
this.name = name;
27+
}
28+
public String getDescription() {
29+
return description;
30+
}
31+
public void setDescription(String description) {
32+
this.description = description;
33+
}
34+
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package io.newgen.springbootstarter.topic;
2+
3+
import java.util.List;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.web.bind.annotation.PathVariable;
7+
import org.springframework.web.bind.annotation.RequestBody;
8+
import org.springframework.web.bind.annotation.RequestMapping;
9+
import org.springframework.web.bind.annotation.RequestMethod;
10+
import org.springframework.web.bind.annotation.RestController;
11+
12+
@RestController
13+
public class TopicController {
14+
15+
@Autowired
16+
private TopicService topicService;
17+
18+
@RequestMapping("/topics")
19+
public List<Topic> getAllTopics(){
20+
return topicService.getAllTopics();
21+
}
22+
23+
@RequestMapping("/topics/{id}")
24+
public Topic getTopic(@PathVariable String id){
25+
return topicService.getTopic(id);
26+
27+
}
28+
29+
@RequestMapping(method = RequestMethod.POST, value="/topics")
30+
public void addTopic(@RequestBody Topic topic){
31+
topicService.addTopic(topic);
32+
33+
}
34+
35+
@RequestMapping(method = RequestMethod.PUT, value="/topics")
36+
public void updateTopic(@RequestBody Topic topic){
37+
topicService.updateTopic(topic);
38+
}
39+
40+
@RequestMapping(method = RequestMethod.DELETE, value="/topics")
41+
public void deleteTopic(@RequestBody Topic topic){
42+
topicService.deleteTopic(topic);
43+
}
44+
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package io.newgen.springbootstarter.topic;
2+
3+
import org.springframework.data.repository.CrudRepository;
4+
5+
public interface TopicRepository extends CrudRepository<Topic, String>{
6+
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package io.newgen.springbootstarter.topic;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.stereotype.Service;
8+
9+
@Service
10+
public class TopicService {
11+
12+
@Autowired
13+
private TopicRepository topicRepository;
14+
15+
public List<Topic> getAllTopics(){
16+
List<Topic> topic = new ArrayList<>();
17+
topicRepository.findAll().forEach(topic::add);
18+
return topic;
19+
}
20+
21+
public Topic getTopic(String id){
22+
return topicRepository.findOne(id);
23+
}
24+
25+
public void addTopic(Topic topic) {
26+
topicRepository.save(topic);
27+
}
28+
29+
public void updateTopic(Topic topic) {
30+
topicRepository.save(topic);
31+
}
32+
33+
public void deleteTopic(Topic topic) {
34+
topicRepository.delete(topic);
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
server.port=8081

0 commit comments

Comments
 (0)