From 45c306c1437d5edff625b9031fd3cd0fb8babb0f Mon Sep 17 00:00:00 2001 From: Ruidy Date: Sun, 4 Feb 2024 15:52:59 +0100 Subject: [PATCH] refactoring --- constants/routes.go | 2 +- internal/domains/booking/models.go | 20 +++++++++ internal/domains/booking/service.go | 13 ++++++ internal/server/handlers.go | 34 +++++++-------- internal/server/server.go | 12 +++--- internal/views/booking_by_id.templ | 7 ++++ internal/views/booking_by_id_templ.go | 59 +++++++++++++++++++++++++++ main.go | 9 ++-- 8 files changed, 125 insertions(+), 31 deletions(-) create mode 100644 internal/domains/booking/models.go create mode 100644 internal/domains/booking/service.go create mode 100644 internal/views/booking_by_id.templ create mode 100644 internal/views/booking_by_id_templ.go diff --git a/constants/routes.go b/constants/routes.go index 7480fdb..2f1b344 100644 --- a/constants/routes.go +++ b/constants/routes.go @@ -2,5 +2,5 @@ package constants const ( RouteBooking = "/bookings" - RouteNewBooking = "bookings/new" + RouteNewBooking = "/bookings/new" ) diff --git a/internal/domains/booking/models.go b/internal/domains/booking/models.go new file mode 100644 index 0000000..43ff94a --- /dev/null +++ b/internal/domains/booking/models.go @@ -0,0 +1,20 @@ +package booking + +import ( + "time" + + "gorm.io/gorm" +) + +type Booking struct { + gorm.Model + Id int + Name string `gorm:"column:customer_name"` + PhoneNumber string + CustomerNumber string `gorm:"column:customers"` + Email string + From time.Time + To time.Time + Platform string + PlatformFees string +} diff --git a/internal/domains/booking/service.go b/internal/domains/booking/service.go new file mode 100644 index 0000000..5803986 --- /dev/null +++ b/internal/domains/booking/service.go @@ -0,0 +1,13 @@ +package booking + +import ( + "gorm.io/gorm" +) + +type Service struct { + db *gorm.DB +} + +func NewService(db *gorm.DB) *Service { + return &Service{db: db} +} diff --git a/internal/server/handlers.go b/internal/server/handlers.go index 92a6ed6..42a7d5b 100644 --- a/internal/server/handlers.go +++ b/internal/server/handlers.go @@ -1,6 +1,7 @@ package server import ( + "fmt" "net/http" "time" @@ -8,6 +9,7 @@ import ( "github.com/labstack/gommon/log" "github.com/rjNemo/rentease/constants" + "github.com/rjNemo/rentease/internal/domains/booking" "github.com/rjNemo/rentease/internal/views" ) @@ -37,7 +39,6 @@ func (s Server) handleCreateBooking() echo.HandlerFunc { Platform string `form:"platform"` PlatformFees string `form:"platform_fees"` } - nb := new(NewBooking) err := c.Bind(nb) if err != nil { @@ -50,19 +51,7 @@ func (s Server) handleCreateBooking() echo.HandlerFunc { ts, err = parseTime(c.FormValue("to")) nb.To = ts - type Booking struct { - Id int - Name string `gorm:"column:customer_name"` - PhoneNumber string - CustomerNumber string `gorm:"column:customers"` - Email string - From time.Time - To time.Time - Platform string - PlatformFees string - } - // create a booking object - result := s.db.Create(&Booking{ + b := &booking.Booking{ Name: nb.Name, PhoneNumber: nb.PhoneNumber, CustomerNumber: nb.CustomerNumber, @@ -71,12 +60,17 @@ func (s Server) handleCreateBooking() echo.HandlerFunc { To: nb.To, Platform: nb.Platform, PlatformFees: nb.PlatformFees, - }) - log.Info(result.Error) - log.Info(result.RowsAffected) - // store it - // redirect to the booking page - return nil + } + _ = s.db.Create(b) + return c.Redirect(http.StatusSeeOther, fmt.Sprintf("%s/%d", constants.RouteBooking, b.Id)) + } +} + +func (s Server) handleBookingPage() echo.HandlerFunc { + return func(c echo.Context) error { + component := views.BookingById() + return s.renderTempl(c, http.StatusOK, component) + } } diff --git a/internal/server/server.go b/internal/server/server.go index b7f20e1..2635204 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -16,23 +16,22 @@ import ( "gorm.io/gorm" "github.com/rjNemo/rentease/constants" + "github.com/rjNemo/rentease/internal/domains/booking" ) type Server struct { Router *echo.Echo db *gorm.DB - //ms *services.MemberService - //us *services.UserService - addr string + bs *booking.Service + addr string } func New(db *gorm.DB) *Server { return &Server{ Router: echo.New(), db: db, - //ms: services.NewMemberService(db), - //us: services.NewUserService(db), - addr: fmt.Sprintf("0.0.0.0:%s", os.Getenv("PORT")), + bs: booking.NewService(db), + addr: fmt.Sprintf("0.0.0.0:%s", os.Getenv("PORT")), } } @@ -53,6 +52,7 @@ func (s Server) MountHandlers() { s.Router.GET("/", s.handleHomePage()) 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()) } func (s Server) Start() { diff --git a/internal/views/booking_by_id.templ b/internal/views/booking_by_id.templ new file mode 100644 index 0000000..400e8d0 --- /dev/null +++ b/internal/views/booking_by_id.templ @@ -0,0 +1,7 @@ +package views + +templ BookingById() { + @BaseLayout() { +

ok

+ } +} \ No newline at end of file diff --git a/internal/views/booking_by_id_templ.go b/internal/views/booking_by_id_templ.go new file mode 100644 index 0000000..41a9a90 --- /dev/null +++ b/internal/views/booking_by_id_templ.go @@ -0,0 +1,59 @@ +// Code generated by templ - DO NOT EDIT. + +// templ: version: 0.2.476 +package views + +//lint:file-ignore SA4006 This context is only used if a nested component is present. + +import "github.com/a-h/templ" +import "context" +import "io" +import "bytes" + +func BookingById() templ.Component { + return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) { + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer) + if !templ_7745c5c3_IsBuffer { + templ_7745c5c3_Buffer = templ.GetBuffer() + defer templ.ReleaseBuffer(templ_7745c5c3_Buffer) + } + 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_Var2 := templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) { + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer) + if !templ_7745c5c3_IsBuffer { + templ_7745c5c3_Buffer = templ.GetBuffer() + defer templ.ReleaseBuffer(templ_7745c5c3_Buffer) + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("

") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Var3 := `ok ` + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(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 + } + if !templ_7745c5c3_IsBuffer { + _, templ_7745c5c3_Err = io.Copy(templ_7745c5c3_W, templ_7745c5c3_Buffer) + } + return templ_7745c5c3_Err + }) + templ_7745c5c3_Err = BaseLayout().Render(templ.WithChildren(ctx, templ_7745c5c3_Var2), templ_7745c5c3_Buffer) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + if !templ_7745c5c3_IsBuffer { + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) + } + return templ_7745c5c3_Err + }) +} diff --git a/main.go b/main.go index 3540f08..8aa4972 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( "gorm.io/driver/postgres" "gorm.io/gorm" + "github.com/rjNemo/rentease/internal/domains/booking" "github.com/rjNemo/rentease/internal/server" ) @@ -26,10 +27,10 @@ func main() { log.Fatalf("error connecting to the database %s\n", err) } - //err = db.AutoMigrate(&services.User{}, &models.Member{}, &models.MeetingNote{}) - //if err != nil { - // log.Fatalf("error migrating the database %s\n", err) - //} + err = db.AutoMigrate(&booking.Booking{}) + if err != nil { + log.Fatalf("error migrating the database %s\n", err) + } s := server.New(db) s.MountHandlers()