gitee.com/woood2/luca@v1.0.4/internal/repository/app.go (about) 1 package repository 2 3 import ( 4 "context" 5 "gitee.com/woood2/luca/internal/errcode" 6 "gitee.com/woood2/luca/internal/layer" 7 "github.com/pkg/errors" 8 "go.uber.org/zap" 9 "gorm.io/gorm" 10 "time" 11 ) 12 13 //entity 14 type App struct { 15 ID int 16 Appkey string 17 Secret string 18 Name string 19 Url string 20 Logo string 21 Contacts string 22 Disabled int 23 Createtime time.Time `gorm:"column:createtime;default:null"` 24 Updatetime time.Time `gorm:"column:updatetime;default:null"` 25 Remark string 26 } 27 28 func (App) TableName() string { 29 return "app" 30 } 31 32 //interface 33 type AppRepo interface { 34 //errcode.AppNotExists 35 GetByKey(ctx context.Context, appkey string) (*App, error) 36 37 List(ctx context.Context, appkey string, name string) ([]*App, error) 38 39 ExistByKey(ctx context.Context, appkey string) (bool, error) 40 41 ExistByName(ctx context.Context, name string) (bool, error) 42 43 Create(ctx context.Context, app *App) (int, error) 44 45 Edit(ctx context.Context, app *App) error 46 47 Delete(ctx context.Context, app *App) error 48 49 SetDisabled(ctx context.Context, appkey string, disabled int) error 50 } 51 52 type AppRepoFactory interface { 53 Produce(logger *zap.Logger, db *gorm.DB) AppRepo 54 } 55 56 //factory impl 57 func NewAppRepoImplFactory() *AppRepoImplFactory { 58 return &AppRepoImplFactory{} 59 } 60 61 type AppRepoImplFactory struct { 62 } 63 64 func (f AppRepoImplFactory) Produce(logger *zap.Logger, db *gorm.DB) AppRepo { 65 return NewAppRepoImpl(logger, db) 66 } 67 68 //repo impl 69 func NewAppRepoImpl(logger *zap.Logger, db *gorm.DB) *AppRepoImpl { 70 return &AppRepoImpl{ 71 Repository: layer.Repository{Logger: logger, DB: db}, 72 } 73 } 74 75 type AppRepoImpl struct { 76 layer.Repository 77 } 78 79 func (repo *AppRepoImpl) GetByKey(ctx context.Context, appkey string) (*App, error) { 80 app := new(App) 81 result := repo.DB.WithContext(ctx).Where("appkey = ?", appkey).First(app) 82 83 if result.Error == nil { 84 return app, nil 85 } 86 if errors.Is(result.Error, gorm.ErrRecordNotFound) { 87 return nil, errcode.AppNotExists 88 } else { 89 return nil, errors.Wrapf(result.Error, "AppRepoImpl:GetByKey(%s) failed", appkey) 90 } 91 } 92 93 func (repo *AppRepoImpl) List(ctx context.Context, appkey string, name string) ([]*App, error) { 94 cond := "" 95 var args []interface{} 96 if appkey != "" { 97 cond = "appkey LIKE ?" 98 args = append(args, "%"+appkey+"%") 99 } 100 if name != "" { 101 if len(args) > 0 { 102 cond += " AND " 103 } 104 cond += "name LIKE ?" 105 args = append(args, "%"+name+"%") 106 } 107 108 var apps []*App 109 var err error 110 if len(args) > 0 { 111 result := repo.DB.WithContext(ctx).Where(cond, args...).Order("id asc").Find(&apps) 112 err = result.Error 113 } else { 114 result := repo.DB.WithContext(ctx).Order("id asc").Find(&apps) 115 err = result.Error 116 } 117 if err == nil { 118 return apps, nil 119 } else { 120 return apps, errors.Wrapf(err, "AppRepoImpl:List(%s, %s) failed", appkey, name) 121 } 122 } 123 124 func (repo *AppRepoImpl) ExistByKey(ctx context.Context, appkey string) (bool, error) { 125 app := new(App) 126 result := repo.DB.WithContext(ctx).Where("appkey = ?", appkey).First(app) 127 if result.Error == nil { 128 return true, nil 129 } 130 if errors.Is(result.Error, gorm.ErrRecordNotFound) { 131 return false, nil 132 } 133 return false, errors.Wrapf(result.Error, "AppRepoImpl:ExistByKey(%s) failed", appkey) 134 } 135 136 func (repo *AppRepoImpl) ExistByName(ctx context.Context, name string) (bool, error) { 137 app := new(App) 138 result := repo.DB.WithContext(ctx).Where("name = ?", name).First(app) 139 if result.Error == nil { 140 return true, nil 141 } 142 if errors.Is(result.Error, gorm.ErrRecordNotFound) { 143 return false, nil 144 } 145 return false, errors.Wrapf(result.Error, "AppRepoImpl:ExistByName(%s) failed", name) 146 } 147 148 func (repo *AppRepoImpl) Create(ctx context.Context, app *App) (int, error) { 149 result := repo.DB.WithContext(ctx).Create(app) 150 if result.Error == nil { 151 return app.ID, nil 152 } else { 153 return 0, errors.Wrap(result.Error, "AppRepoImpl:Create failed") 154 } 155 } 156 157 func (repo *AppRepoImpl) Edit(ctx context.Context, app *App) error { 158 result := repo.DB.WithContext(ctx).Save(app) 159 if result.Error == nil { 160 return nil 161 } else { 162 return errors.Wrap(result.Error, "AppRepoImpl:Edit failed") 163 } 164 } 165 166 func (repo *AppRepoImpl) Delete(ctx context.Context, app *App) error { 167 result := repo.DB.WithContext(ctx).Delete(app) 168 if result.Error == nil { 169 return nil 170 } else { 171 return errors.Wrapf(result.Error, "AppRepoImpl:Delete(%d) failed", app.ID) 172 } 173 } 174 175 func (repo *AppRepoImpl) SetDisabled(ctx context.Context, appkey string, disabled int) error { 176 result := repo.DB.WithContext(ctx).Model(&App{}).Where("appkey = ? ", appkey).Update("disabled", disabled) 177 if result.Error == nil { 178 return nil 179 } else { 180 return errors.Wrapf(result.Error, "AppRepoImpl:SetDisabled(%s, %d) failed", appkey, disabled) 181 } 182 }