Beispiel #1
0
// Create method to satisfy `api2go.DataSource` interface
func (s *userResource) Create(obj interface{}, r api2go.Request) (string, error) {
	user, ok := obj.(User)
	if !ok {
		return "", api2go.NewHTTPError(errors.New("Invalid instance given"), "Invalid instance given", http.StatusBadRequest)
	}

	if _, ok := s.users[user.GetID()]; ok {
		return "", api2go.NewHTTPError(errors.New("User exists"), "User exists", http.StatusConflict)
	}

	s.idCount++
	id := fmt.Sprintf("%d", s.idCount)
	user.SetID(id)

	// check references and get embedded objects
	for _, chocID := range user.ChocolatesIDs {
		choc, err := s.chocStorage.GetOne(chocID)
		if err != nil {
			return "", err
		}

		user.Chocolates = append(user.Chocolates, choc)
	}

	s.users[id] = user

	return id, nil
}
// Create a new choc
func (c ChocolateResource) Create(obj interface{}, r api2go.Request) (api2go.Responder, error) {
	choc, ok := obj.(model.Chocolate)
	if !ok {
		return &Response{}, api2go.NewHTTPError(errors.New("Invalid instance given"), "Invalid instance given", http.StatusBadRequest)
	}

	id := c.ChocStorage.Insert(choc)
	err := choc.SetID(id)
	if err != nil {
		return &Response{}, api2go.NewHTTPError(errors.New("Non-integer ID given"), "Non-integer ID given", http.StatusInternalServerError)
	}
	return &Response{Res: choc, Code: http.StatusCreated}, nil
}
Beispiel #3
0
//Create satisfies api2go create interface
func (s UserSource) Create(obj interface{}, r api2go.Request) (api2go.Responder, error) {
	user, ok := obj.(User)
	if !ok {
		return &common.Response{}, api2go.NewHTTPError(errors.New("Invalid instance given"), "Invalid instance given", http.StatusBadRequest)
	}

	err := s.connection.Collection("user").Save(&user)

	if err != nil {
		return &common.Response{}, api2go.NewHTTPError(err, err.Error(), http.StatusBadRequest)
	}

	return &common.Response{Res: user, Code: http.StatusCreated}, nil
}
Beispiel #4
0
// FindOne to satisfy `api2go.DataSource` interface
// this method should return the user with the given ID, otherwise an error
func (s *userResource) FindOne(ID string, r api2go.Request) (interface{}, error) {
	if user, ok := s.users[ID]; ok {
		return user, nil
	}

	return nil, api2go.NewHTTPError(errors.New("Not Found"), "Not Found", http.StatusNotFound)
}
Beispiel #5
0
//Update stores all changes on the user
func (s UserResource) Update(obj interface{}, r api2go.Request) error {
	user, ok := obj.(model.User)
	if !ok {
		return api2go.NewHTTPError(errors.New("Invalid instance given"), "Invalid instance given", http.StatusBadRequest)
	}

	return s.UserStorage.Update(user)
}
Beispiel #6
0
// Update a choc
func (c ChocolateResource) Update(obj interface{}, r api2go.Request) error {
	choc, ok := obj.(model.Chocolate)
	if !ok {
		return api2go.NewHTTPError(errors.New("Invalid instance given"), "Invalid instance given", http.StatusBadRequest)
	}

	return c.ChocStorage.Update(choc)
}
Beispiel #7
0
// Create a new choc
func (c ChocolateResource) Create(obj interface{}, r api2go.Request) (string, error) {
	choc, ok := obj.(model.Chocolate)
	if !ok {
		return "", api2go.NewHTTPError(errors.New("Invalid instance given"), "Invalid instance given", http.StatusBadRequest)
	}

	return c.ChocStorage.Insert(choc), nil
}
// GetOne user
func (s UserStorage) GetOne(id string) (model.User, error) {
	intID, err := strconv.ParseInt(id, 10, 64)
	if err != nil {
		errMessage := fmt.Sprintf("User id must be integer: %s", id)
		return model.User{}, api2go.NewHTTPError(errors.New(errMessage), errMessage, http.StatusBadRequest)
	}
	return s.getOneWithAssociations(intID)
}
// Create method to satisfy `api2go.DataSource` interface
func (s UserResource) Create(obj interface{}, r api2go.Request) (api2go.Responder, error) {
	user, ok := obj.(model.User)
	if !ok {
		return &Response{}, api2go.NewHTTPError(errors.New("Invalid instance given"), "Invalid instance given", http.StatusBadRequest)
	}

	id, err := s.UserStorage.Insert(user)
	if err != nil {
		return &Response{}, api2go.NewHTTPError(errors.New("Faild to create a user"), "Faild to create a user", http.StatusInternalServerError)
	}
	err = user.SetID(id)
	if err != nil {
		return &Response{}, api2go.NewHTTPError(errors.New("Non-integer ID given"), "Non-integer ID given", http.StatusInternalServerError)
	}

	return &Response{Res: user, Code: http.StatusCreated}, nil
}
Beispiel #10
0
// GetOne user
func (s UserStorage) GetOne(id string) (model.User, error) {
	user, ok := s.users[id]
	if ok {
		return *user, nil
	}
	errMessage := fmt.Sprintf("User for id %s not found", id)
	return model.User{}, api2go.NewHTTPError(errors.New(errMessage), errMessage, http.StatusNotFound)
}
//Update stores all changes on the user
func (s UserResource) Update(obj interface{}, r api2go.Request) (api2go.Responder, error) {
	user, ok := obj.(model.User)
	if !ok {
		return &Response{}, api2go.NewHTTPError(errors.New("Invalid instance given"), "Invalid instance given", http.StatusBadRequest)
	}

	err := s.UserStorage.Update(user)
	return &Response{Res: user, Code: http.StatusNoContent}, err
}
Beispiel #12
0
// Update a choc
func (c ChocolateResource) Update(obj interface{}, r api2go.Request) (api2go.Responder, error) {
	choc, ok := obj.(model.Chocolate)
	if !ok {
		return &Response{}, api2go.NewHTTPError(errors.New("Invalid instance given"), "Invalid instance given", http.StatusBadRequest)
	}

	err := c.ChocStorage.Update(choc)
	return &Response{Res: choc, Code: http.StatusNoContent}, err
}
Beispiel #13
0
// Create method to satisfy `api2go.DataSource` interface
func (s UserResource) Create(obj interface{}, r api2go.Request) (string, error) {
	user, ok := obj.(model.User)
	if !ok {
		return "", api2go.NewHTTPError(errors.New("Invalid instance given"), "Invalid instance given", http.StatusBadRequest)
	}

	id := s.UserStorage.Insert(user)

	return id, nil
}
Beispiel #14
0
// Create a new choc
func (c ChocolateResource) Create(obj interface{}, r api2go.Request) (api2go.Responder, error) {
	choc, ok := obj.(model.Chocolate)
	if !ok {
		return &Response{}, api2go.NewHTTPError(errors.New("Invalid instance given"), "Invalid instance given", http.StatusBadRequest)
	}

	id := c.ChocStorage.Insert(choc)
	choc.ID = id
	return &Response{Res: choc, Code: http.StatusCreated}, nil
}
Beispiel #15
0
// Create method to satisfy `api2go.DataSource` interface
func (s UserResource) Create(obj interface{}, r api2go.Request) (api2go.Responder, error) {
	user, ok := obj.(model.User)
	if !ok {
		return &Response{}, api2go.NewHTTPError(errors.New("Invalid instance given"), "Invalid instance given", http.StatusBadRequest)
	}

	id := s.UserStorage.Insert(user)
	user.ID = id

	return &Response{Res: user, Code: http.StatusCreated}, nil
}
func (s UserStorage) getOneWithAssociations(id int64) (model.User, error) {
	var user model.User
	s.db.First(&user, id)
	s.db.Model(&user).Related(&user.Chocolates, "Chocolates")
	if err := s.db.Error; err == gorm.ErrRecordNotFound {
		errMessage := fmt.Sprintf("User for id %s not found", id)
		return model.User{}, api2go.NewHTTPError(errors.New(errMessage), errMessage, http.StatusNotFound)
	} else if err != nil {
		return model.User{}, err
	}
	user.ChocolatesIDs = make([]string, len(user.Chocolates))
	for i, choc := range user.Chocolates {
		user.ChocolatesIDs[i] = choc.GetID()
	}
	return user, nil
}
// FindOne to satisfy `api2go.DataSource` interface
// this method should return the user with the given ID, otherwise an error
func (s UserResource) FindOne(ID string, r api2go.Request) (api2go.Responder, error) {
	user, err := s.UserStorage.GetOne(ID)
	if err != nil {
		return &Response{}, api2go.NewHTTPError(err, err.Error(), http.StatusNotFound)
	}

	user.Chocolates = []model.Chocolate{}
	for _, chocolateID := range user.ChocolatesIDs {
		choc, err := s.ChocStorage.GetOne(chocolateID)
		if err != nil {
			return &Response{}, err
		}
		user.Chocolates = append(user.Chocolates, choc)
	}
	return &Response{Res: user}, nil
}
Beispiel #18
0
//Update stores all changes on the user
func (s *userResource) Update(obj interface{}, r api2go.Request) error {
	user, ok := obj.(User)
	if !ok {
		return api2go.NewHTTPError(errors.New("Invalid instance given"), "Invalid instance given", http.StatusBadRequest)
	}

	// check references and get embedded objects, in real world, you would make database queries and check all your references
	user.Chocolates = []*Chocolate{}
	for _, chocID := range user.ChocolatesIDs {
		choc, err := s.chocStorage.GetOne(chocID)
		if err != nil {
			return err
		}

		user.Chocolates = append(user.Chocolates, choc)
	}

	s.users[user.GetID()] = user

	return nil
}
Beispiel #19
0
func (controller *Book) FindAll(request api2go.Request) (interface{}, error) {
	return nil, api2go.NewHTTPError(nil, "This functionality has not been implemented", 400)
}
Beispiel #20
0
func (controller *Author) Update(object interface{}, request api2go.Request) error {
	return api2go.NewHTTPError(nil, "This functionality has not been implemented", 400)
}
Beispiel #21
0
func (controller *Author) Delete(id string, request api2go.Request) error {
	return api2go.NewHTTPError(nil, "This functionality has not been implemented", 400)

}
Beispiel #22
0
func (controller *Author) Create(object interface{}, request api2go.Request) (string, error) {
	return "", api2go.NewHTTPError(nil, "This functionality has not been implemented", 400)

}
Beispiel #23
0
func (controller *Author) FindMultiple([]string, api2go.Request) (interface{}, error) {
	return nil, api2go.NewHTTPError(nil, "This functionality has not been implemented", 400)
}
Beispiel #24
0
func (controller *Blog) FindOne(id string, request api2go.Request) (interface{}, error) {
	return nil, api2go.NewHTTPError(nil, "This functionality has not been implemented", 400)
}