package district

import (
	"context"
	"git.noahlan.cn/n-admin/n-admin-server/rpc/core/ent/district"
	"git.noahlan.cn/n-admin/n-admin-server/rpc/core/internal/utils/hander"

	"git.noahlan.cn/n-admin/n-admin-server/rpc/core/internal/svc"
	"git.noahlan.cn/n-admin/n-admin-server/rpc/core/types/core"
)

type GetDistrictLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetDistrictLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetDistrictLogic {
	return &GetDistrictLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

// District management
func (l *GetDistrictLogic) GetDistrict(in *core.DistrictCodeReq) (*core.DistrictInfo, error) {
	dbData, err := l.svcCtx.DB.District.Query().
		Where(district.CodeEQ(in.Code)).
		First(l.ctx)
	if err != nil {
		return nil, hander.HandleEntErr(err, in)
	}

	return l.svcCtx.Convert.ConvertDistrictRpc(l.ctx, dbData), nil
}
