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
25 changes: 16 additions & 9 deletions school/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,31 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<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.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<!-- Manual security dependencies -->

<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-actuator</artifactId>
</dependency>

</dependencies>

<build>
Expand All @@ -61,4 +68,4 @@
</plugins>
</build>

</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@EnableWebMvc
@EnableJpaAuditing
@SpringBootApplication
public class SchoolApplication
{

public static void main(String[] args)
{
SpringApplication.run(SchoolApplication.class, args);
public class SchoolApplication {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(SchoolApplication.class, args);
DispatcherServlet dispatcherServlet = (DispatcherServlet) ctx.getBean("dispatcherServlet");
dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,48 @@
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.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Array;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

@RestController
@RequestMapping(value = "/courses")
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()
{
logger.info("GET courses/courses has been accessed " + new Date() + new Date().getTime());
ArrayList<Course> myCourses = courseService.findAll();
return new ResponseEntity<>(myCourses, HttpStatus.OK);
}

@GetMapping(value = "/studcount", produces = {"application/json"})
public ResponseEntity<?> getCountStudentsInCourses()
{
logger.info("GET courses/studcount has been accessed " + new Date() + new Date().getTime());
return new ResponseEntity<>(courseService.getCountStudentsInCourse(), HttpStatus.OK);
}

@DeleteMapping("/courses/{courseid}")
public ResponseEntity<?> deleteCourseById(@PathVariable long courseid)
{
logger.info("GET courses/{courseid} has been accessed " + new Date() + new Date().getTime());
courseService.delete(courseid);
return new ResponseEntity<>(HttpStatus.OK);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.lambdaschool.school.model.Student;
import com.lambdaschool.school.service.StudentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
Expand All @@ -12,82 +14,67 @@
import javax.validation.Valid;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/students")
public class StudentController
{
public class StudentController {

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

@Autowired
private StudentService studentService;

// Please note there is no way to add students to course yet!

@GetMapping(value = "/students", produces = {"application/json"})
public ResponseEntity<?> listAllStudents()
{
@GetMapping(value = "/students",
produces = {"application/json"})
public ResponseEntity<?> listAllStudents() {
logger.info("GET students/students has been accessed " + new Date() + new Date().getTime());
List<Student> myStudents = studentService.findAll();
return new ResponseEntity<>(myStudents, HttpStatus.OK);
}

@GetMapping(value = "/Student/{StudentId}",
produces = {"application/json"})
public ResponseEntity<?> getStudentById(
@PathVariable
Long StudentId)
{
public ResponseEntity<?> getStudentById(@PathVariable Long StudentId) {
logger.info("GET students/Student/{StudentId} " + new Date() + new Date().getTime());
Student r = studentService.findStudentById(StudentId);
return new ResponseEntity<>(r, HttpStatus.OK);
}


@GetMapping(value = "/student/namelike/{name}",
produces = {"application/json"})
public ResponseEntity<?> getStudentByNameContaining(
@PathVariable String name)
{
public ResponseEntity<?> getStudentByNameContaining(@PathVariable String name) {
logger.info("GET students/namelike/{name} " + new Date() + new Date().getTime());
List<Student> myStudents = studentService.findStudentByNameLike(name);
return new ResponseEntity<>(myStudents, HttpStatus.OK);
}


@PostMapping(value = "/Student",
consumes = {"application/json"},
produces = {"application/json"})
public ResponseEntity<?> addNewStudent(@Valid
@RequestBody
Student newStudent) throws URISyntaxException
{
consumes = {"application/json"},
produces = {"application/json"})
public ResponseEntity<?> addNewStudent(@Valid @RequestBody Student newStudent) throws URISyntaxException {
logger.info("POST /Student has been posted " + new Date() + new Date().getTime());
newStudent = studentService.save(newStudent);

// set the location header for the newly created resource
HttpHeaders responseHeaders = new HttpHeaders();
URI newStudentURI = ServletUriComponentsBuilder.fromCurrentRequest().path("/{Studentid}").buildAndExpand(newStudent.getStudid()).toUri();
responseHeaders.setLocation(newStudentURI);

return new ResponseEntity<>(null, responseHeaders, HttpStatus.CREATED);
}


@PutMapping(value = "/Student/{Studentid}")
public ResponseEntity<?> updateStudent(
@RequestBody
Student updateStudent,
@PathVariable
long Studentid)
{
public ResponseEntity<?> updateStudent(@RequestBody Student updateStudent, @PathVariable long Studentid) {
logger.info("PUT students/Student/{Studentid} has been edited " + new Date() + new Date().getTime());
studentService.update(updateStudent, Studentid);
return new ResponseEntity<>(HttpStatus.OK);
}


@DeleteMapping("/Student/{Studentid}")
public ResponseEntity<?> deleteStudentById(
@PathVariable
long Studentid)
{
public ResponseEntity<?> deleteStudentById(@PathVariable long Studentid) {
logger.info("DELETE /students/Student/{Studentid} has been deleted " + new Date() + new Date().getTime());
studentService.delete(Studentid);
return new ResponseEntity<>(HttpStatus.OK);
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.lambdaschool.school.exceptions;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@ResponseStatus(value = HttpStatus.NOT_FOUND)
public class ResourceNotFoundException extends RuntimeException {

public ResourceNotFoundException(String message) {
super(message);
}

public ResourceNotFoundException(String message, Throwable cause) {
super(message, cause);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.lambdaschool.school.exceptions;

public class ValidationError {

private String code;
private String message;

public String getCode() {
return code;
}

public void setCode(String code) {
this.code = code;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.lambdaschool.school.handlers;

import com.lambdaschool.school.exceptions.ResourceNotFoundException;
import com.lambdaschool.school.model.ErrorDetail;
import org.springframework.beans.TypeMismatchException;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.NoHandlerFoundException;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

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

@ControllerAdvice
public class RestExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler({ResourceNotFoundException.class})
public ResponseEntity<?> handleResourceNotFoundException(ResourceNotFoundException rnfe, HttpServletRequest request) {
ErrorDetail errorDetail = new ErrorDetail();

errorDetail.setTimestamp(new Date().getTime());
errorDetail.setStatus(HttpStatus.NOT_FOUND.value());

errorDetail.setTitle("Resource Not Found");
errorDetail.setDetail(rnfe.getMessage());
errorDetail.setDeveloperMessage(rnfe.getClass().getName());

return new ResponseEntity<>(errorDetail, null, HttpStatus.NOT_FOUND);
}

@Override
protected ResponseEntity<Object> handleNoHandlerFoundException(NoHandlerFoundException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
ErrorDetail errorDetail = new ErrorDetail();

errorDetail.setTimestamp(new Date().getTime());
errorDetail.setStatus(HttpStatus.NOT_FOUND.value());

errorDetail.setTitle("Path not found");
errorDetail.setDetail(request.getDescription(true));
errorDetail.setDeveloperMessage("Rest Handler Not Found (check for valid URI)");

return new ResponseEntity<>(errorDetail, null, HttpStatus.NOT_FOUND);
}

@Override
protected ResponseEntity<Object> handleTypeMismatch(TypeMismatchException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
ErrorDetail errorDetail = new ErrorDetail();
errorDetail.setTimestamp(new Date().getTime());
errorDetail.setStatus(HttpStatus.BAD_REQUEST.value());

errorDetail.setTitle("Type mismatch");
errorDetail.setDetail(ex.getMessage());
errorDetail.setDeveloperMessage(request.getDescription(true));

return new ResponseEntity<>(errorDetail, null, HttpStatus.BAD_REQUEST);
}
}
Loading