birthdaybot/model/birthdays.go
2023-06-30 00:31:29 +02:00

53 lines
1.3 KiB
Go

package model
import (
"fmt"
"strconv"
"strings"
)
type Birthday struct {
Name string
Email string
Phone string
YearOfBirth int
MonthOfBirth int
DayOfBirth int
}
func NewBirthdayFromRecord(record []string) (*Birthday, error) {
if len(record) != 4 {
return nil, fmt.Errorf("invalid length %d for record", len(record))
}
dateComponents := strings.Split(strings.TrimSpace(record[3]), "/")
if len(dateComponents) != 3 {
return nil, fmt.Errorf("invalid date format in %q", strings.TrimSpace(record[3]))
}
dayOfBirth, err := strconv.Atoi(dateComponents[0])
if err != nil {
return nil, fmt.Errorf("error parsing day of birth from %q: %w", dateComponents[0], err)
}
monthOfBirth, err := strconv.Atoi(dateComponents[1])
if err != nil {
return nil, fmt.Errorf("error parsing month of birth from %q: %w", dateComponents[1], err)
}
yearOfBirth, err := strconv.Atoi(dateComponents[2])
if err != nil {
return nil, fmt.Errorf("error parsing year of birth from %q: %w", dateComponents[2], err)
}
b := &Birthday{
Name: strings.TrimSpace(record[0]),
Email: strings.TrimSpace(record[1]),
Phone: strings.TrimSpace(record[2]),
YearOfBirth: yearOfBirth,
MonthOfBirth: monthOfBirth,
DayOfBirth: dayOfBirth,
}
return b, nil
}