From 9bbd11588b7e900faf3bbedde28c521bb898ab47 Mon Sep 17 00:00:00 2001 From: Ruidy Date: Fri, 16 Feb 2024 13:07:18 +0100 Subject: [PATCH] public handler --- internal/server/handle_reports.go | 115 ++++++++++++++++++++++++++++++ internal/server/handlers.go | 107 +-------------------------- internal/server/routes.go | 6 +- internal/server/server.go | 11 +++ 4 files changed, 132 insertions(+), 107 deletions(-) create mode 100644 internal/server/handle_reports.go diff --git a/internal/server/handle_reports.go b/internal/server/handle_reports.go new file mode 100644 index 0000000..d105492 --- /dev/null +++ b/internal/server/handle_reports.go @@ -0,0 +1,115 @@ +package server + +import ( + "fmt" + "net/http" + "strconv" + "time" + + "github.com/a-h/templ" + "github.com/labstack/echo/v4" + "github.com/rjNemo/rentease/constants" + "github.com/rjNemo/rentease/internal/views" + u "github.com/rjNemo/underscore" +) + +func handleReportsPage() echo.HandlerFunc { + return func(c echo.Context) error { + period := c.QueryParam("period") + if !u.Contains([]string{"month", "year"}, period) { + period = "month" + } + + monthStr := c.QueryParam("month") + month, err := strconv.Atoi(monthStr) + if err != nil || month < 1 || month > 12 { + month = int(time.Now().Month()) + } + + yearStr := c.QueryParam("year") + _, err = strconv.Atoi(yearStr) + if err != nil { + yearStr = time.Now().Format("2006") + } + return renderTempl(c, http.StatusOK, views.Reports(constants.Months, yearStr)) + } +} + +func (s Server) handleComputeReport() echo.HandlerFunc { + return func(c echo.Context) error { + period := c.FormValue("period") + if !u.Contains([]string{"month", "year"}, period) { + return &echo.HTTPError{ + Code: http.StatusBadRequest, + Message: fmt.Sprintf("%q is not a valid period", period), + } + } + + monthStr := c.FormValue("month") + month, err := strconv.Atoi(monthStr) + if err != nil || month < 1 || month > 12 { + return &echo.HTTPError{ + Code: http.StatusBadRequest, + Message: fmt.Sprintf("%q is not a valid month", month), + } + } + + yearStr := c.FormValue("year") + year, err := strconv.Atoi(yearStr) + if err != nil { + return &echo.HTTPError{ + Code: http.StatusBadRequest, + Message: fmt.Sprintf("%q is not a valid year", year), + } + } + + type Result struct { + From time.Time + To time.Time + Id string + CustomerName string + Platform string + Total float64 + PlatformFees float64 + } + res := make([]*Result, 0) + var startDate time.Time + var endDate time.Time + + if period == "month" { + startDate = time.Date(year, time.Month(month), 1, 0, 0, 0, 0, time.UTC) + endDate = time.Date(year, time.Month(month), 31, 0, 0, 0, 0, time.UTC) + } else { + startDate = time.Date(year, time.January, 1, 0, 0, 0, 0, time.UTC) + endDate = time.Date(year, time.December, 31, 0, 0, 0, 0, time.UTC) + } + + s.db. + Raw(` + select id, customer_name, "from", "to", platform, total, platform_fees + from (select sum(price) as total, booking_id + from bookings + join items on bookings.id = items.booking_id + where "from" between ? and ? + group by booking_id) as sbi, + (select * from bookings) as b + where sbi.booking_id = b.id; + `, + startDate.Format("2006-01-02"), endDate.Format("2006-01-02")). + Scan(&res) + + reportVm := u.Map(res, func(r *Result) *views.ReportViewModel { + return &views.ReportViewModel{ + Id: r.Id, + Url: templ.SafeURL(fmt.Sprintf("%s/%s", constants.RouteBooking, r.Id)), + Total: strconv.FormatFloat(r.Total, 'f', 2, 64), + CustomerName: r.CustomerName, + From: r.From.Format("2006-01-02"), + To: r.To.Format("2006-01-02"), + Platform: r.Platform, + PlatformFees: strconv.FormatFloat(r.PlatformFees, 'f', 2, 64), + } + }) + return s.renderTempl(c, http.StatusOK, views.ReportSection(reportVm)) + } +} diff --git a/internal/server/handlers.go b/internal/server/handlers.go index 749424b..6b2cda3 100644 --- a/internal/server/handlers.go +++ b/internal/server/handlers.go @@ -18,10 +18,10 @@ import ( myTime "github.com/rjNemo/rentease/pkg/time" ) -func (s Server) handleHomePage() echo.HandlerFunc { +func handleHomePage() echo.HandlerFunc { return func(ctx echo.Context) error { component := views.Index() - return s.renderTempl(ctx, http.StatusOK, component) + return renderTempl(ctx, http.StatusOK, component) } } @@ -145,9 +145,9 @@ func (s Server) handleCreateItem() echo.HandlerFunc { type NewItem struct { Item string `form:"item"` + PaymentMethod string `form:"method"` Quantity int `form:"quantity"` Price float64 `form:"price"` - PaymentMethod string `form:"method"` } ni := new(NewItem) if err := c.Bind(ni); err != nil { @@ -167,107 +167,6 @@ func (s Server) handleCreateItem() echo.HandlerFunc { } } -func (s Server) handleReportsPage() echo.HandlerFunc { - return func(c echo.Context) error { - period := c.QueryParam("period") - if !u.Contains([]string{"month", "year"}, period) { - period = "month" - } - - monthStr := c.QueryParam("month") - month, err := strconv.Atoi(monthStr) - if err != nil || month < 1 || month > 12 { - month = int(time.Now().Month()) - } - - yearStr := c.QueryParam("year") - _, err = strconv.Atoi(yearStr) - if err != nil { - yearStr = time.Now().Format("2006") - } - return s.renderTempl(c, http.StatusOK, views.Reports(constants.Months, yearStr)) - } -} - -func (s Server) handleComputeReport() echo.HandlerFunc { - return func(c echo.Context) error { - period := c.FormValue("period") - if !u.Contains([]string{"month", "year"}, period) { - return &echo.HTTPError{ - Code: http.StatusBadRequest, - Message: fmt.Sprintf("%q is not a valid period", period), - } - } - - monthStr := c.FormValue("month") - month, err := strconv.Atoi(monthStr) - if err != nil || month < 1 || month > 12 { - return &echo.HTTPError{ - Code: http.StatusBadRequest, - Message: fmt.Sprintf("%q is not a valid month", month), - } - } - - yearStr := c.FormValue("year") - year, err := strconv.Atoi(yearStr) - if err != nil { - return &echo.HTTPError{ - Code: http.StatusBadRequest, - Message: fmt.Sprintf("%q is not a valid year", year), - } - } - - type Result struct { - From time.Time - To time.Time - Id string - CustomerName string - Platform string - Total float64 - PlatformFees float64 - } - res := make([]*Result, 0) - var startDate time.Time - var endDate time.Time - - if period == "month" { - startDate = time.Date(year, time.Month(month), 1, 0, 0, 0, 0, time.UTC) - endDate = time.Date(year, time.Month(month), 31, 0, 0, 0, 0, time.UTC) - } else { - startDate = time.Date(year, time.January, 1, 0, 0, 0, 0, time.UTC) - endDate = time.Date(year, time.December, 31, 0, 0, 0, 0, time.UTC) - } - - s.db. - Raw(` - select id, customer_name, "from", "to", platform, total, platform_fees - from (select sum(price) as total, booking_id - from bookings - join items on bookings.id = items.booking_id - where "from" between ? and ? - group by booking_id) as sbi, - (select * from bookings) as b - where sbi.booking_id = b.id; - `, - startDate.Format("2006-01-02"), endDate.Format("2006-01-02")). - Scan(&res) - - reportVm := u.Map(res, func(r *Result) *views.ReportViewModel { - return &views.ReportViewModel{ - Id: r.Id, - Url: templ.SafeURL(fmt.Sprintf("%s/%s", constants.RouteBooking, r.Id)), - Total: strconv.FormatFloat(r.Total, 'f', 2, 64), - CustomerName: r.CustomerName, - From: r.From.Format("2006-01-02"), - To: r.To.Format("2006-01-02"), - Platform: r.Platform, - PlatformFees: strconv.FormatFloat(r.PlatformFees, 'f', 2, 64), - } - }) - return s.renderTempl(c, http.StatusOK, views.ReportSection(reportVm)) - } -} - func handleCreateInvoicePdf(ps *pdf.PdfService) echo.HandlerFunc { return func(c echo.Context) error { err := ps.BuildInvoice() diff --git a/internal/server/routes.go b/internal/server/routes.go index c861c98..cb27bab 100644 --- a/internal/server/routes.go +++ b/internal/server/routes.go @@ -24,13 +24,13 @@ func (s Server) MountHandlers() { // static assets s.Router.Static("/static", "assets") // landing page - s.Router.GET("/", s.handleHomePage()) + s.Router.GET("/", handleHomePage()) s.Router.GET(constants.RouteBooking, s.handleListBookingPage()) s.Router.GET(constants.RouteNewBooking, s.handleNewBookingPage()) s.Router.POST(constants.RouteNewBooking, s.handleCreateBooking()) s.Router.GET(fmt.Sprintf("%s/:id", constants.RouteBooking), s.handleBookingPage()) s.Router.POST(fmt.Sprintf("%s/:id/items", constants.RouteBooking), s.handleCreateItem()) - s.Router.GET(constants.RouteReports, s.handleReportsPage()) - s.Router.GET(fmt.Sprintf("%s/do", constants.RouteReports), s.handleComputeReport()) + s.Router.GET("/reports", handleReportsPage()) + s.Router.GET("/reports/do", s.handleComputeReport()) s.Router.GET("/pdf", handleCreateInvoicePdf(s.ps)) } diff --git a/internal/server/server.go b/internal/server/server.go index 4b4f50d..0c1ce42 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -52,6 +52,17 @@ func (s Server) Start() { } } +func renderTempl(c echo.Context, status int, t templ.Component) error { + c.Response().Writer.WriteHeader(status) + + err := t.Render(context.Background(), c.Response().Writer) + if err != nil { + return c.String(http.StatusInternalServerError, "failed to render response template") + } + + return nil +} + func (s Server) renderTempl(c echo.Context, status int, t templ.Component) error { c.Response().Writer.WriteHeader(status)