func ShowNews(newsIdHex, userIdHex string) (apiNews *duoerlapi.News, err error) { newsId, err := utils.ToObjectId(newsIdHex) if err != nil { utils.PrintStackAndError(err) return } dbNews, err := news.FindById(newsId) if err != nil { utils.PrintStackAndError(err) return } brand, err := brands.FindById(dbNews.BrandId) if err != nil { utils.PrintStackAndError(err) return } author, err := users.FindById(dbNews.AuthorId) if err != nil { utils.PrintStackAndError(err) return } apiNews = toApiNews(dbNews, brand, author) return }
func AuthenticateUser() Middleware { return func(env Env, app App) (status Status, headers Headers, body Body) { userId := services.FetchUserIdFromSession(env) if userId != "" { if user, _ := users.FindById(bson.ObjectIdHex(userId)); user != nil { services.PutUserToEnv(env, user) } } return app(env) } }
func HardAuthenUser() Middleware { return func(env Env, app App) (status Status, headers Headers, body Body) { userId := services.FetchUserIdFromSession(env) if userId == "" { return Redirect(http.StatusFound, "/logout") } user, _ := users.FindById(bson.ObjectIdHex(userId)) if user == nil { return Redirect(http.StatusFound, "/logout") } services.PutUserToEnv(env, user) return app(env) } }
func ShowProduct(productId, userId string) (apiProduct *duoerlapi.Product, err error) { productOId, err := utils.ToObjectId(productId) if err != nil { utils.PrintStackAndError(err) return } product, err := products.FindById(productOId) if err != nil { utils.PrintStackAndError(err) return } brand, err := brands.FindById(product.BrandId) if err != nil { utils.PrintStackAndError(err) return } author, err := users.FindById(product.AuthorId) if err != nil { utils.PrintStackAndError(err) return } apiProduct = toApiProduct(product, brand, author) // Not login user if userId == "" { return } if wishItem, _ := GetWishItem(userId, productId); wishItem != nil { apiProduct.HasWished = true } if ownItem, _ := GetOwnItem(userId, productId); ownItem != nil { apiProduct.HasOwned = true } return }
func ShowNote(noteIdHex, userIdHex string) (apiNote *duoerlapi.Note, err error) { noteId, err := utils.ToObjectId(noteIdHex) if err != nil { utils.PrintStackAndError(err) return } note, err := notes.FindById(noteId) if err != nil { utils.PrintStackAndError(err) return } author, err := users.FindById(note.AuthorId) if err != nil { utils.PrintStackAndError(err) return } apiNote = toApiNote(note, author) return }