From abf37d8413289453d9943c0c99168cce13e125ac Mon Sep 17 00:00:00 2001 From: Ruidy Date: Fri, 23 Aug 2024 09:55:19 +0200 Subject: [PATCH] add search by custumer name --- internal/booking/service.go | 14 +++ internal/server/handle_bookings.go | 18 +++- internal/view/booking_lines.templ | 30 ++++++ internal/view/booking_lines_templ.go | 144 +++++++++++++++++++++++++++ internal/view/bookings_list.templ | 48 ++++----- internal/view/bookings_list_templ.go | 113 ++------------------- 6 files changed, 230 insertions(+), 137 deletions(-) create mode 100644 internal/view/booking_lines.templ create mode 100644 internal/view/booking_lines_templ.go diff --git a/internal/booking/service.go b/internal/booking/service.go index e6d77a2..bc9d6ec 100644 --- a/internal/booking/service.go +++ b/internal/booking/service.go @@ -37,6 +37,20 @@ func (bs Service) All() []*Line { return bookings } +func (bs Service) Search(value string) []*Line { + bookings := make([]*Line, 0) + bs.db.Raw(` + select bookings.id, customer_name, "from", "to", platform, sum(price * quantity) as total, canceled + from bookings + left join items on bookings.id = items.booking_id + where customer_name ilike ? + group by bookings.id + order by id desc; + `, fmt.Sprintf("%%%s%%", value)). + Scan(&bookings) + return bookings +} + func (bs Service) Create(From time.Time, To time.Time, Name, PhoneNumber, Email, Platform string, CustomerNumber int, PlatformFees float64, externalId *string, ) *Booking { diff --git a/internal/server/handle_bookings.go b/internal/server/handle_bookings.go index 4d6a060..a4dd5a3 100644 --- a/internal/server/handle_bookings.go +++ b/internal/server/handle_bookings.go @@ -23,7 +23,14 @@ import ( func handleBookingListPage(bs *booking.Service, hc *config.Host) echo.HandlerFunc { return func(c echo.Context) error { - bookings := bs.All() + search := c.FormValue("search") + + bookings := make([]*booking.Line, 0) + if search != "" { + bookings = bs.Search(search) + } else { + bookings = bs.All() + } bvm := u.Map(bookings, func(b *booking.Line) *view.ListBookingsViewModel { return &view.ListBookingsViewModel{ @@ -38,8 +45,13 @@ func handleBookingListPage(bs *booking.Service, hc *config.Host) echo.HandlerFun } }) - component := view.ListBookings(bvm) - return renderTempl(c, http.StatusOK, component) + if search != "" { + component := view.BookingLines(bvm) + return renderTempl(c, http.StatusOK, component) + } else { + component := view.ListBookings(bvm) + return renderTempl(c, http.StatusOK, component) + } } } diff --git a/internal/view/booking_lines.templ b/internal/view/booking_lines.templ new file mode 100644 index 0000000..7b876ab --- /dev/null +++ b/internal/view/booking_lines.templ @@ -0,0 +1,30 @@ +package view + +import () + +templ BookingLines(bookings []*ListBookingsViewModel) { + + for _, b := range bookings { + + + + { b.Id } + + + + + { b.Name } + + + { b.Total } + { b.From } + { b.To } + { b.Platform } + + } + +} diff --git a/internal/view/booking_lines_templ.go b/internal/view/booking_lines_templ.go new file mode 100644 index 0000000..d295b4c --- /dev/null +++ b/internal/view/booking_lines_templ.go @@ -0,0 +1,144 @@ +// Code generated by templ - DO NOT EDIT. + +// templ: version: v0.2.747 +package view + +//lint:file-ignore SA4006 This context is only used if a nested component is present. + +import "github.com/a-h/templ" +import templruntime "github.com/a-h/templ/runtime" + +import () + +func BookingLines(bookings []*ListBookingsViewModel) templ.Component { + return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { + templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } + ctx = templ.InitializeContext(ctx) + templ_7745c5c3_Var1 := templ.GetChildren(ctx) + if templ_7745c5c3_Var1 == nil { + templ_7745c5c3_Var1 = templ.NopComponent + } + ctx = templ.ClearChildren(ctx) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + for _, b := range bookings { + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + var templ_7745c5c3_Var3 string + templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(b.Id) + if templ_7745c5c3_Err != nil { + return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/view/booking_lines.templ`, Line: 11, Col: 12} + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3)) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + var templ_7745c5c3_Var4 string + templ_7745c5c3_Var4, templ_7745c5c3_Err = templ.JoinStringErrs(b.Name) + if templ_7745c5c3_Err != nil { + return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/view/booking_lines.templ`, Line: 20, Col: 14} + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var4)) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + var templ_7745c5c3_Var5 string + templ_7745c5c3_Var5, templ_7745c5c3_Err = templ.JoinStringErrs(b.Total) + if templ_7745c5c3_Err != nil { + return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/view/booking_lines.templ`, Line: 23, Col: 17} + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var5)) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + var templ_7745c5c3_Var6 string + templ_7745c5c3_Var6, templ_7745c5c3_Err = templ.JoinStringErrs(b.From) + if templ_7745c5c3_Err != nil { + return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/view/booking_lines.templ`, Line: 24, Col: 16} + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var6)) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + var templ_7745c5c3_Var7 string + templ_7745c5c3_Var7, templ_7745c5c3_Err = templ.JoinStringErrs(b.To) + if templ_7745c5c3_Err != nil { + return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/view/booking_lines.templ`, Line: 25, Col: 14} + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var7)) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + var templ_7745c5c3_Var8 string + templ_7745c5c3_Var8, templ_7745c5c3_Err = templ.JoinStringErrs(b.Platform) + if templ_7745c5c3_Err != nil { + return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/view/booking_lines.templ`, Line: 26, Col: 20} + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var8)) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + return templ_7745c5c3_Err + }) +} diff --git a/internal/view/bookings_list.templ b/internal/view/bookings_list.templ index 59f66d8..75a8dc9 100644 --- a/internal/view/bookings_list.templ +++ b/internal/view/bookings_list.templ @@ -1,13 +1,26 @@ package view -import "github.com/rjNemo/rentease/internal/view/layout" +import ( + "github.com/rjNemo/rentease/internal/view/layout" +) templ ListBookings(bookings []*ListBookingsViewModel) { @layout.BaseLayout() { -
-

Bookings

-

Overview of the activity

-
+
+
+

Bookings

+

Overview of the activity

+
+ +
@@ -20,30 +33,7 @@ templ ListBookings(bookings []*ListBookingsViewModel) { - - for _, b := range bookings { - - - - - - - - - } - + @BookingLines(bookings)
Platform
- - { b.Id } - - - - { b.Name } - - { b.Total }{ b.From }{ b.To }{ b.Platform }
} diff --git a/internal/view/bookings_list_templ.go b/internal/view/bookings_list_templ.go index 22f74d5..c1d4ec0 100644 --- a/internal/view/bookings_list_templ.go +++ b/internal/view/bookings_list_templ.go @@ -8,7 +8,9 @@ package view import "github.com/a-h/templ" import templruntime "github.com/a-h/templ/runtime" -import "github.com/rjNemo/rentease/internal/view/layout" +import ( + "github.com/rjNemo/rentease/internal/view/layout" +) func ListBookings(bookings []*ListBookingsViewModel) templ.Component { return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { @@ -40,114 +42,15 @@ func ListBookings(bookings []*ListBookingsViewModel) templ.Component { }() } ctx = templ.InitializeContext(ctx) - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("

Bookings

Overview of the activity

") + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("

Bookings

Overview of the activity

IDNameTotal (€)FromToPlatform
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - for _, b := range bookings { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } + templ_7745c5c3_Err = BookingLines(bookings).Render(ctx, templ_7745c5c3_Buffer) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
IDNameTotal (€)FromToPlatform
") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - var templ_7745c5c3_Var4 string - templ_7745c5c3_Var4, templ_7745c5c3_Err = templ.JoinStringErrs(b.Id) - if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/view/bookings_list.templ`, Line: 28, Col: 15} - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var4)) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - var templ_7745c5c3_Var5 string - templ_7745c5c3_Var5, templ_7745c5c3_Err = templ.JoinStringErrs(b.Name) - if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/view/bookings_list.templ`, Line: 37, Col: 17} - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var5)) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - var templ_7745c5c3_Var6 string - templ_7745c5c3_Var6, templ_7745c5c3_Err = templ.JoinStringErrs(b.Total) - if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/view/bookings_list.templ`, Line: 40, Col: 20} - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var6)) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - var templ_7745c5c3_Var7 string - templ_7745c5c3_Var7, templ_7745c5c3_Err = templ.JoinStringErrs(b.From) - if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/view/bookings_list.templ`, Line: 41, Col: 19} - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var7)) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - var templ_7745c5c3_Var8 string - templ_7745c5c3_Var8, templ_7745c5c3_Err = templ.JoinStringErrs(b.To) - if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/view/bookings_list.templ`, Line: 42, Col: 17} - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var8)) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - var templ_7745c5c3_Var9 string - templ_7745c5c3_Var9, templ_7745c5c3_Err = templ.JoinStringErrs(b.Platform) - if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/view/bookings_list.templ`, Line: 43, Col: 23} - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var9)) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
") + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err }