Поправил ответ для пользователей, чтобы приходило название кафедры, а не ID
This commit is contained in:
@@ -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(
|
||||
.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(),
|
||||
u.getDepartmentId()
|
||||
))
|
||||
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(
|
||||
.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(),
|
||||
u.getDepartmentId()
|
||||
))
|
||||
departmentName);
|
||||
})
|
||||
.toList();
|
||||
logger.info("Получено {} преподавателей", response.size());
|
||||
return response;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -213,7 +213,7 @@ export async function initUsers() {
|
||||
<td>${escapeHtml(u.username)}</td>
|
||||
<td>${escapeHtml(u.fullName || '-')}</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>
|
||||
<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 fullName = document.getElementById('new-fullname').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) {
|
||||
showAlert('create-alert', 'Заполните все поля', 'error');
|
||||
|
||||
@@ -28,7 +28,7 @@
|
||||
<input type="text" id="new-jobtitle" placeholder="Студент / Доцент" required>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="new-department">ID кафедры</label>
|
||||
<label for="new-department">Кафедра</label>
|
||||
<input type="number" id="new-department" placeholder="ID" required>
|
||||
</div>
|
||||
<button type="submit" class="btn-primary">Создать</button>
|
||||
|
||||
Reference in New Issue
Block a user