init
This commit is contained in:
@@ -1,75 +0,0 @@
|
||||
package space.luminic.finance.api
|
||||
|
||||
import io.swagger.v3.oas.annotations.enums.SecuritySchemeType
|
||||
import io.swagger.v3.oas.annotations.security.SecurityScheme
|
||||
import jakarta.ws.rs.GET
|
||||
import org.springframework.web.bind.annotation.DeleteMapping
|
||||
import org.springframework.web.bind.annotation.GetMapping
|
||||
import org.springframework.web.bind.annotation.PathVariable
|
||||
import org.springframework.web.bind.annotation.PostMapping
|
||||
import org.springframework.web.bind.annotation.PutMapping
|
||||
import org.springframework.web.bind.annotation.RequestBody
|
||||
import org.springframework.web.bind.annotation.RequestMapping
|
||||
import org.springframework.web.bind.annotation.RestController
|
||||
import space.luminic.finance.dtos.AccountDTO
|
||||
import space.luminic.finance.dtos.TransactionDTO
|
||||
import space.luminic.finance.mappers.AccountMapper.toDto
|
||||
import space.luminic.finance.mappers.TransactionMapper.toDto
|
||||
import space.luminic.finance.models.Account
|
||||
import space.luminic.finance.services.AccountService
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/spaces/{spaceId}/accounts")
|
||||
@SecurityScheme(
|
||||
name = "bearerAuth",
|
||||
type = SecuritySchemeType.HTTP,
|
||||
bearerFormat = "JWT",
|
||||
scheme = "bearer"
|
||||
)
|
||||
class AccountController(
|
||||
private val accountService: AccountService
|
||||
) {
|
||||
|
||||
|
||||
@GetMapping
|
||||
suspend fun getAccounts(@PathVariable spaceId: String): List<AccountDTO> {
|
||||
return accountService.getAccounts(spaceId).map { it.toDto() }
|
||||
}
|
||||
|
||||
@GetMapping("/{accountId}")
|
||||
suspend fun getAccount(@PathVariable spaceId: String, @PathVariable accountId: String): AccountDTO {
|
||||
return accountService.getAccount(accountId, spaceId).toDto()
|
||||
}
|
||||
|
||||
@GetMapping("/{accountId}/transactions")
|
||||
suspend fun getAccountTransactions(
|
||||
@PathVariable spaceId: String,
|
||||
@PathVariable accountId: String
|
||||
): List<TransactionDTO> {
|
||||
return accountService.getAccountTransactions(spaceId, accountId).map { it.toDto() }
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
suspend fun createAccount(
|
||||
@PathVariable spaceId: String,
|
||||
@RequestBody accountDTO: AccountDTO.CreateAccountDTO
|
||||
): AccountDTO {
|
||||
return accountService.createAccount(spaceId, accountDTO).toDto()
|
||||
}
|
||||
|
||||
@PutMapping("/{accountId}")
|
||||
suspend fun updateAccount(
|
||||
@PathVariable spaceId: String,
|
||||
@PathVariable accountId: String,
|
||||
@RequestBody accountDTO: AccountDTO.UpdateAccountDTO
|
||||
): AccountDTO {
|
||||
return accountService.updateAccount(spaceId, accountDTO).toDto()
|
||||
}
|
||||
|
||||
@DeleteMapping("/{accountId}")
|
||||
suspend fun deleteAccount(@PathVariable spaceId: String, @PathVariable accountId: String) {
|
||||
accountService.deleteAccount(accountId, spaceId)
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,23 +1,18 @@
|
||||
package space.luminic.finance.api
|
||||
|
||||
|
||||
import kotlinx.coroutines.reactive.awaitSingle
|
||||
import org.slf4j.LoggerFactory
|
||||
import org.springframework.security.core.context.ReactiveSecurityContextHolder
|
||||
import org.springframework.security.core.context.SecurityContextHolder
|
||||
import org.springframework.web.bind.annotation.*
|
||||
import space.luminic.finance.dtos.UserDTO.*
|
||||
import space.luminic.finance.dtos.UserDTO
|
||||
import space.luminic.finance.dtos.UserDTO.AuthUserDTO
|
||||
import space.luminic.finance.dtos.UserDTO.RegisterUserDTO
|
||||
import space.luminic.finance.mappers.UserMapper.toDto
|
||||
import space.luminic.finance.services.AuthService
|
||||
import space.luminic.finance.services.UserService
|
||||
import kotlin.jvm.javaClass
|
||||
import kotlin.to
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/auth")
|
||||
class AuthController(
|
||||
private val userService: UserService,
|
||||
private val authService: AuthService
|
||||
) {
|
||||
|
||||
@@ -31,26 +26,28 @@ class AuthController(
|
||||
}
|
||||
|
||||
@PostMapping("/login")
|
||||
suspend fun login(@RequestBody request: AuthUserDTO): Map<String, String> {
|
||||
val token = authService.login(request.username, request.password)
|
||||
fun login(@RequestBody request: AuthUserDTO): Map<String, String> {
|
||||
val token = authService.login(request.username.lowercase(), request.password)
|
||||
return mapOf("token" to token)
|
||||
}
|
||||
|
||||
@PostMapping("/register")
|
||||
suspend fun register(@RequestBody request: RegisterUserDTO): UserDTO {
|
||||
fun register(@RequestBody request: RegisterUserDTO): UserDTO {
|
||||
return authService.register(request.username, request.password, request.firstName).toDto()
|
||||
}
|
||||
|
||||
@PostMapping("/tgLogin")
|
||||
suspend fun tgLogin(@RequestHeader("X-Tg-Id") tgId: String): Map<String, String> {
|
||||
fun tgLogin(@RequestHeader("X-Tg-Id") tgId: String): Map<String, String> {
|
||||
val token = authService.tgLogin(tgId)
|
||||
return mapOf("token" to token)
|
||||
}
|
||||
|
||||
|
||||
@GetMapping("/me")
|
||||
suspend fun getMe(): UserDTO {
|
||||
val securityContext = ReactiveSecurityContextHolder.getContext().awaitSingle()
|
||||
return userService.getByUsername(securityContext.authentication.name).toDto()
|
||||
fun getMe(): UserDTO {
|
||||
logger.info("Get Me")
|
||||
authService.getSecurityUser()
|
||||
|
||||
return authService.getSecurityUser().toDto()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,78 +0,0 @@
|
||||
package space.luminic.finance.api
|
||||
|
||||
import io.swagger.v3.oas.annotations.enums.SecuritySchemeType
|
||||
import io.swagger.v3.oas.annotations.security.SecurityScheme
|
||||
import org.springframework.web.bind.annotation.DeleteMapping
|
||||
import org.springframework.web.bind.annotation.GetMapping
|
||||
import org.springframework.web.bind.annotation.PathVariable
|
||||
import org.springframework.web.bind.annotation.PostMapping
|
||||
import org.springframework.web.bind.annotation.PutMapping
|
||||
import org.springframework.web.bind.annotation.RequestBody
|
||||
import org.springframework.web.bind.annotation.RequestMapping
|
||||
import org.springframework.web.bind.annotation.RequestParam
|
||||
import org.springframework.web.bind.annotation.RestController
|
||||
import space.luminic.finance.dtos.BudgetDTO
|
||||
import space.luminic.finance.dtos.TransactionDTO
|
||||
import space.luminic.finance.mappers.BudgetMapper.toDto
|
||||
import space.luminic.finance.mappers.BudgetMapper.toShortDto
|
||||
import space.luminic.finance.mappers.TransactionMapper.toDto
|
||||
import space.luminic.finance.models.Budget
|
||||
import space.luminic.finance.services.BudgetService
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/spaces/{spaceId}/budgets")
|
||||
@SecurityScheme(
|
||||
name = "bearerAuth",
|
||||
type = SecuritySchemeType.HTTP,
|
||||
bearerFormat = "JWT",
|
||||
scheme = "bearer"
|
||||
)
|
||||
class BudgetController(
|
||||
private val budgetService: BudgetService
|
||||
) {
|
||||
|
||||
@GetMapping
|
||||
suspend fun getBudgets(
|
||||
@PathVariable spaceId: String,
|
||||
@RequestParam(value = "sort", defaultValue = "dateFrom") sortBy: String,
|
||||
@RequestParam("direction", defaultValue = "DESC") sortDirection: String
|
||||
): List<BudgetDTO.BudgetShortInfoDTO> {
|
||||
return budgetService.getBudgets(spaceId, sortBy, sortDirection).map { it.toShortDto() }
|
||||
}
|
||||
|
||||
|
||||
@GetMapping("/{budgetId}")
|
||||
suspend fun getBudgetById(@PathVariable spaceId: String, @PathVariable budgetId: String): BudgetDTO {
|
||||
return budgetService.getBudget(spaceId, budgetId).toDto()
|
||||
}
|
||||
|
||||
@GetMapping("/{budgetId}/transactions")
|
||||
suspend fun getBudgetTransactions(
|
||||
@PathVariable spaceId: String,
|
||||
@PathVariable budgetId: String
|
||||
): BudgetDTO.BudgetTransactionsDTO {
|
||||
return budgetService.getBudgetTransactions(spaceId, budgetId).toDto()
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
suspend fun createBudget(
|
||||
@PathVariable spaceId: String,
|
||||
@RequestBody createBudgetDTO: BudgetDTO.CreateBudgetDTO
|
||||
): BudgetDTO {
|
||||
return budgetService.createBudget(spaceId, Budget.BudgetType.SPECIAL, createBudgetDTO).toDto()
|
||||
}
|
||||
|
||||
@PutMapping("/{budgetId}")
|
||||
suspend fun updateBudget(
|
||||
@PathVariable spaceId: String,
|
||||
@PathVariable budgetId: String,
|
||||
@RequestBody updateBudgetDTO: BudgetDTO.UpdateBudgetDTO
|
||||
): BudgetDTO {
|
||||
return budgetService.updateBudget(spaceId, updateBudgetDTO).toDto()
|
||||
}
|
||||
|
||||
@DeleteMapping("/{budgetId}")
|
||||
suspend fun deleteBudget(@PathVariable spaceId: String, @PathVariable budgetId: String) {
|
||||
budgetService.deleteBudget(spaceId, budgetId)
|
||||
}
|
||||
}
|
||||
@@ -2,16 +2,10 @@ package space.luminic.finance.api
|
||||
|
||||
import io.swagger.v3.oas.annotations.enums.SecuritySchemeType
|
||||
import io.swagger.v3.oas.annotations.security.SecurityScheme
|
||||
import org.springframework.web.bind.annotation.DeleteMapping
|
||||
import org.springframework.web.bind.annotation.GetMapping
|
||||
import org.springframework.web.bind.annotation.PathVariable
|
||||
import org.springframework.web.bind.annotation.PostMapping
|
||||
import org.springframework.web.bind.annotation.PutMapping
|
||||
import org.springframework.web.bind.annotation.RequestBody
|
||||
import org.springframework.web.bind.annotation.RequestMapping
|
||||
import org.springframework.web.bind.annotation.RestController
|
||||
import org.springframework.web.bind.annotation.*
|
||||
import space.luminic.finance.dtos.CategoryDTO
|
||||
import space.luminic.finance.mappers.CategoryMapper.toDto
|
||||
import space.luminic.finance.models.Category
|
||||
import space.luminic.finance.services.CategoryService
|
||||
|
||||
@RestController
|
||||
@@ -23,43 +17,47 @@ import space.luminic.finance.services.CategoryService
|
||||
scheme = "bearer"
|
||||
)
|
||||
class CategoryController(
|
||||
private val categoryService: CategoryService,
|
||||
service: CategoryService
|
||||
private val categoryService: CategoryService
|
||||
) {
|
||||
|
||||
@GetMapping
|
||||
suspend fun getCategories(@PathVariable spaceId: String): List<CategoryDTO> {
|
||||
fun getCategories(@PathVariable spaceId: Int): List<CategoryDTO> {
|
||||
return categoryService.getCategories(spaceId).map { it.toDto() }
|
||||
}
|
||||
|
||||
@GetMapping("/{categoryId}")
|
||||
suspend fun getCategory(@PathVariable spaceId: String, @PathVariable categoryId: String): CategoryDTO {
|
||||
fun getCategory(@PathVariable spaceId: Int, @PathVariable categoryId: Int): CategoryDTO {
|
||||
return categoryService.getCategory(spaceId, categoryId).toDto()
|
||||
}
|
||||
|
||||
|
||||
@PostMapping
|
||||
suspend fun createCategory(
|
||||
@PathVariable spaceId: String,
|
||||
fun createCategory(
|
||||
@PathVariable spaceId: Int,
|
||||
@RequestBody categoryDTO: CategoryDTO.CreateCategoryDTO
|
||||
): CategoryDTO {
|
||||
return categoryService.createCategory(spaceId, categoryDTO).toDto()
|
||||
}
|
||||
|
||||
|
||||
@PostMapping("/_many")
|
||||
fun createManyCategory(@PathVariable spaceId: Int, @RequestBody categoryDTOs: List<Category.CategoryEtalon>): List<CategoryDTO> {
|
||||
return categoryService.createEtalonCategoriesForSpace(spaceId).map { it.toDto() }
|
||||
}
|
||||
|
||||
|
||||
@PutMapping("/{categoryId}")
|
||||
suspend fun updateCategory(
|
||||
@PathVariable spaceId: String,
|
||||
@PathVariable categoryId: String,
|
||||
fun updateCategory(
|
||||
@PathVariable spaceId: Int,
|
||||
@PathVariable categoryId: Int,
|
||||
@RequestBody categoryDTO: CategoryDTO.UpdateCategoryDTO
|
||||
): CategoryDTO {
|
||||
return categoryService.updateCategory(spaceId, categoryDTO).toDto()
|
||||
return categoryService.updateCategory(spaceId, categoryId, categoryDTO).toDto()
|
||||
}
|
||||
|
||||
|
||||
@DeleteMapping("/{categoryId}")
|
||||
suspend fun deleteCategory(@PathVariable spaceId: String, @PathVariable categoryId: String) {
|
||||
fun deleteCategory(@PathVariable spaceId: Int, @PathVariable categoryId: Int) {
|
||||
categoryService.deleteCategory(spaceId, categoryId)
|
||||
}
|
||||
}
|
||||
19
src/main/kotlin/space/luminic/finance/api/GoalController.kt
Normal file
19
src/main/kotlin/space/luminic/finance/api/GoalController.kt
Normal file
@@ -0,0 +1,19 @@
|
||||
package space.luminic.finance.api
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping
|
||||
import org.springframework.web.bind.annotation.PathVariable
|
||||
import org.springframework.web.bind.annotation.RequestMapping
|
||||
import org.springframework.web.bind.annotation.RestController
|
||||
import space.luminic.finance.dtos.GoalDTO
|
||||
import space.luminic.finance.mappers.GoalMapper.toDto
|
||||
import space.luminic.finance.services.GoalService
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/spaces/{spaceId}/goals")
|
||||
class GoalController(private val goalService: GoalService) {
|
||||
|
||||
@GetMapping
|
||||
fun findAll(@PathVariable spaceId: Int): List<GoalDTO> {
|
||||
return goalService.findAllBySpaceId(spaceId).map { it.toDto() }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package space.luminic.finance.api
|
||||
|
||||
import org.springframework.web.bind.annotation.*
|
||||
import space.luminic.finance.dtos.RecurrentOperationDTO
|
||||
import space.luminic.finance.mappers.RecurrentOperationMapper.toDTO
|
||||
import space.luminic.finance.services.RecurrentOperationService
|
||||
|
||||
@RestController
|
||||
@RequestMapping(value = ["/spaces/{spaceId}/recurrents"])
|
||||
class RecurrentOperationController(private val recurrentOperationService: RecurrentOperationService) {
|
||||
|
||||
|
||||
@GetMapping
|
||||
fun findAll(@PathVariable spaceId: Int): List<RecurrentOperationDTO> {
|
||||
return recurrentOperationService.findBySpaceId(spaceId).map { it.toDTO() }
|
||||
}
|
||||
|
||||
@GetMapping("/{operationId}")
|
||||
fun findById(@PathVariable spaceId: Int, @PathVariable operationId: Int): RecurrentOperationDTO {
|
||||
return recurrentOperationService.findBySpaceIdAndId(spaceId, operationId).toDTO()
|
||||
}
|
||||
|
||||
|
||||
@PostMapping
|
||||
fun createOperation(@PathVariable spaceId: Int, @RequestBody createOperation: RecurrentOperationDTO.CreateRecurrentOperationDTO): Map<String, Int> {
|
||||
return mapOf("id" to recurrentOperationService.create(spaceId, createOperation))
|
||||
}
|
||||
|
||||
@PutMapping("/{operationId}")
|
||||
fun updateOperation(@PathVariable spaceId: Int, @PathVariable operationId: Int, @RequestBody operation: RecurrentOperationDTO.UpdateRecurrentOperationDTO): Map<String, Int> {
|
||||
recurrentOperationService.update(spaceId, operationId, operation)
|
||||
return mapOf("id" to operationId)
|
||||
}
|
||||
|
||||
@DeleteMapping("/{operationId}")
|
||||
fun deleteOperation(@PathVariable spaceId: Int, @PathVariable operationId: Int) {
|
||||
recurrentOperationService.delete(spaceId, operationId)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,14 +2,7 @@ package space.luminic.finance.api
|
||||
|
||||
import io.swagger.v3.oas.annotations.enums.SecuritySchemeType
|
||||
import io.swagger.v3.oas.annotations.security.SecurityScheme
|
||||
import org.springframework.web.bind.annotation.DeleteMapping
|
||||
import org.springframework.web.bind.annotation.GetMapping
|
||||
import org.springframework.web.bind.annotation.PathVariable
|
||||
import org.springframework.web.bind.annotation.PostMapping
|
||||
import org.springframework.web.bind.annotation.PutMapping
|
||||
import org.springframework.web.bind.annotation.RequestBody
|
||||
import org.springframework.web.bind.annotation.RequestMapping
|
||||
import org.springframework.web.bind.annotation.RestController
|
||||
import org.springframework.web.bind.annotation.*
|
||||
import space.luminic.finance.dtos.CurrencyDTO
|
||||
import space.luminic.finance.mappers.CurrencyMapper.toDto
|
||||
import space.luminic.finance.services.CurrencyService
|
||||
@@ -27,27 +20,27 @@ class ReferenceController(
|
||||
) {
|
||||
|
||||
@GetMapping("/currencies")
|
||||
suspend fun getCurrencies(): List<CurrencyDTO> {
|
||||
fun getCurrencies(): List<CurrencyDTO> {
|
||||
return currencyService.getCurrencies().map { it.toDto() }
|
||||
}
|
||||
|
||||
@GetMapping("/currencies/{currencyCode}")
|
||||
suspend fun getCurrency(@PathVariable currencyCode: String): CurrencyDTO {
|
||||
fun getCurrency(@PathVariable currencyCode: String): CurrencyDTO {
|
||||
return currencyService.getCurrency(currencyCode).toDto()
|
||||
}
|
||||
|
||||
@PostMapping("/currencies")
|
||||
suspend fun createCurrency(@RequestBody currencyDTO: CurrencyDTO): CurrencyDTO {
|
||||
fun createCurrency(@RequestBody currencyDTO: CurrencyDTO): CurrencyDTO {
|
||||
return currencyService.createCurrency(currencyDTO).toDto()
|
||||
}
|
||||
|
||||
@PutMapping("/currencies/{currencyCode}")
|
||||
suspend fun updateCurrency(@PathVariable currencyCode: String, @RequestBody currencyDTO: CurrencyDTO): CurrencyDTO {
|
||||
fun updateCurrency(@PathVariable currencyCode: String, @RequestBody currencyDTO: CurrencyDTO): CurrencyDTO {
|
||||
return currencyService.updateCurrency(currencyDTO).toDto()
|
||||
}
|
||||
|
||||
@DeleteMapping("/currencies/{currencyCode}")
|
||||
suspend fun deleteCurrency(@PathVariable currencyCode: String) {
|
||||
fun deleteCurrency(@PathVariable currencyCode: String) {
|
||||
currencyService.deleteCurrency(currencyCode)
|
||||
}
|
||||
}
|
||||
@@ -2,17 +2,9 @@ package space.luminic.finance.api
|
||||
|
||||
import io.swagger.v3.oas.annotations.enums.SecuritySchemeType
|
||||
import io.swagger.v3.oas.annotations.security.SecurityScheme
|
||||
import org.springframework.web.bind.annotation.DeleteMapping
|
||||
import org.springframework.web.bind.annotation.GetMapping
|
||||
import org.springframework.web.bind.annotation.PathVariable
|
||||
import org.springframework.web.bind.annotation.PostMapping
|
||||
import org.springframework.web.bind.annotation.PutMapping
|
||||
import org.springframework.web.bind.annotation.RequestBody
|
||||
import org.springframework.web.bind.annotation.RequestMapping
|
||||
import org.springframework.web.bind.annotation.RestController
|
||||
import org.springframework.web.bind.annotation.*
|
||||
import space.luminic.finance.dtos.SpaceDTO
|
||||
import space.luminic.finance.mappers.SpaceMapper.toDto
|
||||
import space.luminic.finance.models.Space
|
||||
import space.luminic.finance.services.SpaceService
|
||||
|
||||
@RestController
|
||||
@@ -29,27 +21,27 @@ class SpaceController(
|
||||
|
||||
|
||||
@GetMapping
|
||||
suspend fun getSpaces(): List<SpaceDTO> {
|
||||
fun getSpaces(): List<SpaceDTO> {
|
||||
return spaceService.getSpaces().map { it.toDto() }
|
||||
}
|
||||
|
||||
@GetMapping("/{spaceId}")
|
||||
suspend fun getSpace(@PathVariable spaceId: String): SpaceDTO {
|
||||
fun getSpace(@PathVariable spaceId: Int): SpaceDTO {
|
||||
return spaceService.getSpace(spaceId).toDto()
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
suspend fun createSpace(@RequestBody space: SpaceDTO.CreateSpaceDTO): SpaceDTO {
|
||||
return spaceService.createSpace(space).toDto()
|
||||
fun createSpace(@RequestBody space: SpaceDTO.CreateSpaceDTO): Map<String, Int> {
|
||||
return mapOf("id" to spaceService.createSpace(space) )
|
||||
}
|
||||
|
||||
@PutMapping("/{spaceId}")
|
||||
suspend fun updateSpace(@PathVariable spaceId: String, @RequestBody space: SpaceDTO.UpdateSpaceDTO): SpaceDTO {
|
||||
return spaceService.updateSpace(spaceId, space).toDto()
|
||||
fun updateSpace(@PathVariable spaceId: Int, @RequestBody space: SpaceDTO.UpdateSpaceDTO): Map<String, Int> {
|
||||
return mapOf("id" to spaceService.updateSpace(spaceId, space) )
|
||||
}
|
||||
|
||||
@DeleteMapping("/{spaceId}")
|
||||
suspend fun deleteSpace(@PathVariable spaceId: String) {
|
||||
fun deleteSpace(@PathVariable spaceId: Int) {
|
||||
spaceService.deleteSpace(spaceId)
|
||||
}
|
||||
}
|
||||
@@ -2,14 +2,7 @@ package space.luminic.finance.api
|
||||
|
||||
import io.swagger.v3.oas.annotations.enums.SecuritySchemeType
|
||||
import io.swagger.v3.oas.annotations.security.SecurityScheme
|
||||
import org.springframework.web.bind.annotation.DeleteMapping
|
||||
import org.springframework.web.bind.annotation.GetMapping
|
||||
import org.springframework.web.bind.annotation.PathVariable
|
||||
import org.springframework.web.bind.annotation.PostMapping
|
||||
import org.springframework.web.bind.annotation.PutMapping
|
||||
import org.springframework.web.bind.annotation.RequestBody
|
||||
import org.springframework.web.bind.annotation.RequestMapping
|
||||
import org.springframework.web.bind.annotation.RestController
|
||||
import org.springframework.web.bind.annotation.*
|
||||
import space.luminic.finance.dtos.TransactionDTO
|
||||
import space.luminic.finance.mappers.TransactionMapper.toDto
|
||||
import space.luminic.finance.services.TransactionService
|
||||
@@ -25,34 +18,32 @@ import space.luminic.finance.services.TransactionService
|
||||
)
|
||||
class TransactionController (
|
||||
private val transactionService: TransactionService,
|
||||
service: TransactionService,
|
||||
transactionService1: TransactionService,
|
||||
){
|
||||
|
||||
|
||||
@GetMapping
|
||||
suspend fun getTransactions(@PathVariable spaceId: String) : List<TransactionDTO>{
|
||||
fun getTransactions(@PathVariable spaceId: Int) : List<TransactionDTO>{
|
||||
return transactionService.getTransactions(spaceId, TransactionService.TransactionsFilter(),"date", "DESC").map { it.toDto() }
|
||||
}
|
||||
|
||||
@GetMapping("/{transactionId}")
|
||||
suspend fun getTransaction(@PathVariable spaceId: String, @PathVariable transactionId: String): TransactionDTO {
|
||||
fun getTransaction(@PathVariable spaceId: Int, @PathVariable transactionId: Int): TransactionDTO {
|
||||
return transactionService.getTransaction(spaceId, transactionId).toDto()
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
suspend fun createTransaction(@PathVariable spaceId: String, @RequestBody transactionDTO: TransactionDTO.CreateTransactionDTO): TransactionDTO {
|
||||
return transactionService.createTransaction(spaceId, transactionDTO).toDto()
|
||||
fun createTransaction(@PathVariable spaceId: Int, @RequestBody transactionDTO: TransactionDTO.CreateTransactionDTO): Map<String, Int> {
|
||||
return mapOf("id" to transactionService.createTransaction(spaceId, transactionDTO))
|
||||
}
|
||||
|
||||
|
||||
@PutMapping("/{transactionId}")
|
||||
suspend fun updateTransaction(@PathVariable spaceId: String, @PathVariable transactionId: String, @RequestBody transactionDTO: TransactionDTO.UpdateTransactionDTO): TransactionDTO {
|
||||
return transactionService.updateTransaction(spaceId, transactionDTO).toDto()
|
||||
fun updateTransaction(@PathVariable spaceId: Int, @PathVariable transactionId: Int, @RequestBody transactionDTO: TransactionDTO.UpdateTransactionDTO): Map<String, Int> {
|
||||
return mapOf("id" to transactionService.updateTransaction(spaceId, transactionId, transactionDTO))
|
||||
}
|
||||
|
||||
@DeleteMapping("/{transactionId}")
|
||||
suspend fun deleteTransaction(@PathVariable spaceId: String, @PathVariable transactionId: String) {
|
||||
fun deleteTransaction(@PathVariable spaceId: Int, @PathVariable transactionId: Int) {
|
||||
transactionService.deleteTransaction(spaceId, transactionId)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,109 +1,112 @@
|
||||
package space.luminic.finance.api.exceptionHandlers
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest
|
||||
import org.springframework.http.HttpStatus
|
||||
import org.springframework.http.ResponseEntity
|
||||
import org.springframework.http.server.reactive.ServerHttpRequest
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice
|
||||
import org.springframework.web.server.ServerWebExchange
|
||||
import reactor.core.publisher.Mono
|
||||
import space.luminic.finance.configs.AuthException
|
||||
import space.luminic.finance.models.NotFoundException
|
||||
|
||||
@RestControllerAdvice
|
||||
class GlobalExceptionHandler {
|
||||
|
||||
data class ErrorResponse(
|
||||
val timestamp: Long = System.currentTimeMillis(),
|
||||
val status: Int,
|
||||
val error: String,
|
||||
val message: String?,
|
||||
val path: String,
|
||||
)
|
||||
|
||||
// Изменённый параметр request
|
||||
fun constructErrorBody(
|
||||
e: Exception,
|
||||
message: String,
|
||||
status: HttpStatus,
|
||||
request: ServerHttpRequest
|
||||
): Map<String, Any?> {
|
||||
val errorResponse = mapOf(
|
||||
"timestamp" to System.currentTimeMillis(),
|
||||
"status" to status.value(),
|
||||
"error" to message,
|
||||
"message" to e.message,
|
||||
"path" to request.path.value()
|
||||
request: HttpServletRequest // <--- Изменённый тип
|
||||
): ErrorResponse {
|
||||
return ErrorResponse(
|
||||
status = status.value(),
|
||||
error = message,
|
||||
message = e.message,
|
||||
path = request.requestURI // <--- Получаем путь через HttpServletRequest
|
||||
)
|
||||
return errorResponse
|
||||
}
|
||||
|
||||
@ExceptionHandler(AuthException::class)
|
||||
// Изменённый параметр exchange
|
||||
fun handleAuthenticationException(
|
||||
ex: AuthException,
|
||||
exchange: ServerWebExchange
|
||||
): Mono<ResponseEntity<Map<String, Any?>>?> {
|
||||
request: HttpServletRequest // <--- Изменённый тип
|
||||
): ResponseEntity<ErrorResponse>? {
|
||||
ex.printStackTrace()
|
||||
|
||||
return Mono.just(
|
||||
ResponseEntity(
|
||||
constructErrorBody(
|
||||
ex,
|
||||
ex.message.toString(),
|
||||
HttpStatus.UNAUTHORIZED,
|
||||
exchange.request
|
||||
), HttpStatus.UNAUTHORIZED
|
||||
)
|
||||
return ResponseEntity(
|
||||
constructErrorBody(
|
||||
ex,
|
||||
ex.message.toString(),
|
||||
HttpStatus.UNAUTHORIZED,
|
||||
request // <--- Передаём HttpServletRequest
|
||||
),
|
||||
HttpStatus.UNAUTHORIZED
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@ExceptionHandler(NotFoundException::class)
|
||||
// Изменённый параметр exchange
|
||||
fun handleNotFoundException(
|
||||
e: NotFoundException,
|
||||
exchange: ServerWebExchange
|
||||
): Mono<ResponseEntity<Map<String, Any?>>?> {
|
||||
request: HttpServletRequest // <--- Изменённый тип
|
||||
): ResponseEntity<ErrorResponse>? {
|
||||
e.printStackTrace()
|
||||
|
||||
return Mono.just(
|
||||
ResponseEntity(
|
||||
constructErrorBody(
|
||||
e,
|
||||
e.message.toString(),
|
||||
HttpStatus.NOT_FOUND,
|
||||
exchange.request
|
||||
), HttpStatus.NOT_FOUND
|
||||
)
|
||||
return ResponseEntity(
|
||||
constructErrorBody(
|
||||
e,
|
||||
e.message.toString(),
|
||||
HttpStatus.NOT_FOUND,
|
||||
request // <--- Передаём HttpServletRequest
|
||||
),
|
||||
HttpStatus.NOT_FOUND
|
||||
)
|
||||
}
|
||||
|
||||
@ExceptionHandler(IllegalArgumentException::class)
|
||||
// Изменённый параметр exchange
|
||||
fun handleIllegalArgumentException(
|
||||
e: IllegalArgumentException,
|
||||
exchange: ServerWebExchange
|
||||
): Mono<ResponseEntity<Map<String, Any?>>?> {
|
||||
request: HttpServletRequest // <--- Изменённый тип
|
||||
): ResponseEntity<ErrorResponse>? {
|
||||
e.printStackTrace()
|
||||
|
||||
return Mono.just(
|
||||
ResponseEntity(
|
||||
constructErrorBody(
|
||||
e,
|
||||
e.message.toString(),
|
||||
HttpStatus.BAD_REQUEST,
|
||||
exchange.request
|
||||
), HttpStatus.BAD_REQUEST
|
||||
)
|
||||
return ResponseEntity(
|
||||
constructErrorBody(
|
||||
e,
|
||||
e.message.toString(),
|
||||
HttpStatus.BAD_REQUEST,
|
||||
request // <--- Передаём HttpServletRequest
|
||||
),
|
||||
HttpStatus.BAD_REQUEST
|
||||
)
|
||||
}
|
||||
|
||||
@ExceptionHandler(Exception::class)
|
||||
// Изменённый параметр exchange
|
||||
fun handleGenericException(
|
||||
e: Exception,
|
||||
exchange: ServerWebExchange
|
||||
): Mono<out ResponseEntity<out Map<String, Any?>>?> {
|
||||
request: HttpServletRequest // <--- Изменённый тип
|
||||
): ResponseEntity<ErrorResponse>? {
|
||||
e.printStackTrace()
|
||||
|
||||
|
||||
return Mono.just(
|
||||
ResponseEntity(
|
||||
constructErrorBody(
|
||||
e,
|
||||
e.message.toString(),
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
exchange.request
|
||||
), HttpStatus.INTERNAL_SERVER_ERROR
|
||||
)
|
||||
return ResponseEntity(
|
||||
constructErrorBody(
|
||||
e,
|
||||
e.message.toString(),
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
request // <--- Передаём HttpServletRequest
|
||||
),
|
||||
HttpStatus.INTERNAL_SERVER_ERROR
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,49 +1,37 @@
|
||||
package space.luminic.finance.api.exceptionHandlers
|
||||
import org.springframework.boot.autoconfigure.web.WebProperties
|
||||
import org.springframework.boot.autoconfigure.web.reactive.error.AbstractErrorWebExceptionHandler
|
||||
import org.springframework.boot.web.error.ErrorAttributeOptions
|
||||
import org.springframework.boot.web.reactive.error.ErrorAttributes
|
||||
import org.springframework.context.ApplicationContext
|
||||
import org.springframework.core.annotation.Order
|
||||
import org.springframework.http.MediaType
|
||||
import org.springframework.http.codec.ServerCodecConfigurer
|
||||
import org.springframework.stereotype.Component
|
||||
import org.springframework.web.reactive.function.BodyInserters
|
||||
import org.springframework.web.reactive.function.server.*
|
||||
import reactor.core.publisher.Mono
|
||||
|
||||
@Component
|
||||
@Order(-2)
|
||||
class GlobalErrorWebExceptionHandler(
|
||||
errorAttributes: ErrorAttributes,
|
||||
applicationContext: ApplicationContext,
|
||||
serverCodecConfigurer: ServerCodecConfigurer
|
||||
) : AbstractErrorWebExceptionHandler(
|
||||
errorAttributes,
|
||||
WebProperties.Resources(),
|
||||
applicationContext
|
||||
) {
|
||||
|
||||
init {
|
||||
super.setMessageWriters(serverCodecConfigurer.writers)
|
||||
super.setMessageReaders(serverCodecConfigurer.readers)
|
||||
}
|
||||
|
||||
override fun getRoutingFunction(errorAttributes: ErrorAttributes): RouterFunction<ServerResponse> {
|
||||
return RouterFunctions.route(RequestPredicates.all(), this::renderErrorResponse)
|
||||
}
|
||||
|
||||
private fun renderErrorResponse(request: ServerRequest): Mono<ServerResponse> {
|
||||
val errorAttributesMap = getErrorAttributes(
|
||||
request,
|
||||
ErrorAttributeOptions.of(
|
||||
ErrorAttributeOptions.Include.MESSAGE
|
||||
)
|
||||
)
|
||||
return ServerResponse.status(401)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.body(BodyInserters.fromValue(errorAttributesMap))
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
//
|
||||
//@Component
|
||||
//@Order(-2)
|
||||
//class GlobalErrorWebExceptionHandler(
|
||||
// errorAttributes: ErrorAttributes,
|
||||
// applicationContext: ApplicationContext,
|
||||
// serverCodecConfigurer: ServerCodecConfigurer
|
||||
//) : AbstractErrorWebExceptionHandler(
|
||||
// errorAttributes,
|
||||
// WebProperties.Resources(),
|
||||
// applicationContext
|
||||
//) {
|
||||
//
|
||||
// init {
|
||||
// super.setMessageWriters(serverCodecConfigurer.writers)
|
||||
// super.setMessageReaders(serverCodecConfigurer.readers)
|
||||
// }
|
||||
//
|
||||
// override fun getRoutingFunction(errorAttributes: ErrorAttributes): RouterFunction<ServerResponse> {
|
||||
// return RouterFunctions.route(RequestPredicates.all(), this::renderErrorResponse)
|
||||
// }
|
||||
//
|
||||
// private fun renderErrorResponse(request: ServerRequest): Mono<ServerResponse> {
|
||||
// val errorAttributesMap = getErrorAttributes(
|
||||
// request,
|
||||
// ErrorAttributeOptions.of(
|
||||
// ErrorAttributeOptions.Include.MESSAGE
|
||||
// )
|
||||
// )
|
||||
// return ServerResponse.status(401)
|
||||
// .contentType(MediaType.APPLICATION_JSON)
|
||||
// .body(BodyInserters.fromValue(errorAttributesMap))
|
||||
// }
|
||||
//
|
||||
//
|
||||
//}
|
||||
|
||||
Reference in New Issue
Block a user