refactoring

This commit is contained in:
Ruidy 2024-02-04 15:52:59 +01:00
parent 24362b95eb
commit 45c306c143
No known key found for this signature in database
GPG key ID: E00F51288CB857CC
8 changed files with 125 additions and 31 deletions

View file

@ -2,5 +2,5 @@ package constants
const (
RouteBooking = "/bookings"
RouteNewBooking = "bookings/new"
RouteNewBooking = "/bookings/new"
)

View file

@ -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
}

View file

@ -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}
}

View file

@ -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)
}
}

View file

@ -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() {

View file

@ -0,0 +1,7 @@
package views
templ BookingById() {
@BaseLayout() {
<p> ok </p>
}
}

View file

@ -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("<p>")
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("</p>")
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
})
}

View file

@ -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()