package se.kits.gakusei.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import se.kits.gakusei.content.model.Course;
import se.kits.gakusei.content.repository.CourseRepository;

@RestController
public class CourseController {

    @Autowired
    CourseRepository courseRepository;

    @RequestMapping(
            value = "api/courses",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    public ResponseEntity<Iterable<Course>> getAllCourses(){
        Iterable courses = courseRepository.findAll();

        if(courses != null){
            return new ResponseEntity<Iterable<Course>>(courses, HttpStatus.OK);
        } else {
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }    }

    @RequestMapping(
            value = "api/courses/{courseID}",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    public ResponseEntity<Course> getCourseByID(@PathVariable(value = "courseID") Long courseID){
        Course course = courseRepository.findOne(courseID);

        if(course != null){
            return new ResponseEntity<>(course, HttpStatus.OK);
        } else {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    }

    @RequestMapping(
            value = "api/courses/{courseName}",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    public ResponseEntity<Course> getCourseByName(@PathVariable(value = "courseName") String courseName){
        Course course = courseRepository.findByName(courseName);

        if(course != null){
            return new ResponseEntity<>(course, HttpStatus.OK);
        } else {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    }

    @RequestMapping(
            value = "api/courses/{courseCode}",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    public ResponseEntity<Course> getCourseByCode(@PathVariable(value = "courseCode") String courseCode){
        Course course = courseRepository.findByCourseCode(courseCode);

        if(course != null){
            return new ResponseEntity<>(course, HttpStatus.OK);
        } else {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    }

}

@@ -1530,181 +1530,44 @@
     
-if(course != null)%7B%0A            return new ResponseEntity%3C%3E(course, HttpStatus.OK);%0A        %7D else %7B%0A            return new ResponseEntity%3C%3E(HttpStatus.NOT_FOUND);%0A        %7D
+return createResponseEntity(course);
 %0A   
@@ -1930,181 +1930,44 @@
     
-if(course != null)%7B%0A            return new ResponseEntity%3C%3E(course, HttpStatus.OK);%0A        %7D else %7B%0A            return new ResponseEntity%3C%3E(HttpStatus.NOT_FOUND);%0A        %7D
+return createResponseEntity(course);
 %0A   
@@ -2332,25 +2332,153 @@
         
-if(course
+return createResponseEntity(course);%0A    %7D%0A%0A    private ResponseEntity%3CCourse%3E createResponseEntity(Course toReturn)%7B%0A        if(toReturn
  != null
@@ -2520,22 +2520,24 @@
 ntity%3C%3E(
-course
+toReturn
 , HttpSt

Refactor code to reduce duplication