From 37cadd8b2001391b2f39e058e148e1e7b9a5a025 Mon Sep 17 00:00:00 2001 From: Ruidy Date: Thu, 22 Feb 2024 22:08:46 +0100 Subject: [PATCH] add customer number seed --- Makefile | 4 ++-- config/host.go | 8 ++++++-- internal/pdf/service.go | 5 +++-- internal/server/handle_pdf.go | 5 +++-- internal/server/handler_booking.go | 5 +++-- internal/server/routes.go | 4 ++-- internal/server/server.go | 2 ++ 7 files changed, 21 insertions(+), 12 deletions(-) diff --git a/Makefile b/Makefile index 5396947..86eeb8a 100644 --- a/Makefile +++ b/Makefile @@ -10,9 +10,9 @@ run: build dev: templ @air cmd/main.go templ: - @templ generate --watch & + @templ generate --watch --proxy=http://localhost:${PORT} & format: - @templ generate + @templ generate internal/views @templ fmt . @go fmt ./... lint: diff --git a/config/host.go b/config/host.go index 014bac4..60e3b1c 100644 --- a/config/host.go +++ b/config/host.go @@ -1,7 +1,11 @@ package config -type Host struct{} +type Host struct { + CustomerSeed int +} func NewHost() *Host { - return &Host{} + return &Host{ + CustomerSeed: 286, + } } diff --git a/internal/pdf/service.go b/internal/pdf/service.go index 8639ee2..09906c6 100644 --- a/internal/pdf/service.go +++ b/internal/pdf/service.go @@ -12,6 +12,7 @@ import ( "github.com/labstack/gommon/log" + "github.com/rjNemo/rentease/config" "github.com/rjNemo/rentease/internal/booking" u "github.com/rjNemo/underscore" ) @@ -32,14 +33,14 @@ func NewPdfService() *PdfService { } } -func (ps PdfService) BuildInvoice(b *booking.Booking) error { +func (ps PdfService) BuildInvoice(b *booking.Booking, hc *config.Host) error { data := struct { Context map[string]any `json:"context"` Path string `json:"path"` ProjectId string `json:"projectId"` }{ Context: map[string]any{ - "id": fmt.Sprintf("VFNI%04s", strconv.Itoa(b.Id)), + "id": fmt.Sprintf("VFNI%04s", strconv.Itoa(b.Id+hc.CustomerSeed)), "name": b.Name, "phone_number": b.PhoneNumber, "customers_number": b.CustomerNumber, diff --git a/internal/server/handle_pdf.go b/internal/server/handle_pdf.go index 790359a..5346fd5 100644 --- a/internal/server/handle_pdf.go +++ b/internal/server/handle_pdf.go @@ -4,11 +4,12 @@ import ( "strconv" "github.com/labstack/echo/v4" + "github.com/rjNemo/rentease/config" "github.com/rjNemo/rentease/internal/booking" "github.com/rjNemo/rentease/internal/pdf" ) -func handleCreateInvoicePdf(bs *booking.Service, ps *pdf.PdfService) echo.HandlerFunc { +func handleCreateInvoicePdf(bs *booking.Service, ps *pdf.PdfService, hc *config.Host) echo.HandlerFunc { return func(c echo.Context) error { idStr := c.Param("id") id, err := strconv.Atoi(idStr) @@ -18,7 +19,7 @@ func handleCreateInvoicePdf(bs *booking.Service, ps *pdf.PdfService) echo.Handle b := bs.One(id) - err = ps.BuildInvoice(b) + err = ps.BuildInvoice(b, hc) if err != nil { return err } diff --git a/internal/server/handler_booking.go b/internal/server/handler_booking.go index 47c96df..d1d8ac4 100644 --- a/internal/server/handler_booking.go +++ b/internal/server/handler_booking.go @@ -11,6 +11,7 @@ import ( "github.com/labstack/gommon/log" u "github.com/rjNemo/underscore" + "github.com/rjNemo/rentease/config" "github.com/rjNemo/rentease/constants" "github.com/rjNemo/rentease/internal/booking" "github.com/rjNemo/rentease/internal/views" @@ -72,7 +73,7 @@ func handleCreateBooking(bs *booking.Service) echo.HandlerFunc { } } -func handleBookingPage(bs *booking.Service) echo.HandlerFunc { +func handleBookingPage(bs *booking.Service, hc *config.Host) echo.HandlerFunc { return func(c echo.Context) error { idStr := c.Param("id") id, err := strconv.Atoi(idStr) @@ -83,7 +84,7 @@ func handleBookingPage(bs *booking.Service) echo.HandlerFunc { b := bs.One(id) bvm := &views.BookingViewModel{ - Id: fmt.Sprintf("%04s", strconv.Itoa(b.Id)), + Id: fmt.Sprintf("%04s", strconv.Itoa(b.Id+hc.CustomerSeed)), Name: b.Name, PhoneNumber: b.PhoneNumber, CustomerNumber: strconv.Itoa(b.CustomerNumber), diff --git a/internal/server/routes.go b/internal/server/routes.go index 35dadc2..f57482a 100644 --- a/internal/server/routes.go +++ b/internal/server/routes.go @@ -5,9 +5,9 @@ func (s Server) MountHandlers() { s.Router.GET("/bookings", handleListBookingPage(s.bs)) s.Router.GET("/bookings/new", handleNewBookingPage()) s.Router.POST("/bookings/new", handleCreateBooking(s.bs)) - s.Router.GET("/bookings/:id", handleBookingPage(s.bs)) + s.Router.GET("/bookings/:id", handleBookingPage(s.bs, s.hc)) s.Router.POST("/bookings/:id/items", handleCreateItem(s.bs)) - s.Router.GET("/bookings/pdf/:id", handleCreateInvoicePdf(s.bs, s.ps)) + s.Router.GET("/bookings/pdf/:id", handleCreateInvoicePdf(s.bs, s.ps, s.hc)) s.Router.GET("/reports", handleReportsPage()) s.Router.GET("/reports/do", handleComputeReport(s.bs)) } diff --git a/internal/server/server.go b/internal/server/server.go index e76c8af..10ee67a 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -23,6 +23,7 @@ type Server struct { Router *echo.Echo bs *booking.Service ps *pdf.PdfService + hc *config.Host addr string } @@ -31,6 +32,7 @@ func New(bs *booking.Service, ps *pdf.PdfService, hc *config.Host) *Server { Router: NewRouter(), bs: bs, ps: ps, + hc: hc, addr: fmt.Sprintf("0.0.0.0:%s", os.Getenv("PORT")), } s.MountHandlers()