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 CreateNews(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 } authorId, err := utils.ToObjectId(input.AuthorId) if err != nil { utils.PrintStackAndError(err) return } dbNews := &news.News{ Id: newsId, BrandId: brandId, Article: *articles.NewArticle(input.Title, input.Content, authorId), } 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 }
// Make the service object in web socket connection func MakeWsService(orgIdHex, userIdHex string) (wsService *WsService, err error) { userId, err := utils.ToObjectId(userIdHex) if err != nil { utils.PrintStackAndError(err) return } activeOrg, err := MyActiveOrg(orgIdHex) if err != nil { utils.PrintStackAndError(err) return } wsService = new(WsService) onlineUser, err := activeOrg.GetOnlineUserById(userId) if err != nil { utils.PrintStackAndError(err) return } wsService.OnlineUser = onlineUser wsService.LoggedInUser = onlineUser.User wsService.CurrentOrg = activeOrg.Organization wsService.AllDBs = activeOrg.AllDBs return }
func MyActiveOrg(orgIdHex string) (activeOrg *ws.ActiveOrg, err error) { mu.Lock() defer mu.Unlock() // Validation: The org id should be valid orgId, err := utils.ToObjectId(orgIdHex) if err != nil { utils.PrintStackAndError(err) return } // Already running in the map activeOrg, exist := activeOrgMap[orgIdHex] if exist { return } // Should init the org and put into map for further use org, err := organizations.FindById(orgId) if err != nil { utils.PrintStackAndError(err) return } // Find and maintain all dbs for handling shared groups allDBs := []*mgodb.Database{org.Database} embedOrgs, err := organizations.FindByIds(org.EmbededOrgIds) if err != nil { utils.PrintStackAndError(err) return } for _, embedOrg := range embedOrgs { allDBs = append(allDBs, embedOrg.Database) } // Init the activeOrg and put it into the map activeOrg = &ws.ActiveOrg{ OrgId: orgIdHex, Organization: org, OnlineUsers: make(map[bson.ObjectId]*ws.OnlineUser), Broadcast: make(chan ws.GenericPushingMessage), CloseSign: make(chan bool), AllDBs: allDBs, } go runActiveOrg(activeOrg) activeOrgMap[orgIdHex] = activeOrg return }
func GetNewsInBrand(brandIdHex string) (apiNews []*duoerlapi.News, err error) { brandId, err := utils.ToObjectId(brandIdHex) if err != nil { utils.PrintStackAndError(err) return } newz, err := news.FindSomeByBrandId(brandId) if err != nil { utils.PrintStackAndError(err) return } for _, dbNews := range newz { apiNews = append(apiNews, toApiNews(dbNews, nil, nil)) } 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 }