Skip to content

Commit

Permalink
Add ScheduleingUrl in schedule point response
Browse files Browse the repository at this point in the history
  • Loading branch information
masayag committed Nov 19, 2023
1 parent ea727b8 commit 7284664
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 63 deletions.
75 changes: 38 additions & 37 deletions pkg/api/api.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions pkg/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,6 @@ paths:
responses:
"200":
description: OK
content:
application/json:
schema:
$ref: "#/components/schemas/Station"
"400":
description: Error.
content:
Expand Down Expand Up @@ -327,6 +323,7 @@ components:
- date
- open_time
- close_time
- scheduling_url
properties:
station_id:
type: integer
Expand All @@ -348,6 +345,8 @@ components:
format: time
is_open:
type: boolean
scheduling_url:
type: string

Station:
type: object
Expand Down Expand Up @@ -382,6 +381,7 @@ components:
- date
- open_time
- close_time
- scheduling_url
properties:
id:
type: integer
Expand All @@ -403,6 +403,8 @@ components:
close_time:
type: string
format: time
scheduling_url:
type: string
station_status:
type: array
items:
Expand Down
15 changes: 8 additions & 7 deletions pkg/scraper/mada.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/scraper/schedule_datawriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 11 additions & 8 deletions server/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type StationSchedulePoint struct {
CloseTime string
LastStatus bool
UserID *int
SchedulingUrl string
}

const layout = "15:04"
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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))).
Expand Down
13 changes: 7 additions & 6 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"github.com/il-blood-donation-info/blood-donation-backend/pkg/api"
"gorm.io/gorm"
"time"
)

// StrictBloodInfoServer implements StrictServerInterface
Expand All @@ -32,7 +33,7 @@ func (s StrictBloodInfoServer) GetSchedule(ctx context.Context, request api.GetS
}

// GetStations gets all stations
func (s StrictBloodInfoServer) GetStations(ctx context.Context, request api.GetStationsRequestObject) (api.GetStationsResponseObject, error) {
func (s StrictBloodInfoServer) GetStations(ctx context.Context, יrequest api.GetStationsRequestObject) (api.GetStationsResponseObject, error) {
var stations []api.Station
tx := s.db.Find(&stations)
if tx.Error != nil {
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 7284664

Please sign in to comment.