package com.school.project.controller;

import com.school.project.model.Student;
import com.school.project.service.interfaces.StudentService;
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.util.List;
import java.util.Optional;

@RestController
public class StudentController {
    @Autowired(required=true)
    public StudentService studentService;

    @PostMapping("/student")
    public Student saveStudent(@RequestBody Student student){
        return studentService.saveStudent(student);
    }

    @GetMapping("/students")
    public ResponseEntity<List<Student>> readAllStudents(){
        List<Student> students=studentService.readAllStudents();
        if(students.size()<=0){
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }

        return ResponseEntity.of(Optional.of(students));

    }

    @GetMapping("student/{id}")
    public Optional<Student> readStudentById(@PathVariable("id") Long id){
       return studentService.readStudentById(id);

    }

    @PutMapping("student/{id}")
    public Student updateStudentById(@RequestBody Student student,@PathVariable("id") Long id){
        return studentService.updateStudentById(student,id);
    }

    @DeleteMapping("student/{id}")
    public void deleteStudentById(@PathVariable("id") Long id){
        studentService.deleteStudentById(id);
    }
}