// Minimal Spring Boot controller demonstrating a CRUD REST endpoint.
// This is a fragment — full project layout: see Lab 6.
//
// Companion files (omitted): Student.java entity, StudentRepository interface,
// StudentService.java, application.properties, pom.xml.

package tz.ac.suza.wt.students;

import jakarta.validation.Valid;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;

import java.util.List;

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

    private final StudentService svc;

    public StudentController(StudentService svc) {
        this.svc = svc;
    }

    @GetMapping
    public List<Student> list() {
        return svc.all();
    }

    @GetMapping("/{id}")
    public Student get(@PathVariable Long id) {
        return svc.byId(id);
    }

    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public Student create(@Valid @RequestBody Student s) {
        return svc.create(s);
    }

    @PutMapping("/{id}")
    public Student update(@PathVariable Long id, @Valid @RequestBody Student s) {
        return svc.update(id, s);
    }

    @DeleteMapping("/{id}")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void delete(@PathVariable Long id) {
        svc.delete(id);
    }
}
