store booking to db

This commit is contained in:
Ruidy 2024-06-15 07:39:23 +02:00
parent 1ec2f5365c
commit 0b5c12716c
No known key found for this signature in database
GPG key ID: E00F51288CB857CC

View file

@ -8,6 +8,7 @@ import (
"strings" "strings"
"time" "time"
"github.com/labstack/gommon/log"
u "github.com/rjNemo/underscore" u "github.com/rjNemo/underscore"
"gorm.io/gorm" "gorm.io/gorm"
"gorm.io/gorm/clause" "gorm.io/gorm/clause"
@ -221,15 +222,15 @@ func (bs Service) Cancel(id int) {
bs.db.Model(&b).Update("canceled", true) bs.db.Model(&b).Update("canceled", true)
} }
type BookingInfo struct {
Content string `json:"content"`
}
func (bs Service) ParseFromApi(rawContent string) (*Booking, error) { func (bs Service) ParseFromApi(rawContent string) (*Booking, error) {
type BookingInfo struct {
Content string `json:"content"`
}
var bookingInfo BookingInfo var bookingInfo BookingInfo
err := json.Unmarshal([]byte(rawContent), &bookingInfo) err := json.Unmarshal([]byte(rawContent), &bookingInfo)
if err != nil { 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", " ") content := strings.ReplaceAll(strings.TrimSpace(bookingInfo.Content), "\u00a0", " ")
@ -258,17 +259,9 @@ func (bs Service) ParseFromApi(rawContent string) (*Booking, error) {
"item": item, "item": item,
"price": standardRate, "price": standardRate,
} }
log.Info(result)
jsonResult, err := json.MarshalIndent(result, "", " ") b := bs.Create(*formatDate(arrivalDate), *formatDate(departureDate), customerName, "", customerEmail, "Booking", customerNumber, commissionAmount)
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)
}
return b, nil return b, nil
} }
@ -321,19 +314,18 @@ func extractString(pattern, content string) string {
return strings.TrimSpace(match[0]) return strings.TrimSpace(match[0])
} }
func formatDate(date string) string { func formatDate(date string) *time.Time {
months := map[string]string{ months := map[string]string{
"janv.": "01", "fév": "02", "mar": "03", "avr": "04", "janv.": "01", "fév": "02", "mar": "03", "avr": "04",
"mai": "05", "jun": "06", "juil.": "07", "aoû": "08", "mai": "05", "jun": "06", "juil.": "07", "aoû": "08",
"sep": "09", "oct": "10", "nov": "11", "déc": "12", "sep": "09", "oct": "10", "nov": "11", "déc": "12",
} }
re := regexp.MustCompile(`(\d+)\s([^\s]+)\s(\d{4})`) parts := strings.Split(date, " ")
match := re.FindStringSubmatch(date) dateString := fmt.Sprintf("%s-%s-%s", parts[2], months[parts[1]], parts[0])
if len(match) > 3 {
day := match[1] t, err := time.Parse(time.DateOnly, dateString)
month := months[match[2]] if err != nil {
year := match[3][2:] return nil
return fmt.Sprintf("%02s/%02s/%s", day, month, year)
} }
return "" return &t
} }