feat: implement UserService and add REST controllers
This commit is contained in:
@@ -0,0 +1,57 @@
|
|||||||
|
package com.example.bankcards.controller;
|
||||||
|
|
||||||
|
import com.example.bankcards.dto.CardResponse;
|
||||||
|
import com.example.bankcards.dto.CreateCardRequest;
|
||||||
|
import com.example.bankcards.service.CardService;
|
||||||
|
import jakarta.validation.Valid;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.data.domain.Pageable;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.security.access.prepost.PreAuthorize;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api/admin/cards")
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
@PreAuthorize("hasAuthority('ROLE_ADMIN')")
|
||||||
|
public class AdminCardController {
|
||||||
|
|
||||||
|
private final CardService cardService;
|
||||||
|
|
||||||
|
@PostMapping
|
||||||
|
public ResponseEntity<CardResponse> createCard(
|
||||||
|
@Valid @RequestBody CreateCardRequest request
|
||||||
|
) {
|
||||||
|
return ResponseEntity.status(HttpStatus.CREATED).body(
|
||||||
|
cardService.createCard(request)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
public ResponseEntity<Page<CardResponse>> getAllCards(Pageable pageable) {
|
||||||
|
return ResponseEntity.ok(cardService.getAllCards(pageable));
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/{id}")
|
||||||
|
public ResponseEntity<CardResponse> getCard(@PathVariable Long id) {
|
||||||
|
return ResponseEntity.ok(cardService.getCard(id, null, true));
|
||||||
|
}
|
||||||
|
|
||||||
|
@PatchMapping("/{id}/block")
|
||||||
|
public ResponseEntity<CardResponse> blockCard(@PathVariable Long id) {
|
||||||
|
return ResponseEntity.ok(cardService.blockCard(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
@PatchMapping("/{id}/activate")
|
||||||
|
public ResponseEntity<CardResponse> activateCard(@PathVariable Long id) {
|
||||||
|
return ResponseEntity.ok(cardService.activateCard(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping("/{id}")
|
||||||
|
public ResponseEntity<Void> deleteCard(@PathVariable Long id) {
|
||||||
|
cardService.deleteCard(id);
|
||||||
|
return ResponseEntity.noContent().build();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
package com.example.bankcards.controller;
|
||||||
|
|
||||||
|
import com.example.bankcards.dto.UpdateUserRoleRequest;
|
||||||
|
import com.example.bankcards.dto.UserResponse;
|
||||||
|
import com.example.bankcards.service.UserService;
|
||||||
|
import jakarta.validation.Valid;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.data.domain.Pageable;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.security.access.prepost.PreAuthorize;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api/admin/users")
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
@PreAuthorize("hasAuthority('ROLE_ADMIN')")
|
||||||
|
public class AdminUserController {
|
||||||
|
|
||||||
|
private final UserService userService;
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
public ResponseEntity<Page<UserResponse>> getAllUsers(Pageable pageable) {
|
||||||
|
return ResponseEntity.ok(userService.getAllUsers(pageable));
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/{id}")
|
||||||
|
public ResponseEntity<UserResponse> getUser(@PathVariable Long id) {
|
||||||
|
return ResponseEntity.ok(userService.getUser(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
@PatchMapping("/{id}/role")
|
||||||
|
public ResponseEntity<UserResponse> updateRole(
|
||||||
|
@PathVariable Long id,
|
||||||
|
@Valid @RequestBody UpdateUserRoleRequest request
|
||||||
|
) {
|
||||||
|
return ResponseEntity.ok(userService.updateRole(id, request));
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping("/{id}")
|
||||||
|
public ResponseEntity<Void> deleteUser(@PathVariable Long id) {
|
||||||
|
userService.deleteUser(id);
|
||||||
|
return ResponseEntity.noContent().build();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
package com.example.bankcards.controller;
|
||||||
|
|
||||||
|
import com.example.bankcards.dto.CardResponse;
|
||||||
|
import com.example.bankcards.dto.TransferRequest;
|
||||||
|
import com.example.bankcards.entity.CardStatus;
|
||||||
|
import com.example.bankcards.service.CardService;
|
||||||
|
import jakarta.validation.Valid;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.data.domain.Pageable;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
||||||
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api/cards")
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class CardController {
|
||||||
|
|
||||||
|
private final CardService cardService;
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
public ResponseEntity<Page<CardResponse>> getMyCards(
|
||||||
|
@AuthenticationPrincipal UserDetails userDetails,
|
||||||
|
@RequestParam(required = false) CardStatus status,
|
||||||
|
Pageable pageable
|
||||||
|
) {
|
||||||
|
return ResponseEntity.ok(
|
||||||
|
cardService.getMyCards(userDetails.getUsername(), status, pageable)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/{id}")
|
||||||
|
public ResponseEntity<CardResponse> getCard(
|
||||||
|
@PathVariable Long id,
|
||||||
|
@AuthenticationPrincipal UserDetails userDetails
|
||||||
|
) {
|
||||||
|
return ResponseEntity.ok(
|
||||||
|
cardService.getCard(id, userDetails.getUsername(), false)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/{id}/request-block")
|
||||||
|
public ResponseEntity<Void> requestBlock(
|
||||||
|
@PathVariable Long id,
|
||||||
|
@AuthenticationPrincipal UserDetails userDetails
|
||||||
|
) {
|
||||||
|
cardService.requestBlock(id, userDetails.getUsername());
|
||||||
|
return ResponseEntity.ok().build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/transfer")
|
||||||
|
public ResponseEntity<Void> transfer(
|
||||||
|
@Valid @RequestBody TransferRequest request,
|
||||||
|
@AuthenticationPrincipal UserDetails userDetails
|
||||||
|
) {
|
||||||
|
cardService.transfer(request, userDetails.getUsername());
|
||||||
|
return ResponseEntity.ok().build();
|
||||||
|
}
|
||||||
|
}
|
||||||
57
src/main/java/com/example/bankcards/service/UserService.java
Normal file
57
src/main/java/com/example/bankcards/service/UserService.java
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
package com.example.bankcards.service;
|
||||||
|
|
||||||
|
import com.example.bankcards.dto.UpdateUserRoleRequest;
|
||||||
|
import com.example.bankcards.dto.UserResponse;
|
||||||
|
import com.example.bankcards.entity.User;
|
||||||
|
import com.example.bankcards.exception.ResourceNotFoundException;
|
||||||
|
import com.example.bankcards.repository.UserRepository;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.data.domain.Pageable;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class UserService {
|
||||||
|
|
||||||
|
private final UserRepository userRepository;
|
||||||
|
|
||||||
|
public Page<UserResponse> getAllUsers(Pageable pageable) {
|
||||||
|
return userRepository.findAll(pageable).map(this::toResponse);
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserResponse getUser(Long id) {
|
||||||
|
User user = userRepository
|
||||||
|
.findById(id)
|
||||||
|
.orElseThrow(() ->
|
||||||
|
new ResourceNotFoundException("User not found: " + id)
|
||||||
|
);
|
||||||
|
return toResponse(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserResponse updateRole(Long id, UpdateUserRoleRequest request) {
|
||||||
|
User user = userRepository
|
||||||
|
.findById(id)
|
||||||
|
.orElseThrow(() ->
|
||||||
|
new ResourceNotFoundException("User not found: " + id)
|
||||||
|
);
|
||||||
|
user.setRole(request.getRole());
|
||||||
|
return toResponse(userRepository.save(user));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void deleteUser(Long id) {
|
||||||
|
if (!userRepository.existsById(id)) {
|
||||||
|
throw new ResourceNotFoundException("User not found: " + id);
|
||||||
|
}
|
||||||
|
userRepository.deleteById(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserResponse toResponse(User user) {
|
||||||
|
UserResponse response = new UserResponse();
|
||||||
|
response.setId(user.getId());
|
||||||
|
response.setUsername(user.getUsername());
|
||||||
|
response.setEmail(user.getEmail());
|
||||||
|
response.setRole(user.getRole());
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user