Example #1
0
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
}
Example #2
0
func UpdateBrand(input *duoerlapi.BrandInput) (originInput *duoerlapi.BrandInput, err error) {
	originInput = input

	brandOId, err := utils.ToObjectId(input.Id)
	if err != nil {
		utils.PrintStackAndError(err)
		return
	}

	brand, err := brands.FindById(brandOId)
	if err != nil {
		utils.PrintStackAndError(err)
		return
	}

	brand.Name = input.Name
	brand.Alias = input.Alias
	brand.Intro = input.Intro
	brand.Country = input.Country
	brand.Logo = input.Logo
	brand.Website = input.Website

	if err = brand.Save(); err != nil {
		utils.PrintStackAndError(err)
		return
	}

	return
}
Example #3
0
func ShowBrand(brandId, userId string) (apiBrand *duoerlapi.Brand, err error) {
	brandOId, err := utils.ToObjectId(brandId)
	if err != nil {
		utils.PrintStackAndError(err)
		return
	}

	brand, err := brands.FindById(brandOId)
	if err != nil {
		utils.PrintStackAndError(err)
		return
	}

	apiBrand = toApiBrand(brand)
	apiBrand.BrandStats = getBrandStats(brandOId)

	// Not login user
	if userId == "" {
		return
	}

	if followBrand := GetFollowBrand(userId, brandId); followBrand != nil {
		apiBrand.HasFollowed = true
	}

	return
}
Example #4
0
func EditBrand(brandId string) (brandInput *duoerlapi.BrandInput, err error) {

	brandOId, err := utils.ToObjectId(brandId)
	if err != nil {
		utils.PrintStackAndError(err)
		return
	}

	brand, err := brands.FindById(brandOId)
	if err != nil {
		utils.PrintStackAndError(err)
		return
	}

	brandInput = toBrandInput(brand)

	return
}
Example #5
0
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
}