Поправил ответ для пользователей, чтобы приходило название кафедры, а не ID

This commit is contained in:
ProstoDenya01
2026-03-27 19:02:27 +03:00
parent 6f33e23e17
commit 0b9d063266
4 changed files with 46 additions and 42 deletions

View File

@@ -2,8 +2,10 @@ package com.magistr.app.controller;
import com.magistr.app.dto.CreateUserRequest;
import com.magistr.app.dto.UserResponse;
import com.magistr.app.model.Department;
import com.magistr.app.model.Role;
import com.magistr.app.model.User;
import com.magistr.app.repository.DepartmentRepository;
import com.magistr.app.repository.UserRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -22,11 +24,13 @@ public class UserController {
private static final Logger logger = LoggerFactory.getLogger(UserController.class);
private final UserRepository userRepository;
private final DepartmentRepository departmentRepository;
private final BCryptPasswordEncoder passwordEncoder;
public UserController(UserRepository userRepository, BCryptPasswordEncoder passwordEncoder) {
public UserController(UserRepository userRepository, BCryptPasswordEncoder passwordEncoder, DepartmentRepository departmentRepository) {
this.userRepository = userRepository;
this.passwordEncoder = passwordEncoder;
this.departmentRepository = departmentRepository;
}
@GetMapping
@@ -36,14 +40,19 @@ public class UserController {
List<User> users = userRepository.findAll();
List<UserResponse> response = users.stream()
.map(u -> new UserResponse(
u.getId(),
u.getUsername(),
u.getRole().name(),
u.getFullName(),
u.getJobTitle(),
u.getDepartmentId()
))
.map(u -> {
String departmentName = departmentRepository.findById(u.getDepartmentId())
.map(Department::getDepartmentName)
.orElse("Неизвестно");
return new UserResponse(
u.getId(),
u.getUsername(),
u.getRole().name(),
u.getFullName(),
u.getJobTitle(),
departmentName);
})
.toList();
logger.info("Получено {} пользователей", response.size());
return response;
@@ -62,14 +71,19 @@ public class UserController {
List<User> users = userRepository.findByRole(Role.TEACHER);
List<UserResponse> response = users.stream()
.map(u -> new UserResponse(
u.getId(),
u.getUsername(),
u.getRole().name(),
u.getFullName(),
u.getJobTitle(),
u.getDepartmentId()
))
.map(u -> {
String departmentName = departmentRepository.findById(u.getDepartmentId())
.map(Department::getDepartmentName)
.orElse("Неизвестно");
return new UserResponse(
u.getId(),
u.getUsername(),
u.getRole().name(),
u.getFullName(),
u.getJobTitle(),
departmentName);
})
.toList();
logger.info("Получено {} преподавателей", response.size());
return response;

View File

@@ -10,11 +10,21 @@ public class UserResponse {
private String role;
private String fullName;
private String jobTitle;
private String departmentName;
private Long departmentId;
public UserResponse() {
}
public UserResponse(Long id, String username, String role, String fullName, String jobTitle, String departmentName) {
this.id = id;
this.username = username;
this.role = role;
this.fullName = fullName;
this.jobTitle = jobTitle;
this.departmentName = departmentName;
}
public UserResponse(Long id, String username, String role, String fullName, String jobTitle, Long departmentId) {
this.id = id;
this.username = username;
@@ -36,47 +46,27 @@ public class UserResponse {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getJobTitle() {
return jobTitle;
}
public void setJobTitle(String jobTitle) {
this.jobTitle = jobTitle;
public String getDepartmentName() {
return departmentName;
}
public Long getDepartmentId() {
return departmentId;
}
public void setDepartmentId(Long departmentId) {
this.departmentId = departmentId;
}
}