docs: add README + ci: add tests
This commit is contained in:
@@ -4,10 +4,9 @@ import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class BankcardsApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(BankcardsApplication.class, args);
|
||||
}
|
||||
public class BankCardsApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(BankCardsApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import com.example.bankcards.security.JwtAuthenticationFilter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.security.authentication.AuthenticationManager;
|
||||
import org.springframework.security.authentication.AuthenticationProvider;
|
||||
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
|
||||
@@ -43,6 +44,8 @@ public class SecurityConfig {
|
||||
"/swagger-ui.html"
|
||||
)
|
||||
.permitAll()
|
||||
.requestMatchers(HttpMethod.GET, "/api/admin/**")
|
||||
.hasAuthority("ROLE_ADMIN")
|
||||
.requestMatchers("/api/admin/**")
|
||||
.hasAuthority("ROLE_ADMIN")
|
||||
.anyRequest()
|
||||
|
||||
@@ -3,6 +3,9 @@ package com.example.bankcards.controller;
|
||||
import com.example.bankcards.dto.CardResponse;
|
||||
import com.example.bankcards.dto.CreateCardRequest;
|
||||
import com.example.bankcards.service.CardService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.validation.Valid;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.data.domain.Page;
|
||||
@@ -16,11 +19,14 @@ import org.springframework.web.bind.annotation.*;
|
||||
@RequestMapping("/api/admin/cards")
|
||||
@RequiredArgsConstructor
|
||||
@PreAuthorize("hasAuthority('ROLE_ADMIN')")
|
||||
@Tag(name = "Cards (Admin)", description = "Admin operations on cards")
|
||||
@SecurityRequirement(name = "Bearer Authentication")
|
||||
public class AdminCardController {
|
||||
|
||||
private final CardService cardService;
|
||||
|
||||
@PostMapping
|
||||
@Operation(summary = "Create a new card")
|
||||
public ResponseEntity<CardResponse> createCard(
|
||||
@Valid @RequestBody CreateCardRequest request
|
||||
) {
|
||||
@@ -30,26 +36,31 @@ public class AdminCardController {
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
@Operation(summary = "Get all cards")
|
||||
public ResponseEntity<Page<CardResponse>> getAllCards(Pageable pageable) {
|
||||
return ResponseEntity.ok(cardService.getAllCards(pageable));
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
@Operation(summary = "Get card by id")
|
||||
public ResponseEntity<CardResponse> getCard(@PathVariable Long id) {
|
||||
return ResponseEntity.ok(cardService.getCard(id, null, true));
|
||||
}
|
||||
|
||||
@PatchMapping("/{id}/block")
|
||||
@Operation(summary = "Block a card")
|
||||
public ResponseEntity<CardResponse> blockCard(@PathVariable Long id) {
|
||||
return ResponseEntity.ok(cardService.blockCard(id));
|
||||
}
|
||||
|
||||
@PatchMapping("/{id}/activate")
|
||||
@Operation(summary = "Activate a card")
|
||||
public ResponseEntity<CardResponse> activateCard(@PathVariable Long id) {
|
||||
return ResponseEntity.ok(cardService.activateCard(id));
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
@Operation(summary = "Delete a card")
|
||||
public ResponseEntity<Void> deleteCard(@PathVariable Long id) {
|
||||
cardService.deleteCard(id);
|
||||
return ResponseEntity.noContent().build();
|
||||
|
||||
@@ -3,6 +3,9 @@ package com.example.bankcards.controller;
|
||||
import com.example.bankcards.dto.UpdateUserRoleRequest;
|
||||
import com.example.bankcards.dto.UserResponse;
|
||||
import com.example.bankcards.service.UserService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.validation.Valid;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.data.domain.Page;
|
||||
@@ -15,21 +18,26 @@ import org.springframework.web.bind.annotation.*;
|
||||
@RequestMapping("/api/admin/users")
|
||||
@RequiredArgsConstructor
|
||||
@PreAuthorize("hasAuthority('ROLE_ADMIN')")
|
||||
@Tag(name = "Users (Admin)", description = "Admin operations on users")
|
||||
@SecurityRequirement(name = "Bearer Authentication")
|
||||
public class AdminUserController {
|
||||
|
||||
private final UserService userService;
|
||||
|
||||
@GetMapping
|
||||
@Operation(summary = "Get all users")
|
||||
public ResponseEntity<Page<UserResponse>> getAllUsers(Pageable pageable) {
|
||||
return ResponseEntity.ok(userService.getAllUsers(pageable));
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
@Operation(summary = "Get user by id")
|
||||
public ResponseEntity<UserResponse> getUser(@PathVariable Long id) {
|
||||
return ResponseEntity.ok(userService.getUser(id));
|
||||
}
|
||||
|
||||
@PatchMapping("/{id}/role")
|
||||
@Operation(summary = "Update user role")
|
||||
public ResponseEntity<UserResponse> updateRole(
|
||||
@PathVariable Long id,
|
||||
@Valid @RequestBody UpdateUserRoleRequest request
|
||||
@@ -38,6 +46,7 @@ public class AdminUserController {
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
@Operation(summary = "Delete user")
|
||||
public ResponseEntity<Void> deleteUser(@PathVariable Long id) {
|
||||
userService.deleteUser(id);
|
||||
return ResponseEntity.noContent().build();
|
||||
|
||||
@@ -4,6 +4,8 @@ import com.example.bankcards.dto.AuthResponse;
|
||||
import com.example.bankcards.dto.LoginRequest;
|
||||
import com.example.bankcards.dto.RegisterRequest;
|
||||
import com.example.bankcards.service.AuthService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.validation.Valid;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
@@ -12,11 +14,13 @@ import org.springframework.web.bind.annotation.*;
|
||||
@RestController
|
||||
@RequestMapping("/api/auth")
|
||||
@RequiredArgsConstructor
|
||||
@Tag(name = "Authentication", description = "Register and login")
|
||||
public class AuthController {
|
||||
|
||||
private final AuthService authService;
|
||||
|
||||
@PostMapping("/register")
|
||||
@Operation(summary = "Register a new user")
|
||||
public ResponseEntity<AuthResponse> register(
|
||||
@Valid @RequestBody RegisterRequest request
|
||||
) {
|
||||
@@ -24,6 +28,7 @@ public class AuthController {
|
||||
}
|
||||
|
||||
@PostMapping("/login")
|
||||
@Operation(summary = "Login and get JWT token")
|
||||
public ResponseEntity<AuthResponse> login(
|
||||
@Valid @RequestBody LoginRequest request
|
||||
) {
|
||||
|
||||
@@ -4,6 +4,9 @@ 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 io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.validation.Valid;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.data.domain.Page;
|
||||
@@ -16,11 +19,14 @@ import org.springframework.web.bind.annotation.*;
|
||||
@RestController
|
||||
@RequestMapping("/api/cards")
|
||||
@RequiredArgsConstructor
|
||||
@Tag(name = "Cards (User)", description = "User operations on their own cards")
|
||||
@SecurityRequirement(name = "Bearer Authentication")
|
||||
public class CardController {
|
||||
|
||||
private final CardService cardService;
|
||||
|
||||
@GetMapping
|
||||
@Operation(summary = "Get my cards with optional filter by status")
|
||||
public ResponseEntity<Page<CardResponse>> getMyCards(
|
||||
@AuthenticationPrincipal UserDetails userDetails,
|
||||
@RequestParam(required = false) CardStatus status,
|
||||
@@ -32,6 +38,7 @@ public class CardController {
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
@Operation(summary = "Get a specific card (must own it)")
|
||||
public ResponseEntity<CardResponse> getCard(
|
||||
@PathVariable Long id,
|
||||
@AuthenticationPrincipal UserDetails userDetails
|
||||
@@ -42,6 +49,7 @@ public class CardController {
|
||||
}
|
||||
|
||||
@PostMapping("/{id}/request-block")
|
||||
@Operation(summary = "Request to block own card")
|
||||
public ResponseEntity<Void> requestBlock(
|
||||
@PathVariable Long id,
|
||||
@AuthenticationPrincipal UserDetails userDetails
|
||||
@@ -51,6 +59,7 @@ public class CardController {
|
||||
}
|
||||
|
||||
@PostMapping("/transfer")
|
||||
@Operation(summary = "Transfer money between own cards")
|
||||
public ResponseEntity<Void> transfer(
|
||||
@Valid @RequestBody TransferRequest request,
|
||||
@AuthenticationPrincipal UserDetails userDetails
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.example.bankcards.entity;
|
||||
|
||||
public enum CardStatus {
|
||||
ACTIVE, BLOCKED, EXPIRED
|
||||
ACTIVE,
|
||||
BLOCKED,
|
||||
EXPIRED,
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.example.bankcards.entity;
|
||||
|
||||
public enum Role {
|
||||
ROLE_USER,
|
||||
ROLE_ADMIN
|
||||
ROLE_USER,
|
||||
ROLE_ADMIN,
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.example.bankcards.entity;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import java.util.List;
|
||||
import lombok.*;
|
||||
|
||||
@Entity
|
||||
@@ -27,4 +28,11 @@ public class User {
|
||||
@Enumerated(EnumType.STRING)
|
||||
@Column(nullable = false)
|
||||
private Role role;
|
||||
|
||||
@OneToMany(
|
||||
mappedBy = "owner",
|
||||
cascade = CascadeType.ALL,
|
||||
fetch = FetchType.LAZY
|
||||
)
|
||||
private List<Card> cards;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.example.bankcards.repository;
|
||||
|
||||
import com.example.bankcards.entity.Card;
|
||||
import com.example.bankcards.entity.CardStatus;
|
||||
import java.util.List;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
@@ -13,4 +14,5 @@ public interface CardRepository extends JpaRepository<Card, Long> {
|
||||
CardStatus status,
|
||||
Pageable pageable
|
||||
);
|
||||
List<Card> findByOwnerId(Long ownerId);
|
||||
}
|
||||
|
||||
@@ -43,7 +43,8 @@ public class CardService {
|
||||
.status(CardStatus.ACTIVE)
|
||||
.balance(request.getInitialBalance())
|
||||
.build();
|
||||
return toResponse(cardRepository.save(card));
|
||||
card = cardRepository.save(card);
|
||||
return toResponse(card);
|
||||
}
|
||||
|
||||
public Page<CardResponse> getAllCards(Pageable pageable) {
|
||||
|
||||
@@ -49,8 +49,7 @@ public class CardEncryptionUtil {
|
||||
if (cardNumber == null || cardNumber.length() < 4) {
|
||||
return "****";
|
||||
}
|
||||
return (
|
||||
"**** **** **** " + cardNumber.substring(cardNumber.length() - 4)
|
||||
);
|
||||
String lastFour = cardNumber.substring(cardNumber.length() - 4);
|
||||
return "**** **** **** " + lastFour;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user