diff --git a/cli/actions/model.go b/cli/actions/model.go index e55bd4e..a8571ae 100644 --- a/cli/actions/model.go +++ b/cli/actions/model.go @@ -27,14 +27,18 @@ func GenModel(ctx *cli.Context) error { if dirAbsPath, err = mkdirIfNotExist(dir); err != nil { return err } - if file, err = os.Create(dirAbsPath + "/" + pattern + "_model.go"); err != nil { - return err - } - - m := codegen.NewPoModelStructInfo(pattern, url, pkg) - if err = m.GenCode(file); err == nil { - return logFinishAndFmt(dirAbsPath) + for _, table := range strings.Split(pattern, ",") { + if file, err = os.Create(dirAbsPath + "/" + table + "_model.go"); err != nil { + return err + } + m := codegen.NewPoModelStructInfo(table, url, pkg) + if err = m.GenCode(file); err != nil { + return err + } + if err := logFinishAndFmt(file.Name());err!=nil{ + return err + } } - return err + return nil } diff --git a/cli/actions/repo.go b/cli/actions/repo.go index 6c8c9a3..0f341a9 100644 --- a/cli/actions/repo.go +++ b/cli/actions/repo.go @@ -20,15 +20,19 @@ func GenRepo(ctx *cli.Context) error { if dirAbsPath, err = mkdirIfNotExist(dir); err != nil { return err } - if file, err = os.Create(dirAbsPath + "/" + pattern + "_repo.go"); err != nil { - return err - } - - m := codegen.NewPoModelStructInfo(pattern, url, pkg) - r := codegen.NewRepoGener(file, m, pkg) - if err = r.GenCode(); err == nil { - return logFinishAndFmt(dirAbsPath) + for _, table := range strings.Split(pattern, ",") { + if file, err = os.Create(dirAbsPath + "/" + table + "_repo.go"); err != nil { + return err + } + m := codegen.NewPoModelStructInfo(table, url, pkg) + r := codegen.NewRepoGener(file, m, pkg) + if err = r.GenCode(); err != nil { + return err + } + if err := logFinishAndFmt(file.Name());err!=nil{ + return err + } } - return err + return nil } diff --git a/cli/codegen/po_model_gen.go b/cli/codegen/po_model_gen.go index c127b06..505a374 100644 --- a/cli/codegen/po_model_gen.go +++ b/cli/codegen/po_model_gen.go @@ -163,7 +163,9 @@ func (m *PoModelStructInfo) initTableComment() error { if strings.Contains(string(lineStr), ") ENGINE=") { // end line keys := strings.Split(string(lineStr), "'") - m.TableComment = keys[len(keys)-2] + if len(keys) >= 3 { // 找到注释 + m.TableComment = keys[len(keys)-2] + } return nil } } diff --git a/example/internal/biz/model/user.go b/example/internal/biz/model/user.go new file mode 100644 index 0000000..920feac --- /dev/null +++ b/example/internal/biz/model/user.go @@ -0,0 +1,25 @@ +package model + +import "github.com/gozelus/zelus_rest/example/internal/biz/repos" + +type User struct { + ID int64 + NickName string + AvatarGuid string + Phone string + + bindRepo *repos.UserBindsModelRepoImp + userInfoRepo *repos.UsersModelRepoImp +} + +func NewUser(bindRepo *repos.UserBindsModelRepoImp, userInfoRepo *repos.UsersModelRepoImp) *User { + return &User{ + bindRepo: bindRepo, + userInfoRepo: userInfoRepo, + } +} + +func (u *User) Save() error { + if u.ID == 0 { + } +} diff --git a/example/internal/biz/repos/episode_counters_repo.go b/example/internal/biz/repos/episode_counters_repo.go new file mode 100644 index 0000000..291a9d7 --- /dev/null +++ b/example/internal/biz/repos/episode_counters_repo.go @@ -0,0 +1,76 @@ +package repos + +import ( + rest "github.com/gozelus/zelus_rest" + "github.com/pkg/errors" + "gorm.io/gorm" +) + +type EpisodeCountersModelRepoImp struct { + db *gorm.DB +} + +// FindManyWithId 根据唯一索引 PRIMARY 生成 +func (repo *EpisodeCountersModelRepoImp) FindManyWithId(ctx rest.Context, ids []int64) (map[int64]*models.EpisodeCountersModel, error) { + resp := map[int64]*models.EpisodeCountersModel{} + var results []*models.EpisodeCountersModel + db := repo.db.WithContext(ctx).Table("episode_counters"). + Where("id in (?)", ids) + if err := db.Find(&results).Error; err != nil { + return nil, errors.WithStack(err) + } + for _, r := range results { + resp[r.Id] = r + } + return resp, nil +} + +// FindOneWithId 根据唯一索引 PRIMARY 生成 +func (repo *EpisodeCountersModelRepoImp) FindOneWithId(ctx rest.Context, id int64) (*models.EpisodeCountersModel, error) { + resp := &models.EpisodeCountersModel{} + db := repo.db.WithContext(ctx).Table("episode_counters"). + Where("id = ?", id) + if err := db.First(resp).Error; err != nil { + return nil, errors.WithStack(err) + } + return resp, nil +} + +// FirstOrCreateWithId 根据唯一索引 PRIMARY 生成 +func (repo *EpisodeCountersModelRepoImp) FirstOrCreateWithId(ctx rest.Context, id int64, data *models.EpisodeCountersModel) error { + resp := data + db := repo.db.WithContext(ctx).Table("episode_counters"). + Where("id = ?", id) + if err := db.FirstOrCreate(resp).Error; err != nil { + return errors.WithStack(err) + } + return nil +} + +// DeleteOneWithId 根据唯一索引 PRIMARY 生成 +func (repo *EpisodeCountersModelRepoImp) DeleteOneWithId(ctx rest.Context, id int64) error { + db := repo.db.WithContext(ctx).Table("episode_counters"). + Where("id = ?", id) + if err := db.Delete(models.EpisodeCountersModel{}).Error; err != nil { + return errors.WithStack(err) + } + return nil +} + +// UpdateOneWithId 根据唯一索引 PRIMARY 生成 +func (repo *EpisodeCountersModelRepoImp) UpdateOneWithId(ctx rest.Context, id int64, attr map[string]interface{}) error { + db := repo.db.WithContext(ctx).Table("episode_counters"). + Where("id = ?", id) + if err := db.Updates(attr).Error; err != nil { + return errors.WithStack(err) + } + return nil +} + +// Insert 默认生成的创建函数 +func (repo *EpisodeCountersModelRepoImp) Insert(ctx rest.Context, data *models.EpisodeCountersModel) error { + if err := repo.db.WithContext(ctx).Table("episode_counters").Create(data).Error; err != nil { + return errors.WithStack(err) + } + return nil +} diff --git a/example/internal/biz/repos/episodes_repo.go b/example/internal/biz/repos/episodes_repo.go new file mode 100644 index 0000000..362531a --- /dev/null +++ b/example/internal/biz/repos/episodes_repo.go @@ -0,0 +1,77 @@ +package repos + +import ( + rest "github.com/gozelus/zelus_rest" + "github.com/gozelus/zelus_rest/example/internal/data/db" + "github.com/pkg/errors" + "gorm.io/gorm" +) + +type EpisodesModelRepoImp struct { + db *gorm.DB +} + +// FindManyWithId 根据唯一索引 PRIMARY 生成 +func (repo *EpisodesModelRepoImp) FindManyWithId(ctx rest.Context, ids []int64) (map[int64]*models.EpisodesModel, error) { + resp := map[int64]*models.EpisodesModel{} + var results []*models.EpisodesModel + db := repo.db.WithContext(ctx).Table("episodes"). + Where("id in (?)", ids) + if err := db.Find(&results).Error; err != nil { + return nil, errors.WithStack(err) + } + for _, r := range results { + resp[r.Id] = r + } + return resp, nil +} + +// FindOneWithId 根据唯一索引 PRIMARY 生成 +func (repo *EpisodesModelRepoImp) FindOneWithId(ctx rest.Context, id int64) (*models.EpisodesModel, error) { + resp := &models.EpisodesModel{} + db := repo.db.WithContext(ctx).Table("episodes"). + Where("id = ?", id) + if err := db.First(resp).Error; err != nil { + return nil, errors.WithStack(err) + } + return resp, nil +} + +// FirstOrCreateWithId 根据唯一索引 PRIMARY 生成 +func (repo *EpisodesModelRepoImp) FirstOrCreateWithId(ctx rest.Context, id int64, data *models.EpisodesModel) error { + resp := data + db := repo.db.WithContext(ctx).Table("episodes"). + Where("id = ?", id) + if err := db.FirstOrCreate(resp).Error; err != nil { + return errors.WithStack(err) + } + return nil +} + +// DeleteOneWithId 根据唯一索引 PRIMARY 生成 +func (repo *EpisodesModelRepoImp) DeleteOneWithId(ctx rest.Context, id int64) error { + db := repo.db.WithContext(ctx).Table("episodes"). + Where("id = ?", id) + if err := db.Delete(models.EpisodesModel{}).Error; err != nil { + return errors.WithStack(err) + } + return nil +} + +// UpdateOneWithId 根据唯一索引 PRIMARY 生成 +func (repo *EpisodesModelRepoImp) UpdateOneWithId(ctx rest.Context, id int64, attr map[string]interface{}) error { + db := repo.db.WithContext(ctx).Table("episodes"). + Where("id = ?", id) + if err := db.Updates(attr).Error; err != nil { + return errors.WithStack(err) + } + return nil +} + +// Insert 默认生成的创建函数 +func (repo *EpisodesModelRepoImp) Insert(ctx rest.Context, data *models.EpisodesModel) error { + if err := repo.db.WithContext(ctx).Table("episodes").Create(data).Error; err != nil { + return errors.WithStack(err) + } + return nil +} diff --git a/example/internal/biz/repos/like_episode_user_relations_repo.go b/example/internal/biz/repos/like_episode_user_relations_repo.go new file mode 100644 index 0000000..8e63bf5 --- /dev/null +++ b/example/internal/biz/repos/like_episode_user_relations_repo.go @@ -0,0 +1,146 @@ +package repos + +import ( + "time" + + models "github.com/gozelus/zelus_rest/example/internal/data/db" + + rest "github.com/gozelus/zelus_rest" + "github.com/pkg/errors" + "gorm.io/gorm" +) + +type LikeEpisodeUserRelationsModelRepoImp struct { + db *gorm.DB +} + +// ListEpisodeIdByUserIdOrderByCreateTime 根据索引 idx_user_id_create_time_episode_id 生成 +func (repo *LikeEpisodeUserRelationsModelRepoImp) ListEpisodeIdByUserIdOrderByCreateTime(ctx rest.Context, userId int64, limit int64, createTime time.Time) ([]*models.LikeEpisodeUserRelationsModel, bool, error) { + var resp []*models.LikeEpisodeUserRelationsModel + var hasMore bool + if err := repo.db.WithContext(ctx).Table("like_episode_user_relations"). + Select("episode_id"). + Where("user_id = ?", userId). + Where("create_time < ?", createTime). + Order("create_time desc"). + Limit(int(limit + 1)). + Find(&resp).Error; err != nil { + return nil, false, errors.WithStack(err) + } + hasMore = len(resp) > int(limit) + if hasMore { + resp = resp[:len(resp)-1] + } + return resp, hasMore, nil +} + +// FindManyWithId 根据唯一索引 PRIMARY 生成 +func (repo *LikeEpisodeUserRelationsModelRepoImp) FindManyWithId(ctx rest.Context, ids []int64) (map[int64]*models.LikeEpisodeUserRelationsModel, error) { + resp := map[int64]*models.LikeEpisodeUserRelationsModel{} + var results []*models.LikeEpisodeUserRelationsModel + db := repo.db.WithContext(ctx).Table("like_episode_user_relations"). + Where("id in (?)", ids) + if err := db.Find(&results).Error; err != nil { + return nil, errors.WithStack(err) + } + for _, r := range results { + resp[r.Id] = r + } + return resp, nil +} + +// FindOneWithId 根据唯一索引 PRIMARY 生成 +func (repo *LikeEpisodeUserRelationsModelRepoImp) FindOneWithId(ctx rest.Context, id int64) (*models.LikeEpisodeUserRelationsModel, error) { + resp := &models.LikeEpisodeUserRelationsModel{} + db := repo.db.WithContext(ctx).Table("like_episode_user_relations"). + Where("id = ?", id) + if err := db.First(resp).Error; err != nil { + return nil, errors.WithStack(err) + } + return resp, nil +} + +// FindOneWithEpisodeIdUserId 根据唯一索引 uniq_idx_episode_id_user_id 生成 +func (repo *LikeEpisodeUserRelationsModelRepoImp) FindOneWithEpisodeIdUserId(ctx rest.Context, episodeId int64, userId int64) (*models.LikeEpisodeUserRelationsModel, error) { + resp := &models.LikeEpisodeUserRelationsModel{} + db := repo.db.WithContext(ctx).Table("like_episode_user_relations"). + Where("episode_id = ?", episodeId). + Where("user_id = ?", userId) + if err := db.First(resp).Error; err != nil { + return nil, errors.WithStack(err) + } + return resp, nil +} + +// FirstOrCreateWithId 根据唯一索引 PRIMARY 生成 +func (repo *LikeEpisodeUserRelationsModelRepoImp) FirstOrCreateWithId(ctx rest.Context, id int64, data *models.LikeEpisodeUserRelationsModel) error { + resp := data + db := repo.db.WithContext(ctx).Table("like_episode_user_relations"). + Where("id = ?", id) + if err := db.FirstOrCreate(resp).Error; err != nil { + return errors.WithStack(err) + } + return nil +} + +// FirstOrCreateWithEpisodeIdUserId 根据唯一索引 uniq_idx_episode_id_user_id 生成 +func (repo *LikeEpisodeUserRelationsModelRepoImp) FirstOrCreateWithEpisodeIdUserId(ctx rest.Context, episodeId int64, userId int64, data *models.LikeEpisodeUserRelationsModel) error { + resp := data + db := repo.db.WithContext(ctx).Table("like_episode_user_relations"). + Where("episode_id = ?", episodeId). + Where("user_id = ?", userId) + if err := db.FirstOrCreate(resp).Error; err != nil { + return errors.WithStack(err) + } + return nil +} + +// DeleteOneWithId 根据唯一索引 PRIMARY 生成 +func (repo *LikeEpisodeUserRelationsModelRepoImp) DeleteOneWithId(ctx rest.Context, id int64) error { + db := repo.db.WithContext(ctx).Table("like_episode_user_relations"). + Where("id = ?", id) + if err := db.Delete(models.LikeEpisodeUserRelationsModel{}).Error; err != nil { + return errors.WithStack(err) + } + return nil +} + +// DeleteOneWithEpisodeIdUserId 根据唯一索引 uniq_idx_episode_id_user_id 生成 +func (repo *LikeEpisodeUserRelationsModelRepoImp) DeleteOneWithEpisodeIdUserId(ctx rest.Context, episodeId int64, userId int64) error { + db := repo.db.WithContext(ctx).Table("like_episode_user_relations"). + Where("episode_id = ?", episodeId). + Where("user_id = ?", userId) + if err := db.Delete(models.LikeEpisodeUserRelationsModel{}).Error; err != nil { + return errors.WithStack(err) + } + return nil +} + +// UpdateOneWithId 根据唯一索引 PRIMARY 生成 +func (repo *LikeEpisodeUserRelationsModelRepoImp) UpdateOneWithId(ctx rest.Context, id int64, attr map[string]interface{}) error { + db := repo.db.WithContext(ctx).Table("like_episode_user_relations"). + Where("id = ?", id) + if err := db.Updates(attr).Error; err != nil { + return errors.WithStack(err) + } + return nil +} + +// UpdateOneWithEpisodeIdUserId 根据唯一索引 uniq_idx_episode_id_user_id 生成 +func (repo *LikeEpisodeUserRelationsModelRepoImp) UpdateOneWithEpisodeIdUserId(ctx rest.Context, episodeId int64, userId int64, attr map[string]interface{}) error { + db := repo.db.WithContext(ctx).Table("like_episode_user_relations"). + Where("episode_id = ?", episodeId). + Where("user_id = ?", userId) + if err := db.Updates(attr).Error; err != nil { + return errors.WithStack(err) + } + return nil +} + +// Insert 默认生成的创建函数 +func (repo *LikeEpisodeUserRelationsModelRepoImp) Insert(ctx rest.Context, data *models.LikeEpisodeUserRelationsModel) error { + if err := repo.db.WithContext(ctx).Table("like_episode_user_relations").Create(data).Error; err != nil { + return errors.WithStack(err) + } + return nil +} diff --git a/example/internal/biz/repos/playlist_episode_relations_repo.go b/example/internal/biz/repos/playlist_episode_relations_repo.go new file mode 100644 index 0000000..b4f623a --- /dev/null +++ b/example/internal/biz/repos/playlist_episode_relations_repo.go @@ -0,0 +1,144 @@ +package repos + +import ( + "time" + + rest "github.com/gozelus/zelus_rest" + "github.com/pkg/errors" + "gorm.io/gorm" +) + +type PlaylistEpisodeRelationsModelRepoImp struct { + db *gorm.DB +} + +// ListEpisodeIdByPlaylistIdOrderByCreateTime 根据索引 idx_playlist_id_create_time_episode_id 生成 +func (repo *PlaylistEpisodeRelationsModelRepoImp) ListEpisodeIdByPlaylistIdOrderByCreateTime(ctx rest.Context, playlistId int64, limit int64, createTime time.Time) ([]*models.PlaylistEpisodeRelationsModel, bool, error) { + var resp []*models.PlaylistEpisodeRelationsModel + var hasMore bool + if err := repo.db.WithContext(ctx).Table("playlist_episode_relations"). + Select("episode_id"). + Where("playlist_id = ?", playlistId). + Where("create_time < ?", createTime). + Order("create_time desc"). + Limit(int(limit + 1)). + Find(&resp).Error; err != nil { + return nil, false, errors.WithStack(err) + } + hasMore = len(resp) > int(limit) + if hasMore { + resp = resp[:len(resp)-1] + } + return resp, hasMore, nil +} + +// FindManyWithId 根据唯一索引 PRIMARY 生成 +func (repo *PlaylistEpisodeRelationsModelRepoImp) FindManyWithId(ctx rest.Context, ids []int64) (map[int64]*models.PlaylistEpisodeRelationsModel, error) { + resp := map[int64]*models.PlaylistEpisodeRelationsModel{} + var results []*models.PlaylistEpisodeRelationsModel + db := repo.db.WithContext(ctx).Table("playlist_episode_relations"). + Where("id in (?)", ids) + if err := db.Find(&results).Error; err != nil { + return nil, errors.WithStack(err) + } + for _, r := range results { + resp[r.Id] = r + } + return resp, nil +} + +// FindOneWithId 根据唯一索引 PRIMARY 生成 +func (repo *PlaylistEpisodeRelationsModelRepoImp) FindOneWithId(ctx rest.Context, id int64) (*models.PlaylistEpisodeRelationsModel, error) { + resp := &models.PlaylistEpisodeRelationsModel{} + db := repo.db.WithContext(ctx).Table("playlist_episode_relations"). + Where("id = ?", id) + if err := db.First(resp).Error; err != nil { + return nil, errors.WithStack(err) + } + return resp, nil +} + +// FindOneWithPlaylistIdEpisodeId 根据唯一索引 uniq_idx_playlist_id_episode_id 生成 +func (repo *PlaylistEpisodeRelationsModelRepoImp) FindOneWithPlaylistIdEpisodeId(ctx rest.Context, playlistId int64, episodeId int64) (*models.PlaylistEpisodeRelationsModel, error) { + resp := &models.PlaylistEpisodeRelationsModel{} + db := repo.db.WithContext(ctx).Table("playlist_episode_relations"). + Where("playlist_id = ?", playlistId). + Where("episode_id = ?", episodeId) + if err := db.First(resp).Error; err != nil { + return nil, errors.WithStack(err) + } + return resp, nil +} + +// FirstOrCreateWithId 根据唯一索引 PRIMARY 生成 +func (repo *PlaylistEpisodeRelationsModelRepoImp) FirstOrCreateWithId(ctx rest.Context, id int64, data *models.PlaylistEpisodeRelationsModel) error { + resp := data + db := repo.db.WithContext(ctx).Table("playlist_episode_relations"). + Where("id = ?", id) + if err := db.FirstOrCreate(resp).Error; err != nil { + return errors.WithStack(err) + } + return nil +} + +// FirstOrCreateWithPlaylistIdEpisodeId 根据唯一索引 uniq_idx_playlist_id_episode_id 生成 +func (repo *PlaylistEpisodeRelationsModelRepoImp) FirstOrCreateWithPlaylistIdEpisodeId(ctx rest.Context, playlistId int64, episodeId int64, data *models.PlaylistEpisodeRelationsModel) error { + resp := data + db := repo.db.WithContext(ctx).Table("playlist_episode_relations"). + Where("playlist_id = ?", playlistId). + Where("episode_id = ?", episodeId) + if err := db.FirstOrCreate(resp).Error; err != nil { + return errors.WithStack(err) + } + return nil +} + +// DeleteOneWithId 根据唯一索引 PRIMARY 生成 +func (repo *PlaylistEpisodeRelationsModelRepoImp) DeleteOneWithId(ctx rest.Context, id int64) error { + db := repo.db.WithContext(ctx).Table("playlist_episode_relations"). + Where("id = ?", id) + if err := db.Delete(models.PlaylistEpisodeRelationsModel{}).Error; err != nil { + return errors.WithStack(err) + } + return nil +} + +// DeleteOneWithPlaylistIdEpisodeId 根据唯一索引 uniq_idx_playlist_id_episode_id 生成 +func (repo *PlaylistEpisodeRelationsModelRepoImp) DeleteOneWithPlaylistIdEpisodeId(ctx rest.Context, playlistId int64, episodeId int64) error { + db := repo.db.WithContext(ctx).Table("playlist_episode_relations"). + Where("playlist_id = ?", playlistId). + Where("episode_id = ?", episodeId) + if err := db.Delete(models.PlaylistEpisodeRelationsModel{}).Error; err != nil { + return errors.WithStack(err) + } + return nil +} + +// UpdateOneWithId 根据唯一索引 PRIMARY 生成 +func (repo *PlaylistEpisodeRelationsModelRepoImp) UpdateOneWithId(ctx rest.Context, id int64, attr map[string]interface{}) error { + db := repo.db.WithContext(ctx).Table("playlist_episode_relations"). + Where("id = ?", id) + if err := db.Updates(attr).Error; err != nil { + return errors.WithStack(err) + } + return nil +} + +// UpdateOneWithPlaylistIdEpisodeId 根据唯一索引 uniq_idx_playlist_id_episode_id 生成 +func (repo *PlaylistEpisodeRelationsModelRepoImp) UpdateOneWithPlaylistIdEpisodeId(ctx rest.Context, playlistId int64, episodeId int64, attr map[string]interface{}) error { + db := repo.db.WithContext(ctx).Table("playlist_episode_relations"). + Where("playlist_id = ?", playlistId). + Where("episode_id = ?", episodeId) + if err := db.Updates(attr).Error; err != nil { + return errors.WithStack(err) + } + return nil +} + +// Insert 默认生成的创建函数 +func (repo *PlaylistEpisodeRelationsModelRepoImp) Insert(ctx rest.Context, data *models.PlaylistEpisodeRelationsModel) error { + if err := repo.db.WithContext(ctx).Table("playlist_episode_relations").Create(data).Error; err != nil { + return errors.WithStack(err) + } + return nil +} diff --git a/example/internal/biz/repos/playlists_repo.go b/example/internal/biz/repos/playlists_repo.go new file mode 100644 index 0000000..2179b11 --- /dev/null +++ b/example/internal/biz/repos/playlists_repo.go @@ -0,0 +1,77 @@ +package repos + +import ( + rest "github.com/gozelus/zelus_rest" + "github.com/gozelus/zelus_rest/example/internal/data/db" + "github.com/pkg/errors" + "gorm.io/gorm" +) + +type PlaylistsModelRepoImp struct { + db *gorm.DB +} + +// FindManyWithId 根据唯一索引 PRIMARY 生成 +func (repo *PlaylistsModelRepoImp) FindManyWithId(ctx rest.Context, ids []int64) (map[int64]*models.PlaylistsModel, error) { + resp := map[int64]*models.PlaylistsModel{} + var results []*models.PlaylistsModel + db := repo.db.WithContext(ctx).Table("playlists"). + Where("id in (?)", ids) + if err := db.Find(&results).Error; err != nil { + return nil, errors.WithStack(err) + } + for _, r := range results { + resp[r.Id] = r + } + return resp, nil +} + +// FindOneWithId 根据唯一索引 PRIMARY 生成 +func (repo *PlaylistsModelRepoImp) FindOneWithId(ctx rest.Context, id int64) (*models.PlaylistsModel, error) { + resp := &models.PlaylistsModel{} + db := repo.db.WithContext(ctx).Table("playlists"). + Where("id = ?", id) + if err := db.First(resp).Error; err != nil { + return nil, errors.WithStack(err) + } + return resp, nil +} + +// FirstOrCreateWithId 根据唯一索引 PRIMARY 生成 +func (repo *PlaylistsModelRepoImp) FirstOrCreateWithId(ctx rest.Context, id int64, data *models.PlaylistsModel) error { + resp := data + db := repo.db.WithContext(ctx).Table("playlists"). + Where("id = ?", id) + if err := db.FirstOrCreate(resp).Error; err != nil { + return errors.WithStack(err) + } + return nil +} + +// DeleteOneWithId 根据唯一索引 PRIMARY 生成 +func (repo *PlaylistsModelRepoImp) DeleteOneWithId(ctx rest.Context, id int64) error { + db := repo.db.WithContext(ctx).Table("playlists"). + Where("id = ?", id) + if err := db.Delete(models.PlaylistsModel{}).Error; err != nil { + return errors.WithStack(err) + } + return nil +} + +// UpdateOneWithId 根据唯一索引 PRIMARY 生成 +func (repo *PlaylistsModelRepoImp) UpdateOneWithId(ctx rest.Context, id int64, attr map[string]interface{}) error { + db := repo.db.WithContext(ctx).Table("playlists"). + Where("id = ?", id) + if err := db.Updates(attr).Error; err != nil { + return errors.WithStack(err) + } + return nil +} + +// Insert 默认生成的创建函数 +func (repo *PlaylistsModelRepoImp) Insert(ctx rest.Context, data *models.PlaylistsModel) error { + if err := repo.db.WithContext(ctx).Table("playlists").Create(data).Error; err != nil { + return errors.WithStack(err) + } + return nil +} diff --git a/example/internal/biz/repos/podcast_counters_repo.go b/example/internal/biz/repos/podcast_counters_repo.go new file mode 100644 index 0000000..861738a --- /dev/null +++ b/example/internal/biz/repos/podcast_counters_repo.go @@ -0,0 +1,77 @@ +package repos + +import ( + rest "github.com/gozelus/zelus_rest" + "github.com/gozelus/zelus_rest/example/internal/data/db" + "github.com/pkg/errors" + "gorm.io/gorm" +) + +type PodcastCountersModelRepoImp struct { + db *gorm.DB +} + +// FindManyWithId 根据唯一索引 PRIMARY 生成 +func (repo *PodcastCountersModelRepoImp) FindManyWithId(ctx rest.Context, ids []int64) (map[int64]*models.PodcastCountersModel, error) { + resp := map[int64]*models.PodcastCountersModel{} + var results []*models.PodcastCountersModel + db := repo.db.WithContext(ctx).Table("podcast_counters"). + Where("id in (?)", ids) + if err := db.Find(&results).Error; err != nil { + return nil, errors.WithStack(err) + } + for _, r := range results { + resp[r.Id] = r + } + return resp, nil +} + +// FindOneWithId 根据唯一索引 PRIMARY 生成 +func (repo *PodcastCountersModelRepoImp) FindOneWithId(ctx rest.Context, id int64) (*models.PodcastCountersModel, error) { + resp := &models.PodcastCountersModel{} + db := repo.db.WithContext(ctx).Table("podcast_counters"). + Where("id = ?", id) + if err := db.First(resp).Error; err != nil { + return nil, errors.WithStack(err) + } + return resp, nil +} + +// FirstOrCreateWithId 根据唯一索引 PRIMARY 生成 +func (repo *PodcastCountersModelRepoImp) FirstOrCreateWithId(ctx rest.Context, id int64, data *models.PodcastCountersModel) error { + resp := data + db := repo.db.WithContext(ctx).Table("podcast_counters"). + Where("id = ?", id) + if err := db.FirstOrCreate(resp).Error; err != nil { + return errors.WithStack(err) + } + return nil +} + +// DeleteOneWithId 根据唯一索引 PRIMARY 生成 +func (repo *PodcastCountersModelRepoImp) DeleteOneWithId(ctx rest.Context, id int64) error { + db := repo.db.WithContext(ctx).Table("podcast_counters"). + Where("id = ?", id) + if err := db.Delete(models.PodcastCountersModel{}).Error; err != nil { + return errors.WithStack(err) + } + return nil +} + +// UpdateOneWithId 根据唯一索引 PRIMARY 生成 +func (repo *PodcastCountersModelRepoImp) UpdateOneWithId(ctx rest.Context, id int64, attr map[string]interface{}) error { + db := repo.db.WithContext(ctx).Table("podcast_counters"). + Where("id = ?", id) + if err := db.Updates(attr).Error; err != nil { + return errors.WithStack(err) + } + return nil +} + +// Insert 默认生成的创建函数 +func (repo *PodcastCountersModelRepoImp) Insert(ctx rest.Context, data *models.PodcastCountersModel) error { + if err := repo.db.WithContext(ctx).Table("podcast_counters").Create(data).Error; err != nil { + return errors.WithStack(err) + } + return nil +} diff --git a/example/internal/biz/repos/podcasts_repo.go b/example/internal/biz/repos/podcasts_repo.go new file mode 100644 index 0000000..be3e06e --- /dev/null +++ b/example/internal/biz/repos/podcasts_repo.go @@ -0,0 +1,77 @@ +package repos + +import ( + rest "github.com/gozelus/zelus_rest" + "github.com/gozelus/zelus_rest/example/internal/data/db" + "github.com/pkg/errors" + "gorm.io/gorm" +) + +type PodcastsModelRepoImp struct { + db *gorm.DB +} + +// FindManyWithId 根据唯一索引 PRIMARY 生成 +func (repo *PodcastsModelRepoImp) FindManyWithId(ctx rest.Context, ids []int64) (map[int64]*models.PodcastsModel, error) { + resp := map[int64]*models.PodcastsModel{} + var results []*models.PodcastsModel + db := repo.db.WithContext(ctx).Table("podcasts"). + Where("id in (?)", ids) + if err := db.Find(&results).Error; err != nil { + return nil, errors.WithStack(err) + } + for _, r := range results { + resp[r.Id] = r + } + return resp, nil +} + +// FindOneWithId 根据唯一索引 PRIMARY 生成 +func (repo *PodcastsModelRepoImp) FindOneWithId(ctx rest.Context, id int64) (*models.PodcastsModel, error) { + resp := &models.PodcastsModel{} + db := repo.db.WithContext(ctx).Table("podcasts"). + Where("id = ?", id) + if err := db.First(resp).Error; err != nil { + return nil, errors.WithStack(err) + } + return resp, nil +} + +// FirstOrCreateWithId 根据唯一索引 PRIMARY 生成 +func (repo *PodcastsModelRepoImp) FirstOrCreateWithId(ctx rest.Context, id int64, data *models.PodcastsModel) error { + resp := data + db := repo.db.WithContext(ctx).Table("podcasts"). + Where("id = ?", id) + if err := db.FirstOrCreate(resp).Error; err != nil { + return errors.WithStack(err) + } + return nil +} + +// DeleteOneWithId 根据唯一索引 PRIMARY 生成 +func (repo *PodcastsModelRepoImp) DeleteOneWithId(ctx rest.Context, id int64) error { + db := repo.db.WithContext(ctx).Table("podcasts"). + Where("id = ?", id) + if err := db.Delete(models.PodcastsModel{}).Error; err != nil { + return errors.WithStack(err) + } + return nil +} + +// UpdateOneWithId 根据唯一索引 PRIMARY 生成 +func (repo *PodcastsModelRepoImp) UpdateOneWithId(ctx rest.Context, id int64, attr map[string]interface{}) error { + db := repo.db.WithContext(ctx).Table("podcasts"). + Where("id = ?", id) + if err := db.Updates(attr).Error; err != nil { + return errors.WithStack(err) + } + return nil +} + +// Insert 默认生成的创建函数 +func (repo *PodcastsModelRepoImp) Insert(ctx rest.Context, data *models.PodcastsModel) error { + if err := repo.db.WithContext(ctx).Table("podcasts").Create(data).Error; err != nil { + return errors.WithStack(err) + } + return nil +} diff --git a/example/internal/biz/repos/recent_play_episode_user_relations_repo.go b/example/internal/biz/repos/recent_play_episode_user_relations_repo.go new file mode 100644 index 0000000..0bbba7a --- /dev/null +++ b/example/internal/biz/repos/recent_play_episode_user_relations_repo.go @@ -0,0 +1,145 @@ +package repos + +import ( + "github.com/gozelus/zelus_rest/example/internal/data/db" + "time" + + rest "github.com/gozelus/zelus_rest" + "github.com/pkg/errors" + "gorm.io/gorm" +) + +type RecentPlayEpisodeUserRelationsModelRepoImp struct { + db *gorm.DB +} + +// ListEpisodeIdByUserIdOrderByCreateTime 根据索引 idx_user_id_target_type_create_time_target_id 生成 +func (repo *RecentPlayEpisodeUserRelationsModelRepoImp) ListEpisodeIdByUserIdOrderByCreateTime(ctx rest.Context, userId int64, limit int64, createTime time.Time) ([]*models.RecentPlayEpisodeUserRelationsModel, bool, error) { + var resp []*models.RecentPlayEpisodeUserRelationsModel + var hasMore bool + if err := repo.db.WithContext(ctx).Table("recent_play_episode_user_relations"). + Select("episode_id"). + Where("user_id = ?", userId). + Where("create_time < ?", createTime). + Order("create_time desc"). + Limit(int(limit + 1)). + Find(&resp).Error; err != nil { + return nil, false, errors.WithStack(err) + } + hasMore = len(resp) > int(limit) + if hasMore { + resp = resp[:len(resp)-1] + } + return resp, hasMore, nil +} + +// FindManyWithId 根据唯一索引 PRIMARY 生成 +func (repo *RecentPlayEpisodeUserRelationsModelRepoImp) FindManyWithId(ctx rest.Context, ids []int64) (map[int64]*models.RecentPlayEpisodeUserRelationsModel, error) { + resp := map[int64]*models.RecentPlayEpisodeUserRelationsModel{} + var results []*models.RecentPlayEpisodeUserRelationsModel + db := repo.db.WithContext(ctx).Table("recent_play_episode_user_relations"). + Where("id in (?)", ids) + if err := db.Find(&results).Error; err != nil { + return nil, errors.WithStack(err) + } + for _, r := range results { + resp[r.Id] = r + } + return resp, nil +} + +// FindOneWithId 根据唯一索引 PRIMARY 生成 +func (repo *RecentPlayEpisodeUserRelationsModelRepoImp) FindOneWithId(ctx rest.Context, id int64) (*models.RecentPlayEpisodeUserRelationsModel, error) { + resp := &models.RecentPlayEpisodeUserRelationsModel{} + db := repo.db.WithContext(ctx).Table("recent_play_episode_user_relations"). + Where("id = ?", id) + if err := db.First(resp).Error; err != nil { + return nil, errors.WithStack(err) + } + return resp, nil +} + +// FindOneWithUserIdEpisodeId 根据唯一索引 uniq_idx_user_id_target_id_target_type 生成 +func (repo *RecentPlayEpisodeUserRelationsModelRepoImp) FindOneWithUserIdEpisodeId(ctx rest.Context, userId int64, episodeId int64) (*models.RecentPlayEpisodeUserRelationsModel, error) { + resp := &models.RecentPlayEpisodeUserRelationsModel{} + db := repo.db.WithContext(ctx).Table("recent_play_episode_user_relations"). + Where("user_id = ?", userId). + Where("episode_id = ?", episodeId) + if err := db.First(resp).Error; err != nil { + return nil, errors.WithStack(err) + } + return resp, nil +} + +// FirstOrCreateWithId 根据唯一索引 PRIMARY 生成 +func (repo *RecentPlayEpisodeUserRelationsModelRepoImp) FirstOrCreateWithId(ctx rest.Context, id int64, data *models.RecentPlayEpisodeUserRelationsModel) error { + resp := data + db := repo.db.WithContext(ctx).Table("recent_play_episode_user_relations"). + Where("id = ?", id) + if err := db.FirstOrCreate(resp).Error; err != nil { + return errors.WithStack(err) + } + return nil +} + +// FirstOrCreateWithUserIdEpisodeId 根据唯一索引 uniq_idx_user_id_target_id_target_type 生成 +func (repo *RecentPlayEpisodeUserRelationsModelRepoImp) FirstOrCreateWithUserIdEpisodeId(ctx rest.Context, userId int64, episodeId int64, data *models.RecentPlayEpisodeUserRelationsModel) error { + resp := data + db := repo.db.WithContext(ctx).Table("recent_play_episode_user_relations"). + Where("user_id = ?", userId). + Where("episode_id = ?", episodeId) + if err := db.FirstOrCreate(resp).Error; err != nil { + return errors.WithStack(err) + } + return nil +} + +// DeleteOneWithId 根据唯一索引 PRIMARY 生成 +func (repo *RecentPlayEpisodeUserRelationsModelRepoImp) DeleteOneWithId(ctx rest.Context, id int64) error { + db := repo.db.WithContext(ctx).Table("recent_play_episode_user_relations"). + Where("id = ?", id) + if err := db.Delete(models.RecentPlayEpisodeUserRelationsModel{}).Error; err != nil { + return errors.WithStack(err) + } + return nil +} + +// DeleteOneWithUserIdEpisodeId 根据唯一索引 uniq_idx_user_id_target_id_target_type 生成 +func (repo *RecentPlayEpisodeUserRelationsModelRepoImp) DeleteOneWithUserIdEpisodeId(ctx rest.Context, userId int64, episodeId int64) error { + db := repo.db.WithContext(ctx).Table("recent_play_episode_user_relations"). + Where("user_id = ?", userId). + Where("episode_id = ?", episodeId) + if err := db.Delete(models.RecentPlayEpisodeUserRelationsModel{}).Error; err != nil { + return errors.WithStack(err) + } + return nil +} + +// UpdateOneWithId 根据唯一索引 PRIMARY 生成 +func (repo *RecentPlayEpisodeUserRelationsModelRepoImp) UpdateOneWithId(ctx rest.Context, id int64, attr map[string]interface{}) error { + db := repo.db.WithContext(ctx).Table("recent_play_episode_user_relations"). + Where("id = ?", id) + if err := db.Updates(attr).Error; err != nil { + return errors.WithStack(err) + } + return nil +} + +// UpdateOneWithUserIdEpisodeId 根据唯一索引 uniq_idx_user_id_target_id_target_type 生成 +func (repo *RecentPlayEpisodeUserRelationsModelRepoImp) UpdateOneWithUserIdEpisodeId(ctx rest.Context, userId int64, episodeId int64, attr map[string]interface{}) error { + db := repo.db.WithContext(ctx).Table("recent_play_episode_user_relations"). + Where("user_id = ?", userId). + Where("episode_id = ?", episodeId) + if err := db.Updates(attr).Error; err != nil { + return errors.WithStack(err) + } + return nil +} + +// Insert 默认生成的创建函数 +func (repo *RecentPlayEpisodeUserRelationsModelRepoImp) Insert(ctx rest.Context, data *models.RecentPlayEpisodeUserRelationsModel) error { + if err := repo.db.WithContext(ctx).Table("recent_play_episode_user_relations").Create(data).Error; err != nil { + return errors.WithStack(err) + } + return nil +} diff --git a/example/internal/biz/repos/sub_podcast_user_relations_repo.go b/example/internal/biz/repos/sub_podcast_user_relations_repo.go new file mode 100644 index 0000000..f2091b6 --- /dev/null +++ b/example/internal/biz/repos/sub_podcast_user_relations_repo.go @@ -0,0 +1,145 @@ +package repos + +import ( + "github.com/gozelus/zelus_rest/example/internal/data/db" + "time" + + rest "github.com/gozelus/zelus_rest" + "github.com/pkg/errors" + "gorm.io/gorm" +) + +type SubPodcastUserRelationsModelRepoImp struct { + db *gorm.DB +} + +// ListPodcastIdByUserIdOrderByCreateTime 根据索引 idx_user_id_create_time_podcast_id 生成 +func (repo *SubPodcastUserRelationsModelRepoImp) ListPodcastIdByUserIdOrderByCreateTime(ctx rest.Context, userId int64, limit int64, createTime time.Time) ([]*models.SubPodcastUserRelationsModel, bool, error) { + var resp []*models.SubPodcastUserRelationsModel + var hasMore bool + if err := repo.db.WithContext(ctx).Table("sub_podcast_user_relations"). + Select("podcast_id"). + Where("user_id = ?", userId). + Where("create_time < ?", createTime). + Order("create_time desc"). + Limit(int(limit + 1)). + Find(&resp).Error; err != nil { + return nil, false, errors.WithStack(err) + } + hasMore = len(resp) > int(limit) + if hasMore { + resp = resp[:len(resp)-1] + } + return resp, hasMore, nil +} + +// FindManyWithId 根据唯一索引 PRIMARY 生成 +func (repo *SubPodcastUserRelationsModelRepoImp) FindManyWithId(ctx rest.Context, ids []int64) (map[int64]*models.SubPodcastUserRelationsModel, error) { + resp := map[int64]*models.SubPodcastUserRelationsModel{} + var results []*models.SubPodcastUserRelationsModel + db := repo.db.WithContext(ctx).Table("sub_podcast_user_relations"). + Where("id in (?)", ids) + if err := db.Find(&results).Error; err != nil { + return nil, errors.WithStack(err) + } + for _, r := range results { + resp[r.Id] = r + } + return resp, nil +} + +// FindOneWithId 根据唯一索引 PRIMARY 生成 +func (repo *SubPodcastUserRelationsModelRepoImp) FindOneWithId(ctx rest.Context, id int64) (*models.SubPodcastUserRelationsModel, error) { + resp := &models.SubPodcastUserRelationsModel{} + db := repo.db.WithContext(ctx).Table("sub_podcast_user_relations"). + Where("id = ?", id) + if err := db.First(resp).Error; err != nil { + return nil, errors.WithStack(err) + } + return resp, nil +} + +// FindOneWithPodcastIdUserId 根据唯一索引 uniq_idx_podcast_id_user_id 生成 +func (repo *SubPodcastUserRelationsModelRepoImp) FindOneWithPodcastIdUserId(ctx rest.Context, podcastId int64, userId int64) (*models.SubPodcastUserRelationsModel, error) { + resp := &models.SubPodcastUserRelationsModel{} + db := repo.db.WithContext(ctx).Table("sub_podcast_user_relations"). + Where("podcast_id = ?", podcastId). + Where("user_id = ?", userId) + if err := db.First(resp).Error; err != nil { + return nil, errors.WithStack(err) + } + return resp, nil +} + +// FirstOrCreateWithId 根据唯一索引 PRIMARY 生成 +func (repo *SubPodcastUserRelationsModelRepoImp) FirstOrCreateWithId(ctx rest.Context, id int64, data *models.SubPodcastUserRelationsModel) error { + resp := data + db := repo.db.WithContext(ctx).Table("sub_podcast_user_relations"). + Where("id = ?", id) + if err := db.FirstOrCreate(resp).Error; err != nil { + return errors.WithStack(err) + } + return nil +} + +// FirstOrCreateWithPodcastIdUserId 根据唯一索引 uniq_idx_podcast_id_user_id 生成 +func (repo *SubPodcastUserRelationsModelRepoImp) FirstOrCreateWithPodcastIdUserId(ctx rest.Context, podcastId int64, userId int64, data *models.SubPodcastUserRelationsModel) error { + resp := data + db := repo.db.WithContext(ctx).Table("sub_podcast_user_relations"). + Where("podcast_id = ?", podcastId). + Where("user_id = ?", userId) + if err := db.FirstOrCreate(resp).Error; err != nil { + return errors.WithStack(err) + } + return nil +} + +// DeleteOneWithId 根据唯一索引 PRIMARY 生成 +func (repo *SubPodcastUserRelationsModelRepoImp) DeleteOneWithId(ctx rest.Context, id int64) error { + db := repo.db.WithContext(ctx).Table("sub_podcast_user_relations"). + Where("id = ?", id) + if err := db.Delete(models.SubPodcastUserRelationsModel{}).Error; err != nil { + return errors.WithStack(err) + } + return nil +} + +// DeleteOneWithPodcastIdUserId 根据唯一索引 uniq_idx_podcast_id_user_id 生成 +func (repo *SubPodcastUserRelationsModelRepoImp) DeleteOneWithPodcastIdUserId(ctx rest.Context, podcastId int64, userId int64) error { + db := repo.db.WithContext(ctx).Table("sub_podcast_user_relations"). + Where("podcast_id = ?", podcastId). + Where("user_id = ?", userId) + if err := db.Delete(models.SubPodcastUserRelationsModel{}).Error; err != nil { + return errors.WithStack(err) + } + return nil +} + +// UpdateOneWithId 根据唯一索引 PRIMARY 生成 +func (repo *SubPodcastUserRelationsModelRepoImp) UpdateOneWithId(ctx rest.Context, id int64, attr map[string]interface{}) error { + db := repo.db.WithContext(ctx).Table("sub_podcast_user_relations"). + Where("id = ?", id) + if err := db.Updates(attr).Error; err != nil { + return errors.WithStack(err) + } + return nil +} + +// UpdateOneWithPodcastIdUserId 根据唯一索引 uniq_idx_podcast_id_user_id 生成 +func (repo *SubPodcastUserRelationsModelRepoImp) UpdateOneWithPodcastIdUserId(ctx rest.Context, podcastId int64, userId int64, attr map[string]interface{}) error { + db := repo.db.WithContext(ctx).Table("sub_podcast_user_relations"). + Where("podcast_id = ?", podcastId). + Where("user_id = ?", userId) + if err := db.Updates(attr).Error; err != nil { + return errors.WithStack(err) + } + return nil +} + +// Insert 默认生成的创建函数 +func (repo *SubPodcastUserRelationsModelRepoImp) Insert(ctx rest.Context, data *models.SubPodcastUserRelationsModel) error { + if err := repo.db.WithContext(ctx).Table("sub_podcast_user_relations").Create(data).Error; err != nil { + return errors.WithStack(err) + } + return nil +} diff --git a/example/internal/biz/repos/user_binds_repo.go b/example/internal/biz/repos/user_binds_repo.go new file mode 100644 index 0000000..5eaaf23 --- /dev/null +++ b/example/internal/biz/repos/user_binds_repo.go @@ -0,0 +1,123 @@ +package repos + +import ( + rest "github.com/gozelus/zelus_rest" + "github.com/gozelus/zelus_rest/example/internal/data/db" + "github.com/pkg/errors" + "gorm.io/gorm" +) + +type UserBindsModelRepoImp struct { + db *gorm.DB +} + +// FindManyWithId 根据唯一索引 PRIMARY 生成 +func (repo *UserBindsModelRepoImp) FindManyWithId(ctx rest.Context, ids []int64) (map[int64]*models.UserBindsModel, error) { + resp := map[int64]*models.UserBindsModel{} + var results []*models.UserBindsModel + db := repo.db.WithContext(ctx).Table("user_binds"). + Where("id in (?)", ids) + if err := db.Find(&results).Error; err != nil { + return nil, errors.WithStack(err) + } + for _, r := range results { + resp[r.Id] = r + } + return resp, nil +} + +// FindOneWithId 根据唯一索引 PRIMARY 生成 +func (repo *UserBindsModelRepoImp) FindOneWithId(ctx rest.Context, id int64) (*models.UserBindsModel, error) { + resp := &models.UserBindsModel{} + db := repo.db.WithContext(ctx).Table("user_binds"). + Where("id = ?", id) + if err := db.First(resp).Error; err != nil { + return nil, errors.WithStack(err) + } + return resp, nil +} + +// FindOneWithBindCodeBindType 根据唯一索引 uniq_idx_bind_code_bind_type 生成 +func (repo *UserBindsModelRepoImp) FindOneWithBindCodeBindType(ctx rest.Context, bindCode string, bindType int64) (*models.UserBindsModel, error) { + resp := &models.UserBindsModel{} + db := repo.db.WithContext(ctx).Table("user_binds"). + Where("bind_code = ?", bindCode). + Where("bind_type = ?", bindType) + if err := db.First(resp).Error; err != nil { + return nil, errors.WithStack(err) + } + return resp, nil +} + +// FirstOrCreateWithId 根据唯一索引 PRIMARY 生成 +func (repo *UserBindsModelRepoImp) FirstOrCreateWithId(ctx rest.Context, id int64, data *models.UserBindsModel) error { + resp := data + db := repo.db.WithContext(ctx).Table("user_binds"). + Where("id = ?", id) + if err := db.FirstOrCreate(resp).Error; err != nil { + return errors.WithStack(err) + } + return nil +} + +// FirstOrCreateWithBindCodeBindType 根据唯一索引 uniq_idx_bind_code_bind_type 生成 +func (repo *UserBindsModelRepoImp) FirstOrCreateWithBindCodeBindType(ctx rest.Context, bindCode string, bindType int64, data *models.UserBindsModel) error { + resp := data + db := repo.db.WithContext(ctx).Table("user_binds"). + Where("bind_code = ?", bindCode). + Where("bind_type = ?", bindType) + if err := db.FirstOrCreate(resp).Error; err != nil { + return errors.WithStack(err) + } + return nil +} + +// DeleteOneWithId 根据唯一索引 PRIMARY 生成 +func (repo *UserBindsModelRepoImp) DeleteOneWithId(ctx rest.Context, id int64) error { + db := repo.db.WithContext(ctx).Table("user_binds"). + Where("id = ?", id) + if err := db.Delete(models.UserBindsModel{}).Error; err != nil { + return errors.WithStack(err) + } + return nil +} + +// DeleteOneWithBindCodeBindType 根据唯一索引 uniq_idx_bind_code_bind_type 生成 +func (repo *UserBindsModelRepoImp) DeleteOneWithBindCodeBindType(ctx rest.Context, bindCode string, bindType int64) error { + db := repo.db.WithContext(ctx).Table("user_binds"). + Where("bind_code = ?", bindCode). + Where("bind_type = ?", bindType) + if err := db.Delete(models.UserBindsModel{}).Error; err != nil { + return errors.WithStack(err) + } + return nil +} + +// UpdateOneWithId 根据唯一索引 PRIMARY 生成 +func (repo *UserBindsModelRepoImp) UpdateOneWithId(ctx rest.Context, id int64, attr map[string]interface{}) error { + db := repo.db.WithContext(ctx).Table("user_binds"). + Where("id = ?", id) + if err := db.Updates(attr).Error; err != nil { + return errors.WithStack(err) + } + return nil +} + +// UpdateOneWithBindCodeBindType 根据唯一索引 uniq_idx_bind_code_bind_type 生成 +func (repo *UserBindsModelRepoImp) UpdateOneWithBindCodeBindType(ctx rest.Context, bindCode string, bindType int64, attr map[string]interface{}) error { + db := repo.db.WithContext(ctx).Table("user_binds"). + Where("bind_code = ?", bindCode). + Where("bind_type = ?", bindType) + if err := db.Updates(attr).Error; err != nil { + return errors.WithStack(err) + } + return nil +} + +// Insert 默认生成的创建函数 +func (repo *UserBindsModelRepoImp) Insert(ctx rest.Context, data *models.UserBindsModel) error { + if err := repo.db.WithContext(ctx).Table("user_binds").Create(data).Error; err != nil { + return errors.WithStack(err) + } + return nil +} diff --git a/example/internal/biz/repos/users_repo.go b/example/internal/biz/repos/users_repo.go new file mode 100644 index 0000000..4ac22ba --- /dev/null +++ b/example/internal/biz/repos/users_repo.go @@ -0,0 +1,76 @@ +package repos + +import ( + rest "github.com/gozelus/zelus_rest" + "github.com/pkg/errors" + "gorm.io/gorm" +) + +type UsersModelRepoImp struct { + db *gorm.DB +} + +// FindManyWithId 根据唯一索引 PRIMARY 生成 +func (repo *UsersModelRepoImp) FindManyWithId(ctx rest.Context, ids []int64) (map[int64]*models.UsersModel, error) { + resp := map[int64]*models.UsersModel{} + var results []*models.UsersModel + db := repo.db.WithContext(ctx).Table("users"). + Where("id in (?)", ids) + if err := db.Find(&results).Error; err != nil { + return nil, errors.WithStack(err) + } + for _, r := range results { + resp[r.Id] = r + } + return resp, nil +} + +// FindOneWithId 根据唯一索引 PRIMARY 生成 +func (repo *UsersModelRepoImp) FindOneWithId(ctx rest.Context, id int64) (*models.UsersModel, error) { + resp := &models.UsersModel{} + db := repo.db.WithContext(ctx).Table("users"). + Where("id = ?", id) + if err := db.First(resp).Error; err != nil { + return nil, errors.WithStack(err) + } + return resp, nil +} + +// FirstOrCreateWithId 根据唯一索引 PRIMARY 生成 +func (repo *UsersModelRepoImp) FirstOrCreateWithId(ctx rest.Context, id int64, data *models.UsersModel) error { + resp := data + db := repo.db.WithContext(ctx).Table("users"). + Where("id = ?", id) + if err := db.FirstOrCreate(resp).Error; err != nil { + return errors.WithStack(err) + } + return nil +} + +// DeleteOneWithId 根据唯一索引 PRIMARY 生成 +func (repo *UsersModelRepoImp) DeleteOneWithId(ctx rest.Context, id int64) error { + db := repo.db.WithContext(ctx).Table("users"). + Where("id = ?", id) + if err := db.Delete(models.UsersModel{}).Error; err != nil { + return errors.WithStack(err) + } + return nil +} + +// UpdateOneWithId 根据唯一索引 PRIMARY 生成 +func (repo *UsersModelRepoImp) UpdateOneWithId(ctx rest.Context, id int64, attr map[string]interface{}) error { + db := repo.db.WithContext(ctx).Table("users"). + Where("id = ?", id) + if err := db.Updates(attr).Error; err != nil { + return errors.WithStack(err) + } + return nil +} + +// Insert 默认生成的创建函数 +func (repo *UsersModelRepoImp) Insert(ctx rest.Context, data *models.UsersModel) error { + if err := repo.db.WithContext(ctx).Table("users").Create(data).Error; err != nil { + return errors.WithStack(err) + } + return nil +} diff --git a/example/internal/data/db/episode_counters_model.go b/example/internal/data/db/episode_counters_model.go new file mode 100644 index 0000000..0158ac5 --- /dev/null +++ b/example/internal/data/db/episode_counters_model.go @@ -0,0 +1,12 @@ +// Code generated by ZelusCtl. DO NOT EDIT. + +package models + +// EpisodeCountersModel 单集统计数据 +type EpisodeCountersModel struct { + Id int64 `gorm:"id"` // 主键id + PlayCount int64 `gorm:"play_count"` // 单集播放数 + CommentCount int64 `gorm:"comment_count"` // 单集评论数 + BarrageCount int64 `gorm:"barrage_count"` // 单集弹幕数 + LikeCount int64 `gorm:"like_count"` // 单集点赞数 +} diff --git a/example/internal/data/db/episodes_model.go b/example/internal/data/db/episodes_model.go new file mode 100644 index 0000000..da38031 --- /dev/null +++ b/example/internal/data/db/episodes_model.go @@ -0,0 +1,16 @@ +// Code generated by ZelusCtl. DO NOT EDIT. + +package models + +import "time" + +// EpisodesModel 单集详情表 +type EpisodesModel struct { + Id int64 `gorm:"id"` // 主键ID + CreateTime time.Time `gorm:"create_time"` // 创建时间 + UpdateTime time.Time `gorm:"update_time"` // 更新时间 + PodcastId int64 `gorm:"podcast_id"` // 单集所属节目id + Title string `gorm:"title"` // 标题 + Subtitle string `gorm:"subtitle"` // 子标题 + TotalTime int64 `gorm:"total_time"` // 时长,单位秒 +} diff --git a/example/internal/data/db/like_episode_user_relations_model.go b/example/internal/data/db/like_episode_user_relations_model.go new file mode 100644 index 0000000..476343d --- /dev/null +++ b/example/internal/data/db/like_episode_user_relations_model.go @@ -0,0 +1,14 @@ +// Code generated by ZelusCtl. DO NOT EDIT. + +package models + +import "time" + +// LikeEpisodeUserRelationsModel 描述用户与单集的点赞关系 +type LikeEpisodeUserRelationsModel struct { + Id int64 `gorm:"id"` // 唯一id + EpisodeId int64 `gorm:"episode_id"` // 单集id + UserId int64 `gorm:"user_id"` // 用户id + CreateTime time.Time `gorm:"create_time"` // 创建时间 + UpdateTime time.Time `gorm:"update_time"` // 更新时间 +} diff --git a/example/internal/data/db/playlist_episode_relations_model.go b/example/internal/data/db/playlist_episode_relations_model.go new file mode 100644 index 0000000..04fe813 --- /dev/null +++ b/example/internal/data/db/playlist_episode_relations_model.go @@ -0,0 +1,15 @@ +// Code generated by ZelusCtl. DO NOT EDIT. + +package models + +import "time" + +// PlaylistEpisodeRelationsModel 描述播单和单集直接的关系 +type PlaylistEpisodeRelationsModel struct { + Id int64 `gorm:"id"` // 主键ID + CreateTime time.Time `gorm:"create_time"` // 创建时间 + UpdateTime time.Time `gorm:"update_time"` // 更新时间 + UserId int64 `gorm:"user_id"` // 用户id + EpisodeId int64 `gorm:"episode_id"` // 单集id + PlaylistId int64 `gorm:"playlist_id"` // 播单id +} diff --git a/example/internal/data/db/playlists_model.go b/example/internal/data/db/playlists_model.go new file mode 100644 index 0000000..8db8b34 --- /dev/null +++ b/example/internal/data/db/playlists_model.go @@ -0,0 +1,15 @@ +// Code generated by ZelusCtl. DO NOT EDIT. + +package models + +import "time" + +// PlaylistsModel 播单详情 +type PlaylistsModel struct { + Id int64 `gorm:"id"` // 播单唯一id + CreateTime time.Time `gorm:"create_time"` // 创建时间 + UpdateTime time.Time `gorm:"update_time"` // 更新时间 + UserId int64 `gorm:"user_id"` // 用户id + Title string `gorm:"title"` // 播单标题 + Description string `gorm:"description"` // 播单描述 +} diff --git a/example/internal/data/db/podcast_counters_model.go b/example/internal/data/db/podcast_counters_model.go new file mode 100644 index 0000000..71fbd55 --- /dev/null +++ b/example/internal/data/db/podcast_counters_model.go @@ -0,0 +1,12 @@ +// Code generated by ZelusCtl. DO NOT EDIT. + +package models + +// PodcastCountersModel 节目统计数据 +type PodcastCountersModel struct { + Id int64 `gorm:"id"` // 节目id + PlayCount int64 `gorm:"play_count"` // 单集播放数量汇总 + CommentCount int64 `gorm:"comment_count"` // 单集评论数量汇总 + BarrageCount int64 `gorm:"barrage_count"` // 单集弹幕数量汇总 + SubCount int64 `gorm:"sub_count"` // 订阅数 +} diff --git a/example/internal/data/db/podcasts_model.go b/example/internal/data/db/podcasts_model.go new file mode 100644 index 0000000..1518c1a --- /dev/null +++ b/example/internal/data/db/podcasts_model.go @@ -0,0 +1,15 @@ +// Code generated by ZelusCtl. DO NOT EDIT. + +package models + +import "time" + +// PodcastsModel 节目详情表 +type PodcastsModel struct { + Id int64 `gorm:"id"` // 唯一主键id + CreateTime time.Time `gorm:"create_time"` // 创建时间 + UpdateTime time.Time `gorm:"update_time"` // 更新时间 + Name string `gorm:"name"` // 节目名称 + Subtitle string `gorm:"subtitle"` // 节目子标题 + DescriptionGuid string `gorm:"description_guid"` // 节目描述guid +} diff --git a/example/internal/data/db/recent_play_episode_user_relations_model.go b/example/internal/data/db/recent_play_episode_user_relations_model.go new file mode 100644 index 0000000..c95fbec --- /dev/null +++ b/example/internal/data/db/recent_play_episode_user_relations_model.go @@ -0,0 +1,14 @@ +// Code generated by ZelusCtl. DO NOT EDIT. + +package models + +import "time" + +// RecentPlayEpisodeUserRelationsModel 用于描述用户最近播放的单集与用户的关系 +type RecentPlayEpisodeUserRelationsModel struct { + Id int64 `gorm:"id"` // 唯一主键id + UserId int64 `gorm:"user_id"` // 用户id + EpisodeId int64 `gorm:"episode_id"` // 单集id + CreateTime time.Time `gorm:"create_time"` // 创建时间 + UpdateTime time.Time `gorm:"update_time"` // 更新时间 +} diff --git a/example/internal/data/db/sub_podcast_user_relations_model.go b/example/internal/data/db/sub_podcast_user_relations_model.go new file mode 100644 index 0000000..37df718 --- /dev/null +++ b/example/internal/data/db/sub_podcast_user_relations_model.go @@ -0,0 +1,14 @@ +// Code generated by ZelusCtl. DO NOT EDIT. + +package models + +import "time" + +// SubPodcastUserRelationsModel 用于描述用户与订阅的节目关系 +type SubPodcastUserRelationsModel struct { + Id int64 `gorm:"id"` // 主键id + UserId int64 `gorm:"user_id"` // 用户id + PodcastId int64 `gorm:"podcast_id"` // 节目id + UpdateTime time.Time `gorm:"update_time"` // 更新时间 + CreateTime time.Time `gorm:"create_time"` // 创建时间 +} diff --git a/example/internal/data/db/user_binds_model.go b/example/internal/data/db/user_binds_model.go new file mode 100644 index 0000000..2a04790 --- /dev/null +++ b/example/internal/data/db/user_binds_model.go @@ -0,0 +1,15 @@ +// Code generated by ZelusCtl. DO NOT EDIT. + +package models + +import "time" + +// UserBindsModel 描述用户id与绑定手机、wx等关系 +type UserBindsModel struct { + Id int64 `gorm:"id"` // 唯一主键id + UserId int64 `gorm:"user_id"` // 唯一用户id + BindType int64 `gorm:"bind_type"` // 绑定类型 + BindCode string `gorm:"bind_code"` // 绑定的值 + CreateTime time.Time `gorm:"create_time"` // 创建时间 + UpdateTime time.Time `gorm:"update_time"` // 更新时间 +} diff --git a/example/internal/data/users_model.go b/example/internal/data/db/users_model.go similarity index 100% rename from example/internal/data/users_model.go rename to example/internal/data/db/users_model.go