Skip to content

Commit 30a8bfc

Browse files
aaqilnizdhmlau
authored andcommitted
fix: remove repeated code from controller in case of same table hasManyThrough relation
Signed-off-by: Muhammad Aaqil <[email protected]>
1 parent d1ddfc2 commit 30a8bfc

File tree

9 files changed

+375
-3
lines changed

9 files changed

+375
-3
lines changed

packages/cli/generators/relation/templates/controller-relation-template-has-many-through.ts.ejs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import {
1515
post,
1616
requestBody,
1717
} from '@loopback/rest';
18-
import {
19-
<%= sourceModelClassName %>,
18+
import {<%if (sourceModelClassName != targetModelClassName) { %>
19+
<%= sourceModelClassName %>,<% } %>
2020
<%= throughModelClassName %>,
2121
<%= targetModelClassName %>,
2222
} from '../models';

packages/cli/snapshots/integration/generators/relation.has-many-through.integration.snapshots.js

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,3 +701,203 @@ export class DoctorRepository extends DefaultCrudRepository<
701701
}
702702
703703
`;
704+
705+
706+
exports[`lb4 relation HasManyThrough generates model relation with same table answers {"relationType":"hasManyThrough","sourceModel":"User","destinationModel":"User","throughModel":"Friend"} controller file has been created with hasManyThrough relation with same table 1`] = `
707+
import {
708+
Count,
709+
CountSchema,
710+
Filter,
711+
repository,
712+
Where,
713+
} from '@loopback/repository';
714+
import {
715+
del,
716+
get,
717+
getModelSchemaRef,
718+
getWhereSchemaFor,
719+
param,
720+
patch,
721+
post,
722+
requestBody,
723+
} from '@loopback/rest';
724+
import {
725+
Friend,
726+
User,
727+
} from '../models';
728+
import {UserRepository} from '../repositories';
729+
730+
export class UserUserController {
731+
constructor(
732+
@repository(UserRepository) protected userRepository: UserRepository,
733+
) { }
734+
735+
@get('/users/{id}/users', {
736+
responses: {
737+
'200': {
738+
description: 'Array of User has many User through Friend',
739+
content: {
740+
'application/json': {
741+
schema: {type: 'array', items: getModelSchemaRef(User)},
742+
},
743+
},
744+
},
745+
},
746+
})
747+
async find(
748+
@param.path.number('id') id: number,
749+
@param.query.object('filter') filter?: Filter<User>,
750+
): Promise<User[]> {
751+
return this.userRepository.users(id).find(filter);
752+
}
753+
754+
@post('/users/{id}/users', {
755+
responses: {
756+
'200': {
757+
description: 'create a User model instance',
758+
content: {'application/json': {schema: getModelSchemaRef(User)}},
759+
},
760+
},
761+
})
762+
async create(
763+
@param.path.number('id') id: typeof User.prototype.id,
764+
@requestBody({
765+
content: {
766+
'application/json': {
767+
schema: getModelSchemaRef(User, {
768+
title: 'NewUserInUser',
769+
exclude: ['id'],
770+
}),
771+
},
772+
},
773+
}) user: Omit<User, 'id'>,
774+
): Promise<User> {
775+
return this.userRepository.users(id).create(user);
776+
}
777+
778+
@patch('/users/{id}/users', {
779+
responses: {
780+
'200': {
781+
description: 'User.User PATCH success count',
782+
content: {'application/json': {schema: CountSchema}},
783+
},
784+
},
785+
})
786+
async patch(
787+
@param.path.number('id') id: number,
788+
@requestBody({
789+
content: {
790+
'application/json': {
791+
schema: getModelSchemaRef(User, {partial: true}),
792+
},
793+
},
794+
})
795+
user: Partial<User>,
796+
@param.query.object('where', getWhereSchemaFor(User)) where?: Where<User>,
797+
): Promise<Count> {
798+
return this.userRepository.users(id).patch(user, where);
799+
}
800+
801+
@del('/users/{id}/users', {
802+
responses: {
803+
'200': {
804+
description: 'User.User DELETE success count',
805+
content: {'application/json': {schema: CountSchema}},
806+
},
807+
},
808+
})
809+
async delete(
810+
@param.path.number('id') id: number,
811+
@param.query.object('where', getWhereSchemaFor(User)) where?: Where<User>,
812+
): Promise<Count> {
813+
return this.userRepository.users(id).delete(where);
814+
}
815+
}
816+
817+
`;
818+
819+
820+
exports[`lb4 relation HasManyThrough generates model relation with same table answers {"relationType":"hasManyThrough","sourceModel":"User","destinationModel":"User","throughModel":"Friend"} has correct default foreign keys 1`] = `
821+
import {Entity, model, property} from '@loopback/repository';
822+
823+
@model()
824+
export class Friend extends Entity {
825+
@property({
826+
type: 'number',
827+
id: true,
828+
default: 0,
829+
})
830+
id?: number;
831+
832+
@property({
833+
type: 'number',
834+
})
835+
userId?: number;
836+
837+
@property({
838+
type: 'number',
839+
})
840+
friendId?: number;
841+
842+
constructor(data?: Partial<Friend>) {
843+
super(data);
844+
}
845+
}
846+
847+
`;
848+
849+
850+
exports[`lb4 relation HasManyThrough generates model relation with same table answers {"relationType":"hasManyThrough","sourceModel":"User","destinationModel":"User","throughModel":"Friend"} has correct imports and relation name users 1`] = `
851+
import {Entity, model, property, hasMany} from '@loopback/repository';
852+
import {Friend} from './friend.model';
853+
854+
@model()
855+
export class User extends Entity {
856+
@property({
857+
type: 'number',
858+
id: true,
859+
default: 0,
860+
})
861+
id?: number;
862+
863+
@property({
864+
type: 'string',
865+
})
866+
email?: string;
867+
868+
@hasMany(() => User, {through: {model: () => Friend}})
869+
users: User[];
870+
871+
constructor(data?: Partial<User>) {
872+
super(data);
873+
}
874+
}
875+
876+
`;
877+
878+
879+
exports[`lb4 relation HasManyThrough generates model relation with same table answers {"relationType":"hasManyThrough","sourceModel":"User","destinationModel":"User","throughModel":"Friend"} has correct imports and relation name users 2`] = `
880+
import {inject, Getter} from '@loopback/core';
881+
import {DefaultCrudRepository, repository, HasManyThroughRepositoryFactory} from '@loopback/repository';
882+
import {DbDataSource} from '../datasources';
883+
import {User, Friend} from '../models';
884+
import {FriendRepository} from './friend.repository';
885+
886+
export class UserRepository extends DefaultCrudRepository<
887+
User,
888+
typeof User.prototype.id
889+
> {
890+
891+
public readonly users: HasManyThroughRepositoryFactory<User, typeof User.prototype.id,
892+
Friend,
893+
typeof User.prototype.id
894+
>;
895+
896+
constructor(@inject('datasources.db') dataSource: DbDataSource, @repository.getter('FriendRepository') protected friendRepositoryGetter: Getter<FriendRepository>, @repository.getter('UserRepository') protected userRepositoryGetter: Getter<UserRepository>,) {
897+
super(User, dataSource);
898+
this.users = this.createHasManyThroughRepositoryFactoryFor('users', userRepositoryGetter, friendRepositoryGetter,);
899+
this.registerInclusionResolver('users', this.users.inclusionResolver);
900+
}
901+
}
902+
903+
`;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export class UserUserController {}

packages/cli/test/fixtures/relation/index.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,26 @@ const SourceEntries = {
162162
file: 'employee.controller.ts',
163163
content: readSourceFile('./controllers/employee.controller.ts'),
164164
},
165+
UserModel: {
166+
path: MODEL_APP_PATH,
167+
file: 'user.model.ts',
168+
content: readSourceFile('./models/user.model.ts'),
169+
},
170+
FriendModel: {
171+
path: MODEL_APP_PATH,
172+
file: 'friend.model.ts',
173+
content: readSourceFile('./models/friend.model.ts'),
174+
},
175+
UserRepository: {
176+
path: REPOSITORY_APP_PATH,
177+
file: 'user.repository.ts',
178+
content: readSourceFile('./repositories/user.repository.ts'),
179+
},
180+
FriendRepository: {
181+
path: REPOSITORY_APP_PATH,
182+
file: 'friend.repository.ts',
183+
content: readSourceFile('./repositories/friend.repository.ts'),
184+
},
165185
};
166186
exports.SourceEntries = SourceEntries;
167187

@@ -226,6 +246,10 @@ exports.SANDBOX_FILES = [
226246
SourceEntries.DoctorPatientController,
227247
SourceEntries.EmployeeModel,
228248
SourceEntries.EmployeeController,
249+
SourceEntries.UserModel,
250+
SourceEntries.FriendModel,
251+
SourceEntries.UserRepository,
252+
SourceEntries.FriendRepository,
229253
];
230254

231255
exports.SANDBOX_FILES2 = [
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import {Entity, model, property} from '@loopback/repository';
2+
3+
@model()
4+
export class Friend extends Entity {
5+
@property({
6+
type: 'number',
7+
id: true,
8+
default: 0,
9+
})
10+
id?: number;
11+
12+
@property({
13+
type: 'number',
14+
})
15+
userId?: number;
16+
17+
@property({
18+
type: 'number',
19+
})
20+
friendId?: number;
21+
22+
constructor(data?: Partial<Friend>) {
23+
super(data);
24+
}
25+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import {Entity, model, property} from '@loopback/repository';
2+
3+
@model()
4+
export class User extends Entity {
5+
@property({
6+
type: 'number',
7+
id: true,
8+
default: 0,
9+
})
10+
id?: number;
11+
12+
@property({
13+
type: 'string',
14+
})
15+
email?: string;
16+
17+
constructor(data?: Partial<User>) {
18+
super(data);
19+
}
20+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import {inject} from '@loopback/core';
2+
import {DefaultCrudRepository} from '@loopback/repository';
3+
import {DbDataSource} from '../datasources';
4+
import {Friend} from '../models';
5+
6+
export class FriendRepository extends DefaultCrudRepository<
7+
Friend,
8+
typeof Friend.prototype.id
9+
> {
10+
constructor(@inject('datasources.db') dataSource: DbDataSource) {
11+
super(Friend, dataSource);
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import {inject} from '@loopback/core';
2+
import {DefaultCrudRepository} from '@loopback/repository';
3+
import {DbDataSource} from '../datasources';
4+
import {User} from '../models';
5+
6+
export class UserRepository extends DefaultCrudRepository<
7+
User,
8+
typeof User.prototype.id
9+
> {
10+
constructor(@inject('datasources.db') dataSource: DbDataSource) {
11+
super(User, dataSource);
12+
}
13+
}

0 commit comments

Comments
 (0)