Proper context.Context usage
This commit is contained in:
@@ -20,8 +20,8 @@ type Author struct {
|
||||
PicURL string `json:"picurl" bson:"picurl"`
|
||||
}
|
||||
|
||||
func (b *BlogApp) AboutMe() (*Author, error) {
|
||||
res := b.authCol.FindOne(context.Background(), bson.M{"_id": "caleb_gardner"})
|
||||
func (b *BlogApp) AboutMe(ctx context.Context) (*Author, error) {
|
||||
res := b.authCol.FindOne(ctx, bson.M{"_id": "caleb_gardner"})
|
||||
if res.Err() != nil {
|
||||
log.Println("error getting about me:", res.Err())
|
||||
if res.Err() == mongo.ErrNoDocuments {
|
||||
@@ -39,7 +39,7 @@ func (b *BlogApp) AboutMe() (*Author, error) {
|
||||
}
|
||||
|
||||
func (b *BlogApp) reqAuthorInfo(w http.ResponseWriter, r *http.Request) {
|
||||
res := b.authCol.FindOne(context.Background(), r.PathValue("authorID"))
|
||||
res := b.authCol.FindOne(r.Context(), r.PathValue("authorID"))
|
||||
if res.Err() == mongo.ErrNoDocuments {
|
||||
backend.ReturnError(w, http.StatusNotFound, "notFound", "Author with ID "+r.PathValue("authorID")+" not found")
|
||||
return
|
||||
@@ -85,7 +85,7 @@ func (b *BlogApp) addAuthorInfo(w http.ResponseWriter, r *http.Request) {
|
||||
if i != 1 {
|
||||
newID += strconv.Itoa(i)
|
||||
}
|
||||
collisionCheck := b.authCol.FindOne(context.Background(), bson.M{"name": newAuth.Name})
|
||||
collisionCheck := b.authCol.FindOne(r.Context(), bson.M{"name": newAuth.Name})
|
||||
if collisionCheck.Err() == mongo.ErrNoDocuments {
|
||||
newAuth.ID = newID
|
||||
break
|
||||
@@ -95,7 +95,7 @@ func (b *BlogApp) addAuthorInfo(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
}
|
||||
_, err = b.authCol.InsertOne(context.Background(), newAuth)
|
||||
_, err = b.authCol.InsertOne(r.Context(), newAuth)
|
||||
if err != nil {
|
||||
log.Println("error inserting new author:", err)
|
||||
backend.ReturnError(w, http.StatusInternalServerError, "internal", "Server Error")
|
||||
@@ -136,7 +136,7 @@ func (b *BlogApp) updateAuthorInfo(w http.ResponseWriter, r *http.Request) {
|
||||
if rawUpd["picurl"] != "" {
|
||||
actlUpd["picurl"] = rawUpd["picurl"]
|
||||
}
|
||||
res, err := b.authCol.UpdateByID(context.Background(), r.PathValue("authorID"), actlUpd)
|
||||
res, err := b.authCol.UpdateByID(r.Context(), r.PathValue("authorID"), actlUpd)
|
||||
if err != nil {
|
||||
if err == mongo.ErrNoDocuments {
|
||||
backend.ReturnError(w, http.StatusNotFound, "notFound", "Blog with ID "+r.PathValue("blogID")+" not found")
|
||||
|
||||
+15
-15
@@ -33,8 +33,8 @@ func (b *BlogApp) ConvertBlog(blog *Blog) {
|
||||
}
|
||||
}
|
||||
|
||||
func (b *BlogApp) GetAuthor(blog *Blog) (*Author, error) {
|
||||
res := b.authCol.FindOne(context.Background(), bson.M{"_id": blog.Author})
|
||||
func (b *BlogApp) GetAuthor(ctx context.Context, blog *Blog) (*Author, error) {
|
||||
res := b.authCol.FindOne(ctx, bson.M{"_id": blog.Author})
|
||||
if res.Err() != nil {
|
||||
if res.Err() == mongo.ErrNoDocuments {
|
||||
return nil, backend.ErrNotFound
|
||||
@@ -46,14 +46,14 @@ func (b *BlogApp) GetAuthor(blog *Blog) (*Author, error) {
|
||||
return &author, err
|
||||
}
|
||||
|
||||
func (b *BlogApp) Blog(ID string) (*Blog, error) {
|
||||
func (b *BlogApp) Blog(ctx context.Context, ID string) (*Blog, error) {
|
||||
b.cacheMutex.RLock()
|
||||
blog, has := b.blogCache[ID]
|
||||
b.cacheMutex.RUnlock()
|
||||
if has {
|
||||
return &blog, nil
|
||||
}
|
||||
res := b.blogCol.FindOne(context.Background(), bson.M{"_id": ID, "draft": false})
|
||||
res := b.blogCol.FindOne(ctx, bson.M{"_id": ID, "draft": false})
|
||||
if res.Err() != nil {
|
||||
if res.Err() == mongo.ErrNoDocuments {
|
||||
return nil, backend.ErrNotFound
|
||||
@@ -85,7 +85,7 @@ func (b *BlogApp) reqBlog(w http.ResponseWriter, r *http.Request) {
|
||||
backend.ReturnError(w, http.StatusBadRequest, "badRequest", "Must provide a blogID")
|
||||
return
|
||||
}
|
||||
blog, err := b.Blog(blogID)
|
||||
blog, err := b.Blog(r.Context(), blogID)
|
||||
if err != nil {
|
||||
if err == backend.ErrNotFound {
|
||||
backend.ReturnError(w, http.StatusNotFound, "notFound", "Not blog found with the given ID")
|
||||
@@ -130,7 +130,7 @@ func (b *BlogApp) createBlog(w http.ResponseWriter, r *http.Request) {
|
||||
newBlog.CreateTime = tim
|
||||
newBlog.UpdateTime = tim
|
||||
newBlog.Author = hdr.User.Username
|
||||
_, err = b.blogCol.InsertOne(context.Background(), newBlog)
|
||||
_, err = b.blogCol.InsertOne(r.Context(), newBlog)
|
||||
if err != nil {
|
||||
log.Println("error when inserting new blog:", err)
|
||||
backend.ReturnError(w, http.StatusInternalServerError, "internal", "Server Error")
|
||||
@@ -177,7 +177,7 @@ func (b *BlogApp) updateBlog(w http.ResponseWriter, r *http.Request) {
|
||||
reqUpd["blog"] = blog
|
||||
}
|
||||
reqUpd["updateTime"] = time.Now().Unix()
|
||||
res, err := b.blogCol.UpdateByID(context.Background(), r.PathValue("blogID"), reqUpd)
|
||||
res, err := b.blogCol.UpdateByID(r.Context(), r.PathValue("blogID"), reqUpd)
|
||||
if err != nil {
|
||||
if err == mongo.ErrNoDocuments {
|
||||
backend.ReturnError(w, http.StatusNotFound, "notFound", "Blog with ID "+r.PathValue("blogID")+" not found")
|
||||
@@ -193,8 +193,8 @@ func (b *BlogApp) updateBlog(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
}
|
||||
|
||||
func (b *BlogApp) LatestBlogs(page int64) ([]*Blog, error) {
|
||||
res, err := b.blogCol.Find(context.Background(), bson.M{"staticPage": false, "draft": false}, options.Find().
|
||||
func (b *BlogApp) LatestBlogs(ctx context.Context, page int64) ([]*Blog, error) {
|
||||
res, err := b.blogCol.Find(ctx, bson.M{"staticPage": false, "draft": false}, options.Find().
|
||||
SetSort(bson.M{"createTime": -1}).
|
||||
SetLimit(5).
|
||||
SetSkip(page*5))
|
||||
@@ -205,7 +205,7 @@ func (b *BlogApp) LatestBlogs(page int64) ([]*Blog, error) {
|
||||
return nil, err
|
||||
}
|
||||
var out []*Blog
|
||||
err = res.All(context.Background(), &out)
|
||||
err = res.All(ctx, &out)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -225,7 +225,7 @@ func (b *BlogApp) reqLatestBlogs(w http.ResponseWriter, r *http.Request) {
|
||||
page = 0
|
||||
}
|
||||
}
|
||||
blogs, err := b.LatestBlogs(int64(page))
|
||||
blogs, err := b.LatestBlogs(r.Context(), int64(page))
|
||||
if err != nil && err != backend.ErrNotFound {
|
||||
log.Println("error getting latest blogs:", err)
|
||||
backend.ReturnError(w, http.StatusInternalServerError, "internal", "internal error")
|
||||
@@ -245,8 +245,8 @@ type BlogListResult struct {
|
||||
CreateTime int `json:"createTime" bson:"createTime"`
|
||||
}
|
||||
|
||||
func (b *BlogApp) BlogList(page int64) ([]BlogListResult, error) {
|
||||
res, err := b.blogCol.Find(context.Background(), bson.M{}, options.Find().
|
||||
func (b *BlogApp) BlogList(ctx context.Context, page int64) ([]BlogListResult, error) {
|
||||
res, err := b.blogCol.Find(ctx, bson.M{}, options.Find().
|
||||
SetProjection(bson.M{"_id": 1, "createTime": 1}).
|
||||
SetSort(bson.M{"createTime": 1}).
|
||||
SetLimit(50).
|
||||
@@ -258,7 +258,7 @@ func (b *BlogApp) BlogList(page int64) ([]BlogListResult, error) {
|
||||
return nil, err
|
||||
}
|
||||
var out []BlogListResult
|
||||
err = res.All(context.Background(), &out)
|
||||
err = res.All(ctx, &out)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -275,7 +275,7 @@ func (b *BlogApp) reqBlogList(w http.ResponseWriter, r *http.Request) {
|
||||
page = 0
|
||||
}
|
||||
}
|
||||
blogList, err := b.BlogList(int64(page))
|
||||
blogList, err := b.BlogList(r.Context(), int64(page))
|
||||
if err != nil && err != backend.ErrNotFound {
|
||||
log.Println("error getting blog list:", err)
|
||||
backend.ReturnError(w, http.StatusInternalServerError, "internal", "internal error")
|
||||
|
||||
@@ -22,22 +22,22 @@ type PortfolioProject struct {
|
||||
} `json:"language" bson:"language"`
|
||||
}
|
||||
|
||||
func (b *BlogApp) Projects(languageFilter string) ([]PortfolioProject, error) {
|
||||
func (b *BlogApp) Projects(ctx context.Context, languageFilter string) ([]PortfolioProject, error) {
|
||||
filter := bson.M{}
|
||||
if languageFilter != "" {
|
||||
filter["language.language"] = languageFilter
|
||||
}
|
||||
res, err := b.portfolioCol.Find(context.Background(), filter, options.Find().SetSort(bson.M{"order": 1}))
|
||||
res, err := b.portfolioCol.Find(ctx, filter, options.Find().SetSort(bson.M{"order": 1}))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var out []PortfolioProject
|
||||
err = res.All(context.Background(), &out)
|
||||
err = res.All(ctx, &out)
|
||||
return out, err
|
||||
}
|
||||
|
||||
func (b *BlogApp) reqPortfolio(w http.ResponseWriter, r *http.Request) {
|
||||
folio, err := b.Projects(r.URL.Query().Get("lang"))
|
||||
folio, err := b.Projects(r.Context(), r.URL.Query().Get("lang"))
|
||||
if err != nil {
|
||||
backend.ReturnError(w, http.StatusInternalServerError, "internal", "Server Error")
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user