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

View File

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