Добавил все новые поля (departmentId, course) для групп. Добавил логирование для GroupController.java
This commit is contained in:
@@ -6,6 +6,9 @@ import com.magistr.app.model.EducationForm;
|
|||||||
import com.magistr.app.model.StudentGroup;
|
import com.magistr.app.model.StudentGroup;
|
||||||
import com.magistr.app.repository.EducationFormRepository;
|
import com.magistr.app.repository.EducationFormRepository;
|
||||||
import com.magistr.app.repository.GroupRepository;
|
import com.magistr.app.repository.GroupRepository;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
@@ -17,6 +20,8 @@ import java.util.Optional;
|
|||||||
@RequestMapping("/api/groups")
|
@RequestMapping("/api/groups")
|
||||||
public class GroupController {
|
public class GroupController {
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(GroupController.class);
|
||||||
|
|
||||||
private final GroupRepository groupRepository;
|
private final GroupRepository groupRepository;
|
||||||
private final EducationFormRepository educationFormRepository;
|
private final EducationFormRepository educationFormRepository;
|
||||||
|
|
||||||
@@ -28,56 +33,105 @@ public class GroupController {
|
|||||||
|
|
||||||
@GetMapping
|
@GetMapping
|
||||||
public List<GroupResponse> getAllGroups() {
|
public List<GroupResponse> getAllGroups() {
|
||||||
return groupRepository.findAll().stream()
|
logger.info("Получен запрос на получение всех групп");
|
||||||
.map(g -> new GroupResponse(
|
|
||||||
|
try {
|
||||||
|
List<StudentGroup> groups = groupRepository.findAll();
|
||||||
|
|
||||||
|
List<GroupResponse> response = groups.stream()
|
||||||
|
.map(g -> new GroupResponse(
|
||||||
g.getId(),
|
g.getId(),
|
||||||
g.getName(),
|
g.getName(),
|
||||||
g.getGroupSize(),
|
g.getGroupSize(),
|
||||||
g.getEducationForm().getId(),
|
g.getEducationForm().getId(),
|
||||||
g.getEducationForm().getName()))
|
g.getEducationForm().getName(),
|
||||||
.toList();
|
g.getDepartmentId(),
|
||||||
|
g.getCourse()
|
||||||
|
))
|
||||||
|
.toList();
|
||||||
|
logger.info("Получено {} групп", response.size());
|
||||||
|
return response;
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error("Ошибка при получении списка групп: {}", e.getMessage(), e);
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping
|
@PostMapping
|
||||||
public ResponseEntity<?> createGroup(@RequestBody CreateGroupRequest request) {
|
public ResponseEntity<?> createGroup(@RequestBody CreateGroupRequest request) {
|
||||||
if (request.getName() == null || request.getName().isBlank()) {
|
logger.info("Получен запрос на создание новой группы: name = {}, groupSize = {}, educationFormId = {}, departmentId = {}, course = {}",
|
||||||
return ResponseEntity.badRequest().body(Map.of("message", "Название группы обязательно"));
|
request.getName(), request.getGroupSize(), request.getEducationFormId(), request.getDepartmentId(), request.getCourse());
|
||||||
}
|
try {
|
||||||
if (groupRepository.findByName(request.getName().trim()).isPresent()) {
|
if (request.getName() == null || request.getName().isBlank()) {
|
||||||
return ResponseEntity.badRequest().body(Map.of("message", "Группа с таким названием уже существует"));
|
String errorMessage = "Название группы обязательно";
|
||||||
}
|
logger.error("Ошибка валидации: {}", errorMessage);
|
||||||
if (request.getGroupSize() == null) {
|
return ResponseEntity.badRequest().body(Map.of("message", errorMessage));
|
||||||
return ResponseEntity.badRequest().body(Map.of("message", "Численность группы обязательна"));
|
}
|
||||||
}
|
if (groupRepository.findByName(request.getName().trim()).isPresent()) {
|
||||||
if (request.getEducationFormId() == null) {
|
String errorMessage = "Группа с таким названием уже существует";
|
||||||
return ResponseEntity.badRequest().body(Map.of("message", "Форма обучения обязательна"));
|
logger.error("Ошибка валидации: {}", errorMessage);
|
||||||
}
|
return ResponseEntity.badRequest().body(Map.of("message", errorMessage));
|
||||||
|
}
|
||||||
|
if (request.getGroupSize() == null) {
|
||||||
|
String errorMessage = "Численность группы обязательна";
|
||||||
|
logger.error("Ошибка валидации: {}", errorMessage);
|
||||||
|
return ResponseEntity.badRequest().body(Map.of("message", errorMessage));
|
||||||
|
}
|
||||||
|
if (request.getEducationFormId() == null) {
|
||||||
|
String errorMessage = "Форма обучения обязательна";
|
||||||
|
logger.error("Ошибка валидации: {}", errorMessage);
|
||||||
|
return ResponseEntity.badRequest().body(Map.of("message", errorMessage));
|
||||||
|
}
|
||||||
|
if (request.getDepartmentId() == null || request.getDepartmentId() == 0) {
|
||||||
|
String errorMessage = "ID кафедры обязателен";
|
||||||
|
logger.error("Ошибка валидации: {}", errorMessage);
|
||||||
|
return ResponseEntity.badRequest().body(Map.of("message", errorMessage));
|
||||||
|
}
|
||||||
|
if (request.getCourse() == null || request.getCourse() == 0) {
|
||||||
|
String errorMessage = "Курс обязателен";
|
||||||
|
logger.error("Ошибка валидации: {}", errorMessage);
|
||||||
|
return ResponseEntity.badRequest().body(Map.of("message", errorMessage));
|
||||||
|
}
|
||||||
|
|
||||||
Optional<EducationForm> efOpt = educationFormRepository.findById(request.getEducationFormId());
|
Optional<EducationForm> efOpt = educationFormRepository.findById(request.getEducationFormId());
|
||||||
if (efOpt.isEmpty()) {
|
if (efOpt.isEmpty()) {
|
||||||
return ResponseEntity.badRequest().body(Map.of("message", "Форма обучения не найдена"));
|
return ResponseEntity.badRequest().body(Map.of("message", "Форма обучения не найдена"));
|
||||||
|
}
|
||||||
|
|
||||||
|
StudentGroup group = new StudentGroup();
|
||||||
|
group.setName(request.getName().trim());
|
||||||
|
group.setGroupSize(request.getGroupSize());
|
||||||
|
group.setEducationForm(efOpt.get());
|
||||||
|
group.setDepartmentId(request.getDepartmentId());
|
||||||
|
group.setCourse(request.getCourse());
|
||||||
|
groupRepository.save(group);
|
||||||
|
|
||||||
|
logger.info("Группа успешно создана с ID - {}", group.getId());
|
||||||
|
|
||||||
|
return ResponseEntity.ok(new GroupResponse(
|
||||||
|
group.getId(),
|
||||||
|
group.getName(),
|
||||||
|
group.getGroupSize(),
|
||||||
|
group.getEducationForm().getId(),
|
||||||
|
group.getEducationForm().getName(),
|
||||||
|
group.getDepartmentId(),
|
||||||
|
group.getCourse()));
|
||||||
|
} catch (Exception e ) {
|
||||||
|
logger.error("Ошибка при создании группы: {}", e.getMessage(), e);
|
||||||
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||||
|
.body(Map.of("message", "Произошла ошибка при создании группы: " + e.getMessage()));
|
||||||
}
|
}
|
||||||
|
|
||||||
StudentGroup group = new StudentGroup();
|
|
||||||
group.setName(request.getName().trim());
|
|
||||||
group.setGroupSize(request.getGroupSize());
|
|
||||||
group.setEducationForm(efOpt.get());
|
|
||||||
groupRepository.save(group);
|
|
||||||
|
|
||||||
return ResponseEntity.ok(new GroupResponse(
|
|
||||||
group.getId(),
|
|
||||||
group.getName(),
|
|
||||||
group.getGroupSize(),
|
|
||||||
group.getEducationForm().getId(),
|
|
||||||
group.getEducationForm().getName()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@DeleteMapping("/{id}")
|
@DeleteMapping("/{id}")
|
||||||
public ResponseEntity<?> deleteGroup(@PathVariable Long id) {
|
public ResponseEntity<?> deleteGroup(@PathVariable Long id) {
|
||||||
|
logger.info("Получен запрос на удаление группы с ID - {}", id);
|
||||||
if (!groupRepository.existsById(id)) {
|
if (!groupRepository.existsById(id)) {
|
||||||
|
logger.info("Группа с ID - {} не найдена", id);
|
||||||
return ResponseEntity.notFound().build();
|
return ResponseEntity.notFound().build();
|
||||||
}
|
}
|
||||||
groupRepository.deleteById(id);
|
groupRepository.deleteById(id);
|
||||||
|
logger.info("Группа с ID - {} успешно удалена", id);
|
||||||
return ResponseEntity.ok(Map.of("message", "Группа удалена"));
|
return ResponseEntity.ok(Map.of("message", "Группа удалена"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ public class CreateGroupRequest {
|
|||||||
private String name;
|
private String name;
|
||||||
private Long groupSize;
|
private Long groupSize;
|
||||||
private Long educationFormId;
|
private Long educationFormId;
|
||||||
|
private Long departmentId;
|
||||||
|
private Integer course;
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return name;
|
return name;
|
||||||
@@ -29,4 +31,20 @@ public class CreateGroupRequest {
|
|||||||
public void setEducationFormId(Long educationFormId) {
|
public void setEducationFormId(Long educationFormId) {
|
||||||
this.educationFormId = educationFormId;
|
this.educationFormId = educationFormId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Long getDepartmentId() {
|
||||||
|
return departmentId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDepartmentId(Long departmentId) {
|
||||||
|
this.departmentId = departmentId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getCourse() {
|
||||||
|
return course;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCourse(Integer course) {
|
||||||
|
this.course = course;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,13 +7,17 @@ public class GroupResponse {
|
|||||||
private Long groupSize;
|
private Long groupSize;
|
||||||
private Long educationFormId;
|
private Long educationFormId;
|
||||||
private String educationFormName;
|
private String educationFormName;
|
||||||
|
private Long departmentId;
|
||||||
|
private Integer course;
|
||||||
|
|
||||||
public GroupResponse(Long id, String name, Long groupSize, Long educationFormId, String educationFormName) {
|
public GroupResponse(Long id, String name, Long groupSize, Long educationFormId, String educationFormName, Long departmentId, Integer course) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.groupSize = groupSize;
|
this.groupSize = groupSize;
|
||||||
this.educationFormId = educationFormId;
|
this.educationFormId = educationFormId;
|
||||||
this.educationFormName = educationFormName;
|
this.educationFormName = educationFormName;
|
||||||
|
this.departmentId = departmentId;
|
||||||
|
this.course = course;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Long getId() {
|
public Long getId() {
|
||||||
@@ -35,4 +39,12 @@ public class GroupResponse {
|
|||||||
public String getEducationFormName() {
|
public String getEducationFormName() {
|
||||||
return educationFormName;
|
return educationFormName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Long getDepartmentId() {
|
||||||
|
return departmentId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getCourse() {
|
||||||
|
return course;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,6 +20,12 @@ public class StudentGroup {
|
|||||||
@JoinColumn(name = "education_form_id", nullable = false)
|
@JoinColumn(name = "education_form_id", nullable = false)
|
||||||
private EducationForm educationForm;
|
private EducationForm educationForm;
|
||||||
|
|
||||||
|
@Column(name = "department_id", nullable = false)
|
||||||
|
private Long departmentId;
|
||||||
|
|
||||||
|
@Column(name = "course", nullable = false)
|
||||||
|
private Integer course;
|
||||||
|
|
||||||
public StudentGroup() {
|
public StudentGroup() {
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,4 +60,20 @@ public class StudentGroup {
|
|||||||
public void setEducationForm(EducationForm educationForm) {
|
public void setEducationForm(EducationForm educationForm) {
|
||||||
this.educationForm = educationForm;
|
this.educationForm = educationForm;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Long getDepartmentId() {
|
||||||
|
return departmentId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDepartmentId(Long departmentId) {
|
||||||
|
this.departmentId = departmentId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getCourse() {
|
||||||
|
return course;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCourse(Integer course) {
|
||||||
|
this.course = course;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user