import { Request, Response } from "express";
import { AbstractController } from "../abstract.controller";
import { SocialNetworkProvidersModel } from "../../models/social-network/social-network-provider.model";
import { SocialNetworkProvidersMethods } from "../../methods/social-network/social-network-provider.method";

export class SocialNetworkProvidersController extends AbstractController {
  methods: SocialNetworkProvidersMethods;
  constructor() {
    super(SocialNetworkProvidersModel);
    this.methods = new SocialNetworkProvidersMethods(
      SocialNetworkProvidersModel
    );
  }
  /**
   *
   * @param {*} req
   * @param {*} res
   */
  async getItems(req: Request, res: Response, next) {
    try {
      const { filter, sort } = this.params.aqp(req.query);
      const [, options] = this.params.optionsPaginate(req);
      let aggregate = this.model.aggregate([
        {
          $match: filter,
        },
        {
          $match: { deleted: false },
        },
      ]);
      const data = await this.model.aggregatePaginate(
        { ...aggregate },
        {
          ...options,
          sort,
        }
      );
      res.send(data);
    } catch (e) {
      this.params.handleHttpError(res, e);
    }
  }
}
