func UpdateNews(input *duoerlapi.NewsInput) (originInput *duoerlapi.NewsInput, err error) { originInput = input newsId, err := utils.ToObjectId(input.Id) if err != nil { utils.PrintStackAndError(err) return } brandId, err := utils.ToObjectId(input.BrandId) if err != nil { utils.PrintStackAndError(err) return } dbNews, err := news.FindById(newsId) if err != nil { utils.PrintStackAndError(err) return } dbNews.BrandId = brandId dbNews.Title = input.Title dbNews.Content = input.Content if err = dbNews.Save(); err != nil { utils.PrintStackAndError(err) return } return }
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 EditNews(user *users.User, newsIdHex string) (newsInput *duoerlapi.NewsInput, 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 } if dbNews.AuthorId != user.Id { err = global.PermissionDeniedError return } newsInput = toNewsInput(dbNews) return }