Skip to content

Commit

Permalink
Added search by layer hash
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewAR2 committed Dec 28, 2020
1 parent d2fdc3e commit be63936
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
7 changes: 7 additions & 0 deletions api/httpserver/rest/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ func (s *Service) SearchHandler(w http.ResponseWriter, r *http.Request) {
buf.WriteString(fmt.Sprintf("\"redirect\":\"/smeshers/%v\"", idStr))
break
}
if s.storage.GetLayersCount(s.ctx, &bson.D{{"hash", idStr}}) > 0 {
layer, err := s.storage.GetLayer(s.ctx, &bson.D{{"hash", idStr}})
if (err == nil) {
buf.WriteString(fmt.Sprintf("\"redirect\":\"/layers/%v\"", layer.Number))
break
}
}
return nil, http.StatusNotFound, errors.New("Not found")
default:
objId, err := primitive.ObjectIDFromHex(idStr);
Expand Down
1 change: 1 addition & 0 deletions storage/layer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

func (s *Storage) InitLayersStorage(ctx context.Context) error {
_, err := s.db.Collection("layers").Indexes().CreateOne(ctx, mongo.IndexModel{Keys: bson.D{{"number", 1}}, Options: options.Index().SetName("numberIndex").SetUnique(true)});
_, err = s.db.Collection("layers").Indexes().CreateOne(ctx, mongo.IndexModel{Keys: bson.D{{"hash", 1}}, Options: options.Index().SetName("hashIndex").SetUnique(true)});
return err
}

Expand Down

0 comments on commit be63936

Please sign in to comment.