Example #1
0
func CreateFollowBrand(userId, brandId string) (err error) {

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

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

	// Validation, check if the record has been created
	followBrand, err := followbrands.FindByUserAndBrandId(userOId, brandOId)
	if followBrand != nil {
		return
	}

	followBrand = &followbrands.FollowBrand{
		UserId:  userOId,
		BrandId: brandOId,
	}

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

	return
}
Example #2
0
func CreatePost(input *duoerlapi.PostInput) (originInput *duoerlapi.PostInput, err error) {
	originInput = input

	// simple validation
	if input.Content == "" {
		err = global.CanNotBeBlankError
		return
	}

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

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

	post := &posts.Post{
		Id:       postId,
		Content:  input.Content,
		AuthorId: authorId,
	}

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

	return
}
Example #3
0
func AddWishItem(userId, productId string) (err error) {

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

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

	// Validation, check if the record has been created
	wishItem, err := wishitems.FindByUserAndProductId(userOId, productOId)
	if wishItem != nil {
		return
	}

	wishItem = &wishitems.WishItem{
		UserId:    userOId,
		ProductId: productOId,
	}

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

	return
}
Example #4
0
func CreateNote(input *duoerlapi.NoteInput) (originInput *duoerlapi.NoteInput, err error) {
	originInput = input

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

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

	note := &notes.Note{
		Id:      noteId,
		Article: *articles.NewArticle(input.Title, input.Content, authorId),
	}

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

	return
}
Example #5
0
func AddOwnItem(ownItemInput *duoerlapi.OwnItemInput) (err error) {

	userOId, err := utils.ToObjectId(ownItemInput.UserId)
	if err != nil {
		utils.PrintStackAndError(err)
		return
	}

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

	// Validation, check if the record has been created
	ownItem, err := ownitems.FindByUserAndProductId(userOId, productOId)
	if ownItem != nil {
		return
	}

	ownItem = &ownitems.OwnItem{
		UserId:    userOId,
		ProductId: productOId,
		GotFrom:   ownItemInput.GotFrom,
	}

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

	return
}
Example #6
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
}
Example #7
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 #8
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 #9
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 #10
0
func CreateBrand(brandInput *duoerlapi.BrandInput) (input *duoerlapi.BrandInput, err error) {
	input = brandInput

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

	brand := &brands.Brand{
		Id:      oId,
		Name:    brandInput.Name,
		Alias:   brandInput.Alias,
		Intro:   brandInput.Intro,
		Country: brandInput.Country,
		Website: brandInput.Website,
		Logo:    brandInput.Logo,
	}

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

	return
}
Example #11
0
func newAttribute(name, parentIdHex string) *Attribute {
	parentId, _ := utils.ToObjectId(parentIdHex)
	return &Attribute{
		Name:     name,
		AType:    TYPE_CATEGORY,
		ParentId: parentId,
	}
}
Example #12
0
func GetFollowBrand(userId, brandId string) (followBrand *followbrands.FollowBrand) {
	userOId, err := utils.ToObjectId(userId)
	if err != nil {
		utils.PrintStackAndError(err)
		return
	}

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

	followBrand, _ = followbrands.FindByUserAndBrandId(userOId, brandOId)

	return
}
Example #13
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 #14
0
func GetWishItem(userId, productId string) (wishItem *wishitems.WishItem, err error) {

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

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

	wishItem, err = wishitems.FindByUserAndProductId(userOId, productOId)

	return

}
Example #15
0
func GetOwnItem(userId, productId string) (ownItem *ownitems.OwnItem, err error) {

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

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

	ownItem, _ = ownitems.FindByUserAndProductId(userOId, productOId)

	return

}
Example #16
0
func RemoveOwnItem(userId, productId string) (err error) {
	userOId, err := utils.ToObjectId(userId)
	if err != nil {
		utils.PrintStackAndError(err)
		return
	}

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

	err = ownitems.DeleteByUserAndProductId(userOId, productOId)
	if err != nil {
		utils.PrintStackAndError(err)
		return
	}

	return
}
Example #17
0
func DeleteFollowBrand(userId, brandId string) (err error) {

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

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

	err = followbrands.DeleteByUserAndBrandId(userOId, brandOId)
	if err != nil {
		utils.PrintStackAndError(err)
		return
	}

	return
}
Example #18
0
func FetchByIdHex(idHex string) (user *User, err error) {
	userId, err := utils.ToObjectId(idHex)
	if err != nil {
		utils.PrintStackAndError(err)
		return
	}

	user, err = FindById(userId)
	if err != nil {
		utils.PrintStackAndError(err)
		return
	}

	return
}
Example #19
0
func LikeReview(userIdHex, reviewIdHex string) (count int, err error) {
	userId, err := utils.ToObjectId(userIdHex)
	if err != nil {
		utils.PrintStackAndError(err)
		return
	}

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

	review, err := reviews.FindById(reviewId)
	if err != nil {
		utils.PrintStackAndError(err)
		return
	}

	validated := review.ValidateLikeAction(userId)
	if validated.HasError() {
		count = len(review.LikedByIds)
		err = validated.ToError()
		return
	}

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

	count = len(review.LikedByIds)

	return
}
Example #20
0
func GetBrandFollowers(brandIdHex string) (apiUsers []*duoerlapi.User, err error) {

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

	followbrandz, err := followbrands.FindByBrandId(brandId)
	if err != nil {
		utils.PrintStackAndError(err)
		return
	}

	maxNum := len(followbrandz)
	// Get random number users
	if maxNum > configs.BRAND_SHOW_FOLLOWER_NUM {
		randIndex, err := randutil.IntRange(0, maxNum)
		if err != nil {
			utils.PrintStackAndError(err)
			randIndex = 0
		}

		leftIndex := randIndex - configs.BRAND_SHOW_FOLLOWER_NUM
		if leftIndex < 0 {
			followbrandz = followbrandz[0:configs.BRAND_SHOW_FOLLOWER_NUM]
		} else {
			followbrandz = followbrandz[leftIndex:randIndex]
		}
	}

	followerIds := []bson.ObjectId{}
	for _, followBrand := range followbrandz {
		followerIds = append(followerIds, followBrand.UserId)
	}

	followers, err := users.FindByIds(followerIds)
	if err != nil {
		utils.PrintStackAndError(err)
		return
	}

	apiUsers = toApiUsers(followers)

	return
}
Example #21
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 #22
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 #23
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
}
Example #24
0
func GetUserNotes(userIdHex string) (apiNotes []*duoerlapi.Note, err error) {

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

	notez, err := notes.FindSomeByUserId(userId)
	if err != nil {
		utils.PrintStackAndError(err)
		return
	}

	for _, note := range notez {
		apiNotes = append(apiNotes, toApiNote(note, nil))
	}

	return
}
Example #25
0
func GetBrandProducts(brandId string) (apiProducts []*duoerlapi.Product, err error) {

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

	productz, err := products.FindByBrandId(brandOId)
	if err != nil {
		utils.PrintStackAndError(err)
		return
	}

	for _, product := range productz {
		apiProducts = append(apiProducts, toApiProduct(product, nil, nil))
	}

	return
}
Example #26
0
func GetUserPosts(userIdHex string) (apiPosts []*duoerlapi.Post, err error) {

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

	postz, err := posts.FindSomeByAuthorId(userId)
	if err != nil {
		utils.PrintStackAndError(err)
		return
	}

	for _, post := range postz {
		apiPosts = append(apiPosts, toApiPost(post, nil))
	}

	return
}
Example #27
0
func GetReviewsInBrand(brandIdHex string) (apiReviews []*duoerlapi.Review, err error) {

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

	reviewz, err := reviews.FindSomeByBrandId(brandId)
	if err != nil {
		utils.PrintStackAndError(err)
		return
	}

	apiReviews, err = makeApiReviews(reviewz)
	if err != nil {
		utils.PrintStackAndError(err)
		return
	}

	return
}
Example #28
0
func ShowNote(noteIdHex, userIdHex string) (apiNote *duoerlapi.Note, err error) {
	noteId, err := utils.ToObjectId(noteIdHex)
	if err != nil {
		utils.PrintStackAndError(err)
		return
	}

	note, err := notes.FindById(noteId)
	if err != nil {
		utils.PrintStackAndError(err)
		return
	}

	author, err := users.FindById(note.AuthorId)
	if err != nil {
		utils.PrintStackAndError(err)
		return
	}

	apiNote = toApiNote(note, author)

	return
}