mirror of
https://github.com/rjNemo/rentease.git
synced 2026-06-06 02:36:49 +00:00
all booking to service
This commit is contained in:
parent
d2d0fc4337
commit
dc00eb33a8
5 changed files with 15 additions and 10 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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())
|
||||
|
|
|
|||
|
|
@ -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")),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
3
main.go
3
main.go
|
|
@ -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()
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue