esoe
6 months ago
4 changed files with 129 additions and 3 deletions
@ -0,0 +1,72 @@
@@ -0,0 +1,72 @@
|
||||
package ru.molokoin.resourceserviceapi.controllers; |
||||
|
||||
import java.util.List; |
||||
|
||||
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.DeleteMapping; |
||||
import org.springframework.web.bind.annotation.GetMapping; |
||||
import org.springframework.web.bind.annotation.PathVariable; |
||||
import org.springframework.web.bind.annotation.PostMapping; |
||||
import org.springframework.web.bind.annotation.PutMapping; |
||||
import org.springframework.web.bind.annotation.RequestBody; |
||||
import org.springframework.web.bind.annotation.RequestMapping; |
||||
import org.springframework.web.bind.annotation.RestController; |
||||
|
||||
import ru.molokoin.resourceserviceapi.entities.Student; |
||||
import ru.molokoin.resourceserviceapi.repositories.StudentFace; |
||||
|
||||
@RestController |
||||
@RequestMapping(path = "/", consumes = {"*/*"}) |
||||
public class StudentController { |
||||
@Autowired |
||||
private StudentFace repo; |
||||
|
||||
@GetMapping("/student/list") |
||||
public ResponseEntity<List<Student>> getStudents(){ |
||||
return new ResponseEntity<>(repo.findAll(), HttpStatus.OK); |
||||
} |
||||
|
||||
@GetMapping("/student/{id}") |
||||
public ResponseEntity<?> getStudentByID(@PathVariable Integer id){ |
||||
return new ResponseEntity<>(repo.findStudentById(id), HttpStatus.OK); |
||||
} |
||||
|
||||
@PostMapping(path = "/student/create", |
||||
consumes = MediaType.APPLICATION_JSON_VALUE, |
||||
produces = MediaType.APPLICATION_JSON_VALUE) |
||||
public ResponseEntity<?> saveStudent(@RequestBody Student student) { |
||||
repo.save(student); |
||||
return new ResponseEntity<>(student, HttpStatus.CREATED); |
||||
} |
||||
|
||||
@PutMapping(path = "/student/update/{id}", |
||||
consumes = MediaType.APPLICATION_JSON_VALUE, |
||||
produces = MediaType.APPLICATION_JSON_VALUE) |
||||
public ResponseEntity<?> updateStudent(@PathVariable Integer id, @RequestBody Student student) { |
||||
Student s = repo.findStudentById(id); |
||||
s.setFirst_name(student.getFirst_name()); |
||||
s.setSecond_name(student.getSecond_name()); |
||||
s.setLast_name(student.getLast_name()); |
||||
s.setProfession(student.getProfession()); |
||||
s.setCategory(student.getCategory()); |
||||
s.setDirection(student.getDirection()); |
||||
s.setSnils(student.getSnils()); |
||||
s.setBerth(student.getBerth()); |
||||
s.setSitizenship(student.getSitizenship()); |
||||
s.setSex(student.getSex()); |
||||
s.setOrganization(student.getOrganization()); |
||||
repo.save(s); |
||||
return new ResponseEntity<>(repo.findStudentById(id), HttpStatus.CREATED); |
||||
} |
||||
|
||||
@DeleteMapping("/student/delete/{id}") |
||||
public ResponseEntity<String> deleteStudent(@PathVariable Long id){ |
||||
Student s = repo.findStudentById(id); |
||||
System.out.println(s.toString()); |
||||
repo.delete(s); |
||||
return new ResponseEntity<>("Запись id#" + id + " удалена ... ", HttpStatus.OK); |
||||
} |
||||
} |
@ -0,0 +1,44 @@
@@ -0,0 +1,44 @@
|
||||
package ru.molokoin.resourceserviceapi.entities; |
||||
|
||||
import java.util.Date; |
||||
|
||||
import org.hibernate.annotations.OnDelete; |
||||
import org.hibernate.annotations.OnDeleteAction; |
||||
|
||||
import jakarta.persistence.Entity; |
||||
import jakarta.persistence.FetchType; |
||||
import jakarta.persistence.GeneratedValue; |
||||
import jakarta.persistence.GenerationType; |
||||
import jakarta.persistence.Id; |
||||
import jakarta.persistence.ManyToOne; |
||||
import lombok.AllArgsConstructor; |
||||
import lombok.Data; |
||||
import lombok.NoArgsConstructor; |
||||
|
||||
/** |
||||
* Сведения об обученных |
||||
*/ |
||||
@NoArgsConstructor |
||||
@AllArgsConstructor |
||||
@Entity |
||||
@Data |
||||
public class Student { |
||||
@Id |
||||
@GeneratedValue(strategy=GenerationType.AUTO) |
||||
private long id; |
||||
private String first_name;//Имя
|
||||
private String second_name;//Отчество
|
||||
private String last_name;//Фамилия
|
||||
private String profession;//Профессия
|
||||
private String category;//Категория (ИТР или рабочий)
|
||||
private String direction;//Структурное подразделение
|
||||
private String snils;//СНИЛС
|
||||
private Date berth;//Дата рождения
|
||||
private String sitizenship;//Гражданство
|
||||
private String sex;//Пол (м/ж)
|
||||
|
||||
@ManyToOne(fetch = FetchType.EAGER, optional = false) |
||||
@OnDelete(action = OnDeleteAction.SET_NULL) |
||||
private Organization organization;//Организация - работодатель
|
||||
|
||||
} |
@ -0,0 +1,12 @@
@@ -0,0 +1,12 @@
|
||||
package ru.molokoin.resourceserviceapi.repositories; |
||||
|
||||
import java.util.List; |
||||
|
||||
import org.springframework.data.repository.ListCrudRepository; |
||||
|
||||
import ru.molokoin.resourceserviceapi.entities.Student; |
||||
|
||||
public interface StudentFace extends ListCrudRepository<Student, Long>{ |
||||
List<Student> findAll(); |
||||
Student findStudentById(long id); |
||||
} |
Loading…
Reference in new issue