mirror of
https://github.com/rjNemo/rentease.git
synced 2026-06-10 20:56:50 +00:00
239 lines
6.1 KiB
Go
239 lines
6.1 KiB
Go
package server
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/a-h/templ"
|
|
"github.com/labstack/echo/v4"
|
|
"github.com/labstack/gommon/log"
|
|
u "github.com/rjNemo/underscore"
|
|
|
|
"github.com/rjNemo/rentease/constants"
|
|
"github.com/rjNemo/rentease/internal/domains/booking"
|
|
"github.com/rjNemo/rentease/internal/views"
|
|
myTime "github.com/rjNemo/rentease/pkg/time"
|
|
)
|
|
|
|
func (s Server) handleHomePage() echo.HandlerFunc {
|
|
return func(ctx echo.Context) error {
|
|
component := views.Index()
|
|
return s.renderTempl(ctx, http.StatusOK, component)
|
|
}
|
|
}
|
|
|
|
func (s Server) handleListBookingPage() echo.HandlerFunc {
|
|
return func(c echo.Context) error {
|
|
bookings := make([]*booking.Booking, 0)
|
|
_ = s.db.Find(&bookings)
|
|
|
|
bvm := u.Map(bookings, func(b *booking.Booking) *views.ListBookingsViewModel {
|
|
return &views.ListBookingsViewModel{
|
|
Id: strconv.Itoa(b.Id),
|
|
Url: templ.SafeURL(fmt.Sprintf("%s/%d", constants.RouteBooking, b.Id)),
|
|
From: b.From.Format("2006-01-02"),
|
|
To: b.To.Format("2006-01-02"),
|
|
Platform: b.Platform,
|
|
Name: b.Name,
|
|
}
|
|
})
|
|
|
|
component := views.ListBookings(bvm)
|
|
return s.renderTempl(c, http.StatusOK, component)
|
|
}
|
|
}
|
|
|
|
func (s Server) handleNewBookingPage() echo.HandlerFunc {
|
|
return func(c echo.Context) error {
|
|
component := views.NewBooking(constants.Platforms)
|
|
return s.renderTempl(c, http.StatusOK, component)
|
|
}
|
|
}
|
|
|
|
func (s Server) handleCreateBooking() echo.HandlerFunc {
|
|
return func(c echo.Context) error {
|
|
type NewBooking struct {
|
|
Name string `form:"name"`
|
|
PhoneNumber string `form:"phone_number"`
|
|
CustomerNumber string `form:"customer_number"`
|
|
Email string `form:"email"`
|
|
From time.Time `json:"from"`
|
|
To time.Time `from:"to"`
|
|
Platform string `form:"platform"`
|
|
PlatformFees string `form:"platform_fees"`
|
|
}
|
|
nb := new(NewBooking)
|
|
err := c.Bind(nb)
|
|
if err != nil {
|
|
log.Warn(err)
|
|
return err
|
|
}
|
|
|
|
ts, _ := myTime.ParseFromForm(c.FormValue("from"))
|
|
nb.From = ts
|
|
ts, _ = myTime.ParseFromForm(c.FormValue("to"))
|
|
nb.To = ts
|
|
|
|
b := &booking.Booking{
|
|
Name: nb.Name,
|
|
PhoneNumber: nb.PhoneNumber,
|
|
CustomerNumber: nb.CustomerNumber,
|
|
Email: nb.Email,
|
|
From: nb.From,
|
|
To: nb.To,
|
|
Platform: nb.Platform,
|
|
PlatformFees: nb.PlatformFees,
|
|
}
|
|
_ = 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 {
|
|
idStr := c.Param("id")
|
|
id, err := strconv.Atoi(idStr)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
b := &booking.Booking{Id: id}
|
|
s.db.Preload("Items").First(b)
|
|
|
|
component := views.BookingById(b, constants.Items, constants.Platforms, constants.PaymentMethods)
|
|
return s.renderTempl(c, http.StatusOK, component)
|
|
|
|
}
|
|
}
|
|
|
|
func (s Server) handleCreateItem() echo.HandlerFunc {
|
|
return func(c echo.Context) error {
|
|
bookingIdStr := c.Param("id")
|
|
bid, err := strconv.Atoi(bookingIdStr)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
type NewItem struct {
|
|
Item string `form:"item"`
|
|
Quantity int `form:"quantity"`
|
|
Price string `form:"price"`
|
|
PaymentMethod string `form:"method"`
|
|
}
|
|
ni := new(NewItem)
|
|
if err := c.Bind(ni); err != nil {
|
|
log.Warn(err)
|
|
return err
|
|
}
|
|
|
|
i := &booking.Item{
|
|
BookingId: bid,
|
|
Item: ni.Item,
|
|
Quantity: ni.Quantity,
|
|
Price: ni.Price,
|
|
PaymentMethod: ni.PaymentMethod,
|
|
}
|
|
_ = s.db.Create(i)
|
|
return s.renderTempl(c, http.StatusCreated, views.LineItem(i))
|
|
}
|
|
}
|
|
|
|
func (s Server) handleReportsPage() echo.HandlerFunc {
|
|
return func(c echo.Context) error {
|
|
period := c.QueryParam("period")
|
|
if !u.Contains([]string{"month", "year"}, period) {
|
|
period = "month"
|
|
}
|
|
|
|
monthStr := c.QueryParam("month")
|
|
month, err := strconv.Atoi(monthStr)
|
|
if err != nil || month < 1 || month > 12 {
|
|
month = int(time.Now().Month())
|
|
}
|
|
|
|
yearStr := c.QueryParam("year")
|
|
_, err = strconv.Atoi(yearStr)
|
|
if err != nil {
|
|
yearStr = time.Now().Format("2006")
|
|
}
|
|
return s.renderTempl(c, http.StatusOK, views.Reports(constants.Months, yearStr))
|
|
}
|
|
}
|
|
|
|
func (s Server) handleComputeReport() echo.HandlerFunc {
|
|
return func(c echo.Context) error {
|
|
period := c.QueryParam("period")
|
|
if !u.Contains([]string{"month", "year"}, period) {
|
|
return &echo.HTTPError{
|
|
Code: http.StatusBadRequest,
|
|
Message: fmt.Sprintf("%q is not a valid period", period),
|
|
}
|
|
}
|
|
|
|
monthStr := c.QueryParam("month")
|
|
month, err := strconv.Atoi(monthStr)
|
|
if err != nil || month < 1 || month > 12 {
|
|
return &echo.HTTPError{
|
|
Code: http.StatusBadRequest,
|
|
Message: fmt.Sprintf("%q is not a valid month", month),
|
|
}
|
|
}
|
|
|
|
yearStr := c.QueryParam("year")
|
|
year, err := strconv.Atoi(yearStr)
|
|
if err != nil {
|
|
return &echo.HTTPError{
|
|
Code: http.StatusBadRequest,
|
|
Message: fmt.Sprintf("%q is not a valid year", year),
|
|
}
|
|
}
|
|
// TODO: compute report view model
|
|
return s.renderTempl(c, http.StatusOK, views.ReportSection())
|
|
}
|
|
}
|
|
|
|
func (s Server) handleCreateInvoicePdf() echo.HandlerFunc {
|
|
return func(c echo.Context) error {
|
|
data := struct {
|
|
Context map[string]any `json:"context"`
|
|
Path string `json:"path"`
|
|
ProjectId string `json:"projectId"`
|
|
}{
|
|
Context: map[string]interface{}{},
|
|
Path: "index.html",
|
|
ProjectId: os.Getenv("HTMLDOCS_PROJECT_ID"),
|
|
}
|
|
|
|
payload, err := json.Marshal(data)
|
|
if err != nil {
|
|
fmt.Println("Error marshalling JSON:", err)
|
|
return err
|
|
}
|
|
|
|
req, err := http.NewRequest("POST", os.Getenv("HTMLDOCS_URL"), bytes.NewBuffer(payload))
|
|
if err != nil {
|
|
fmt.Println("Error creating request:", err)
|
|
return err
|
|
}
|
|
|
|
req.Header.Set("Authorization", "Bearer "+os.Getenv("HTMLDOCS_KEY"))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
client := &http.Client{}
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
fmt.Println("Error sending request:", err)
|
|
return err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
fmt.Println("Response Status:", resp.Status)
|
|
fmt.Println("Response Status:", resp.Body)
|
|
|
|
return nil
|
|
}
|
|
}
|