fix spaces
This commit is contained in:
@@ -34,10 +34,10 @@ class BudgetController(
|
||||
return financialService.getBudgets("123")
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
fun getBudget(@PathVariable id: String): Mono<BudgetDTO> {
|
||||
return financialService.getBudget(id)
|
||||
}
|
||||
// @GetMapping("/{id}")
|
||||
// fun getBudget(@PathVariable id: String): Mono<BudgetDTO> {
|
||||
// return financialService.getBudget(id)
|
||||
// }
|
||||
|
||||
|
||||
// @GetMapping("/by-dates")
|
||||
@@ -51,10 +51,10 @@ class BudgetController(
|
||||
return ResponseEntity.ok(financialService.getBudgetCategories(id))
|
||||
}
|
||||
|
||||
@GetMapping("/{id}/transactions")
|
||||
fun getBudgetTransactions(@PathVariable id: String): Mono<Map<String, List<Transaction>>> {
|
||||
return financialService.getBudgetTransactionsByType(id)
|
||||
}
|
||||
// @GetMapping("/{id}/transactions")
|
||||
// fun getBudgetTransactions(@PathVariable id: String): Mono<Map<String, List<Transaction>>> {
|
||||
// return financialService.getBudgetTransactionsByType(id)
|
||||
// }
|
||||
|
||||
// @PostMapping("/")
|
||||
// fun createBudget(@RequestBody budgetCreationDTO: BudgetCreationDTO): Mono<Budget> {
|
||||
@@ -64,23 +64,12 @@ class BudgetController(
|
||||
// )
|
||||
// }
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
fun deleteBudget(@PathVariable id: String): Mono<Void> {
|
||||
return financialService.deleteBudget(id)
|
||||
}
|
||||
// @DeleteMapping("/{id}")
|
||||
// fun deleteBudget(@PathVariable id: String): Mono<Void> {
|
||||
// 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")
|
||||
|
||||
@@ -7,6 +7,7 @@ import org.springframework.web.bind.annotation.*
|
||||
import org.springframework.web.client.HttpClientErrorException
|
||||
import reactor.core.publisher.Flux
|
||||
import reactor.core.publisher.Mono
|
||||
import space.luminic.budgerapp.controllers.BudgetController.LimitValue
|
||||
import space.luminic.budgerapp.controllers.dtos.BudgetCreationDTO
|
||||
import space.luminic.budgerapp.models.*
|
||||
import space.luminic.budgerapp.services.CategoryService
|
||||
@@ -39,7 +40,10 @@ class SpaceController(
|
||||
|
||||
@PostMapping
|
||||
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
|
||||
//
|
||||
@GetMapping("{spaceId}/budgets")
|
||||
@GetMapping("/{spaceId}/budgets")
|
||||
fun getBudgets(@PathVariable spaceId: String): Mono<List<Budget>> {
|
||||
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")
|
||||
fun createBudget(
|
||||
@PathVariable spaceId: String,
|
||||
@RequestBody budgetCreationDTO: BudgetCreationDTO,
|
||||
): 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
|
||||
//
|
||||
@GetMapping("/{spaceId}/transactions")
|
||||
fun getTransactions(
|
||||
@PathVariable spaceId: String,
|
||||
@@ -125,7 +161,6 @@ class SpaceController(
|
||||
}
|
||||
|
||||
|
||||
|
||||
@GetMapping("/{spaceId}/transactions/{id}")
|
||||
fun getTransaction(
|
||||
@PathVariable spaceId: String,
|
||||
@@ -140,31 +175,29 @@ class SpaceController(
|
||||
}
|
||||
|
||||
@PostMapping("/{spaceId}/transactions")
|
||||
fun createTransaction(@PathVariable spaceId: String, @RequestBody transaction: Transaction): ResponseEntity<Any> {
|
||||
try {
|
||||
return ResponseEntity.ok(financialService.createTransaction(spaceId, transaction))
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
return ResponseEntity(e.message, HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
|
||||
fun createTransaction(@PathVariable spaceId: String, @RequestBody transaction: Transaction): Mono<Transaction> {
|
||||
return spaceService.isValidRequest(spaceId).flatMap {
|
||||
financialService.createTransaction(spaceId, transaction)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@PutMapping("/{spaceId}/transactions/{id}")
|
||||
fun editTransaction(
|
||||
@PathVariable spaceId: String, @PathVariable id: String, @RequestBody transaction: Transaction
|
||||
): ResponseEntity<Any> {
|
||||
try {
|
||||
return ResponseEntity.ok(financialService.editTransaction(transaction))
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
return ResponseEntity(e.message, HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
): Mono<Transaction> {
|
||||
return spaceService.isValidRequest(spaceId).flatMap {
|
||||
transaction.space = it
|
||||
financialService.editTransaction(transaction)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@DeleteMapping("/{spaceId}/transactions/{id}")
|
||||
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
|
||||
): Mono<Category> {
|
||||
return spaceService.isValidRequest(spaceId).flatMap {
|
||||
categoryService.createCategory(it,category)
|
||||
financialService.createCategory(it, category)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -83,12 +83,12 @@ class TransactionController(private val financialService: FinancialService) {
|
||||
}
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
fun deleteTransaction(@PathVariable id: String): Mono<Void> {
|
||||
|
||||
return financialService.deleteTransaction(id)
|
||||
|
||||
}
|
||||
// @DeleteMapping("/{id}")
|
||||
// fun deleteTransaction(@PathVariable id: String): Mono<Void> {
|
||||
//
|
||||
// return financialService.deleteTransaction(id)
|
||||
//
|
||||
// }
|
||||
|
||||
|
||||
@GetMapping("/{id}/child")
|
||||
|
||||
@@ -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()
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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(),
|
||||
)
|
||||
|
||||
}
|
||||
}
|
||||
@@ -10,12 +10,7 @@ import org.springframework.context.ApplicationEventPublisher
|
||||
import org.springframework.data.domain.Sort
|
||||
import org.springframework.data.domain.Sort.Direction
|
||||
import org.springframework.data.mongodb.core.ReactiveMongoTemplate
|
||||
import org.springframework.data.mongodb.core.aggregation.Aggregation.limit
|
||||
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.aggregation.Aggregation.*
|
||||
import org.springframework.data.mongodb.core.query.Criteria
|
||||
import org.springframework.data.mongodb.core.query.isEqualTo
|
||||
import org.springframework.stereotype.Service
|
||||
@@ -47,7 +42,6 @@ class CategoryService(
|
||||
}
|
||||
|
||||
|
||||
|
||||
// @Cacheable("categories")
|
||||
fun getCategories(spaceId: String, type: String? = null, sortBy: String, direction: String): Mono<List<Category>> {
|
||||
val matchCriteria = mutableListOf<Criteria>()
|
||||
@@ -62,11 +56,13 @@ class CategoryService(
|
||||
val sort = sort(Sort.by(direction, sortBy))
|
||||
|
||||
val lookupSpaces = lookup("spaces", "space.\$id", "_id", "spaceDetails")
|
||||
val project = project("_id", "type", "name", "description", "icon")
|
||||
|
||||
val aggregationBuilder = mutableListOf(
|
||||
|
||||
lookupSpaces,
|
||||
match.takeIf { matchCriteria.isNotEmpty() },
|
||||
project,
|
||||
sort,
|
||||
).filterNotNull()
|
||||
|
||||
@@ -96,11 +92,7 @@ class CategoryService(
|
||||
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)
|
||||
fun editCategory(category: Category): Mono<Category> {
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user