Files
epic-ent/internal/repository/hero_repo.go

224 lines
4.9 KiB
Go

package repository
import (
"context"
"database/sql"
"time"
"github.com/pkg/errors"
"epic-ent/internal/domain/dto"
"epic-ent/internal/domain/vo"
"epic-ent/internal/ent"
"epic-ent/internal/ent/epicheroinfo"
)
type HeroRepository struct {
client *ent.Client
}
func NewHeroRepository(client *ent.Client) *HeroRepository {
return &HeroRepository{client: client}
}
func (r *HeroRepository) Create(ctx context.Context, req dto.HeroCreateRequest) (vo.Hero, error) {
create := r.client.EpicHeroInfo.Create().
SetHeroName(valueOrEmpty(req.HeroName)).
SetHeroCode(valueOrEmpty(req.HeroCode)).
SetHeroAttrLv60(valueOrEmpty(req.HeroAttrLv60)).
SetCreator(valueOrEmpty(req.Creator)).
SetUpdater(valueOrEmpty(req.Updater)).
SetDeleted(valueOrFalse(req.Deleted)).
SetNickName(valueOrEmpty(req.NickName)).
SetRarity(valueOrEmpty(req.Rarity)).
SetRole(valueOrEmpty(req.Role)).
SetZodiac(valueOrEmpty(req.Zodiac)).
SetHeadImgURL(valueOrEmpty(req.HeadImgURL)).
SetAttribute(valueOrEmpty(req.Attribute)).
SetRemark(valueOrEmpty(req.Remark)).
SetRawJSON(valueOrEmpty(req.RawJSON)).
SetContentJSONSet(valueOrEmpty(req.ContentJSONSet))
if req.CreateTime != nil {
create.SetCreateTime(*req.CreateTime)
}
if req.UpdateTime != nil {
create.SetUpdateTime(*req.UpdateTime)
}
if req.UpdateTimeSet != nil {
create.SetUpdateTimeSet(*req.UpdateTimeSet)
}
hero, err := create.Save(ctx)
if err != nil {
return vo.Hero{}, errors.Wrap(err, "create hero")
}
return toVO(hero), nil
}
func (r *HeroRepository) GetByID(ctx context.Context, id int64) (vo.Hero, error) {
hero, err := r.client.EpicHeroInfo.Query().
Where(
epicheroinfo.IDEQ(id),
epicheroinfo.DeletedEQ(false),
).
Only(ctx)
if err != nil {
if ent.IsNotFound(err) {
return vo.Hero{}, sql.ErrNoRows
}
return vo.Hero{}, errors.Wrap(err, "get hero by id")
}
return toVO(hero), nil
}
func (r *HeroRepository) Update(ctx context.Context, id int64, req dto.HeroUpdateRequest) (vo.Hero, error) {
updated := false
update := r.client.EpicHeroInfo.Update().
Where(
epicheroinfo.IDEQ(id),
epicheroinfo.DeletedEQ(false),
)
if req.HeroName != nil {
update.SetHeroName(*req.HeroName)
updated = true
}
if req.HeroCode != nil {
update.SetHeroCode(*req.HeroCode)
updated = true
}
if req.HeroAttrLv60 != nil {
update.SetHeroAttrLv60(*req.HeroAttrLv60)
updated = true
}
if req.Updater != nil {
update.SetUpdater(*req.Updater)
updated = true
}
if req.UpdateTime != nil {
update.SetUpdateTime(*req.UpdateTime)
updated = true
}
if req.Deleted != nil {
update.SetDeleted(*req.Deleted)
updated = true
}
if req.NickName != nil {
update.SetNickName(*req.NickName)
updated = true
}
if req.Rarity != nil {
update.SetRarity(*req.Rarity)
updated = true
}
if req.Role != nil {
update.SetRole(*req.Role)
updated = true
}
if req.Zodiac != nil {
update.SetZodiac(*req.Zodiac)
updated = true
}
if req.HeadImgURL != nil {
update.SetHeadImgURL(*req.HeadImgURL)
updated = true
}
if req.Attribute != nil {
update.SetAttribute(*req.Attribute)
updated = true
}
if req.Remark != nil {
update.SetRemark(*req.Remark)
updated = true
}
if req.RawJSON != nil {
update.SetRawJSON(*req.RawJSON)
updated = true
}
if req.ContentJSONSet != nil {
update.SetContentJSONSet(*req.ContentJSONSet)
updated = true
}
if req.UpdateTimeSet != nil {
update.SetUpdateTimeSet(*req.UpdateTimeSet)
updated = true
}
if !updated {
return r.GetByID(ctx, id)
}
if req.UpdateTime == nil {
update.SetUpdateTime(time.Now())
}
affected, err := update.Save(ctx)
if err != nil {
return vo.Hero{}, errors.Wrap(err, "update hero")
}
if affected == 0 {
return vo.Hero{}, sql.ErrNoRows
}
return r.GetByID(ctx, id)
}
func (r *HeroRepository) Delete(ctx context.Context, id int64) error {
affected, err := r.client.EpicHeroInfo.Update().
Where(
epicheroinfo.IDEQ(id),
epicheroinfo.DeletedEQ(false),
).
SetDeleted(true).
SetUpdateTime(time.Now()).
Save(ctx)
if err != nil {
return errors.Wrap(err, "delete hero")
}
if affected == 0 {
return sql.ErrNoRows
}
return nil
}
func toVO(hero *ent.EpicHeroInfo) vo.Hero {
return vo.Hero{
ID: hero.ID,
HeroName: hero.HeroName,
HeroCode: hero.HeroCode,
HeroAttrLv60: hero.HeroAttrLv60,
Creator: hero.Creator,
CreateTime: hero.CreateTime,
Updater: hero.Updater,
UpdateTime: hero.UpdateTime,
Deleted: hero.Deleted,
NickName: hero.NickName,
Rarity: hero.Rarity,
Role: hero.Role,
Zodiac: hero.Zodiac,
HeadImgURL: hero.HeadImgURL,
Attribute: hero.Attribute,
Remark: hero.Remark,
RawJSON: hero.RawJSON,
ContentJSONSet: hero.ContentJSONSet,
UpdateTimeSet: hero.UpdateTimeSet,
}
}
func valueOrEmpty(v *string) string {
if v == nil {
return ""
}
return *v
}
func valueOrFalse(v *bool) bool {
if v == nil {
return false
}
return *v
}