feat: add universities and restaurants (#4)
Features: - Allow to search for individual resources by their name - Add new resources and their handler: Universities, Restaurants - Added new parameter to reverse output order Reviewed-on: #4 Co-authored-by: bdoerfchen <git@bissendorf.co> Co-committed-by: bdoerfchen <git@bissendorf.co>
This commit is contained in:
35
model/external/stwbremen/restaurant.go
vendored
Normal file
35
model/external/stwbremen/restaurant.go
vendored
Normal file
@ -0,0 +1,35 @@
|
||||
package stwbremen
|
||||
|
||||
type RestaurantList struct {
|
||||
Items []string `json:"items"`
|
||||
}
|
||||
|
||||
type Restaurant struct {
|
||||
Title string `json:"title"`
|
||||
ID string `json:"id"`
|
||||
Image string `json:"image"`
|
||||
Address string `json:"address"`
|
||||
|
||||
OpeningHours
|
||||
}
|
||||
|
||||
type OpeningHours struct {
|
||||
OpeningTimes []OpeningTime `json:"openingTimes"`
|
||||
OffseasonOpeningTimes []OpeningTime `json:"offseasonOpeningTimes"`
|
||||
OffseasonStart DateOnly `json:"offseasonStart"`
|
||||
OffseasonEnd DateOnly `json:"offseasonEnd"`
|
||||
ChangedTimes []ChangedTime `json:"changedTimes"`
|
||||
}
|
||||
|
||||
type OpeningTime struct {
|
||||
Weekday string `json:"weekday"`
|
||||
OpeningTime TimeOnly `json:"openingTime"`
|
||||
ClosingTime TimeOnly `json:"closingTime"`
|
||||
}
|
||||
|
||||
type ChangedTime struct {
|
||||
StartDate DateOnly `json:"startDate"`
|
||||
EndDate DateOnly `json:"endDate"`
|
||||
OpeningTime TimeOnly `json:"openingTime"`
|
||||
ClosingTime TimeOnly `json:"closingTime"`
|
||||
}
|
||||
53
model/external/stwbremen/types.go
vendored
Normal file
53
model/external/stwbremen/types.go
vendored
Normal file
@ -0,0 +1,53 @@
|
||||
package stwbremen
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
)
|
||||
|
||||
type TimeOnly struct {
|
||||
time.Time
|
||||
}
|
||||
|
||||
// UnmarshalJSON parses a JSON string in HH:MM:SS format into a TimeOnly
|
||||
func (t *TimeOnly) UnmarshalJSON(b []byte) error {
|
||||
var s string
|
||||
if err := json.Unmarshal(b, &s); err != nil {
|
||||
return err
|
||||
}
|
||||
parsed, err := time.Parse(time.TimeOnly, s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
t.Time = parsed
|
||||
return nil
|
||||
}
|
||||
|
||||
// MarshalJSON converts the TimeOnly to a JSON string in HH:MM:SS format
|
||||
func (t TimeOnly) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(t.Format(time.TimeOnly))
|
||||
}
|
||||
|
||||
type DateOnly struct {
|
||||
time.Time
|
||||
}
|
||||
|
||||
// UnmarshalJSON parses a JSON string in YYYY-MM-DD format into a DateOnly
|
||||
func (d *DateOnly) UnmarshalJSON(b []byte) error {
|
||||
var s string
|
||||
if err := json.Unmarshal(b, &s); err != nil {
|
||||
return err
|
||||
}
|
||||
parsed, err := time.Parse(time.DateOnly, s)
|
||||
if err != nil {
|
||||
d.Time = time.Time{}
|
||||
} else {
|
||||
d.Time = parsed
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// MarshalJSON converts the DateOnly to a JSON string in YYYY-MM-DD format
|
||||
func (d DateOnly) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(d.Format(time.DateOnly))
|
||||
}
|
||||
@ -55,9 +55,10 @@ type ingredient struct {
|
||||
Additionals []string
|
||||
}
|
||||
|
||||
func (d *Meal) Kind() string { return ResourceMeal }
|
||||
func (d *Meal) Name() string { return d.Title }
|
||||
func (d *Meal) Kind() string { return ResourceMeal }
|
||||
func (d *Meal) ItemName() string { return d.Title }
|
||||
|
||||
// Table output
|
||||
func (d *Meal) ColumnNames() []string { return []string{"Location", "Date", "Counter", "Price"} }
|
||||
func (d *Meal) Columns() []any {
|
||||
return []any{d.Location, d.Date.Format(time.DateOnly), d.Counter, d.Prices["Studierende"]}
|
||||
|
||||
@ -10,9 +10,10 @@ type Resource struct {
|
||||
Description string
|
||||
}
|
||||
|
||||
func (r *Resource) Kind() string { return ResourceResource }
|
||||
func (r *Resource) Name() string { return r.ResourceName }
|
||||
func (r *Resource) Kind() string { return ResourceResource }
|
||||
func (r *Resource) ItemName() string { return r.ResourceName }
|
||||
|
||||
// Table output
|
||||
func (r *Resource) ColumnNames() []string { return []string{"Aliases", "Description"} }
|
||||
func (r *Resource) Columns() []any {
|
||||
return []any{strings.Join(r.Aliases, ", "), r.Description}
|
||||
|
||||
21
model/resources/restaurant.go
Normal file
21
model/resources/restaurant.go
Normal file
@ -0,0 +1,21 @@
|
||||
package resources
|
||||
|
||||
const ResourceRestaurant = "restaurant"
|
||||
|
||||
type Restaurant struct {
|
||||
Name string
|
||||
Title string
|
||||
ID string
|
||||
Address string
|
||||
Image string
|
||||
OpeningHours any
|
||||
}
|
||||
|
||||
func (r *Restaurant) Kind() string { return ResourceRestaurant }
|
||||
func (r *Restaurant) ItemName() string { return r.Name }
|
||||
|
||||
// Table output
|
||||
func (r *Restaurant) ColumnNames() []string { return []string{"ID", "Title"} }
|
||||
func (r *Restaurant) Columns() []any {
|
||||
return []any{r.ID, r.Title}
|
||||
}
|
||||
19
model/resources/university.go
Normal file
19
model/resources/university.go
Normal file
@ -0,0 +1,19 @@
|
||||
package resources
|
||||
|
||||
import "strings"
|
||||
|
||||
const ResourceUniversity = "university"
|
||||
|
||||
type University struct {
|
||||
Name string
|
||||
Restaurants []string
|
||||
}
|
||||
|
||||
func (u *University) Kind() string { return ResourceUniversity }
|
||||
func (u *University) ItemName() string { return u.Name }
|
||||
|
||||
// Table output
|
||||
func (u *University) ColumnNames() []string { return []string{"Restaurants"} }
|
||||
func (u *University) Columns() []any {
|
||||
return []any{strings.Join(u.Restaurants, "\r\n")}
|
||||
}
|
||||
Reference in New Issue
Block a user