// SourceInfo retrieves the source object including the canonical identifier. func (n *NeoBackend) SourceInfo(mapping *models.Mapping) (string, int, error) { q := fmt.Sprintf(`match (o:%s)-[r:KNOWS {id:'%s'}]->(s:source {name:'%s'}) return o.canonical`, mapping.ObjectType, mapping.SourceID, mapping.Source) vals, err := graphExec([]string{q}) if err != nil { return "", http.StatusInternalServerError, err } if len(vals) == 0 { return "resource not found", http.StatusNotFound, errors.New("resource not found") } v := vals[0]["o.canonical"] mapping.Canonical = v mapping.Object += "/" + v return mapping.Canonical, http.StatusOK, nil }
// MapCreate creates a source object mapping to retrieve a canonical. func (n *NeoBackend) MapCreate(mapping *models.Mapping) (code int, err error) { // Create the source. q := fmt.Sprintf(`merge (o:%s)-[r:KNOWS {id : '%s'}]->(s:source {name:'%s'}) on create set o.canonical='%s' return o.canonical`, mapping.ObjectType, mapping.SourceID, mapping.Source, mapping.Canonical) vals, err := graphExec([]string{q}) if err != nil { return http.StatusInternalServerError, err } v := vals[0]["o.canonical"] if v != mapping.Canonical { log.Println("POST with ", mapping.Canonical, " but resource already exists with canonical:", v) mapping.Canonical = v return http.StatusConflict, nil } return http.StatusCreated, nil }