Advertisement
Guest User

StudentController.java

a guest
Jun 29th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.70 KB | None | 0 0
  1. package com.ap.model.users.student;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.IOException;
  6. import java.nio.file.Files;
  7. import java.nio.file.Path;
  8. import java.nio.file.Paths;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. import java.util.ResourceBundle;
  12.  
  13. import javax.websocket.server.PathParam;
  14.  
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.core.io.InputStreamResource;
  17. import org.springframework.core.io.Resource;
  18. import org.springframework.http.HttpStatus;
  19. import org.springframework.http.MediaType;
  20. import org.springframework.http.ResponseEntity;
  21. import org.springframework.web.bind.annotation.PathVariable;
  22. import org.springframework.web.bind.annotation.RequestBody;
  23. import org.springframework.web.bind.annotation.RequestMapping;
  24. import org.springframework.web.bind.annotation.RequestMethod;
  25. import org.springframework.web.bind.annotation.RequestParam;
  26. import org.springframework.web.bind.annotation.RequestPart;
  27. import org.springframework.web.bind.annotation.ResponseBody;
  28. import org.springframework.web.bind.annotation.RestController;
  29. import org.springframework.web.multipart.MultipartFile;
  30.  
  31. import com.ap.model.dokument.Dokument;
  32. import com.ap.model.dokument.DokumentService;
  33. import com.ap.model.users.korisnik.Korisnik;
  34. import com.ap.web.dto.DokumentDTO;
  35. import com.ap.web.dto.StudentDTO;
  36.  
  37.  
  38.  
  39.  
  40.  
  41. @RestController
  42. @RequestMapping(value="api/student")
  43. public class StudentController {
  44.    
  45.     @Autowired
  46.     StudentService studentService;
  47.    
  48.     @Autowired
  49.     DokumentService dokumentService;
  50.    
  51.     @RequestMapping(method=RequestMethod.GET)
  52.     public ResponseEntity<List<StudentDTO>> getStudenti(){
  53.         List<Student> studenti = studentService.findAll();
  54.         List<StudentDTO> studentDTOs = new ArrayList<>();
  55.         for (Student student : studenti) {
  56.             studentDTOs.add(new StudentDTO(student));
  57.         }
  58.         return new ResponseEntity<>(studentDTOs, HttpStatus.OK);
  59.        
  60.     }
  61.    
  62.     @RequestMapping(value="/{id}", method=RequestMethod.GET)
  63.     public ResponseEntity<Student> getStudent(@PathVariable("id") Long id){
  64.         Student Student = studentService.findOne(id);
  65.         if(Student == null){
  66.             return new ResponseEntity<>(HttpStatus.NOT_FOUND);
  67.         }
  68.        
  69.         return new ResponseEntity<>(Student, HttpStatus.OK);
  70.     }
  71.  
  72.    
  73.     @RequestMapping(value="/findByUsername", method = RequestMethod.GET)
  74.     public ResponseEntity<StudentDTO> getUser(@RequestParam("username") String username) {
  75.         try {
  76.             Student student = studentService.findByUserName(username);
  77.             StudentDTO studentDTO = new StudentDTO(student);
  78.            
  79.             return new ResponseEntity<StudentDTO>(studentDTO, HttpStatus.OK);
  80.         } catch (Exception ex) {
  81.             return new ResponseEntity<StudentDTO>( HttpStatus.BAD_REQUEST);
  82.         }
  83.     }
  84.    
  85. //  @RequestMapping(method=RequestMethod.POST, consumes="application/json")
  86. //  public ResponseEntity<Student> saveStudent(@RequestBody Student Student){
  87. //     
  88. //     
  89. // 
  90. //      Student = studentService.save(Student);
  91. //      return new ResponseEntity<>( HttpStatus.CREATED);  
  92. //  }
  93.    
  94.     @RequestMapping(method=RequestMethod.PUT, consumes="application/json")
  95.     public ResponseEntity<Student> updateStudent(@RequestBody Student Student){
  96.         //a Student must exist
  97.        
  98.         if (Student == null) {
  99.             return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
  100.         }
  101.        
  102.        
  103.    
  104.         Student = studentService.save(Student);
  105.         return new ResponseEntity<>(HttpStatus.OK);
  106.     }
  107.    
  108.     @RequestMapping( method=RequestMethod.DELETE)
  109.     public ResponseEntity<Void> deleteKurs(@RequestParam("id") Long id){
  110.         Student Student = studentService.findOne(id);
  111.         if (Student != null){
  112.             studentService.remove(id);
  113.             return new ResponseEntity<>(HttpStatus.OK);
  114.         } else {       
  115.             return new ResponseEntity<>(HttpStatus.NOT_FOUND);
  116.         }
  117.     }
  118.    
  119.     @RequestMapping(method=RequestMethod.POST, consumes = {"multipart/form-data"} ,value="/upload")
  120.     @ResponseBody
  121.     public  void  saveEBook(@RequestPart("student") Student student,@RequestPart("file") MultipartFile file) throws IOException{
  122.         String storagePath = ResourceBundle.getBundle("app").getString("storage");
  123.          try {
  124.  
  125.                 // Get the file and save it somewhere
  126.                 byte[] bytes = file.getBytes();
  127.                 Path path = Paths.get(storagePath + file.getOriginalFilename());
  128.                 Files.write(path, bytes);
  129.                 Dokument dokument = new Dokument();
  130.                 dokument.setNaziv(file.getOriginalFilename());
  131.                 dokument.setStudent(student);
  132.                 dokumentService.save(dokument);
  133.                
  134.  
  135.             } catch (IOException e) {
  136.                 e.printStackTrace();
  137.             }
  138.    
  139.        
  140.            
  141.     }
  142.    
  143.     @RequestMapping(path = "/download", method = RequestMethod.GET)
  144.     public ResponseEntity<Resource> download(@RequestParam("fileName") String imeFajla) throws IOException {
  145.         String storagePath = ResourceBundle.getBundle("app").getString("storage");
  146.         File file = new File(storagePath+imeFajla);
  147.  
  148.         InputStreamResource resource = new InputStreamResource(new FileInputStream(file));
  149.  
  150.         return ResponseEntity.ok()
  151.                 .contentType(MediaType.parseMediaType("application/pdf"))
  152.                 .body(resource);
  153.     }
  154.    
  155.     @RequestMapping(path = "/getDokumentsByStudent", method = RequestMethod.GET)
  156.     public ResponseEntity<ArrayList<DokumentDTO>> getDokumentsForStudent(@RequestParam("username") String userName) {
  157.         Student student=studentService.findByUserName(userName);
  158.         List<Dokument> dokumentiStudenta=dokumentService.getByStudent(student);
  159.         ArrayList<DokumentDTO> dokumentDTOs = new ArrayList<>();
  160.         for (Dokument dokument : dokumentiStudenta) {
  161.             dokumentDTOs.add(new DokumentDTO(dokument));
  162.         }
  163.        
  164.         return new ResponseEntity<>(dokumentDTOs,HttpStatus.OK);
  165.     }
  166.        
  167.    
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement