add tags
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
package services
|
||||
|
||||
import(
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"fmt"
|
||||
|
||||
"example.com/gin/test/models"
|
||||
"example.com/gin/test/repositories"
|
||||
@@ -13,20 +13,24 @@ var(
|
||||
|
||||
type ShopItemService struct {}
|
||||
|
||||
func (u *ShopItemService) Create(name string, email string, password string) (models.User, error) {
|
||||
//hash pw
|
||||
hash, err := bcrypt.GenerateFromPassword([]byte(password), 10)
|
||||
|
||||
if err != nil {
|
||||
return models.User{}, err
|
||||
func (u *ShopItemService) NewShopItem(name string, abstract string, description string, price float64, tagIds []string) (models.ShopItem, error) {
|
||||
shopItem := models.ShopItem{
|
||||
Name: name,
|
||||
Abstract: abstract,
|
||||
Description: description,
|
||||
Price: price,
|
||||
IsPublic: true,
|
||||
}
|
||||
|
||||
user := models.User{Name: name, Email: email, Password: string(hash)}
|
||||
_, err = repositories.Users.Create(user)
|
||||
for _, tagId := range tagIds {
|
||||
tag, err := repositories.Tags.GetById(tagId)
|
||||
|
||||
if err != nil {
|
||||
return models.User{}, err
|
||||
if err != nil {
|
||||
return models.ShopItem{}, fmt.Errorf("Could not get tag by id")
|
||||
}
|
||||
|
||||
shopItem.Tags = append(shopItem.Tags, tag)
|
||||
}
|
||||
|
||||
return user, nil
|
||||
return repositories.ShopItems.Create(shopItem)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user