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}
}
func (bs Service) All() []*Booking {
bookings := make([]*Booking, 0)
_ = bs.db.Order("id desc").Find(&bookings)
return bookings
}
type Line struct {
From time.Time
To time.Time

View file

@ -17,10 +17,9 @@ import (
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 {
bookings := make([]*booking.Booking, 0)
_ = s.db.Order("id desc").Find(&bookings)
bookings := bs.All()
bvm := u.Map(bookings, func(b *booking.Booking) *views.ListBookingsViewModel {
return &views.ListBookingsViewModel{
@ -34,7 +33,7 @@ func (s Server) handleListBookingPage() echo.HandlerFunc {
})
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")
// landing page
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.POST(constants.RouteNewBooking, s.handleCreateBooking())
s.Router.GET(fmt.Sprintf("%s/:id", constants.RouteBooking), s.handleBookingPage())

View file

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

View file

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