public handler

This commit is contained in:
Ruidy 2024-02-16 13:07:18 +01:00
parent 1f86cbf3ed
commit 9bbd11588b
No known key found for this signature in database
GPG key ID: E00F51288CB857CC
4 changed files with 132 additions and 107 deletions

View file

@ -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))
}
}

View file

@ -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()

View file

@ -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))
}

View file

@ -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)