fix spaces

This commit is contained in:
xds
2025-02-18 16:16:43 +03:00
parent 2610eb0404
commit 5a689bcd95
8 changed files with 1125 additions and 1106 deletions

View File

@@ -34,10 +34,10 @@ class BudgetController(
return financialService.getBudgets("123") return financialService.getBudgets("123")
} }
@GetMapping("/{id}") // @GetMapping("/{id}")
fun getBudget(@PathVariable id: String): Mono<BudgetDTO> { // fun getBudget(@PathVariable id: String): Mono<BudgetDTO> {
return financialService.getBudget(id) // return financialService.getBudget(id)
} // }
// @GetMapping("/by-dates") // @GetMapping("/by-dates")
@@ -51,10 +51,10 @@ class BudgetController(
return ResponseEntity.ok(financialService.getBudgetCategories(id)) return ResponseEntity.ok(financialService.getBudgetCategories(id))
} }
@GetMapping("/{id}/transactions") // @GetMapping("/{id}/transactions")
fun getBudgetTransactions(@PathVariable id: String): Mono<Map<String, List<Transaction>>> { // fun getBudgetTransactions(@PathVariable id: String): Mono<Map<String, List<Transaction>>> {
return financialService.getBudgetTransactionsByType(id) // return financialService.getBudgetTransactionsByType(id)
} // }
// @PostMapping("/") // @PostMapping("/")
// fun createBudget(@RequestBody budgetCreationDTO: BudgetCreationDTO): Mono<Budget> { // fun createBudget(@RequestBody budgetCreationDTO: BudgetCreationDTO): Mono<Budget> {
@@ -64,23 +64,12 @@ class BudgetController(
// ) // )
// } // }
@DeleteMapping("/{id}") // @DeleteMapping("/{id}")
fun deleteBudget(@PathVariable id: String): Mono<Void> { // fun deleteBudget(@PathVariable id: String): Mono<Void> {
return financialService.deleteBudget(id) // return financialService.deleteBudget(id)
} // }
@PostMapping("/{budgetId}/categories/{catId}/limit")
fun setCategoryLimit(
@PathVariable budgetId: String,
@PathVariable catId: String,
@RequestBody limit: LimitValue,
): ResponseEntity<Any> {
return try {
ResponseEntity.ok(financialService.setCategoryLimit(budgetId, catId, limit.limit))
} catch (e: Exception) {
ResponseEntity.badRequest().body(e.message)
}
}
@GetMapping("/{id}/warns") @GetMapping("/{id}/warns")

View File

@@ -7,6 +7,7 @@ import org.springframework.web.bind.annotation.*
import org.springframework.web.client.HttpClientErrorException import org.springframework.web.client.HttpClientErrorException
import reactor.core.publisher.Flux import reactor.core.publisher.Flux
import reactor.core.publisher.Mono import reactor.core.publisher.Mono
import space.luminic.budgerapp.controllers.BudgetController.LimitValue
import space.luminic.budgerapp.controllers.dtos.BudgetCreationDTO import space.luminic.budgerapp.controllers.dtos.BudgetCreationDTO
import space.luminic.budgerapp.models.* import space.luminic.budgerapp.models.*
import space.luminic.budgerapp.services.CategoryService import space.luminic.budgerapp.services.CategoryService
@@ -39,7 +40,10 @@ class SpaceController(
@PostMapping @PostMapping
fun createSpace(@RequestBody space: SpaceCreateDTO): Mono<Space> { fun createSpace(@RequestBody space: SpaceCreateDTO): Mono<Space> {
return spaceService.createSpace(Space(name=space.name, description = space.description), space.createCategories) return spaceService.createSpace(
Space(name = space.name, description = space.description),
space.createCategories
)
} }
@@ -81,21 +85,53 @@ class SpaceController(
// //
//Budgets API //Budgets API
// //
@GetMapping("{spaceId}/budgets") @GetMapping("/{spaceId}/budgets")
fun getBudgets(@PathVariable spaceId: String): Mono<List<Budget>> { fun getBudgets(@PathVariable spaceId: String): Mono<List<Budget>> {
return financialService.getBudgets(spaceId) return financialService.getBudgets(spaceId)
} }
@GetMapping("/{spaceId}/budgets/{id}")
fun getBudget(@PathVariable spaceId: String, @PathVariable id: String): Mono<BudgetDTO> {
return spaceService.isValidRequest(spaceId).flatMap {
financialService.getBudget(spaceId, id)
}
}
@PostMapping("/{spaceId}/budgets") @PostMapping("/{spaceId}/budgets")
fun createBudget( fun createBudget(
@PathVariable spaceId: String, @PathVariable spaceId: String,
@RequestBody budgetCreationDTO: BudgetCreationDTO, @RequestBody budgetCreationDTO: BudgetCreationDTO,
): Mono<Budget> { ): Mono<Budget> {
return financialService.createBudget(spaceId, budgetCreationDTO.budget, budgetCreationDTO.createRecurrent) return spaceService.isValidRequest(spaceId).flatMap {
financialService.createBudget(spaceId, budgetCreationDTO.budget, budgetCreationDTO.createRecurrent)
}
} }
@DeleteMapping("/{spaceId}/budgets/{id}")
fun deleteBudget(@PathVariable spaceId: String, @PathVariable id: String): Mono<Void> {
return spaceService.isValidRequest(spaceId).flatMap {
financialService.deleteBudget(spaceId, id)
}
}
@PostMapping("/{spaceId}/budgets/{budgetId}/categories/{catId}/limit")
fun setCategoryLimit(
@PathVariable spaceId: String,
@PathVariable budgetId: String,
@PathVariable catId: String,
@RequestBody limit: LimitValue,
): Mono<BudgetCategory> {
return spaceService.isValidRequest(spaceId).flatMap {
financialService.setCategoryLimit(it.id!!,budgetId, catId, limit.limit)
}
}
//
// Transactions API // Transactions API
//
@GetMapping("/{spaceId}/transactions") @GetMapping("/{spaceId}/transactions")
fun getTransactions( fun getTransactions(
@PathVariable spaceId: String, @PathVariable spaceId: String,
@@ -125,7 +161,6 @@ class SpaceController(
} }
@GetMapping("/{spaceId}/transactions/{id}") @GetMapping("/{spaceId}/transactions/{id}")
fun getTransaction( fun getTransaction(
@PathVariable spaceId: String, @PathVariable spaceId: String,
@@ -140,31 +175,29 @@ class SpaceController(
} }
@PostMapping("/{spaceId}/transactions") @PostMapping("/{spaceId}/transactions")
fun createTransaction(@PathVariable spaceId: String, @RequestBody transaction: Transaction): ResponseEntity<Any> { fun createTransaction(@PathVariable spaceId: String, @RequestBody transaction: Transaction): Mono<Transaction> {
try { return spaceService.isValidRequest(spaceId).flatMap {
return ResponseEntity.ok(financialService.createTransaction(spaceId, transaction)) financialService.createTransaction(spaceId, transaction)
} catch (e: Exception) {
e.printStackTrace()
return ResponseEntity(e.message, HttpStatus.INTERNAL_SERVER_ERROR)
} }
} }
@PutMapping("/{spaceId}/transactions/{id}") @PutMapping("/{spaceId}/transactions/{id}")
fun editTransaction( fun editTransaction(
@PathVariable spaceId: String, @PathVariable id: String, @RequestBody transaction: Transaction @PathVariable spaceId: String, @PathVariable id: String, @RequestBody transaction: Transaction
): ResponseEntity<Any> { ): Mono<Transaction> {
try { return spaceService.isValidRequest(spaceId).flatMap {
return ResponseEntity.ok(financialService.editTransaction(transaction)) transaction.space = it
} catch (e: Exception) { financialService.editTransaction(transaction)
e.printStackTrace()
return ResponseEntity(e.message, HttpStatus.INTERNAL_SERVER_ERROR)
} }
} }
@DeleteMapping("/{spaceId}/transactions/{id}") @DeleteMapping("/{spaceId}/transactions/{id}")
fun deleteTransaction(@PathVariable spaceId: String, @PathVariable id: String): Mono<Void> { fun deleteTransaction(@PathVariable spaceId: String, @PathVariable id: String): Mono<Void> {
return financialService.deleteTransaction(id) return spaceService.isValidRequest(spaceId).flatMap {
financialService.getTransactionById(id).flatMap { financialService.deleteTransaction(it) }
}
} }
// //
@@ -200,7 +233,7 @@ class SpaceController(
@PathVariable spaceId: String, @RequestBody category: Category @PathVariable spaceId: String, @RequestBody category: Category
): Mono<Category> { ): Mono<Category> {
return spaceService.isValidRequest(spaceId).flatMap { return spaceService.isValidRequest(spaceId).flatMap {
categoryService.createCategory(it,category) financialService.createCategory(it, category)
} }
} }

View File

@@ -83,12 +83,12 @@ class TransactionController(private val financialService: FinancialService) {
} }
} }
@DeleteMapping("/{id}") // @DeleteMapping("/{id}")
fun deleteTransaction(@PathVariable id: String): Mono<Void> { // fun deleteTransaction(@PathVariable id: String): Mono<Void> {
//
return financialService.deleteTransaction(id) // return financialService.deleteTransaction(id)
//
} // }
@GetMapping("/{id}/child") @GetMapping("/{id}/child")

View File

@@ -0,0 +1,55 @@
package space.luminic.budgerapp.mappers
import com.mongodb.DBRef
import org.bson.Document
import org.springframework.stereotype.Component
import space.luminic.budgerapp.models.*
import java.time.ZoneId
@Component
class BudgetMapper : FromDocumentMapper {
override fun fromDocument(document: Document): Budget {
return Budget(
id = document.getObjectId("_id").toString(),
space = Space(id = document.get("spaceDetails", Document::class.java).getObjectId("_id").toString()),
name = document.getString("name"),
dateFrom = document.getDate("dateFrom").toInstant().atZone(ZoneId.systemDefault()).toLocalDate(),
dateTo = document.getDate("dateTo").toInstant().atZone(ZoneId.systemDefault()).toLocalDate(),
categories = document.getList("categories", Document::class.java).map { cat ->
val categoryDetailed = document.getList("categoriesDetails", Document::class.java).first {
it.getObjectId("_id").toString() == cat.get("category", DBRef::class.java).id.toString()
}
BudgetCategory(
category = Category(
id = cat.get("category", DBRef::class.java).id.toString(),
type = CategoryType(
categoryDetailed.get("type", Document::class.java).getString("code"),
categoryDetailed.get("type", Document::class.java).getString("name")
),
name = categoryDetailed.getString("name"),
description = categoryDetailed.getString("description"),
icon = categoryDetailed.getString("icon")
), currentLimit = cat.getDouble("currentLimit")
)
}.toMutableList(),
incomeCategories = document.getList("incomeCategories", Document::class.java).map { cat ->
val categoryDetailed =
document.getList("incomeCategoriesDetails", Document::class.java).first { it ->
it.getObjectId("_id").toString() == cat.get("category", DBRef::class.java).id.toString()
}
BudgetCategory(
category = Category(
id = cat.get("category", DBRef::class.java).id.toString(),
type = CategoryType(
categoryDetailed.get("type", Document::class.java).getString("code"),
categoryDetailed.get("type", Document::class.java).getString("name")
),
name = categoryDetailed.getString("name"),
description = categoryDetailed.getString("description"),
icon = categoryDetailed.getString("icon")
), currentLimit = cat.getDouble("currentLimit")
)
}.toMutableList()
)
}
}

View File

@@ -0,0 +1,9 @@
package space.luminic.budgerapp.mappers
import org.bson.Document
import space.luminic.budgerapp.models.Budget
interface FromDocumentMapper {
fun fromDocument(document: Document): Any
}

View File

@@ -0,0 +1,47 @@
package space.luminic.budgerapp.mappers
import org.bson.Document
import org.bson.types.ObjectId
import org.springframework.stereotype.Component
import space.luminic.budgerapp.models.*
import java.time.ZoneId
@Component
class TransactionsMapper : FromDocumentMapper {
override fun fromDocument(document: Document): Transaction {
val categoryDocument = document.get("categoryDetails", Document::class.java)
val categoryTypeDocument = categoryDocument["type"] as Document
val spaceDocument = document["spaceDetails"] as Document
val userDocument = document.get("userDetails", Document::class.java)
return Transaction(
id = document.getObjectId("_id").toString(),
type = TransactionType(
document.get("type", Document::class.java).getString("code"),
document.get("type", Document::class.java).getString("name")
),
space = Space(
id = spaceDocument.getObjectId("_id").toString()
),
category = Category(
id = (categoryDocument["_id"] as ObjectId).toString(),
type = CategoryType(
categoryTypeDocument["code"] as String,
categoryTypeDocument["name"] as String
),
name = categoryDocument["name"] as String,
description = categoryDocument["description"] as String,
icon = categoryDocument["icon"] as String
),
comment = document.getString("comment"),
date = document.getDate("date").toInstant().atZone(ZoneId.systemDefault()).toLocalDate(),
amount = document.getDouble("amount"),
isDone = document.getBoolean("isDone"),
user = User(userDocument.getObjectId("_id").toString(), userDocument.getString("username"), userDocument.getString("firstName"),),
parentId = document.getString("parentId"),
createdAt = document.getDate("createdAt").toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(),
)
}
}

View File

@@ -10,12 +10,7 @@ import org.springframework.context.ApplicationEventPublisher
import org.springframework.data.domain.Sort import org.springframework.data.domain.Sort
import org.springframework.data.domain.Sort.Direction import org.springframework.data.domain.Sort.Direction
import org.springframework.data.mongodb.core.ReactiveMongoTemplate import org.springframework.data.mongodb.core.ReactiveMongoTemplate
import org.springframework.data.mongodb.core.aggregation.Aggregation.limit import org.springframework.data.mongodb.core.aggregation.Aggregation.*
import org.springframework.data.mongodb.core.aggregation.Aggregation.lookup
import org.springframework.data.mongodb.core.aggregation.Aggregation.match
import org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation
import org.springframework.data.mongodb.core.aggregation.Aggregation.skip
import org.springframework.data.mongodb.core.aggregation.Aggregation.sort
import org.springframework.data.mongodb.core.query.Criteria import org.springframework.data.mongodb.core.query.Criteria
import org.springframework.data.mongodb.core.query.isEqualTo import org.springframework.data.mongodb.core.query.isEqualTo
import org.springframework.stereotype.Service import org.springframework.stereotype.Service
@@ -47,7 +42,6 @@ class CategoryService(
} }
// @Cacheable("categories") // @Cacheable("categories")
fun getCategories(spaceId: String, type: String? = null, sortBy: String, direction: String): Mono<List<Category>> { fun getCategories(spaceId: String, type: String? = null, sortBy: String, direction: String): Mono<List<Category>> {
val matchCriteria = mutableListOf<Criteria>() val matchCriteria = mutableListOf<Criteria>()
@@ -62,11 +56,13 @@ class CategoryService(
val sort = sort(Sort.by(direction, sortBy)) val sort = sort(Sort.by(direction, sortBy))
val lookupSpaces = lookup("spaces", "space.\$id", "_id", "spaceDetails") val lookupSpaces = lookup("spaces", "space.\$id", "_id", "spaceDetails")
val project = project("_id", "type", "name", "description", "icon")
val aggregationBuilder = mutableListOf( val aggregationBuilder = mutableListOf(
lookupSpaces, lookupSpaces,
match.takeIf { matchCriteria.isNotEmpty() }, match.takeIf { matchCriteria.isNotEmpty() },
project,
sort, sort,
).filterNotNull() ).filterNotNull()
@@ -96,11 +92,7 @@ class CategoryService(
return types return types
} }
@CacheEvict(cacheNames = ["getAllCategories"], allEntries = true)
fun createCategory(space: Space, category: Category): Mono<Category> {
category.space = space
return categoryRepo.save(category)
}
@CacheEvict(cacheNames = ["getAllCategories"], allEntries = true) @CacheEvict(cacheNames = ["getAllCategories"], allEntries = true)
fun editCategory(category: Category): Mono<Category> { fun editCategory(category: Category): Mono<Category> {