-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added daily_quotas table - Implemented all missing endpoints - Improved custom types
- Loading branch information
1 parent
7171b88
commit 7e6ed5f
Showing
27 changed files
with
1,181 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package custom_types | ||
|
||
import ( | ||
"database/sql/driver" | ||
"fmt" | ||
"time" | ||
) | ||
|
||
type Date struct { | ||
_time time.Time | ||
} | ||
|
||
func (d Date) UnderlyingTime() time.Time { | ||
return d._time | ||
} | ||
|
||
func ParseDate(str string) (*Date, error) { | ||
t, err := time.Parse(time.DateOnly, str) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &Date{_time: t}, nil | ||
} | ||
|
||
func (d Date) Equal(arg Date) bool { | ||
return d._time.Format(time.DateOnly) == arg._time.Format(time.DateOnly) | ||
} | ||
|
||
func (d Date) Scan(src any) error { | ||
if src == nil { | ||
return nil | ||
} | ||
|
||
str, ok := src.(string) | ||
if !ok { | ||
return fmt.Errorf("cannot scan type %T into custom_types.Date", src) | ||
} | ||
|
||
parsedDate, err := time.Parse(time.DateOnly, str) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
d._time = parsedDate | ||
return nil | ||
} | ||
|
||
func (d Date) Value() (driver.Value, error) { | ||
return d._time.Format(time.DateOnly), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package custom_types | ||
|
||
import ( | ||
"database/sql/driver" | ||
"fmt" | ||
"time" | ||
) | ||
|
||
type Time struct { | ||
_time time.Time | ||
} | ||
|
||
func (t Time) UnderlyingTime() time.Time { | ||
return t._time | ||
} | ||
|
||
func ParseTime(str string) (*Time, error) { | ||
t, err := time.Parse(time.TimeOnly, str) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &Time{_time: t}, nil | ||
} | ||
|
||
func (t Time) Equal(arg Time) bool { | ||
return t._time.Format(time.TimeOnly) == arg._time.Format(time.TimeOnly) | ||
} | ||
|
||
func (t Time) Scan(src any) error { | ||
if src == nil { | ||
return nil | ||
} | ||
|
||
str, ok := src.(string) | ||
if !ok { | ||
return fmt.Errorf("cannot scan type %T into custom_types.Time", src) | ||
} | ||
|
||
parsedTime, err := time.Parse(time.TimeOnly, str) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
t._time = parsedTime | ||
return nil | ||
} | ||
|
||
func (t Time) Value() (driver.Value, error) { | ||
return t._time.Format(time.TimeOnly), nil | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
package daily_quotas | ||
|
||
import ( | ||
"github.com/szabolcs-horvath/nutrition-tracker/custom_types" | ||
"github.com/szabolcs-horvath/nutrition-tracker/repository" | ||
"github.com/szabolcs-horvath/nutrition-tracker/util" | ||
"net/http" | ||
"strconv" | ||
) | ||
|
||
const Prefix = "/daily_quotas" | ||
|
||
func HandlerFuncs() map[string]http.HandlerFunc { | ||
return map[string]http.HandlerFunc{ | ||
"GET /{id}": findByIdHandler, | ||
"GET /owner/{id}": listByOwnerHandler, | ||
"GET /owner/{id}/date/{date}": findByOwnerAndDateHandler, | ||
"GET /owner/{id}/date/{$}": findByOwnerAndDateHandler, | ||
"POST /{$}": createHandler, | ||
"PUT /{$}": updateHandler, | ||
"DELETE /{id}": deleteHandler, | ||
} | ||
} | ||
|
||
func findByIdHandler(w http.ResponseWriter, r *http.Request) { | ||
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
return | ||
} | ||
dailyQuota, err := repository.FindDailyQuotaById(r.Context(), id) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusNotFound) | ||
return | ||
} | ||
if err = util.WriteJson(w, http.StatusOK, dailyQuota); err != nil { | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
} | ||
} | ||
|
||
func listByOwnerHandler(w http.ResponseWriter, r *http.Request) { | ||
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
return | ||
} | ||
list, err := repository.ListDailyQuotasForUser(r.Context(), id) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
if err = util.WriteJson(w, http.StatusOK, list); err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
} | ||
} | ||
|
||
func findByOwnerAndDateHandler(w http.ResponseWriter, r *http.Request) { | ||
ownerId, err := strconv.ParseInt(r.PathValue("id"), 10, 64) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
return | ||
} | ||
var result *repository.DailyQuota | ||
dateParam := r.PathValue("date") | ||
if dateParam == "" { | ||
dq, dqErr := repository.FindDailyQuotaByOwnerAndCurrentDay(r.Context(), ownerId) | ||
if dqErr != nil { | ||
http.Error(w, dqErr.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
result = dq | ||
} else { | ||
date, parseErr := custom_types.ParseDate(r.PathValue("date")) | ||
if parseErr != nil { | ||
http.Error(w, parseErr.Error(), http.StatusBadRequest) | ||
return | ||
} | ||
dq, dqErr := repository.FindDailyQuotaByOwnerAndDate(r.Context(), ownerId, date.UnderlyingTime()) | ||
if dqErr != nil { | ||
http.Error(w, dqErr.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
result = dq | ||
} | ||
if err = util.WriteJson(w, http.StatusOK, result); err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
} | ||
} | ||
|
||
func createHandler(w http.ResponseWriter, r *http.Request) { | ||
var requestDailyQuota repository.CreateDailyQuotaRequest | ||
if err := util.ReadJson(r, &requestDailyQuota); err != nil { | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
return | ||
} | ||
dailyQuota, err := repository.CreateDailyQuota(r.Context(), requestDailyQuota) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
if err = util.WriteJson(w, http.StatusCreated, dailyQuota); err != nil { | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
} | ||
} | ||
|
||
func updateHandler(w http.ResponseWriter, r *http.Request) { | ||
var requestDailyQuota repository.UpdateDailyQuotaRequest | ||
if err := util.ReadJson(r, &requestDailyQuota); err != nil { | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
return | ||
} | ||
dailyQuota, err := repository.UpdateDailyQuota(r.Context(), requestDailyQuota) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
if err = util.WriteJson(w, http.StatusOK, dailyQuota); err != nil { | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
} | ||
} | ||
|
||
func deleteHandler(w http.ResponseWriter, r *http.Request) { | ||
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
return | ||
} | ||
if err = repository.ArchiveDailyQuota(r.Context(), id); err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
w.WriteHeader(http.StatusNoContent) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.