all booking to service

This commit is contained in:
Ruidy 2024-02-16 15:50:07 +01:00
parent d2d0fc4337
commit dc00eb33a8
No known key found for this signature in database
GPG key ID: E00F51288CB857CC
5 changed files with 15 additions and 10 deletions

View file

@ -14,6 +14,12 @@ func NewService(db *gorm.DB) *Service {
return &Service{db: db} return &Service{db: db}
} }
func (bs Service) All() []*Booking {
bookings := make([]*Booking, 0)
_ = bs.db.Order("id desc").Find(&bookings)
return bookings
}
type Line struct { type Line struct {
From time.Time From time.Time
To time.Time To time.Time

View file

@ -17,10 +17,9 @@ import (
myTime "github.com/rjNemo/rentease/pkg/time" myTime "github.com/rjNemo/rentease/pkg/time"
) )
func (s Server) handleListBookingPage() echo.HandlerFunc { func handleListBookingPage(bs *booking.Service) echo.HandlerFunc {
return func(c echo.Context) error { return func(c echo.Context) error {
bookings := make([]*booking.Booking, 0) bookings := bs.All()
_ = s.db.Order("id desc").Find(&bookings)
bvm := u.Map(bookings, func(b *booking.Booking) *views.ListBookingsViewModel { bvm := u.Map(bookings, func(b *booking.Booking) *views.ListBookingsViewModel {
return &views.ListBookingsViewModel{ return &views.ListBookingsViewModel{
@ -34,7 +33,7 @@ func (s Server) handleListBookingPage() echo.HandlerFunc {
}) })
component := views.ListBookings(bvm) component := views.ListBookings(bvm)
return s.renderTempl(c, http.StatusOK, component) return renderTempl(c, http.StatusOK, component)
} }
} }

View file

@ -25,7 +25,7 @@ func (s Server) MountHandlers() {
s.Router.Static("/static", "assets") s.Router.Static("/static", "assets")
// landing page // landing page
s.Router.GET("/", handleHomePage()) s.Router.GET("/", handleHomePage())
s.Router.GET(constants.RouteBooking, s.handleListBookingPage()) s.Router.GET("/bookings", handleListBookingPage(s.bs))
s.Router.GET(constants.RouteNewBooking, s.handleNewBookingPage()) s.Router.GET(constants.RouteNewBooking, s.handleNewBookingPage())
s.Router.POST(constants.RouteNewBooking, s.handleCreateBooking()) s.Router.POST(constants.RouteNewBooking, s.handleCreateBooking())
s.Router.GET(fmt.Sprintf("%s/:id", constants.RouteBooking), s.handleBookingPage()) s.Router.GET(fmt.Sprintf("%s/:id", constants.RouteBooking), s.handleBookingPage())

View file

@ -25,12 +25,11 @@ type Server struct {
addr string addr string
} }
func New(db *gorm.DB) *Server { func New(bs *booking.Service, ps *pdf.PdfService) *Server {
return &Server{ return &Server{
Router: echo.New(), Router: echo.New(),
db: db, bs: bs,
bs: booking.NewService(db), ps: ps,
ps: pdf.NewPdfService(),
addr: fmt.Sprintf("0.0.0.0:%s", os.Getenv("PORT")), addr: fmt.Sprintf("0.0.0.0:%s", os.Getenv("PORT")),
} }
} }

View file

@ -9,6 +9,7 @@ import (
"gorm.io/gorm" "gorm.io/gorm"
"github.com/rjNemo/rentease/internal/booking" "github.com/rjNemo/rentease/internal/booking"
"github.com/rjNemo/rentease/internal/pdf"
"github.com/rjNemo/rentease/internal/server" "github.com/rjNemo/rentease/internal/server"
) )
@ -32,7 +33,7 @@ func main() {
log.Fatalf("error migrating the database %s\n", err) log.Fatalf("error migrating the database %s\n", err)
} }
s := server.New(db) s := server.New(booking.NewService(db), pdf.NewPdfService())
s.MountHandlers() s.MountHandlers()
s.Start() s.Start()
} }