-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add has one relation for todo-list example
- Loading branch information
Showing
11 changed files
with
247 additions
and
8 deletions.
There are no files selected for viewing
122 changes: 122 additions & 0 deletions
122
examples/todo-list/src/controllers/author.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import { | ||
Count, | ||
CountSchema, | ||
Filter, | ||
repository, | ||
Where, | ||
} from '@loopback/repository'; | ||
import { | ||
post, | ||
param, | ||
get, | ||
getFilterSchemaFor, | ||
getWhereSchemaFor, | ||
patch, | ||
del, | ||
requestBody, | ||
} from '@loopback/rest'; | ||
import {Author} from '../models'; | ||
import {AuthorRepository} from '../repositories'; | ||
|
||
export class AuthorController { | ||
constructor( | ||
@repository(AuthorRepository) | ||
public authorRepository : AuthorRepository, | ||
) {} | ||
|
||
@post('/authors', { | ||
responses: { | ||
'200': { | ||
description: 'Author model instance', | ||
content: {'application/json': {schema: {'x-ts-type': Author}}}, | ||
}, | ||
}, | ||
}) | ||
async create(@requestBody() author: Author): Promise<Author> { | ||
return await this.authorRepository.create(author); | ||
} | ||
|
||
@get('/authors/count', { | ||
responses: { | ||
'200': { | ||
description: 'Author model count', | ||
content: {'application/json': {schema: CountSchema}}, | ||
}, | ||
}, | ||
}) | ||
async count( | ||
@param.query.object('where', getWhereSchemaFor(Author)) where?: Where, | ||
): Promise<Count> { | ||
return await this.authorRepository.count(where); | ||
} | ||
|
||
@get('/authors', { | ||
responses: { | ||
'200': { | ||
description: 'Array of Author model instances', | ||
content: { | ||
'application/json': { | ||
schema: {type: 'array', items: {'x-ts-type': Author}}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
async find( | ||
@param.query.object('filter', getFilterSchemaFor(Author)) filter?: Filter, | ||
): Promise<Author[]> { | ||
return await this.authorRepository.find(filter); | ||
} | ||
|
||
@patch('/authors', { | ||
responses: { | ||
'200': { | ||
description: 'Author PATCH success count', | ||
content: {'application/json': {schema: CountSchema}}, | ||
}, | ||
}, | ||
}) | ||
async updateAll( | ||
@requestBody() author: Author, | ||
@param.query.object('where', getWhereSchemaFor(Author)) where?: Where, | ||
): Promise<Count> { | ||
return await this.authorRepository.updateAll(author, where); | ||
} | ||
|
||
@get('/authors/{id}', { | ||
responses: { | ||
'200': { | ||
description: 'Author model instance', | ||
content: {'application/json': {schema: {'x-ts-type': Author}}}, | ||
}, | ||
}, | ||
}) | ||
async findById(@param.path.string('id') id: string): Promise<Author> { | ||
return await this.authorRepository.findById(id); | ||
} | ||
|
||
@patch('/authors/{id}', { | ||
responses: { | ||
'204': { | ||
description: 'Author PATCH success', | ||
}, | ||
}, | ||
}) | ||
async updateById( | ||
@param.path.string('id') id: string, | ||
@requestBody() author: Author, | ||
): Promise<void> { | ||
await this.authorRepository.updateById(id, author); | ||
} | ||
|
||
@del('/authors/{id}', { | ||
responses: { | ||
'204': { | ||
description: 'Author DELETE success', | ||
}, | ||
}, | ||
}) | ||
async deleteById(@param.path.string('id') id: string): Promise<void> { | ||
await this.authorRepository.deleteById(id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
examples/todo-list/src/controllers/todo-list-author.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import {TodoListRepository} from '../repositories'; | ||
import {repository, Filter} from '@loopback/repository'; | ||
import {param, post, requestBody, get} from '@loopback/rest'; | ||
import {Author} from '../models'; | ||
|
||
export class TodoListAuthorController { | ||
constructor( | ||
@repository(TodoListRepository) protected todoListRepo: TodoListRepository, | ||
) {} | ||
|
||
@post('/todo-lists/{id}/author', { | ||
responses: { | ||
'200': { | ||
description: 'create Author model instance', | ||
content: {'application/json': {schema: {'x-ts-type': Author}}}, | ||
}, | ||
}, | ||
}) | ||
async create( | ||
@param.path.number('id') id: number, | ||
@requestBody() author: Author, | ||
): Promise<Author> { | ||
return await this.todoListRepo.author(id).create(author); | ||
} | ||
|
||
@get('/todo-lists/{id}/author', { | ||
responses: { | ||
'200': { | ||
description: 'The author belonging to the TodoList', | ||
content: {'application/json': {schema: {'x-ts-type': Author}}}, | ||
}, | ||
}, | ||
}) | ||
async find(@param.path.number('id') id: number): Promise<Author | undefined> { | ||
return await this.todoListRepo.author(id).get(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import {Entity, model, property, belongsTo} from '@loopback/repository'; | ||
import {TodoList} from './todo-list.model'; | ||
|
||
@model() | ||
export class Author extends Entity { | ||
@belongsTo( | ||
() => TodoList, | ||
{}, | ||
{ | ||
id: true, | ||
generated: false, | ||
type: 'string', | ||
}, | ||
) | ||
todoListId: string; | ||
|
||
@property({ | ||
type: 'string', | ||
required: true, | ||
}) | ||
name: string; | ||
|
||
constructor(data?: Partial<Author>) { | ||
super(data); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ | |
|
||
export * from './todo.model'; | ||
export * from './todo-list.model'; | ||
export * from './author.model'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { | ||
DefaultCrudRepository, | ||
juggler, | ||
repository, | ||
BelongsToAccessor, | ||
} from '@loopback/repository'; | ||
import {Author, TodoList} from '../models'; | ||
import {DbDataSource} from '../datasources'; | ||
import {inject, Getter} from '@loopback/core'; | ||
import {TodoListRepository} from './todo-list.repository'; | ||
|
||
export class AuthorRepository extends DefaultCrudRepository< | ||
Author, | ||
typeof Author.prototype.todoListId | ||
> { | ||
public readonly todoList: BelongsToAccessor< | ||
TodoList, | ||
typeof Author.prototype.todoListId | ||
>; | ||
constructor( | ||
@inject('datasources.db') dataSource: DbDataSource, | ||
@repository.getter('TodoListRepository') | ||
protected todoListRepositoryGetter: Getter<TodoListRepository>, | ||
) { | ||
super(Author, dataSource); | ||
this.todoList = this._createBelongsToAccessorFor( | ||
'todoList', | ||
todoListRepositoryGetter, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ | |
|
||
export * from './todo.repository'; | ||
export * from './todo-list.repository'; | ||
export * from './author.repository'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters