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