Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 43 additions & 13 deletions school/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@
<version>0.0.1-SNAPSHOT</version>
<name>school</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>11</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
Expand All @@ -27,7 +25,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
Expand All @@ -36,22 +33,56 @@
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<!-- <scope>runtime</scope>-->
<!-- <scope>runtime</scope>-->
</dependency>

<!-- PostgreSQL -->
<!-- <dependency>-->
<!-- <groupId>org.postgresql</groupId>-->
<!-- <artifactId>postgresql</artifactId>-->
<!-- </dependency>-->

<!-- <dependency>-->
<!-- <groupId>org.postgresql</groupId>-->
<!-- <artifactId>postgresql</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.26</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<!-- junit dependency for basic testing -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.3.2</version>
<scope>test</scope>
</dependency>
<!-- Rest assured dependency for rest api-->
<!-- https://mvnrepository.com/artifact/io.rest-assured/spring-mock-mvc -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>spring-mock-mvc</artifactId>
<version>3.3.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
Expand All @@ -60,5 +91,4 @@
</plugin>
</plugins>
</build>

</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class SchoolApplication
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.lambdaschool.school.config;

import org.h2.tools.Server;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.sql.SQLException;

@Configuration
// taken from https://techdev.io/en/developer-blog/querying-the-embedded-h2-database-of-a-spring-boot-application
// necessary for using the database tool built into intellij
//
// Path: tcp://localhost:9092
// Database: mem:testdb
// URL: jdbc:h2:tcp://localhost:9092/mem:testdb
// User: sa

public class H2ServerConfiguration
{

// TCP port for remote connections, default 9092
@Value("${h2.tcp.port:9092}")
private String h2TcpPort;

// Web port, default 8082
@Value("${h2.web.port:8082}")
private String h2WebPort;

/**
* TCP connection to connect with SQL clients to the embedded h2 database.
* <p>
* Connect to "jdbc:h2:tcp://localhost:9092/mem:testdb", username "sa", password empty.
*/
@Bean
@ConditionalOnExpression("${h2.tcp.enabled:true}")
public Server h2TcpServer() throws SQLException
{
return Server.createTcpServer("-tcp", "-tcpAllowOthers", "-tcpPort", h2TcpPort).start();
}

/**
* Web console for the embedded h2 database.
* <p>
* Go to http://localhost:8082 and connect to the database "jdbc:h2:mem:testdb", username "sa", password empty.
*/
@Bean
@ConditionalOnExpression("${h2.web.enabled:true}")
public Server h2WebServer() throws SQLException
{
return Server.createWebServer("-web", "-webAllowOthers", "-webPort", h2WebPort).start();
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.lambdaschool.school.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.Pageable;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;


@Configuration
@EnableSwagger2

public class Swagger2Config
{
@Bean
public Docket api()
{
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors
.basePackage("com.lambdaschool.school"))
.paths(PathSelectors.any())
// .paths(PathSelectors.regex("/**))
.build()
.useDefaultResponseMessages(false) // Allows only my exception responses
.ignoredParameterTypes(Pageable.class) // allows only my paging parameter list
.apiInfo(apiEndPointsInfo());
}
private ApiInfo apiEndPointsInfo()
{
return new ApiInfoBuilder().title("Swagger School")
.description("Swagger School")
.contact(new Contact("Shelby Diamond", "", "shelbydiamond518@gmail.com")).build();
// .license("MIT").licenseUrl("https://github.com/LambdaSchool/java-crudysnacks/blob/master/LICENSE")
// .version("1.0.0").build();
}
}
29 changes: 29 additions & 0 deletions school/src/main/java/com/lambdaschool/school/config/WebConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.lambdaschool.school.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration

public class WebConfig implements WebMvcConfigurer
{
/**
* This still gives the following example WARN
* <p>
* [2019-09-30 12:48:44.459] [servlet.DispatcherServlet] [http-nio-2019-exec-8] [1249] [WARN ] No mapping for GET /
* [2019-09-30 12:48:44.469] [servlet.DispatcherServlet] [http-nio-2019-exec-1] [1249] [WARN ] No mapping for GET /csrf
* <p>
* All works though
*/

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry)
{
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");

registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,59 @@
import com.lambdaschool.school.model.Course;
import com.lambdaschool.school.service.CourseService;
import com.lambdaschool.school.view.CountStudentsInCourses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;

@RestController
@RequestMapping(value = "/courses")
public class CourseController
{
public class CourseController {

private static final Logger logger = LoggerFactory.getLogger(CourseController.class);

@Autowired

private CourseService courseService;

@GetMapping(value = "/courses", produces = {"application/json"})
public ResponseEntity<?> listAllCourses()
{
@GetMapping(value = "/courses",
produces = {"application/json"})
public ResponseEntity<?> listAllCourses(HttpServletRequest request) {
logger.info(request.getMethod() + " " + request.getRequestURI() + " accessed");

ArrayList<Course> myCourses = courseService.findAll();
return new ResponseEntity<>(myCourses, HttpStatus.OK);
}
@GetMapping(value ="/coursespaging", produces = {"application/json"})
public ResponseEntity<?> listAllCoursesByPage(@PageableDefault(page=0, size=3) Pageable pageable) {
ArrayList<Course> myCourse = courseService.findAllPageable(pageable);
return new ResponseEntity<>(myCourse, HttpStatus.OK);
}


@GetMapping(value = "/studcount", produces = {"application/json"})
public ResponseEntity<?> getCountStudentsInCourses()
{
return new ResponseEntity<>(courseService.getCountStudentsInCourse(), HttpStatus.OK);
@GetMapping(value = "/studcount",
produces = {"application/json"})
public ResponseEntity<?> getCountStudentsInCourses(HttpServletRequest request) {
logger.info(request.getMethod() + " " + request.getRequestURI() + " accessed");

ArrayList<CountStudentsInCourses> list = courseService.getCountStudentsInCourse();
return new ResponseEntity<>(list, HttpStatus.OK);
}

@DeleteMapping("/courses/{courseid}")
public ResponseEntity<?> deleteCourseById(@PathVariable long courseid)
{
public ResponseEntity<?> deleteCourseById(
@PathVariable
long courseid, HttpServletRequest request) {
logger.info(request.getMethod() + " " + request.getRequestURI() + " accessed");

courseService.delete(courseid);
return new ResponseEntity<>(HttpStatus.OK);
}
}
}
Loading