2016-03-13 45 views
0

我对如何构建我的路线感到困惑。与时间表我的应用程序处理,并包含三种型号:如何构建Rest API的路线

Employer 
Employer id 
Name and password 

Employee 
id 
Employer id (FK) 
Name and password 

Timesheet 
Employee id (FK) 
Employer id(FK) 
Timestamp 

我想根据员工ID来选择员工的时间表,并能够选择时间表为所有员工的工作使用给定的雇主雇主ID。在这两种情况下,我还希望能够将我选择的时间表限制在特定的一周。

这是我到目前为止有:

scope '/api' do 
scope '/v1' do 
    scope '/employees' do 
    get '/' => 'api_employees#index' 
    post '/' => 'api_employees#create' 
     scope '/:id' do 
     get '/' => 'api_employees#show' 
     put '/' => 'api_employees#update' 
      scope '/timesheets' do 
      get '/' => 'api_timesheets#index' 
      post '/' => 'api_timesheets#create' 
       scope '/:date' do 
       get '/' => 'api_timesheets#show' 
       put '/' => 'api_timesheets#update' 
       end 
      end 
     end 
    end 
end 
end 

我很困惑我是否应该叫“老板”有在它的员工和时间表整个新的领域,或者我应该把我现有的范围在雇主范围内,以避免重复。

回答

1

当我设计路线时,我想到了一个文件系统。例如,就你而言,时间表是文件,其ID是文件名。时间表保存在文件夹timesheets中。如果您是linux/unix用户,请将根路径视为/usr/share,其中资源只能由任何人阅读。

当我想搜索一些时间表时,我将搜索条件添加到查询字符串中,就像在文件系统中按CTRL + F一样。

当我想限制访问某些时间表,我把它们放在用户的home directory,其中有两种/employers/:employer_id/employees/:employee_id的名称(:xxx_id部分可以省略,如果你可以通过登录识别它们)。

所以路线看起来像

scope 'api' do 
scope 'v1' do 

    resources :timesheets, only: [:index, :show] 

    resources :employees, only: [:show, :update] do 
    resources :timesheets, only: [:index, :create] 
    end 

    resources :employers, only: [:show, :update] do 
    resources :timesheets, only: [:index] 
    end 

end 
end 

的“按日期查找”,可以在任何index行动来实现。