Created
June 1, 2017 20:34
-
-
Save GillesVercammen/ce5b082dd6d476eac16ec7e202d2aa9f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.crescendo.sql.informat.controllers; | |
import com.crescendo.sql.informat.DTO.StudentHistoryDTO; | |
import com.crescendo.sql.informat.repositories.StudenthistoryRepository; | |
import com.crescendo.sql.utils.CustomErrorType; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.http.HttpStatus; | |
import org.springframework.http.ResponseEntity; | |
import org.springframework.web.bind.annotation.GetMapping; | |
import org.springframework.web.bind.annotation.PathVariable; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.bind.annotation.RestController; | |
import java.util.List; | |
/** | |
* Created by Gilles on 1/06/2017. | |
*/ | |
@RestController | |
@RequestMapping("/api") | |
public class Controller5 { | |
@Autowired | |
private StudenthistoryRepository studenthistoryRepository; | |
@GetMapping(path = "/studenthistory/{firstname}/{lastname}") | |
public ResponseEntity<List<StudentHistoryDTO>> getHistories(@PathVariable("firstname") String firstname, @PathVariable("lastname") String lastname) { | |
List<StudentHistoryDTO> histories = studenthistoryRepository.findHistoriesByFirstNameAndLastName(firstname, lastname); | |
System.out.println(histories); | |
if (histories == null){ | |
return new ResponseEntity(new CustomErrorType("unable to retrieve history for student " + firstname + ". Not Found"), HttpStatus.NOT_FOUND); | |
} | |
return new ResponseEntity<List<StudentHistoryDTO>>(histories,HttpStatus.OK); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.crescendo.sql.informat.DTO; | |
import javax.persistence.Entity; | |
import javax.persistence.Id; | |
import java.io.Serializable; | |
/** | |
* Created by Gilles on 1/06/2017. | |
*/ | |
public class StudentHistoryDTO implements Serializable { | |
private String firstName; | |
private String lastName; | |
private String result; | |
private String schoolyear; | |
private String courseId; | |
public String getFirstName() { | |
return firstName; | |
} | |
public void setFirstName(String firstName) { | |
this.firstName = firstName; | |
} | |
public String getLastName() { | |
return lastName; | |
} | |
public void setLastName(String lastName) { | |
this.lastName = lastName; | |
} | |
public String getResult() { | |
return result; | |
} | |
public void setResult(String result) { | |
this.result = result; | |
} | |
public String getSchoolyear() { | |
return schoolyear; | |
} | |
public void setSchoolyear(String schoolyear) { | |
this.schoolyear = schoolyear; | |
} | |
public String getCourseId() { | |
return courseId; | |
} | |
public void setCourseId(String courseId) { | |
this.courseId = courseId; | |
} | |
@Override | |
public String toString(){ | |
return "[\"firstName:\""+firstName+",\"lastName:\""+lastName+",\"result:\""+result+",\"schoolyear:\""+schoolyear+",\"courseId:\""+courseId+"]"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.crescendo.sql.informat.repositories; | |
import com.crescendo.sql.informat.models.Studenthistories; | |
import com.crescendo.sql.informat.models.Studiegebied; | |
import org.springframework.data.jpa.repository.JpaRepository; | |
import org.springframework.data.jpa.repository.Query; | |
import org.springframework.data.repository.query.Param; | |
import org.springframework.stereotype.Repository; | |
import java.util.List; | |
/** | |
* Created by Gilles on 30/05/2017. | |
*/ | |
@Repository | |
public interface StudenthistoryRepository extends JpaRepository<Studenthistories, String>, StudenthistoryRepositoryCustom{ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Repository | |
public interface StudenthistoryRepositoryCustom { | |
List<StudentHistoryDTO> findHistoriesByFirstNameAndLastName(String firstname, String lastname); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.crescendo.sql.informat.repositories; | |
import com.crescendo.sql.informat.DTO.StudentHistoryDTO; | |
import org.springframework.stereotype.Repository; | |
import javax.persistence.*; | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* Created by Gilles on 1/06/2017. | |
*/ | |
@Repository | |
public class StudenthistoryRepositoryImpl implements StudenthistoryRepositoryCustom { | |
@PersistenceContext | |
private EntityManager entityManager; | |
@Override | |
public List<StudentHistoryDTO> findHistoriesByFirstNameAndLastName(String firstname, String lastname) { | |
String q = "SELECT f.first_name, f.last_name, i.result, i.schoolyear, i.informatcourses_id FROM firstinformatstudents f INNER JOIN informatstudenthistories i ON f.id = i.informatstudents_id WHERE f.first_name = ? AND f.last_name= ?"; | |
Query query = entityManager.createNativeQuery(q); | |
query.setParameter(1, firstname); | |
query.setParameter(2, lastname); | |
List<Object[]> resultList = query.getResultList(); | |
List<StudentHistoryDTO> studentHistoryDTOS = new ArrayList<StudentHistoryDTO>(); | |
for (Object[] obj : resultList) { | |
StudentHistoryDTO studentHistoryDTO = new StudentHistoryDTO(); | |
studentHistoryDTO.setFirstName(obj[0].toString()); | |
studentHistoryDTO.setLastName(obj[1].toString()); | |
studentHistoryDTO.setResult(obj[2].toString()); | |
studentHistoryDTO.setSchoolyear(obj[3].toString()); | |
studentHistoryDTO.setCourseId(obj[4].toString()); | |
studentHistoryDTOS.add(studentHistoryDTO); | |
} | |
return studentHistoryDTOS; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment