Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Override decorator doesn't work on base class #21

Closed
Diluka opened this issue Jan 11, 2019 · 6 comments
Closed

Override decorator doesn't work on base class #21

Diluka opened this issue Jan 11, 2019 · 6 comments

Comments

@Diluka
Copy link
Contributor

Diluka commented Jan 11, 2019

because of this

Object.getOwnPropertyNames(prototype).forEach((name) => {

I can't put override method on base controller. It needs be overridden in crud decorated class.

@michaelyali
Copy link
Member

The point of creating @Crud() decorator was to provide a possibility to have CRUD actions without any inheritance. So why do you create a base controller?

@Diluka
Copy link
Contributor Author

Diluka commented Jan 14, 2019

since the lib doesn't support paging right now.
first, I try to override controller method to support it. I found every controller has the same code. then I try to use base controller, but failed.
after that, I try to override RepositoryService import from the lib (this is hack). then I found I created controllers all with a blank. I try to use forFeature but failed again #22 .

so why I need create controller with @Crud? all the crud requests shall be handled with GenericCrudController inside the lib.

If someone need override method, setup a route handle before the GenericCrudController and GenericCrudController skip the handled request. What do you think of this?

@michaelyali
Copy link
Member

By saying that the lib doesn't support paging right now, what do you particular mean?

@Diluka
Copy link
Contributor Author

Diluka commented Jan 15, 2019

I mean until #16 merged, I need to use getManyAndCount instead of getMany. But even if #16 has been merged, I still need format result like

{
    "list": [
        {
            "id": 1,
            "username": "player1"
        }
    ],
    "total": 1,
    "pages": 1,
    "size": 1,
    "pageNum": 1,
    "pageSize": 2,
    "startRow": 0,
    "endRow": 0
}

Now I am doing this.

export class PageInfo<T> {

  pageNum: number;
  pageSize: number;
  size: number;

  startRow: number;
  endRow: number;

  total: number;
  pages: number;

  list: T[];

  constructor(page: [T[], number], query: RequestParamsParsed) {
    const [list, total] = page;

    this.list = list;
    this.total = total;
    this.pages = Math.floor((total - 1) / query.limit) + 1;
    this.size = list.length;

    const offset = query.offset || (query.page - 1) * query.limit;

    this.pageNum = query.page;
    this.pageSize = query.limit;
    this.startRow = offset;
    this.endRow = offset + this.size - 1;
  }
}

RepositoryService.prototype.getMany = async function(query: RequestParamsParsed = {}, options: RestfulOptions = {}) {
  const builder = await this.buildQuery(query, options);
  if (query.page && query.limit) {
    const page = await builder.getManyAndCount();
    return new PageInfo(page, query);
  }
  return builder.getMany();
};

so I am finding a common way to override all the getMany method in controllers. that's why I need a base controller

@michaelyali
Copy link
Member

ok, I got your point. So as I said here I'll do this improvement in the near future.

@Diluka
Copy link
Contributor Author

Diluka commented Jan 16, 2019

I will look forward to it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants