add initial application structure with configuration, logging, and health check endpoints
This commit is contained in:
218
internal/repository/hero_repo.go
Normal file
218
internal/repository/hero_repo.go
Normal file
@@ -0,0 +1,218 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"time"
|
||||
|
||||
"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)).
|
||||
SetSetContentJSON(valueOrEmpty(req.SetContentJSON))
|
||||
|
||||
if req.CreateTime != nil {
|
||||
create.SetCreateTime(*req.CreateTime)
|
||||
}
|
||||
if req.UpdateTime != nil {
|
||||
create.SetUpdateTime(*req.UpdateTime)
|
||||
}
|
||||
if req.SetUpdateTime != nil {
|
||||
create.SetSetUpdateTime(*req.SetUpdateTime)
|
||||
}
|
||||
|
||||
hero, err := create.Save(ctx)
|
||||
if err != nil {
|
||||
return vo.Hero{}, err
|
||||
}
|
||||
|
||||
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 {
|
||||
return vo.Hero{}, err
|
||||
}
|
||||
|
||||
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.SetContentJSON != nil {
|
||||
update.SetSetContentJSON(*req.SetContentJSON)
|
||||
updated = true
|
||||
}
|
||||
if req.SetUpdateTime != nil {
|
||||
update.SetSetUpdateTime(*req.SetUpdateTime)
|
||||
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{}, err
|
||||
}
|
||||
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 err
|
||||
}
|
||||
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,
|
||||
SetContentJSON: hero.SetContentJSON,
|
||||
SetUpdateTime: hero.SetUpdateTime,
|
||||
}
|
||||
}
|
||||
|
||||
func valueOrEmpty(v *string) string {
|
||||
if v == nil {
|
||||
return ""
|
||||
}
|
||||
return *v
|
||||
}
|
||||
|
||||
func valueOrFalse(v *bool) bool {
|
||||
if v == nil {
|
||||
return false
|
||||
}
|
||||
return *v
|
||||
}
|
||||
@@ -5,5 +5,6 @@ import "go.uber.org/fx"
|
||||
var Module = fx.Options(
|
||||
fx.Provide(
|
||||
NewHealthRepository,
|
||||
NewHeroRepository,
|
||||
),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user