You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by GitBox <gi...@apache.org> on 2022/09/01 12:52:18 UTC

[GitHub] [beam] vchunikhin commented on a diff in pull request #22967: [Tour Of Beam][backend] get unit content

vchunikhin commented on code in PR #22967:
URL: https://github.com/apache/beam/pull/22967#discussion_r960608751


##########
learning/tour-of-beam/backend/internal/storage/datastore.go:
##########
@@ -227,5 +227,34 @@ func (d *DatastoreDb) SaveContentTrees(ctx context.Context, trees []tob.ContentT
 	return nil
 }
 
+// Get learning unit content by unitId
+func (d *DatastoreDb) GetUnitContent(ctx context.Context, sdk tob.Sdk, unitId string) (unit *tob.Unit, err error) {
+	var tbNodes []TbLearningNode
+	rootKey := pgNameKey(TbLearningPathKind, sdkToKey(sdk), nil)
+
+	query := datastore.NewQuery(TbLearningNodeKind).
+		Namespace(PgNamespace).
+		Ancestor(rootKey).
+		FilterField("id", "=", unitId)
+
+	_, err = d.Client.GetAll(ctx, query, &tbNodes)
+	if err != nil {
+		return nil, fmt.Errorf("query unit failed: %w", err)
+	}
+
+	switch {
+	case len(tbNodes) == 0:
+		return nil, nil

Review Comment:
   From my perspective, it's not good approach to return the double nil to handle this case later. I recommend you return error here.
   
   As for the next step:
   ```
   	if err != nil {
   		log.Println("Get unit content error:", err)
   		finalizeErrResponse(w, http.StatusInternalServerError, INTERNAL_ERROR, "storage error")
   		return
   	}
   	if unit == nil {
   		log.Println("Get unit content error:", err)
   		finalizeErrResponse(w, http.StatusNotFound, NOT_FOUND, "unit not found")
   		return
   	}
   ```
   Maybe the best way to create custom errors and handle them using switch-case
   
   It's  a not major because it's related to your code style. I'm sharing to you my thoughts about that.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org