From 0b5c12716c5a992237c8d608a858a40580a3bddb Mon Sep 17 00:00:00 2001 From: Ruidy Date: Sat, 15 Jun 2024 07:39:23 +0200 Subject: [PATCH] store booking to db --- internal/booking/service.go | 40 +++++++++++++++---------------------- 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/internal/booking/service.go b/internal/booking/service.go index b1a45fb..c5b5454 100644 --- a/internal/booking/service.go +++ b/internal/booking/service.go @@ -8,6 +8,7 @@ import ( "strings" "time" + "github.com/labstack/gommon/log" u "github.com/rjNemo/underscore" "gorm.io/gorm" "gorm.io/gorm/clause" @@ -221,15 +222,15 @@ func (bs Service) Cancel(id int) { bs.db.Model(&b).Update("canceled", true) } -type BookingInfo struct { - Content string `json:"content"` -} - func (bs Service) ParseFromApi(rawContent string) (*Booking, error) { + type BookingInfo struct { + Content string `json:"content"` + } + var bookingInfo BookingInfo err := json.Unmarshal([]byte(rawContent), &bookingInfo) if err != nil { - return nil, fmt.Errorf("Error unmarshalling JSON:", err) + return nil, fmt.Errorf("error unmarshalling JSON: %w", err) } content := strings.ReplaceAll(strings.TrimSpace(bookingInfo.Content), "\u00a0", " ") @@ -258,17 +259,9 @@ func (bs Service) ParseFromApi(rawContent string) (*Booking, error) { "item": item, "price": standardRate, } + log.Info(result) - jsonResult, err := json.MarshalIndent(result, "", " ") - if err != nil { - return nil, fmt.Errorf("error unmarshalling JSON: %w", err) - } - - b := new(Booking) - - if err = json.Unmarshal(jsonResult, b); err != nil { - return nil, fmt.Errorf("error unmarshalling JSON: %w", err) - } + b := bs.Create(*formatDate(arrivalDate), *formatDate(departureDate), customerName, "", customerEmail, "Booking", customerNumber, commissionAmount) return b, nil } @@ -321,19 +314,18 @@ func extractString(pattern, content string) string { return strings.TrimSpace(match[0]) } -func formatDate(date string) string { +func formatDate(date string) *time.Time { months := map[string]string{ "janv.": "01", "fév": "02", "mar": "03", "avr": "04", "mai": "05", "jun": "06", "juil.": "07", "aoû": "08", "sep": "09", "oct": "10", "nov": "11", "déc": "12", } - re := regexp.MustCompile(`(\d+)\s([^\s]+)\s(\d{4})`) - match := re.FindStringSubmatch(date) - if len(match) > 3 { - day := match[1] - month := months[match[2]] - year := match[3][2:] - return fmt.Sprintf("%02s/%02s/%s", day, month, year) + parts := strings.Split(date, " ") + dateString := fmt.Sprintf("%s-%s-%s", parts[2], months[parts[1]], parts[0]) + + t, err := time.Parse(time.DateOnly, dateString) + if err != nil { + return nil } - return "" + return &t }