import I<%= modelName %>Service from "@interfaces/service/I<%= modelName %>Service";
import I<%= modelName %> from "@models/entities/<%= modelName %>";
import { <%= modelName %>ServiceImpl } from "@service/<%= modelName %>ServiceImpl";
import { Controller, Get, Route, Query, Delete, Put, Body, Post, Response, SuccessResponse, Tags } from 'tsoa';
import { ProvideSingleton, inject } from '@ioc/ioc';
import ServiceError from "@common/exception/ServiceError";


/**
* Controller <%= modelName %>Controller
*/
@Route('/<%= modelVar %>')
@ProvideSingleton(<%= modelName %>Controller)
@Tags("<%= modelName %>")
export class <%= modelName %>Controller extends Controller {

  constructor(@inject(<%= modelName %>ServiceImpl) private <%= modelVar %>Service: I<%= modelName %>Service) {
		super();
	}


  /**
   * Save a new <%= modelName %>. The identifier of the entity isn't mandatory so you can leave it undefined (not written). 
   * If you leave it undefined, the 'DAO' layer will generate and assign an ID to you entity automatically.
   */
  @Response<{ message: string }>(400, "Bad request")
  @SuccessResponse("200", "Request Success")
  @Post('/save')
  public async save(@Body() <%= modelVar %>: I<%= modelName %>): Promise<I<%= modelName %>> {
    //Do some business logic...
    try {
			return await this.<%= modelVar %>Service.save(<%= modelVar %>);
		} catch (err) {
      if (err instanceof ServiceError) {
        //Do the business logic error you want...
        return Promise.reject(err.message);
      } else {
        return Promise.reject("Unknown error");
      }
    }
  }




  /**
   * Return a <%= modelName %> by its ID.
   */
  @Response<{ message: string }>(400, "Bad request")
  @SuccessResponse("200", "Request Success")
  @Get('/getById')
  public async getById(@Query() id: string): Promise<I<%= modelName %>>  {
    //Do some business logic...
    try {
			return await this.<%= modelVar %>Service.getById(id);
		} catch (err) {
      if (err instanceof ServiceError) {
        //Do the business logic error you want...
        return Promise.reject(err.message);
      } else {
        return Promise.reject("Unknown error");
      }
    }
  }

  /**
  * Return a set of <%= modelName %>. Page starts at 0.
  */
 @Response<{ message: string }>(400, "Bad request")
 @SuccessResponse("200", "Request Success")
 @Get("/get")
 public async get(@Query() page: number,@Query() size:number): Promise<I<%= modelName %>[]> {
   //Do some business logic...
   try {
     return await this.<%= modelVar %>Service.get(page,size);
   } catch (err) {
     if (err instanceof ServiceError) {
       //Do the business logic error you want...
       return Promise.reject(err.message);
     } else {
       return Promise.reject("Unknown error");
     }
   }
 }


  /**
   * Return all <%= modelName %>.
   */
  @Response<{ message: string }>(400, "Bad request")
  @SuccessResponse("200", "Request Success")
  @Get('/getAll')
  public async getAll(): Promise<I<%= modelName %>[]>  {
    //Do some business logic...
    try {
      return await this.<%= modelVar %>Service.getAll();
    } catch (err) {
      if (err instanceof ServiceError) {
        //Do the business logic error you want...
        return Promise.reject(err.message);
      } else {
        return Promise.reject("Unknown error");
      }
    }
  }


  /**
   * Delete a <%= modelName %> by its ID.
   */
  @Response<{ message: string }>(400, "Bad request")
  @SuccessResponse("200", "Success")
  @Delete('/deleteById')
  public async deleteById(@Query() id: string): Promise<I<%= modelName %>>  {
    try {
			return await this.<%= modelVar %>Service.deleteById(id);
		} catch (err) {
      if (err instanceof ServiceError) {
        //Do the business logic error you want...
        return Promise.reject(err.message);
      } else {
        return Promise.reject("Unknown error");
      }
    }
  }



  /**
   * Update a <%= modelName %> by its ID.
   */
  @Response<{ message: string }>(400, "Bad request")
  @SuccessResponse("200", "Request Success")
  @Put('/update')
  public async update(@Body() <%= modelVar %>: I<%= modelName %>): Promise<I<%= modelName %>> {

    //Do some business logic...
    try {
			return await this.<%= modelVar %>Service.update(<%= modelVar %>);
		} catch (err) {
      if (err instanceof ServiceError) {
        //Do the business logic error you want...
        return Promise.reject(err.message);
      } else {
        return Promise.reject("Unknown error");
      }
    }
  }
}
