This commit is contained in:
xds
2025-04-07 18:26:23 +03:00
parent a38d5068e0
commit af7577c65b
13 changed files with 229 additions and 62 deletions

View File

@@ -1,18 +1,25 @@
package space.luminic.budgerapp.controllers
import com.opencsv.CSVWriter
import kotlinx.coroutines.reactor.awaitSingle
import kotlinx.coroutines.reactor.awaitSingleOrNull
import org.apache.commons.io.IOUtils.writer
import org.bson.Document
import org.slf4j.LoggerFactory
import org.springframework.http.HttpHeaders
import org.springframework.http.HttpStatus
import org.springframework.http.MediaType
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
import space.luminic.budgerapp.controllers.BudgetController.LimitValue
import space.luminic.budgerapp.controllers.dtos.BudgetCreationDTO
import space.luminic.budgerapp.models.*
import space.luminic.budgerapp.services.*
import java.io.ByteArrayOutputStream
import java.io.OutputStreamWriter
import java.time.LocalDate
@RestController
@RequestMapping("/spaces")
class SpaceController(
@@ -172,6 +179,63 @@ class SpaceController(
}
}
@GetMapping("/{spaceId}/transactions/csv")
suspend fun getTransactionsCSV(
@PathVariable spaceId: String,
@RequestParam(value = "transaction_type") transactionType: String? = null,
@RequestParam(value = "category_type") categoryType: String? = null,
@RequestParam(value = "user_id") userId: String? = null,
@RequestParam(value = "is_child") isChild: Boolean? = null,
@RequestParam(value = "limit") limit: Int = 20000,
@RequestParam(value = "offset") offset: Int = 0
): ResponseEntity<Any> {
try {
val bos = ByteArrayOutputStream()
val writer = CSVWriter(OutputStreamWriter(bos))
val CSVHeaders = arrayOf("id", "name", "category")
writer.writeNext(CSVHeaders)
financialService.getTransactions(
spaceId = spaceId,
transactionType = transactionType,
categoryType = categoryType,
userId = userId,
isChild = isChild,
limit = limit,
offset = offset
).awaitSingle().map {
val data = arrayOf(it.id, it.comment, it.category.name)
writer.writeNext(data)
}
writer.close()
val csvData = bos.toByteArray()
val headers = HttpHeaders()
headers.contentType = MediaType.parseMediaType("text/csv")
headers.setContentDispositionFormData("attachment", "pojos.csv")
return ResponseEntity(csvData, headers, HttpStatus.OK)
} catch (e: Exception) {
e.printStackTrace()
return ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR)
}
}
@GetMapping("/{spaceId}/category-predict")
suspend fun getTransactionCategoryPredict(
@PathVariable spaceId: String,
@RequestParam comment: String
): List<Category> {
val user = authService.getSecurityUser()
spaceService.isValidRequest(spaceId, user)
return categoryService.getCategories(
"67af3c0f652da946a7dd9931",
"EXPENSE",
sortBy = "name",
direction = "ASC",
predict = comment
)
}
@GetMapping("/{spaceId}/transactions/{id}")
suspend fun getTransaction(
@@ -181,6 +245,7 @@ class SpaceController(
return financialService.getTransactionById(id)
}
@PostMapping("/{spaceId}/transactions")
suspend fun createTransaction(@PathVariable spaceId: String, @RequestBody transaction: Transaction): Transaction {
val user = authService.getSecurityUser()
@@ -220,7 +285,7 @@ class SpaceController(
): List<Category> {
val user = authService.getSecurityUser()
spaceService.isValidRequest(spaceId, user)
return categoryService.getCategories(spaceId, type, sortBy, direction).awaitSingleOrNull().orEmpty()
return categoryService.getCategories(spaceId, type, sortBy, direction)
}
@GetMapping("/{spaceId}/categories/types")