Example #1
0
// Todo: validation needed. Idea: validate the input object
func UpdateProduct(input *duoerlapi.ProductInput) (originInput *duoerlapi.ProductInput, err error) {
	// When the validation fails, should giving back the originInput for front-end renderring
	originInput = input

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

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

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

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

	product, err := products.FindById(oId)
	if err != nil {
		utils.PrintStackAndError(err)
		return
	}

	product.BrandId = brandObjectId
	product.Name = input.Name
	product.Alias = input.Alias
	product.Image = input.Image
	product.Intro = input.Intro
	product.CategoryId = categoryOId
	product.SubCategoryId = subCategoryOId
	product.EfficacyIds = utils.TurnPlainIdsToObjectIds(input.EfficacyIds)

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

	return
}
Example #2
0
// Todo: Validation Needed
func CreateReview(input *duoerlapi.ReviewInput) (originInput *duoerlapi.ReviewInput, err error) {
	originInput = input

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

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

	// Check if the product exists
	product, err := products.FindById(productOId)
	if err != nil {
		utils.PrintStackAndError(err)
		return
	}

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

	review := &reviews.Review{
		Id:          oId,
		AuthorId:    authorOId,
		ProductId:   productOId,
		BrandId:     product.BrandId,
		Content:     input.Content,
		Rating:      input.Rating,
		EfficacyIds: utils.TurnPlainIdsToObjectIds(input.EfficacyIds),
	}

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

	return
}
Example #3
0
// For edit product form
func EditProduct(productId string) (productInput *duoerlapi.ProductInput, 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
	}

	productInput = toProductInput(product)

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