propper models

This commit is contained in:
Jan 2024-09-15 21:14:48 +02:00
parent 65013d6f12
commit a5bb066df0
4 changed files with 39 additions and 9 deletions

View File

@ -5,3 +5,9 @@ type Meal struct {
Price string Price string
Attributes string Attributes string
} }
func (m *Meal) SetMeal(name string, price string, attributes string) {
m.Name = name
m.Price = price
m.Attributes = attributes
}

View File

@ -4,3 +4,11 @@ type Menu struct {
Name string Name string
Meal []Meal Meal []Meal
} }
func (m *Menu) SetMenu(name string) {
m.Name = name
}
func (m *Menu) AddMeal(meal Meal) {
m.Meal = append(m.Meal, meal)
}

View File

@ -4,3 +4,11 @@ type Day struct {
DayName string DayName string
Menu []Menu Menu []Menu
} }
func (d *Day) SetDay(dayName string) {
d.DayName = dayName
}
func (d *Day) AddMenu(menu Menu) {
d.Menu = append(d.Menu, menu)
}

View File

@ -10,6 +10,8 @@ import (
func ScrapeMensa(h io.ReadCloser) models.Mensa { func ScrapeMensa(h io.ReadCloser) models.Mensa {
var mensa models.Mensa var mensa models.Mensa
var mensaName = ""
var mensaLocation = ""
doc, err := goquery.NewDocumentFromReader(h) doc, err := goquery.NewDocumentFromReader(h)
if err != nil { if err != nil {
@ -17,7 +19,7 @@ func ScrapeMensa(h io.ReadCloser) models.Mensa {
} }
doc.Find("h1.aw-title-header-title").First().Each(func(i int, s *goquery.Selection) { doc.Find("h1.aw-title-header-title").First().Each(func(i int, s *goquery.Selection) {
mensa.Name = s.Text() mensaName = s.Text()
}) })
doc.Find("a.panel-body").Each(func(i int, s *goquery.Selection) { doc.Find("a.panel-body").Each(func(i int, s *goquery.Selection) {
@ -29,14 +31,16 @@ func ScrapeMensa(h io.ReadCloser) models.Mensa {
l = strings.Replace(l, "<br>", " ", -1) l = strings.Replace(l, "<br>", " ", -1)
l = strings.Replace(l, "</br>", " ", -1) l = strings.Replace(l, "</br>", " ", -1)
mensa.Location = l mensaLocation = l
}) })
mensa.SetMensa(mensaName, mensaLocation)
//Day //Day
var day models.Day var day models.Day
doc.Find("h2.aw-menu-title").Each(func(i int, s *goquery.Selection) { doc.Find("h2.aw-menu-title").Each(func(i int, s *goquery.Selection) {
day.DayName = s.Text() day.SetDay(s.Text())
}) })
//Menu //Menu
@ -45,25 +49,29 @@ func ScrapeMensa(h io.ReadCloser) models.Mensa {
var menu models.Menu var menu models.Menu
s.Find("h3.aw-meal-category-name").Each(func(i int, t *goquery.Selection) { s.Find("h3.aw-meal-category-name").Each(func(i int, t *goquery.Selection) {
menu.Name = t.Text() menu.SetMenu(t.Text())
}) })
//Meal //Meal
var meal models.Meal var meal models.Meal
s.Find("div.aw-meal").Each(func(i int, t *goquery.Selection) { s.Find("div.aw-meal").Each(func(i int, t *goquery.Selection) {
mealName := ""
mealPrice := ""
mealAttributes := ""
t.Find("p.aw-meal-description").First().Each(func(i int, u *goquery.Selection) { t.Find("p.aw-meal-description").First().Each(func(i int, u *goquery.Selection) {
meal.Name = u.Text() mealName = u.Text()
}) })
t.Find("div.aw-meal-price").First().Each(func(i int, u *goquery.Selection) { t.Find("div.aw-meal-price").First().Each(func(i int, u *goquery.Selection) {
meal.Price = u.Text() mealPrice = u.Text()
}) })
t.Find("p.aw-meal-attributes").First().Each(func(i int, u *goquery.Selection) { t.Find("p.aw-meal-attributes").First().Each(func(i int, u *goquery.Selection) {
meal.Attributes = u.Text() mealAttributes = u.Text()
}) })
menu.Meal = append(menu.Meal, meal) meal.SetMeal(mealName, mealPrice, mealAttributes)
menu.AddMeal(meal)
}) })
day.Menu = append(day.Menu, menu) day.AddMenu(menu)
}) })
mensa.AddDay(day) mensa.AddDay(day)