w完成删除 shorturl 的逻辑过程
This commit is contained in:
parent
69cec74605
commit
58670c2cee
|
@ -14,6 +14,11 @@ import (
|
|||
"ohurlshortener/utils"
|
||||
)
|
||||
|
||||
func DeleteAccessLogs(shortUrl string) error {
|
||||
query := `DELETE from public.access_logs WHERE short_url = :short_url`
|
||||
return DbNamedExec(query, shortUrl)
|
||||
}
|
||||
|
||||
func FindAccessLogs(shortUrl string) ([]core.AccessLog, error) {
|
||||
found := []core.AccessLog{}
|
||||
query := "SELECT * FROM public.access_logs l WHERE l.short_url = $1 ORDER BY l.id DESC"
|
||||
|
|
|
@ -43,26 +43,39 @@ func DbNamedExec(query string, args interface{}) error {
|
|||
return err
|
||||
}
|
||||
|
||||
func DbExecTx(query string, args ...interface{}) error {
|
||||
tx, err := dbService.Connection.Begin()
|
||||
func DbExecTx(query ...string) error {
|
||||
tx := dbService.Connection.MustBegin()
|
||||
for _, s := range query {
|
||||
tx.MustExec(s)
|
||||
} //end of for
|
||||
err := tx.Commit()
|
||||
if err != nil {
|
||||
return err
|
||||
return tx.Rollback()
|
||||
}
|
||||
defer tx.Commit()
|
||||
|
||||
stmt, err := tx.Prepare(dbService.Connection.Rebind(query))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
||||
_, err = stmt.Exec(args...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
} //end of func
|
||||
|
||||
//
|
||||
//func DbExecTx(query string, args ...interface{}) error {
|
||||
// tx, err := dbService.Connection.Begin()
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// defer tx.Commit()
|
||||
//
|
||||
// stmt, err := tx.Prepare(dbService.Connection.Rebind(query))
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// defer stmt.Close()
|
||||
//
|
||||
// _, err = stmt.Exec(args...)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
//
|
||||
// return nil
|
||||
//}
|
||||
|
||||
func DbGet(query string, dest interface{}, args ...interface{}) error {
|
||||
err := dbService.Connection.Get(dest, query, args...)
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
package storage
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"ohurlshortener/core"
|
||||
"ohurlshortener/utils"
|
||||
)
|
||||
|
@ -20,6 +21,17 @@ func UpdateShortUrl(shortUrl core.ShortUrl) error {
|
|||
return DbNamedExec(query, shortUrl)
|
||||
}
|
||||
|
||||
func DeleteShortUrl(shortUrl core.ShortUrl) error {
|
||||
query := `DELETE from public.short_urls WHERE short_url = :short_url`
|
||||
return DbNamedExec(query, shortUrl)
|
||||
}
|
||||
|
||||
func DeleteShortUrlWithAccessLogs(shortUrl core.ShortUrl) error {
|
||||
query1 := fmt.Sprintf(`DELETE from public.short_urls WHERE short_url = '%s'`, shortUrl.ShortUrl)
|
||||
query2 := fmt.Sprintf(`DELETE from public.access_logs WHERE short_url = '%s'`, shortUrl.ShortUrl)
|
||||
return DbExecTx(query1, query2)
|
||||
} //end of Transaction Action
|
||||
|
||||
func FindShortUrl(url string) (core.ShortUrl, error) {
|
||||
found := core.ShortUrl{}
|
||||
query := `SELECT * FROM public.short_urls WHERE short_url = $1`
|
||||
|
|
|
@ -0,0 +1,35 @@
|
|||
package storage
|
||||
|
||||
import (
|
||||
"ohurlshortener/core"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDeleteShortUrlWithAccessLogs(t *testing.T) {
|
||||
|
||||
init4Test(t)
|
||||
|
||||
url1 := core.ShortUrl{ShortUrl: "hello"}
|
||||
url2 := core.ShortUrl{ShortUrl: "hello"}
|
||||
url3 := core.ShortUrl{ShortUrl: "hello"}
|
||||
|
||||
type args struct {
|
||||
shortUrl core.ShortUrl
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
wantErr bool
|
||||
}{
|
||||
{name: "TestCase1", args: args{url1}, wantErr: false},
|
||||
{name: "TestCase1", args: args{url2}, wantErr: false},
|
||||
{name: "TestCase1", args: args{url3}, wantErr: false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if err := DeleteShortUrlWithAccessLogs(tt.args.shortUrl); (err != nil) != tt.wantErr {
|
||||
t.Errorf("DeleteShortUrlWithAccessLogs() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue