Skip to content

Commit

Permalink
add model some document
Browse files Browse the repository at this point in the history
  • Loading branch information
Rabbbit committed Jan 2, 2021
1 parent b39467c commit a8601e9
Show file tree
Hide file tree
Showing 26 changed files with 1,358 additions and 18 deletions.
20 changes: 12 additions & 8 deletions cli/actions/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
22 changes: 13 additions & 9 deletions cli/actions/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 3 additions & 1 deletion cli/codegen/po_model_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
25 changes: 25 additions & 0 deletions example/internal/biz/model/user.go
Original file line number Diff line number Diff line change
@@ -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 {
}
}
76 changes: 76 additions & 0 deletions example/internal/biz/repos/episode_counters_repo.go
Original file line number Diff line number Diff line change
@@ -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
}
77 changes: 77 additions & 0 deletions example/internal/biz/repos/episodes_repo.go
Original file line number Diff line number Diff line change
@@ -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
}
146 changes: 146 additions & 0 deletions example/internal/biz/repos/like_episode_user_relations_repo.go
Original file line number Diff line number Diff line change
@@ -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
}
Loading

0 comments on commit a8601e9

Please sign in to comment.