func Update(registry kit.Registry, request kit.Request) (kit.Response, apperror.Error) { collection := request.GetContext().MustString("collection") res := registry.Resource(collection) if res == nil || !res.IsPublic() { return nil, &apperror.Err{ Code: "unknown_resource", Message: fmt.Sprintf("The resource '%v' does not exist", collection), } } model, ok := request.GetData().(kit.Model) if !ok { return nil, apperror.New("invalid_data_no_model", "Node model data in request.") } response := res.ApiUpdate(model, request) return response, nil }
func UnserializeRequestMiddleware(registry kit.Registry, request kit.Request) (kit.Response, bool) { // Try to parse json in body. Ignore error since body might not contain json. contentType := request.GetHttpRequest().Header.Get("Content-Type") if strings.Contains(contentType, "json") { // Only read the HTTP body automatically for json content type requests, // since some handlers might need to read it themselfes (see the files package resource). if err := request.ReadHttpBody(); err != nil { return kit.NewErrorResponse(err, "http_body_read_error"), false } else { if request.GetRawData() != nil { if err := request.ParseJsonData(); err != nil { return kit.NewErrorResponse(err, "invalid_json_body", true), false } if request.GetData() != nil { // Successfully parsed json body. // Now try to unserialize. // Determine serializer. serializer := registry.DefaultSerializer() // Check if a custom serializer was specified. if name := request.GetContext().String("request-serializer"); name != "" { serializer = registry.Serializer(name) } if serializer == nil { return kit.NewErrorResponse("unknown_serializer", fmt.Sprintf("The specified request serializer does not exist")), false } else { if err := request.Unserialize(serializer); err != nil { return kit.NewErrorResponse(err, "request_unserialize_error", true), false } } } } } } return nil, false }
func UnserializerMiddleware(registry kit.Registry, request kit.Request) (kit.Response, bool) { serializer := registry.DefaultSerializer() // Try to find custom serializer. data, ok := request.GetData().(map[string]interface{}) if ok { name, ok := data["request_serializer"].(string) if ok { s := registry.Serializer(name) if s == nil { resp := kit.NewErrorResponse("unknown_request_serializer", fmt.Sprintf("The given request serializer %v does not exist", name)) return resp, false } else { serializer = s } } } if err := serializer.UnserializeRequest(request.GetData(), request); err != nil { return kit.NewErrorResponse(err), false } return nil, false }
func Create(registry kit.Registry, request kit.Request) (kit.Response, apperror.Error) { collection := request.GetContext().MustString("collection") res := registry.Resource(collection) if res == nil || !res.IsPublic() { return nil, &apperror.Err{ Code: "unknown_resource", Message: fmt.Sprintf("The resource '%v' does not exist", collection), } } fmt.Printf("data: %v | %+v\n\n", nil, request.GetData()) model, ok := request.GetData().(kit.Model) if !ok { return nil, apperror.New("invalid_data_no_model", "No model data in request.") } response := res.ApiCreate(model, request) if response.GetError() == nil { response.SetHttpStatus(201) } return response, nil }