syntax = "v1"

info(
    title: "xx实例"
    desc: "xx实例"
    author: "author"
    email: "email"
    version: "v1"
)

@server(
    prefix: todo/v1
    group: todo
    jwt: JwtAuth
)
service Core {
	@doc  "详情"
    @handler todoGet
    get /todo/:id (TodoGetRequest) returns (TodoGetResponse)
    @doc  "保存"
	@handler todoSave
    post /todo (TodoSaveRequest) returns (TodoSaveResponse)
    @doc  "删除"
	@handler todoDelete
    delete /todo/:id (TodoDeleteRequest) returns (TodoDeleteResponse)
    @doc  "更新"
	@handler todoUpdate
    put /todo/:id (TodoUpdateRequest) returns (TodoUpdateResponse)
    @doc  "搜索"
	@handler todoSearch
    post /todo/search (TodoSearchRequest) returns (TodoSearchResponse)
}

type (
    TodoGetRequest {
		Id int64 `path:"id"`
	}
    TodoGetResponse struct{
		Todo TodoItem `json:"todo"`
    }

	TodoSaveRequest struct{
		Todo TodoItem `json:"todo"`
    }
    TodoSaveResponse struct{}

	TodoDeleteRequest struct{
        Id int64 `path:"id"`
    }
    TodoDeleteResponse struct{}

	TodoUpdateRequest struct{
		Id int64 `path:"id"`
        Todo TodoItem `json:"todo"`
    }
    TodoUpdateResponse struct{}

 	TodoSearchRequest struct{
         Page int  `json:"page"`
         Size int  `json:"size"`
    }
    TodoSearchResponse{
        List []TodoItem  `json:"list"`
        Total int64 `json:"total"`
    }
	TodoItem struct{
	
	}
)

// logic CRUD
// Save
	//var (
	//	conn = l.svcCtx.DefaultDBConn()
	//	dm   *domain.Todo
	//)
	//// 唯一判断

	//dm = NewDomainTodo(req.Todo)
	//if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error {
	//	dm, err = l.svcCtx.TodoRepository.Insert(l.ctx, conn, dm)
	//	return err
	//}, true); err != nil {
	//	return nil, xerr.NewErrMsg("保存失败")
	//}
	////resp = &types.TodoSaveResponse{}
	//return

//func NewDomainTodo(item types.TodoItem) *domain.Todo {
//	return &domain.Todo{

//	}
//}
//
//func NewTypesTodo(item *domain.Todo) types.TodoItem {
//	return types.TodoItem{
//		Id:     item.Id,
//	}
//}

// Get
	//var (
	//	conn = l.svcCtx.DefaultDBConn()
	//	dm   *domain.Todo
	//)
	//// 货号唯一
	//if dm, err = l.svcCtx.TodoRepository.FindOne(l.ctx, conn, req.Id); err != nil {
	//	return nil, xerr.NewErrMsgErr("不存在", err)
	//}
	//resp = &types.TodoGetResponse{
	//	Todo: NewTypesTodo(dm),
	//}
	//return

// Delete
	//var (
	//	conn = l.svcCtx.DefaultDBConn()
	//	dm   *domain.Todo
	//)
	//if dm, err = l.svcCtx.TodoRepository.FindOne(l.ctx, conn, req.Id); err != nil {
	//	return nil, xerr.NewErrMsgErr("不存在", err)
	//}
	//if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error {
	//	if dm, err = l.svcCtx.TodoRepository.Delete(l.ctx, conn, dm); err != nil {
	//		return err
	//	}
	//	return nil
	//}, true); err != nil {
	//	return nil, xerr.NewErrMsgErr("移除失败", err)
	//}
	//return

// Search
	//var (
	//	conn  = l.svcCtx.DefaultDBConn()
	//	dms   []*domain.Todo
	//	total int64
	//)
	//
	//queryOptions := domain.NewQueryOptions().WithOffsetLimit(req.Page, req.Size).
	//	WithKV("", "")

	//total, dms, err = l.svcCtx.TodoRepository.Find(l.ctx, conn, queryOptions)
	//list := make([]types.TodoItem, 0)
	//for i := range dms {
	//	list = append(list, NewTypesTodo(dms[i]))
	//}
	//resp = &types.TodoSearchResponse{
	//	List:  list,
	//	Total: total,
	//}
	//return

// Update
	//var (
	//	conn = l.svcCtx.DefaultDBConn()
	//	dm   *domain.Todo
	//)
	//if dm, err = l.svcCtx.TodoRepository.FindOne(l.ctx, conn, req.Id); err != nil {
	//	return nil, xerr.NewErrMsgErr("不存在", err)
	//}
	//// 不可编辑判断
	
	//// 赋值

	//// 更新
	//if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error {
	//	dm, err = l.svcCtx.TodoRepository.UpdateWithVersion(l.ctx, conn, dm)
	//	return err
	//}, true); err != nil {
	//	return nil, xerr.NewErrMsg("更新失败")
	//}
	//resp = &types.TodoUpdateResponse{}
	//return