Sto cercando di scrivere un servizio angolare per interfacciare con un'API RESTful. Per semplicità, supponiamo che l'API sia
+ Company
|___+ Department
| |____ Person
|
|____ Person
Notice how person can be under
Company > Department
or directly underCompany
. Each of the entities (Company, Department and Person) support add, edit, list and get_by_id.
Quale delle seguenti interfacce è più idiomatica?
Opzione 1:
// In all cases, get(), put() return $http promise
companyApi().get() // List all companies
companyApi(1).get() // Get company with ID 1
companyApi(1).departments().get() // List all departments
companyApi(1).departments(2).get() // Get department with ID 2
companyApi(1).departments(2).persons().get() // List all persons
companyApi(1).departments(2).persons().put(p) //Add a new person in department 2
companyApi(1).persons().put(p) //Add a new person in company 1
companyApi(1).persons(3).put(p) //Edit person with ID=3 in company 1
companyApi(1).persons(1).remove(p) // Delete a person
Opzione 2:
// In all cases, get(), put() return $http promise
// List all companies
companyApi({
type: 'company'
}).get();
// Get company with ID=1
companyApi({
type: 'company',
companyId: 1
}).get();
// Get department with ID=2 under company 1
companyApi({
type: 'department',
companyId: 1,
departmentId: 2
}).get();
// List persons under department with ID=2 under company 1
companyApi({
type: 'person',
companyId: 1,
departmentId: 2
}).get();
// Get person with ID=3 under department with ID=2 under company 1
companyApi({
type: 'person',
companyId: 1,
departmentId: 2,
personId: 3
}).get();
// Get person with ID=3 under department with ID=2 under company 1
companyApi({
type: 'person',
companyId: 1,
departmentId: 2,
personId: 3
}).get();
// Add person under department with ID=2 under company 1
companyApi({
type: 'person',
companyId: 1,
departmentId: 2,
}).put(personObj);
//Edit person with ID=3 in company 1
companyApi({
type: 'person',
companyId: 1,
departmentId: 2,
personId: 3
}).put(personObj);
// Add person under directly under company 1
companyApi({
type: 'person',
companyId: 1
}).put(personObj);