Пример #1
0
// Todo: validation needed. Idea: validate the input object
func CreateProduct(input *duoerlapi.ProductInput) (originInput *duoerlapi.ProductInput, err error) {
	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
	}

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

	product := &products.Product{
		Id:            oId,
		BrandId:       brandObjectId,
		Name:          input.Name,
		Alias:         input.Alias,
		Intro:         input.Intro,
		Image:         input.Image,
		AuthorId:      authorOId,
		CategoryId:    categoryOId,
		SubCategoryId: subCategoryOId,
		EfficacyIds:   utils.TurnPlainIdsToObjectIds(input.EfficacyIds),
	}

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

	return
}
Пример #2
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
}
Пример #3
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
}