From f72da426b3415b9603d7c09b410fd811b4aee886 Mon Sep 17 00:00:00 2001 From: Moti Asayag Date: Mon, 20 Nov 2023 00:27:33 +0200 Subject: [PATCH] Add ScheduleingUrl in schedule point response --- pkg/api/api.gen.go | 75 +++++++++++++++--------------- pkg/api/openapi.yaml | 10 ++-- pkg/scraper/mada.go | 15 +++--- pkg/scraper/schedule_datawriter.go | 2 +- server/schedule.go | 19 ++++---- server/server.go | 11 +++-- 6 files changed, 70 insertions(+), 62 deletions(-) diff --git a/pkg/api/api.gen.go b/pkg/api/api.gen.go index ed4d612..13b0068 100644 --- a/pkg/api/api.gen.go +++ b/pkg/api/api.gen.go @@ -37,13 +37,14 @@ type ApiError struct { // SchedulePoint defines model for SchedulePoint. type SchedulePoint struct { - Address string `json:"address"` - CloseTime string `json:"close_time"` - Date openapi_types.Date `json:"date"` - IsOpen *bool `json:"is_open,omitempty"` - Name string `json:"name"` - OpenTime string `json:"open_time"` - StationId int64 `gorm:"primaryKey" json:"station_id"` + Address string `json:"address"` + CloseTime string `json:"close_time"` + Date openapi_types.Date `json:"date"` + IsOpen *bool `json:"is_open,omitempty"` + Name string `json:"name"` + OpenTime string `json:"open_time"` + SchedulingUrl string `json:"scheduling_url"` + StationId int64 `gorm:"primaryKey" json:"station_id"` } // Station defines model for Station. @@ -57,10 +58,11 @@ type Station struct { // StationSchedule defines model for StationSchedule. type StationSchedule struct { - CloseTime string `json:"close_time"` - Date time.Time `json:"date"` - Id *int64 `gorm:"primaryKey" json:"id,omitempty"` - OpenTime string `json:"open_time"` + CloseTime string `json:"close_time"` + Date time.Time `json:"date"` + Id *int64 `gorm:"primaryKey" json:"id,omitempty"` + OpenTime string `json:"open_time"` + SchedulingUrl string `json:"scheduling_url"` // StationId The ID of the related station StationId int64 `gorm:"index" json:"station_id"` @@ -541,13 +543,12 @@ type UpdateStationResponseObject interface { VisitUpdateStationResponse(w http.ResponseWriter) error } -type UpdateStation200JSONResponse Station +type UpdateStation200Response struct { +} -func (response UpdateStation200JSONResponse) VisitUpdateStationResponse(w http.ResponseWriter) error { - w.Header().Set("Content-Type", "application/json") +func (response UpdateStation200Response) VisitUpdateStationResponse(w http.ResponseWriter) error { w.WriteHeader(200) - - return json.NewEncoder(w).Encode(response) + return nil } type UpdateStation400JSONResponse ApiError @@ -1004,27 +1005,27 @@ func (sh *strictHandler) UpdateUser(w http.ResponseWriter, r *http.Request, id i // Base64 encoded, gzipped, json marshaled Swagger object var swaggerSpec = []string{ - "H4sIAAAAAAAC/+xY227jNhD9FYIt0Bf5kiYpWvXJ23SLYNtN0DRPiyBgzLHEhUSqJJXLBv73BS+6M7aT", - "rJ0A2TdbHHKGc86ZGekez0VeCA5cKxzfYzVPISf256xgf0oppPldSFGA1AzsSg5KkQTMT31XAI6x0pLx", - "BC+XEZbwf8kkUBx/qg0vospQXH2GucbLCJ/NU6BlBqeCcT30QSiVoFTAR4TnmVBwqVluQ1gImRONY2wf", - "RENzSnTX0D4IGDJ1KQrgLZ9XQmRAuFnkJIdgNGbL5sEoTTQT/JLRjjnj+peDxp5xDQlIHOHbkSAFG80F", - "hQT4CG61JCNNEpuZRMgcx7iQLCfy7gPc4WUfgpY/f4eozm1UZaK5Qie5QdjceY8DjEIGGuglsUDfjhIx", - "+qwEH7GECwk41rKEyD33e83Nxkdu20xvmAezNzaRK03yAt0wnSLzF30RHH5nnMKtpejWcr+CJRUOytPe", - "GDENuT3rRwkLHOMfJo0YJ16JE5/wSi7mLH84kZLcDTQXAnoFkGeteLqAfguVjR6y3iIEz9EjBTWXrHAM", - "x/+lgI6PkFggnQKSkBENFPkdOPpG4Te8rDmiiS7Voxnidq3jR6cgPFn/Z3WIPdJIII3SX5wLKyt6X5Kv", - "ggOlArlhc3gI2fZ9mhREbWxCyJ4rCPT6V1a6O+AEyizkhGXBlQWTSl8+WJ+3SMKMrHJcpIKHV6RwZRl4", - "mRt8ZzRnBsd/oRBSg2yh+MD8ZQlgj+ncvx1SlbIqkG6KhzyxquILMVTK7PQYLYREOeEk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XU9+AC3Pq9sCvqo/KAjU8VbGbcymq12MXd6uBd1/V+E8ypM9gOSbvhc6eUVHx1E8TuprjljILzW", + "44No/SAxeicrUft6chCtfwFmkkVG+STP5W0T6l8OzKqTA/O4Uz175S5cO+20t7FxOqlA17z0C/tvmfYO", + "9K1fbtEva7BqwM1vcmXuM1IHMP7N3j3trh7Cbu3SLX1tQ9qM6jbd4s0edIYCwV4rfbo8WCFPUyqaGct9", + "fejTyX1eCNPJrXk6rR2wjMyep6tNs4g1wX9jGb5WUnTR7FeUNYN2kAFu7YUw4DnL2Xjv5ez1NsIuAVcr", + "mREFdRNm3Z9yRvOIwQ3ksizsxwcrS2Ji/7cgGWKpk9EoN4KZ1Jj8fHx8RAyhvKLenaZOAB3Ra1lh+z7r", + "6aybzwEbN9fN3e+0Ti2uFv8FAAD//8z+QMKVHgAA", } // GetSwagger returns the content of the embedded swagger specification file diff --git a/pkg/api/openapi.yaml b/pkg/api/openapi.yaml index 7df8915..1873888 100644 --- a/pkg/api/openapi.yaml +++ b/pkg/api/openapi.yaml @@ -100,10 +100,6 @@ paths: responses: "200": description: OK - content: - application/json: - schema: - $ref: "#/components/schemas/Station" "400": description: Error. content: @@ -327,6 +323,7 @@ components: - date - open_time - close_time + - scheduling_url properties: station_id: type: integer @@ -348,6 +345,8 @@ components: format: time is_open: type: boolean + scheduling_url: + type: string Station: type: object @@ -382,6 +381,7 @@ components: - date - open_time - close_time + - scheduling_url properties: id: type: integer @@ -403,6 +403,8 @@ components: close_time: type: string format: time + scheduling_url: + type: string station_status: type: array items: diff --git a/pkg/scraper/mada.go b/pkg/scraper/mada.go index bacde9d..a838106 100644 --- a/pkg/scraper/mada.go +++ b/pkg/scraper/mada.go @@ -16,13 +16,14 @@ import ( const customDateLayout = "2006-01-02" type DonationDetail struct { - Name string `json:"Name"` - City string `json:"City"` - Street string `json:"Street"` - NumHouse string `json:"NumHouse"` - FromHour string `json:"FromHour"` - ToHour string `json:"ToHour"` - DateDonation time.Time `json:"DateDonation"` + Name string `json:"Name"` + City string `json:"City"` + Street string `json:"Street"` + NumHouse string `json:"NumHouse"` + FromHour string `json:"FromHour"` + ToHour string `json:"ToHour"` + DateDonation time.Time `json:"DateDonation"` + SchedulingURL string `json:"SchedulingURL"` } type MadaResponse struct { diff --git a/pkg/scraper/schedule_datawriter.go b/pkg/scraper/schedule_datawriter.go index c8abd51..4d98c6a 100644 --- a/pkg/scraper/schedule_datawriter.go +++ b/pkg/scraper/schedule_datawriter.go @@ -69,7 +69,7 @@ func (p ScheduleDataWriter) processDonationDetails(tx *gorm.DB, donationDetails if schedule.Id == nil && p.isScheduleToday(schedule) { schedule.StationStatus = &[]api.StationStatus{{IsOpen: true}} } - + schedule.SchedulingUrl = donation.SchedulingURL if err := tx.Save(&schedule).Error; err != nil { log.Printf("Error while saving schedule: %v", err) return err diff --git a/server/schedule.go b/server/schedule.go index 2648868..004a879 100644 --- a/server/schedule.go +++ b/server/schedule.go @@ -19,6 +19,7 @@ type StationSchedulePoint struct { CloseTime string LastStatus bool UserID *int + SchedulingUrl string } const layout = "15:04" @@ -67,13 +68,14 @@ func ConvertToSchedulePoints(points []StationSchedulePoint) []api.SchedulePoint for _, point := range points { schedulePoint := api.SchedulePoint{ - Address: point.StationAddress, // This field is not provided in StationSchedulePoint - CloseTime: point.CloseTime, // assuming you want HH:MM:SS format - Date: openapi_types.Date{Time: point.Date}, - IsOpen: isOpen(point), //SchedulePoint.IsOpen is &, not showing correct value in json - Name: point.StationName, - OpenTime: point.OpenTime, // assuming you want HH:MM:SS format - StationId: int64(point.StationID), + Address: point.StationAddress, // This field is not provided in StationSchedulePoint + CloseTime: point.CloseTime, // assuming you want HH:MM:SS format + Date: openapi_types.Date{Time: point.Date}, + IsOpen: isOpen(point), //SchedulePoint.IsOpen is &, not showing correct value in json + Name: point.StationName, + OpenTime: point.OpenTime, // assuming you want HH:MM:SS format + StationId: int64(point.StationID), + SchedulingUrl: point.SchedulingUrl, } schedulePoints = append(schedulePoints, schedulePoint) } @@ -141,7 +143,8 @@ func (s *Scheduler) GetStationsFullSchedule(db *gorm.DB) (Schedule, error) { "c.open_time as open_time, "+ "c.close_time as close_time, "+ "t.user_id as user_id, "+ - "COALESCE(t.is_open, true) as last_status"). //Don't we need to differentiate true / false and not defined ? + "COALESCE(t.is_open, true) as last_status,"+ //Don't we need to differentiate true / false and not defined ? + "c.scheduling_url as scheduling_url"). Joins("LEFT JOIN station_schedules c ON c.station_id = s.id"). Joins("LEFT JOIN LATERAL (?) as t ON t.station_schedule_id = c.id", subquery). Where(fmt.Sprintf("date >= '%s'", s.SinceDate.Format(dateFormat))). diff --git a/server/server.go b/server/server.go index cb896be..0d9e9fc 100644 --- a/server/server.go +++ b/server/server.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/il-blood-donation-info/blood-donation-backend/pkg/api" "gorm.io/gorm" + "time" ) // StrictBloodInfoServer implements StrictServerInterface @@ -45,8 +46,8 @@ func (s StrictBloodInfoServer) GetStations(ctx context.Context, request api.GetS // UpdateStation updates station func (s StrictBloodInfoServer) UpdateStation(ctx context.Context, request api.UpdateStationRequestObject) (api.UpdateStationResponseObject, error) { - var station api.Station - tx := s.db.First(&station, request.Id) + var sc api.StationSchedule + tx := s.db.Where("station_id = ? and date = ?", request.Id, time.Now().Format(dateFormat)).First(&sc) if tx.Error != nil { if errors.Is(tx.Error, gorm.ErrRecordNotFound) { // Handle the case when no record is found @@ -59,15 +60,15 @@ func (s StrictBloodInfoServer) UpdateStation(ctx context.Context, request api.Up // add a status to a schedule point stationStatus := api.StationStatus{ - Id: &station.Id, - IsOpen: request.Body.IsOpen, + StationScheduleId: *sc.Id, + IsOpen: request.Body.IsOpen, } tx = s.db.Create(&stationStatus) if tx.Error != nil { return api.UpdateStation500JSONResponse{}, tx.Error } - return api.UpdateStation200JSONResponse{}, nil + return api.UpdateStation200Response{}, nil } // GetUsers get all users