2024-05-27 14:34:32 +05:30

287 lines
9.3 KiB
Kotlin

package com.restapi.controllers
import com.restapi.domain.*
import io.javalin.http.Context
import io.javalin.http.HttpStatus
import io.javalin.http.NotFoundResponse
import io.javalin.http.bodyAsClass
import java.io.FileInputStream
import java.time.LocalDate
object FleetCtrl {
fun create(ctx: Context) {
val fleet = ctx.bodyAsClass<Fleet>()
Session.database.save(fleet)
ctx.json(fleet).status(HttpStatus.CREATED)
}
fun get(ctx: Context) {
val id = ctx.pathParam("id").toLong()
val fleet = Session.database.find(Fleet::class.java, id)
?: throw NotFoundResponse("No fleet found with id $id")
ctx.json(fleet).status(HttpStatus.OK)
}
data class FLTF(val common: CommonFilters, val fleetFilters: FleetFilters)
fun getAll(ctx: Context) {
val filters = ctx.bodyAsClass<FLTF>()
val fleets = searchFleets(filters.common, filters.fleetFilters)
val excel = ctx.queryParam("excel")
if (excel !== null) {
exportFleets(fleets)
val inputStream = FileInputStream("./excel/Fleets.xls")
ctx.result(inputStream).status(HttpStatus.OK)
} else {
ctx.json(fleets).status(HttpStatus.OK)
}
}
fun update(ctx: Context) {
val id = ctx.pathParam("id").toLong()
val fleet =
Session.database.find(Fleet::class.java, id) ?: throw NotFoundResponse("fleet not found for $id")
val updatedFleet = ctx.bodyAsClass<Fleet>()
fleet.patchValues(updatedFleet)
fleet.update()
ctx.json(fleet).status(HttpStatus.OK)
}
fun delete(ctx: Context) {
}
}
object FleetTypeCtrl {
fun create(ctx: Context) {
val fleetType = ctx.bodyAsClass<FleetType>()
Session.database.save(fleetType)
ctx.json(fleetType).status(HttpStatus.CREATED)
}
fun get(ctx: Context) {
val id = ctx.pathParam("id").toLong()
val fleetType = Session.database.find(FleetType::class.java, id)
?: throw NotFoundResponse("No fleetType found with id $id")
ctx.json(fleetType).status(HttpStatus.OK)
}
fun getAll(ctx: Context) {
val fleetTypes = Session.database.find(FleetType::class.java).findList()
val excel = ctx.queryParam("excel")
if (excel !== null) {
// exportFleetTypes(fleetTypes)
val inputStream = FileInputStream("./excel/FleetTypes.xls")
ctx.result(inputStream).status(HttpStatus.OK)
} else {
ctx.json(fleetTypes).status(HttpStatus.OK)
}
}
fun update(ctx: Context) {
val id = ctx.pathParam("id").toLong()
val fleetType =
Session.database.find(FleetType::class.java, id) ?: throw NotFoundResponse("fleetType not found for $id")
val updatedFleetType = ctx.bodyAsClass<FleetType>()
fleetType.patchValues(updatedFleetType)
fleetType.update()
ctx.json(fleetType).status(HttpStatus.OK)
}
}
object RenewalCtrl {
fun create(ctx: Context) {
val renewal = ctx.bodyAsClass<Renewal>()
Session.database.save(renewal)
ctx.json(renewal).status(HttpStatus.CREATED)
}
fun get(ctx: Context) {
val id = ctx.pathParam("id").toLong()
val renewal = Session.database.find(Renewal::class.java, id)
?: throw NotFoundResponse("No renewal found with id $id")
ctx.json(renewal).status(HttpStatus.OK)
}
//data class RNLF(val common: CommonFilters, val renewalFilters: RenewalFilters)
fun getAll(ctx: Context) {
// val filters = ctx.bodyAsClass<FLTF>()
// val renewals = searchRenewals(filters.common, filters.renewalFilters)
val renewals = Session.database.find(Renewal::class.java).findList()
val excel = ctx.queryParam("excel")
if (excel !== null) {
// exportRenewals(renewals)
val inputStream = FileInputStream("./excel/Renewals.xls")
ctx.result(inputStream).status(HttpStatus.OK)
} else {
ctx.json(renewals).status(HttpStatus.OK)
}
}
fun update(ctx: Context) {
val id = ctx.pathParam("id").toLong()
val renewal =
Session.database.find(Renewal::class.java, id) ?: throw NotFoundResponse("renewal not found for $id")
val updatedRenewal = ctx.bodyAsClass<Renewal>()
renewal.patchValues(updatedRenewal)
renewal.update()
ctx.json(renewal).status(HttpStatus.OK)
}
fun delete(ctx: Context) {
}
}
object ReminderCtrl {
fun create(ctx: Context) {
val renewal = ctx.bodyAsClass<Reminder>()
Session.database.save(renewal)
val entry = ReminderLog()
entry.apply {
reminder = renewal
reminderDate = renewal.nextRenewalDate
reminderType = renewal.type
fleet = renewal.fleet
amount = 0.0
actedUpon = false
}
ReminderLogCtrl.create(entry)
ctx.json(renewal).status(HttpStatus.CREATED)
}
fun getAllByFleetId(ctx: Context) {
val fleetId = ctx.pathParam("id").toLong()
val history = Session.database.find(Reminder::class.java)
.where()
.eq("fleet_sys_pk", fleetId)
.findList()
ctx.json(history).status(HttpStatus.OK)
}
fun get(ctx: Context) {
val id = ctx.pathParam("id").toLong()
val renewal = Session.database.find(Reminder::class.java, id)
?: throw NotFoundResponse("No renewal found with id $id")
ctx.json(renewal).status(HttpStatus.OK)
}
//data class RNLF(val common: CommonFilters, val renewalFilters: ReminderFilters)
fun getAll(ctx: Context) {
// val filters = ctx.bodyAsClass<FLTF>()
// val renewals = searchReminders(filters.common, filters.renewalFilters)
val renewals = Session.database.find(Reminder::class.java).findList()
val excel = ctx.queryParam("excel")
if (excel !== null) {
// exportReminders(renewals)
val inputStream = FileInputStream("./excel/Reminders.xls")
ctx.result(inputStream).status(HttpStatus.OK)
} else {
ctx.json(renewals).status(HttpStatus.OK)
}
}
fun update(ctx: Context) {
val id = ctx.pathParam("id").toLong()
val renewal =
Session.database.find(Reminder::class.java, id) ?: throw NotFoundResponse("renewal not found for $id")
val updatedReminder = ctx.bodyAsClass<Reminder>()
renewal.patchValues(updatedReminder)
renewal.update()
ctx.json(renewal).status(HttpStatus.OK)
}
fun delete(ctx: Context) {
}
}
object ReminderLogCtrl {
fun create(entry: ReminderLog) {
// val renewal = ctx.bodyAsClass<ReminderLog>()
Session.database.save(entry)
return
//ctx.json(renewal).status(HttpStatus.CREATED)
}
data class Done(
val logId: Long,
val reminderId: Long,
val renewalDate: LocalDate,
val nextRenewalDate: LocalDate,
val amount: Double,
val documents: List<String>?
)
fun done(ctx: Context) {
val req = ctx.bodyAsClass<Done>()
val reminder =
Session.database.find(Reminder::class.java, req.reminderId) ?: throw NotFoundResponse("No Reminder found")
//update reminder
reminder.apply {
lastRenewalDate = req.renewalDate
nextRenewalDate = req.nextRenewalDate
}
reminder.update()
val entry = Session.database.find(ReminderLog::class.java, req.logId) ?: throw NotFoundResponse("Log not found")
entry.apply {
actedUpon = true
documents = req.documents
amount = req.amount
}
entry.update()
ctx.json(entry).status(HttpStatus.OK)
}
fun getAllByFleetId(ctx: Context) {
val fleetId = ctx.pathParam("id").toLong()
val history = Session.database.find(ReminderLog::class.java)
.where()
.eq("fleet_sys_pk", fleetId)
.findList()
ctx.json(history).status(HttpStatus.OK)
}
fun get(ctx: Context) {
val id = ctx.pathParam("id").toLong()
val renewal = Session.database.find(ReminderLog::class.java, id)
?: throw NotFoundResponse("No renewal found with id $id")
ctx.json(renewal).status(HttpStatus.OK)
}
data class LOGF(val common: CommonFilters, val reminderLogFilters: ReminderLogFilters)
fun getAll(ctx: Context) {
val filters = ctx.bodyAsClass<LOGF>()
val entries = searchReminderLogs(filters.common, filters.reminderLogFilters)
val excel = ctx.queryParam("excel")
if (excel !== null) {
exportReminderLogs(entries)
val inputStream = FileInputStream("./excel/ReminderLogs.xls")
ctx.result(inputStream).status(HttpStatus.OK)
} else {
ctx.json(entries).status(HttpStatus.OK)
}
}
fun update(ctx: Context) {
val id = ctx.pathParam("id").toLong()
val renewal =
Session.database.find(ReminderLog::class.java, id) ?: throw NotFoundResponse("renewal not found for $id")
val updatedReminderLog = ctx.bodyAsClass<ReminderLog>()
renewal.patchValues(updatedReminderLog)
renewal.update()
ctx.json(renewal).status(HttpStatus.OK)
}
fun delete(ctx: Context) {
}
}