-
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.
feat(todo-intermediate): add TodoList
- Loading branch information
Showing
10 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
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
67 changes: 67 additions & 0 deletions
67
examples/todo-intermediate/src/controllers/todo-list.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,67 @@ | ||
import {repository, HasManyRepository} from '@loopback/repository'; | ||
import {TodoListRepository} from '../repositories'; | ||
import {TodoList, Todo} from '../models'; | ||
import {requestBody, param, post, get, del, put, patch} from '@loopback/rest'; | ||
|
||
export class TodoListController { | ||
protected todoRepo: HasManyRepository<TodoList>; | ||
|
||
constructor( | ||
@repository(TodoListRepository) protected todoListRepo: TodoListRepository, | ||
) {} | ||
|
||
@post('/todo-lists') | ||
async createTodoList() { | ||
const newTodoList = {createdAt: new Date()}; | ||
return await this.todoListRepo.create(newTodoList); | ||
} | ||
|
||
@get('/todo-lists/{id}') | ||
async findTodoListById(@param.path.number('id') id: number) { | ||
return await this.todoListRepo.findById(id); | ||
} | ||
|
||
@get('/todo-lists') | ||
async findTodos() { | ||
return await this.todoListRepo.find(); | ||
} | ||
|
||
@put('/todo-lists/{id}') | ||
async replaceTodo( | ||
@param.path.number('id') id: number, | ||
@requestBody() todoList: TodoList, | ||
) { | ||
return await this.todoListRepo.replaceById(id, todoList); | ||
} | ||
|
||
@patch('/todo-lists/{id}') | ||
async updateTodo( | ||
@param.path.number('id') id: number, | ||
@requestBody() todoList: TodoList, | ||
) { | ||
return await this.todoListRepo.updateById(id, todoList); | ||
} | ||
|
||
@del('/todo-lists/{id}') | ||
async deleteTodo(@param.path.number('id') id: number) { | ||
return await this.todoListRepo.deleteById(id); | ||
} | ||
|
||
@post('/todo-lists/{id}/todos') | ||
async createTodoForTodoList( | ||
@param.path.number('id') id: number, | ||
@requestBody() todo: Todo, | ||
) { | ||
return await this.todoListRepo.todos(id).create(todo); | ||
} | ||
|
||
@get('/todo-lists/{id}/todos') | ||
async findTodosOfTodoList(@param.path.number('id') id: number) { | ||
return await this.todoListRepo.todos(id).find(); | ||
} | ||
|
||
@del('/todo-lists/{id}/todos') | ||
async deleteTodosOfTodoList(@param.path.number('id') id: number) { | ||
return await this.todoListRepo.todos(id).delete(); | ||
} | ||
} |
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
6 changes: 6 additions & 0 deletions
6
examples/todo-intermediate/src/datasources/todo-list-db.datasource.json
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,6 @@ | ||
{ | ||
"name": "TodoListDb", | ||
"connector": "memory", | ||
"localStorage": "", | ||
"file": "" | ||
} |
14 changes: 14 additions & 0 deletions
14
examples/todo-intermediate/src/datasources/todo-list-db.datasource.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,14 @@ | ||
import {inject} from '@loopback/core'; | ||
import {juggler, DataSource, AnyObject} from '@loopback/repository'; | ||
const config = require('./todo-list-db.datasource.json'); | ||
|
||
export class TodoListDbDataSource extends juggler.DataSource { | ||
static dataSourceName = 'TodoListDb'; | ||
|
||
constructor( | ||
@inject('datasources.config.TodoListDb', {optional: true}) | ||
dsConfig: AnyObject = config | ||
) { | ||
super(dsConfig); | ||
} | ||
} |
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,10 @@ | ||
import {model, Entity, property, hasMany} from '@loopback/repository'; | ||
import {Todo} from './todo.model'; | ||
|
||
@model() | ||
export class TodoList extends Entity { | ||
@property({id: true}) | ||
id: number; | ||
@property() createdAt: Date; | ||
@hasMany(Todo) todos: Todo[]; | ||
} |
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
26 changes: 26 additions & 0 deletions
26
examples/todo-intermediate/src/repositories/todo-list.repository.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,26 @@ | ||
import { | ||
DefaultCrudRepository, | ||
juggler, | ||
HasManyRepositoryFactory, | ||
repository, | ||
} from '@loopback/repository'; | ||
import {TodoList, Todo} from '../models'; | ||
import {inject} from '@loopback/core'; | ||
import {TodoRepository} from './todo.repository'; | ||
|
||
export class TodoListRepository extends DefaultCrudRepository< | ||
TodoList, | ||
typeof TodoList.prototype.id | ||
> { | ||
public todos: HasManyRepositoryFactory<Todo, typeof TodoList.prototype.id>; | ||
constructor( | ||
@inject('datasources.TodoListDb') protected datasource: juggler.DataSource, | ||
@repository(TodoRepository) protected todoRepository: TodoRepository, | ||
) { | ||
super(TodoList, datasource); | ||
this.todos = this._createHasManyRepositoryFactoryFor( | ||
'todos', | ||
todoRepository, | ||
); | ||
} | ||
} |