From 32d4ea28f5390264edf73c7f8a98679bab75782b Mon Sep 17 00:00:00 2001 From: arsalan Date: Tue, 12 Mar 2024 17:56:11 +0530 Subject: [PATCH] add fleet --- src/main/kotlin/com/restapi/Main.kt | 30 +++++++ .../com/restapi/controllers/Entities.kt | 81 +++++++++++++++++++ .../kotlin/com/restapi/controllers/Filters.kt | 21 +++++ src/main/kotlin/com/restapi/domain/models.kt | 66 +++++++++++++++ src/main/resources/dbmigration/1.15.sql | 33 ++++++++ .../dbmigration/model/1.15.model.xml | 34 ++++++++ 6 files changed, 265 insertions(+) create mode 100644 src/main/resources/dbmigration/1.15.sql create mode 100644 src/main/resources/dbmigration/model/1.15.model.xml diff --git a/src/main/kotlin/com/restapi/Main.kt b/src/main/kotlin/com/restapi/Main.kt index c8df441..a567f37 100644 --- a/src/main/kotlin/com/restapi/Main.kt +++ b/src/main/kotlin/com/restapi/Main.kt @@ -189,6 +189,36 @@ fun main(args: Array) { ) delete("/{id}", PaymentCtrl::delete, Roles(Role.Explicit("ROLE_PAYMENT_CREATE"))) } + path("/fleet") { + post("", FleetCtrl::create, Roles(Role.Explicit("ROLE_FLEET_CREATE"))) + get( + "/{id}", + FleetCtrl::get, + Roles(Role.Explicit("ROLE_FLEET_VIEW", "ROLE_FLEET_CREATE")) + ) + put("/{id}", FleetCtrl::update, Roles(Role.Explicit("ROLE_FLEET_CREATE"))) + post( + "/getAll", + FleetCtrl::getAll, + Roles(Role.Explicit("ROLE_FLEET_CREATE", "ROLE_FLEET_VIEW")) + ) + delete("/{id}", FleetCtrl::delete, Roles(Role.Explicit("ROLE_FLEET_CREATE"))) + } + path("/renewal") { + post("", RenewalCtrl::create, Roles(Role.Explicit("ROLE_FLEET_CREATE"))) + get( + "/{id}", + RenewalCtrl::get, + Roles(Role.Explicit("ROLE_FLEET_VIEW", "ROLE_FLEET_CREATE")) + ) + put("/{id}", RenewalCtrl::update, Roles(Role.Explicit("ROLE_FLEET_CREATE"))) + post( + "/getAll", + RenewalCtrl::getAll, + Roles(Role.Explicit("ROLE_FLEET_CREATE", "ROLE_FLEET_VIEW")) + ) + delete("/{id}", RenewalCtrl::delete, Roles(Role.Explicit("ROLE_FLEET_CREATE"))) + } path("/po") { get("/next", PurchaseOrderCtrl::getNextNum, Roles(Role.Explicit("ROLE_PO_CREATE"))) post("", PurchaseOrderCtrl::create, Roles(Role.Explicit("ROLE_PO_CREATE"))) diff --git a/src/main/kotlin/com/restapi/controllers/Entities.kt b/src/main/kotlin/com/restapi/controllers/Entities.kt index 5e28adf..f667626 100644 --- a/src/main/kotlin/com/restapi/controllers/Entities.kt +++ b/src/main/kotlin/com/restapi/controllers/Entities.kt @@ -1040,3 +1040,84 @@ object InvoiceCtrl { ctx.json(seq).status(HttpStatus.OK) } } + +object FleetCtrl { + fun create(ctx: Context) { + val fleet = ctx.bodyAsClass() + database.save(fleet) + ctx.json(fleet).status(HttpStatus.CREATED) + } + fun get(ctx: Context) { + val id = ctx.pathParam("id").toLong() + val fleet = 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() + 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 = + database.find(Fleet::class.java, id) ?: throw NotFoundResponse("fleet not found for $id") + val updatedFleet= ctx.bodyAsClass() + fleet.patchValues(updatedFleet) + fleet.update() + ctx.json(fleet).status(HttpStatus.OK) + } + fun delete(ctx: Context) { + + } +} + +object RenewalCtrl{ + fun create(ctx: Context) { + val renewal = ctx.bodyAsClass() + database.save(renewal) + ctx.json(renewal).status(HttpStatus.CREATED) + } + fun get(ctx: Context) { + val id = ctx.pathParam("id").toLong() + val renewal = 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() + // val renewals = searchRenewals(filters.common, filters.renewalFilters) + val renewals = 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 = + database.find(Renewal::class.java, id) ?: throw NotFoundResponse("renewal not found for $id") + val updatedRenewal= ctx.bodyAsClass() + renewal.patchValues(updatedRenewal) + renewal.update() + ctx.json(renewal).status(HttpStatus.OK) + } + fun delete(ctx: Context) { + + } +} diff --git a/src/main/kotlin/com/restapi/controllers/Filters.kt b/src/main/kotlin/com/restapi/controllers/Filters.kt index 4407fda..6ba893a 100644 --- a/src/main/kotlin/com/restapi/controllers/Filters.kt +++ b/src/main/kotlin/com/restapi/controllers/Filters.kt @@ -97,6 +97,10 @@ data class PaymentFilters( val amountLessThan: Double = Double.MAX_VALUE ) : CustomFilters +data class FleetFilters( + val fleetNameLike: String = IGNORE +):CustomFilters + fun applyVendorHelper(q: io.ebean.ExpressionList, vids: List?) { if (vids.isNullOrEmpty()) return // q.apply { @@ -266,4 +270,21 @@ fun searchPayments(commonFilters: CommonFilters, paymentFilters: PaymentFilters) return q.findList() } +fun searchFleets(filters: CommonFilters, fleetFilters: FleetFilters) : List { + val q = database.find(Fleet::class.java) + .where() + .ilike("name", "%" + fleetFilters.fleetNameLike + "%") + + q.raw("(insurance_renewal_date >= ? and insurance_renewal_date <= ?)" + + "or (pollution_renewal_date >= ? and pollution_renewal_date <= ?)" + + "or (fitness_renewal_date >= ? and fitness_renewal_date <= ?)", + filters.from, filters.to, filters.from, filters.to, filters.from, filters.to + ) +// applyFromToHelper(q, filters.from, filters.to, "insuranceRenewalDate") +// applyFromToHelper(q, filters.from, filters.to, "pollutionRenewalDate") +// applyFromToHelper(q, filters.from, filters.to, "fitnessRenewalDate") + return q.findList() +} + + //if date is null then fromtoheper drops that/// \ No newline at end of file diff --git a/src/main/kotlin/com/restapi/domain/models.kt b/src/main/kotlin/com/restapi/domain/models.kt index 5969ec4..9121804 100644 --- a/src/main/kotlin/com/restapi/domain/models.kt +++ b/src/main/kotlin/com/restapi/domain/models.kt @@ -7,6 +7,7 @@ import com.fasterxml.jackson.databind.annotation.JsonDeserialize import io.ebean.Model import io.ebean.annotation.* import io.ebean.annotation.Index +import org.openxmlformats.schemas.wordprocessingml.x2006.main.STRestartNumber import java.time.LocalDate import java.time.LocalDateTime import javax.persistence.* @@ -517,4 +518,69 @@ open class Payment : BaseTenantModel() { @ManyToOne var vendor: Vendor? = null +} + + +data class FleetRenewal(val renewal : String, val date : LocalDate) +@Entity +open class Fleet : BaseTenantModel() { + fun patchValues(updated: Fleet) { + this.name = updated.name + this.type = updated.type + this.regNumber = updated.regNumber + this.model = updated.model + this.make = updated.make + this.driver = updated.driver + this.driverContact = updated.driverContact + this.mileage = updated.mileage + this.cost = updated.cost + this.insuranceRenewalDate = updated.insuranceRenewalDate + this.pollutionRenewalDate = updated.pollutionRenewalDate + this.fitnessRenewalDate = updated.fitnessRenewalDate + this.renewals = updated.renewals + } + + var name: String = "" + var type: String = "" + var regNumber: String = "" + var model: String = "" + var make: String = "" + var driver: String = "" + var driverContact: String = "" + var mileage: Double = 0.0 + var cost: Double = 0.0 + var insuranceRenewalDate: LocalDate? = null + var pollutionRenewalDate: LocalDate? = null + var fitnessRenewalDate: LocalDate? = null + @DbJsonB + var renewals: List?=null +} + +@Entity +open class FleetType : BaseTenantModel() { + fun patchValues(updated: FleetType) { + this.name = updated.name + this.personIncharge = updated.personIncharge + } + + var name: String = "" + var personIncharge: String = "" +} + +@Entity +open class Vehicle : BaseTenantModel() { + fun patchValues(updated: Vehicle) { + this.name = updated.name + } + + var name: String = "" +} + +@Entity +open class Renewal : BaseTenantModel() { + fun patchValues(updated: Renewal) { + this.name = updated.name + } + + var name: String = "" } \ No newline at end of file diff --git a/src/main/resources/dbmigration/1.15.sql b/src/main/resources/dbmigration/1.15.sql new file mode 100644 index 0000000..4716a7b --- /dev/null +++ b/src/main/resources/dbmigration/1.15.sql @@ -0,0 +1,33 @@ +-- apply changes +create table fleet ( + sys_pk bigint generated by default as identity not null, + deleted_on timestamp, + current_approval_level integer default 0 not null, + required_approval_levels integer default 0 not null, + mileage float not null, + cost float not null, + insurance_renewal_date date, + pollution_renewal_date date, + fitness_renewal_date date, + deleted boolean default false not null, + version integer default 1 not null, + created_at timestamp default 'now()' not null, + modified_at timestamp default 'now()' not null, + deleted_by varchar(255), + approval_status varchar(8) default 'APPROVED' not null, + tags varchar[] default '{}' not null, + comments jsonb default '[]' not null, + tenant_id varchar(255) not null, + name varchar(255) not null, + type varchar(255) not null, + reg_number varchar(255) not null, + model varchar(255) not null, + make varchar(255) not null, + driver varchar(255) not null, + driver_contact varchar(255) not null, + created_by varchar(255) not null, + modified_by varchar(255) not null, + constraint ck_fleet_approval_status check ( approval_status in ('PENDING','APPROVED','REJECTED')), + constraint pk_fleet primary key (sys_pk) +); + diff --git a/src/main/resources/dbmigration/model/1.15.model.xml b/src/main/resources/dbmigration/model/1.15.model.xml new file mode 100644 index 0000000..4d48a49 --- /dev/null +++ b/src/main/resources/dbmigration/model/1.15.model.xml @@ -0,0 +1,34 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file