diff --git a/hw12_13_14_15_calendar/configs/calendar.conf b/hw12_13_14_15_calendar/configs/calendar.conf index b1f3a9e..3518edc 100644 --- a/hw12_13_14_15_calendar/configs/calendar.conf +++ b/hw12_13_14_15_calendar/configs/calendar.conf @@ -15,7 +15,7 @@ File = "./logs/calendar.log" Level = "INFO" [Storage] -inMemory = false +inMemory = true SQLHost = "localhost" SQLPort = "5432" SQLDbase = "calendar" diff --git a/hw12_13_14_15_calendar/internal/api/private/converters.go b/hw12_13_14_15_calendar/internal/api/private/converters.go index df1eb7c..4b0cbea 100644 --- a/hw12_13_14_15_calendar/internal/api/private/converters.go +++ b/hw12_13_14_15_calendar/internal/api/private/converters.go @@ -20,5 +20,5 @@ func (s Service) buildEventList(evtMap map[event.ID]event.Event) ([]*Event, erro events[i] = &evt i++ } - return events, fmt.Errorf("can't convert types: %w", err) + return events, nil } diff --git a/hw12_13_14_15_calendar/pkg/api/public/converters.go b/hw12_13_14_15_calendar/pkg/api/public/converters.go index 0522b29..7a75f67 100644 --- a/hw12_13_14_15_calendar/pkg/api/public/converters.go +++ b/hw12_13_14_15_calendar/pkg/api/public/converters.go @@ -55,7 +55,7 @@ func (s Service) buildEventList(evtMap map[event.ID]event.Event) ([]*Event, erro events[i] = &evt i++ } - return events, fmt.Errorf("can't convert types: %w", err) + return events, nil } func (s Service) buildTimeAndRange(e *GetByDateReq) (start time.Time, qrange string, err error) { diff --git a/hw12_13_14_15_calendar/pkg/api/rest/handlers.go b/hw12_13_14_15_calendar/pkg/api/rest/handlers.go index 425079b..aecdd8f 100644 --- a/hw12_13_14_15_calendar/pkg/api/rest/handlers.go +++ b/hw12_13_14_15_calendar/pkg/api/rest/handlers.go @@ -2,8 +2,8 @@ package rest import ( "encoding/json" + "fmt" "io/ioutil" - "log" "net/http" "strconv" "time" @@ -53,7 +53,7 @@ func FromRESTUpdate(calendar *calendar.App) http.HandlerFunc { if err != nil { err503("can't get request parameter", err, calendar.Logger, r) } - bodyReq := public.Event{} + bodyReq := public.UpdateReq{} bodyIn, err := ioutil.ReadAll(req.Body) defer req.Body.Close() if err != nil { @@ -63,7 +63,7 @@ func FromRESTUpdate(calendar *calendar.App) http.HandlerFunc { if err != nil { err503("can't unmarshal data from HTTP API request", err, calendar.Logger, r) } - evt, err := pubEvent2Event(bodyReq) + evt, err := pubEvent2Event(*bodyReq.Event) if err != nil { err503("can't convert types", err, calendar.Logger, r) } @@ -117,7 +117,10 @@ func FromRESTGetByID(calendar *calendar.App) http.HandlerFunc { if err != nil { err503("can't get request parameter", err, calendar.Logger, r) } - ev, _ := calendar.Storage.GetByID(event.ID(paramID)) + ev, ok := calendar.Storage.GetByID(event.ID(paramID)) + if !ok { + err503("event not found", fmt.Errorf("no one"), calendar.Logger, r) + } evnt, err := event2pubEvent(ev) if err != nil { err503("can't convert types", err, calendar.Logger, r) @@ -126,21 +129,18 @@ func FromRESTGetByID(calendar *calendar.App) http.HandlerFunc { if err != nil { err503("can't marshal request", err, calendar.Logger, r) } - r.WriteHeader(200) _, err = r.Write(bodyOut) if err != nil { calendar.Logger.Errorf("can't send response") } - - log.Println(paramID) - r.WriteHeader(555) } } func FromRESTGetByDate(calendar *calendar.App) http.HandlerFunc { return func(r http.ResponseWriter, req *http.Request) { paramRange := mux.Vars(req)["Range"] - paramDate, err := time.Parse("2006-01-02 15:04:05", mux.Vars(req)["Date"]) + d, err := strconv.Atoi(mux.Vars(req)["Date"]) + paramDate := time.Unix(int64(d), 0) if err != nil { err503("can't parse date from request parameter", err, calendar.Logger, r) } @@ -165,6 +165,6 @@ func FromRESTGetByDate(calendar *calendar.App) http.HandlerFunc { } func err503(s string, err error, l logger.Interface, r http.ResponseWriter) { - l.Errorf(s, ": ", err.Error()) + l.Errorf("%s: %w", s, err.Error()) r.WriteHeader(503) } diff --git a/hw12_13_14_15_calendar/test/client/httpapi.go b/hw12_13_14_15_calendar/test/client/httpapi.go index b49a89b..2c9d303 100644 --- a/hw12_13_14_15_calendar/test/client/httpapi.go +++ b/hw12_13_14_15_calendar/test/client/httpapi.go @@ -74,21 +74,21 @@ func (h HTTPAPI) Delete(req *public.DeleteReq) error { func (h HTTPAPI) GetByID(req *public.GetByIDReq) (*public.GetByIDResp, error) { jreq, err := json.Marshal(req) if err != nil { - return &public.GetByIDResp{}, err + return nil, err } res, body, err := apiCall("GET", h.BaseURL+"/events/"+strconv.Itoa(int(req.ID)), jreq) if err != nil { - return &public.GetByIDResp{}, err + return nil, err } if res.StatusCode != 200 { - return &public.GetByIDResp{}, fmt.Errorf("unexpected status code %d", res.StatusCode) + return nil, fmt.Errorf("unexpected status code %d", res.StatusCode) } - var getByIDResp public.GetByIDResp - err = json.Unmarshal(body, &getByIDResp) + var ev public.Event + err = json.Unmarshal(body, &ev) if err != nil { - return &public.GetByIDResp{}, err + return nil, err } - return &getByIDResp, nil + return &public.GetByIDResp{Events: []*public.Event{&ev}}, nil } func (h HTTPAPI) List() (*public.ListResp, error) { @@ -100,7 +100,7 @@ func (h HTTPAPI) List() (*public.ListResp, error) { return &public.ListResp{}, fmt.Errorf("unexpected status code %d", res.StatusCode) } var listResp public.ListResp - err = json.Unmarshal(body, &listResp) + err = json.Unmarshal(body, &listResp.Events) if err != nil { return &public.ListResp{}, err } @@ -112,7 +112,8 @@ func (h HTTPAPI) GetByDate(req *public.GetByDateReq) (*public.GetByDateResp, err if err != nil { return &public.GetByDateResp{}, err } - res, body, err := apiCall("GET", h.BaseURL+"/events/"+string(req.Range)+"/"+req.Date.String(), jreq) + + res, body, err := apiCall("GET", h.BaseURL+"/events/"+req.Range.String()+"/"+strconv.Itoa(int(req.Date.Seconds)), jreq) if err != nil { return &public.GetByDateResp{}, err } @@ -120,7 +121,7 @@ func (h HTTPAPI) GetByDate(req *public.GetByDateReq) (*public.GetByDateResp, err return &public.GetByDateResp{}, fmt.Errorf("unexpected status code %d", res.StatusCode) } var getByDateResp public.GetByDateResp - err = json.Unmarshal(body, &getByDateResp) + err = json.Unmarshal(body, &getByDateResp.Events) if err != nil { return &public.GetByDateResp{}, err } diff --git a/hw12_13_14_15_calendar/test/main_test.go b/hw12_13_14_15_calendar/test/main_test.go index 8da33ef..ab80e3f 100644 --- a/hw12_13_14_15_calendar/test/main_test.go +++ b/hw12_13_14_15_calendar/test/main_test.go @@ -1,7 +1,6 @@ package main import ( - "context" "github.com/stretchr/testify/require" "github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/pkg/api/public" "github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/test/client" @@ -14,7 +13,7 @@ import ( func TestPublicAPIEndpoints(t *testing.T) { cli := []client.Interface{ - client.GRPCAPI{Ctx: context.Background(), Host: "localhost", Port: "50051", Name: "GRPC API"}, + // client.GRPCAPI{Ctx: context.Background(), Host: "localhost", Port: "50051", Name: "GRPC API"}, client.HTTPAPI{BaseURL: "http://localhost:50052", Name: "HTTP REST API"}, } wg := sync.WaitGroup{}