Update Delete function to receive ids

pull/2/head
Vinícius Garcia 2020-09-14 15:13:54 -03:00
parent 8e8a9a7309
commit 06a7e37d0e
2 changed files with 24 additions and 17 deletions

View File

@ -70,10 +70,10 @@ func (c Client) Insert(
// Delete deletes one or more instances from the database by id
func (c Client) Delete(
ctx context.Context,
entities ...interface{},
ids ...interface{},
) error {
for _, entity := range entities {
r := c.db.Table(c.tableName).Delete(entity)
for _, id := range ids {
r := c.db.Table(c.tableName).Delete(id)
if r.Error != nil {
return r.Error
}
@ -111,7 +111,7 @@ func (c Client) Update(
var tagNamesCache = map[reflect.Type]map[int]string{}
// structToMap converts any type to a map based on the
// tag named `sql`, i.e. `sql:"map_key_name"`
// tag named `gorm`, i.e. `gorm:"map_key_name"`
//
// This function is efficient in the fact that it caches
// the slower steps of the reflection required to do perform
@ -145,6 +145,7 @@ func structToMap(obj interface{}) (map[string]interface{}, error) {
field = field.Elem()
}
m[names[i]] = field.Interface()
}
@ -159,7 +160,7 @@ func structToMap(obj interface{}) (map[string]interface{}, error) {
func getTagNames(t reflect.Type) map[int]string {
resp := map[int]string{}
for i := 0; i < t.NumField(); i++ {
name := t.Field(i).Tag.Get("sql")
name := t.Field(i).Tag.Get("gorm")
if name == "" {
continue
}

View File

@ -11,9 +11,9 @@ import (
)
type User struct {
ID uint
Name string
CreatedAt time.Time
ID uint `gorm:"id"`
Name string `gorm:"name"`
CreatedAt time.Time `gorm:"created_at"`
}
func TestFind(t *testing.T) {
@ -155,7 +155,7 @@ func TestDelete(t *testing.T) {
t.Fatal("could not create test table!")
}
t.Run("should ignore empty lists of users", func(t *testing.T) {
t.Run("should ignore empty lists of ids", func(t *testing.T) {
db := connectDB(t)
defer db.Close()
@ -169,7 +169,7 @@ func TestDelete(t *testing.T) {
assert.Equal(t, err, nil)
})
t.Run("should delete one user correctly", func(t *testing.T) {
t.Run("should delete one id correctly", func(t *testing.T) {
db := connectDB(t)
defer db.Close()
@ -186,12 +186,18 @@ func TestDelete(t *testing.T) {
err := c.Insert(ctx, &u)
assert.Equal(t, err, nil)
err = c.Delete(ctx, &u)
assert.Equal(t, err, nil)
assert.NotEqual(t, 0, u.ID)
result := User{}
it := c.db.Raw("SELECT * FROM users WHERE id=?", u.ID)
it.Scan(&result)
assert.Equal(t, u.ID, result.ID)
err = c.Delete(ctx, u.ID)
assert.Equal(t, err, nil)
result = User{}
it = c.db.Raw("SELECT * FROM users WHERE id=?", u.ID)
it.Scan(&result)
assert.Equal(t, it.Error, nil)
assert.Equal(t, uint(0), result.ID)
@ -201,8 +207,8 @@ func TestDelete(t *testing.T) {
func TestStructToMap(t *testing.T) {
type S1 struct {
Name string `sql:"name_attr"`
Age int `sql:"age_attr"`
Name string `gorm:"name_attr"`
Age int `gorm:"age_attr"`
}
t.Run("should convert plain structs to maps", func(t *testing.T) {
m, err := structToMap(S1{
@ -231,8 +237,8 @@ func TestStructToMap(t *testing.T) {
})
type S2 struct {
Name *string `sql:"name"`
Age *int `sql:"age"`
Name *string `gorm:"name"`
Age *int `gorm:"age"`
}
t.Run("should not ignore not nil pointers", func(t *testing.T) {