Skip to content

Commit fee132a

Browse files
authored
24 hr availability fix (#595)
* Update productfiles.go Fixes #475
1 parent a7b1d57 commit fee132a

File tree

1 file changed

+41
-23
lines changed

1 file changed

+41
-23
lines changed

api/models/productfiles.go

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -45,29 +45,47 @@ func GetProductFileAvailability(db *pgxpool.Pool, ID uuid.UUID, interval string,
4545
avail := make([]ProductfileAvailability, 0)
4646
startTime := time.Date(d.Year(), d.Month(), d.Day(), 0, 0, 0, 0, d.Location()).Format(time.RFC3339)
4747
endTime := time.Date(d.Year(), d.Month(), d.Day(), 23, 0, 0, 0, d.Location()).Format(time.RFC3339)
48-
if err := pgxscan.Select(context.Background(), db, &avail,
49-
`WITH hours AS (
50-
SELECT generate_series(
51-
$1::timestamp, -- Start of the interval
52-
$2::timestamp, -- End of the interval
53-
$3::interval
54-
) AS hour
55-
)
56-
SELECT
57-
h.hour as datetime,
58-
CASE
59-
WHEN pf.datetime IS NOT NULL THEN TRUE
60-
ELSE FALSE
61-
END AS is_available
62-
FROM
63-
hours h
64-
LEFT JOIN (
65-
select * from productfile where product_id = $4 and datetime >= $5 and datetime <= $6
66-
) pf ON date_trunc('hour', pf.datetime) = h.hour
67-
ORDER BY
68-
h.hour`, startTime, endTime, interval, ID, startTime, endTime,
69-
); err != nil {
70-
return make([]ProductfileAvailability, 0), err
48+
49+
if interval == "24 Hour" {
50+
if err := pgxscan.Select(context.Background(), db, &avail,
51+
`SELECT datetime, TRUE as is_available
52+
FROM productfile
53+
WHERE product_id = $1 AND datetime >= $2 AND datetime <= $3
54+
ORDER BY datetime`, ID, startTime, endTime,
55+
); err != nil {
56+
return make([]ProductfileAvailability, 0), err
57+
}
58+
if len(avail) == 0 {
59+
avail = append(avail, ProductfileAvailability{
60+
Datetime: time.Date(d.Year(), d.Month(), d.Day(), 0, 0, 0, 0, d.Location()),
61+
IsAvailable: false,
62+
})
63+
}
64+
} else {
65+
if err := pgxscan.Select(context.Background(), db, &avail,
66+
`WITH hours AS (
67+
SELECT generate_series(
68+
$1::timestamp, -- Start of the interval
69+
$2::timestamp, -- End of the interval
70+
$3::interval
71+
) AS hour
72+
)
73+
SELECT
74+
h.hour as datetime,
75+
CASE
76+
WHEN pf.datetime IS NOT NULL THEN TRUE
77+
ELSE FALSE
78+
END AS is_available
79+
FROM
80+
hours h
81+
LEFT JOIN (
82+
select * from productfile where product_id = $4 and datetime >= $5 and datetime <= $6
83+
) pf ON date_trunc('hour', pf.datetime) = h.hour
84+
ORDER BY
85+
h.hour`, startTime, endTime, interval, ID, startTime, endTime,
86+
); err != nil {
87+
return make([]ProductfileAvailability, 0), err
88+
}
7189
}
7290
return avail, nil
7391
}

0 commit comments

Comments
 (0)