end-point updateUserStatus added, new DTO: UpdateUserStatusDTO

This commit is contained in:
agavrilov 2025-02-20 14:18:18 +03:00 committed by SunZar
parent 379c9e75a8
commit 90397f7d8e
16 changed files with 51 additions and 23 deletions

View File

@ -2,6 +2,7 @@ package org.example.controller;
import lombok.RequiredArgsConstructor;
import org.example.dto.PassingDTO;
import org.example.dto.UpdateUserStatusDTO;
import org.example.dto.UserDTO;
import org.example.service.PassingService;
import org.example.service.UserService;
@ -53,7 +54,7 @@ public class UserController {
return ResponseEntity.ok(passingService.getAllPassingPaginated(pageable));
}
@PostMapping("/{username/passing/new")
@PostMapping("/{username}/passing/new")
public ResponseEntity<PassingDTO> createPassing(@RequestBody PassingDTO dto) {
return ResponseEntity.status(HttpStatus.CREATED).body(passingService.createPassing(dto));
}
@ -67,4 +68,10 @@ public class UserController {
public ResponseEntity<UserDTO> login(Authentication authentication){
return ResponseEntity.ok(userService.getUserByUsername(authentication.getName()));
}
@PatchMapping("/{username}")
public ResponseEntity<UserDTO> updateUserStatus(@PathVariable String username, @RequestBody UpdateUserStatusDTO dto) {
return ResponseEntity.ok(userService.patchUserStatusByUsername(username, dto));
}
}

View File

@ -10,5 +10,4 @@ public class PassingDTO {
private String type;
private String time;
private String code;
private String authority;
}

View File

@ -0,0 +1,8 @@
package org.example.dto;
import lombok.Data;
@Data
public class UpdateUserStatusDTO {
private String status;
}

View File

@ -11,4 +11,5 @@ public class UserDTO {
private String position;
private String authority;
private String lastvisit;
private String status;
}

View File

@ -24,6 +24,5 @@ public class Passing {
@Column(name = "code")
private String code;
@Column(name = "authorities")
private String authority;
}

View File

@ -39,6 +39,9 @@ public class User implements UserDetails{
@Column(name = "lastvisit")
private String lastvisit;
@Column(name = "status")
private String status;
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return List.of();

View File

@ -9,8 +9,6 @@ import java.util.List;
import java.util.Optional;
public interface PassingRepository extends JpaRepository<Passing, Integer> {
Optional<Passing> findByAuthority(String authority);
List<Passing> findByUsername(String login);
@Override

View File

@ -1,5 +1,6 @@
package org.example.service;
import org.example.dto.UpdateUserStatusDTO;
import org.example.dto.UserDTO;
public interface UserService {
@ -9,5 +10,7 @@ public interface UserService {
UserDTO patchUserByUsername(String username);
UserDTO patchUserStatusByUsername(String username, UpdateUserStatusDTO dto);
}

View File

@ -5,7 +5,6 @@ import org.example.dto.PassingDTO;
import org.example.entity.Passing;
import org.example.entity.User;
import org.example.exception.AuthorityNotFoundException;
import org.example.exception.UserNotFoundException;
import org.example.repository.PassingRepository;
import org.example.repository.UserRepository;
import org.example.service.PassingService;
@ -26,8 +25,8 @@ public class PassingServiceImpl implements PassingService {
@Override
public List<PassingDTO> getAllPassing(String authority) {
Optional<Passing> passingOptional = passingRepository.findByAuthority("ROLE_ADMIN");
if (passingOptional.isEmpty()) {
Optional<User> userOptional = userRepository.findByAuthority("ROLE_ADMIN");
if (userOptional.isEmpty()) {
throw new AuthorityNotFoundException("Ошибка доступа");
}

View File

@ -1,6 +1,7 @@
package org.example.service.impl;
import lombok.RequiredArgsConstructor;
import org.example.dto.UpdateUserStatusDTO;
import org.example.dto.UserDTO;
import org.example.entity.User;
import org.example.exception.UserNotFoundException;
@ -43,4 +44,14 @@ public class UserServiceImpl implements UserService {
return UserMapper.convertToDto(optionalUser.get());
}
@Override
public UserDTO patchUserStatusByUsername(String username, UpdateUserStatusDTO dto) {
User user = userRepository.findByUsername(username).orElseThrow(() -> new UserNotFoundException("логина не существует или неверный"));
user.setStatus(dto.getStatus());
return UserMapper.convertToDto(userRepository.save(user));
}
}

View File

@ -13,7 +13,6 @@ public class PassingMapper {
dto.setType(passing.getType());
dto.setTime(passing.getTime());
dto.setCode(passing.getCode());
dto.setAuthority(passing.getAuthority());
return dto;
}

View File

@ -15,6 +15,7 @@ public class UserMapper {
dto.setPosition(user.getPosition());
dto.setAuthority(user.getAuthority());
dto.setLastvisit(user.getLastvisit());
dto.setStatus(user.getStatus());
return dto;
}

View File

@ -32,6 +32,9 @@
<constraints nullable="false"/>
</column>
<column name="lastvisit" type="VARCHAR(100)"/>
<column name="status" type="VARCHAR(10)">
<constraints nullable="false"/>
</column>
</createTable>
</changeSet>

View File

@ -27,9 +27,6 @@
<column name="code" type="BIGINT">
<constraints nullable="false"/>
</column>
<column name="authorities" type="VARCHAR(20)">
<constraints nullable="false"/>
</column>
</createTable>
</changeSet>

View File

@ -1,5 +1,5 @@
username;password;name;photo;position;authorities;lastVisit
pivanov;$2a$10$eObqG4zk7CbKPPTv0daHm.bcpK6zwyFPpjAVXOeDWrT/3TpVcxpia;Иванов Петр Федорович;https://funnyducks.ru/upload/iblock/0cd/0cdeb7ec3ed6fddda0f90fccee05557d.jpg;Разработчик;ROLE_ADMIN;2024-02-12T08:30:00
ipetrov;$2a$10$eObqG4zk7CbKPPTv0daHm.bcpK6zwyFPpjAVXOeDWrT/3TpVcxpia;Петров Иван Константинович;https://funnyducks.ru/upload/iblock/0cd/0cdeb7ec3ed6fddda0f90fccee05557d.jpg;Аналитик;ROLE_USER;2024-02-30T08:35:00
asemenov;$2a$10$eObqG4zk7CbKPPTv0daHm.bcpK6zwyFPpjAVXOeDWrT/3TpVcxpia;Семенов Анатолий Анатольевич;https://funnyducks.ru/upload/iblock/0cd/0cdeb7ec3ed6fddda0f90fccee05557d.jpg;Разработчик;ROLE_USER;2024-02-31T08:31:00
afedorov;$2a$10$eObqG4zk7CbKPPTv0daHm.bcpK6zwyFPpjAVXOeDWrT/3TpVcxpia;Федоров Александр Сергеевич;https://funnyducks.ru/upload/iblock/0cd/0cdeb7ec3ed6fddda0f90fccee05557d.jpg;Тестировщик;ROLE_USER;2024-02-30T08:36:00
username;password;name;photo;position;authorities;lastVisit;status
pivanov;$2a$10$eObqG4zk7CbKPPTv0daHm.bcpK6zwyFPpjAVXOeDWrT/3TpVcxpia;Иванов Петр Федорович;https://funnyducks.ru/upload/iblock/0cd/0cdeb7ec3ed6fddda0f90fccee05557d.jpg;Разработчик;ROLE_ADMIN;2024-02-12T08:30:00;NoBlock
ipetrov;$2a$10$eObqG4zk7CbKPPTv0daHm.bcpK6zwyFPpjAVXOeDWrT/3TpVcxpia;Петров Иван Константинович;https://funnyducks.ru/upload/iblock/0cd/0cdeb7ec3ed6fddda0f90fccee05557d.jpg;Аналитик;ROLE_USER;2024-02-30T08:35:00;NoBlock
asemenov;$2a$10$eObqG4zk7CbKPPTv0daHm.bcpK6zwyFPpjAVXOeDWrT/3TpVcxpia;Семенов Анатолий Анатольевич;https://funnyducks.ru/upload/iblock/0cd/0cdeb7ec3ed6fddda0f90fccee05557d.jpg;Разработчик;ROLE_USER;2024-02-31T08:31:00;NoBlock
afedorov;$2a$10$eObqG4zk7CbKPPTv0daHm.bcpK6zwyFPpjAVXOeDWrT/3TpVcxpia;Федоров Александр Сергеевич;https://funnyducks.ru/upload/iblock/0cd/0cdeb7ec3ed6fddda0f90fccee05557d.jpg;Тестировщик;ROLE_USER;2024-02-30T08:36:00;NoBlock
1 username password name photo position authorities lastVisit status
2 pivanov $2a$10$eObqG4zk7CbKPPTv0daHm.bcpK6zwyFPpjAVXOeDWrT/3TpVcxpia Иванов Петр Федорович https://funnyducks.ru/upload/iblock/0cd/0cdeb7ec3ed6fddda0f90fccee05557d.jpg Разработчик ROLE_ADMIN 2024-02-12T08:30:00 NoBlock
3 ipetrov $2a$10$eObqG4zk7CbKPPTv0daHm.bcpK6zwyFPpjAVXOeDWrT/3TpVcxpia Петров Иван Константинович https://funnyducks.ru/upload/iblock/0cd/0cdeb7ec3ed6fddda0f90fccee05557d.jpg Аналитик ROLE_USER 2024-02-30T08:35:00 NoBlock
4 asemenov $2a$10$eObqG4zk7CbKPPTv0daHm.bcpK6zwyFPpjAVXOeDWrT/3TpVcxpia Семенов Анатолий Анатольевич https://funnyducks.ru/upload/iblock/0cd/0cdeb7ec3ed6fddda0f90fccee05557d.jpg Разработчик ROLE_USER 2024-02-31T08:31:00 NoBlock
5 afedorov $2a$10$eObqG4zk7CbKPPTv0daHm.bcpK6zwyFPpjAVXOeDWrT/3TpVcxpia Федоров Александр Сергеевич https://funnyducks.ru/upload/iblock/0cd/0cdeb7ec3ed6fddda0f90fccee05557d.jpg Тестировщик ROLE_USER 2024-02-30T08:36:00 NoBlock

View File

@ -1,5 +1,5 @@
username;type;time;code;authorities
pivanov;Карта;12:00;1234567890123456789;ROLE_ADMIN
ipetrov;Вход со смартфона;13:00;9223372036854775807;ROLE_USER
asemenov;Карта;10:00;1234567890123456789;ROLE_USER
pivanov;Вход со смартфона;15:00;1234567890123456789;ROLE_USER
username;type;time;code
pivanov;Карта;2024-02-12T08:30:00;1234567890123456789
ipetrov;Вход со смартфона;2024-02-12T10:30:00;9223372036854775807
asemenov;Карта;2024-02-12T09:30:00;1234567890123456789
pivanov;Вход со смартфона;2024-02-12T08:30:00;1234567890123456789

1 username type time code authorities
2 pivanov Карта 12:00 2024-02-12T08:30:00 1234567890123456789 ROLE_ADMIN
3 ipetrov Вход со смартфона 13:00 2024-02-12T10:30:00 9223372036854775807 ROLE_USER
4 asemenov Карта 10:00 2024-02-12T09:30:00 1234567890123456789 ROLE_USER
5 pivanov Вход со смартфона 15:00 2024-02-12T08:30:00 1234567890123456789 ROLE_USER