I have a "Settings" table in my postgresql database with following data :
id | price | duration | start_mn | end_mn | step | days_ranges
----+-------+----------+----------+--------+------+--------------------------------------------------
1 | 50 | 40 | 540 | 1140 | 30 | [{"weekday":3,"ranges":{"from":599, "to":1079}}]
I want to handle a patch request that would edit a single field such as duration for instance.
Atm I have an http request with this body : {duration: 20}
I'd like to know how to properly patch. On my GO backend these structs are set :
type Settings struct {
Price int
Duration int
StartMn int
EndMn int
Step int
DaysRanges DaysRanges
}
type DaysRanges []struct {
WeekDay int
Ranges struct {
From int
To int
}
}
Now the function that handles it for now :
func PatchSettings(w http.ResponseWriter, r *http.Request) {
var settings Settings
err := json.NewDecoder(r.Body).Decode(&settings)
if err != nil {
panic(err)
}
}
So far I got an object Settings with many fields empty (since only the duration is passed in the body). I can then check each field and if it is not empty process an sql update on that specific field with its value.
But I have a feeling it's quite poor understanding of the PATCH request and the way to deal with it.
I'm looking for a better approach.