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

View File

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

View File

@@ -213,7 +213,7 @@ export async function initUsers() {
<td>${escapeHtml(u.username)}</td> <td>${escapeHtml(u.username)}</td>
<td>${escapeHtml(u.fullName || '-')}</td> <td>${escapeHtml(u.fullName || '-')}</td>
<td>${escapeHtml(u.jobTitle || '-')}</td> <td>${escapeHtml(u.jobTitle || '-')}</td>
<td>${u.departmentId || '-'}</td> <td>${u.departmentName || '-'}</td>
<td><span class="badge ${ROLE_BADGE[u.role] || ''}">${ROLE_LABELS[u.role] || escapeHtml(u.role)}</span></td> <td><span class="badge ${ROLE_BADGE[u.role] || ''}">${ROLE_LABELS[u.role] || escapeHtml(u.role)}</span></td>
<td> <td>
<button class="btn-delete" data-id="${u.id}">Удалить</button> <button class="btn-delete" data-id="${u.id}">Удалить</button>
@@ -383,7 +383,7 @@ export async function initUsers() {
const role = document.getElementById('new-role').value; const role = document.getElementById('new-role').value;
const fullName = document.getElementById('new-fullname').value.trim(); const fullName = document.getElementById('new-fullname').value.trim();
const jobTitle = document.getElementById('new-jobtitle').value.trim(); const jobTitle = document.getElementById('new-jobtitle').value.trim();
const departmentId = document.getElementById('new-department').value; const department = document.getElementById('new-department').value;
if (!username || !password || !fullName || !jobTitle || !departmentId) { if (!username || !password || !fullName || !jobTitle || !departmentId) {
showAlert('create-alert', 'Заполните все поля', 'error'); showAlert('create-alert', 'Заполните все поля', 'error');

View File

@@ -28,7 +28,7 @@
<input type="text" id="new-jobtitle" placeholder="Студент / Доцент" required> <input type="text" id="new-jobtitle" placeholder="Студент / Доцент" required>
</div> </div>
<div class="form-group"> <div class="form-group">
<label for="new-department">ID кафедры</label> <label for="new-department">Кафедра</label>
<input type="number" id="new-department" placeholder="ID" required> <input type="number" id="new-department" placeholder="ID" required>
</div> </div>
<button type="submit" class="btn-primary">Создать</button> <button type="submit" class="btn-primary">Создать</button>