-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Rabbbit
committed
Jan 2, 2021
1 parent
b39467c
commit a8601e9
Showing
26 changed files
with
1,358 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
146
example/internal/biz/repos/like_episode_user_relations_repo.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.