Skip to content

Commit

Permalink
feat(todo-intermediate): add TodoList
Browse files Browse the repository at this point in the history
  • Loading branch information
shimks committed Jul 11, 2018
1 parent c3e28f8 commit 6fe307d
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/todo-intermediate/src/controllers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
// License text available at https://opensource.org/licenses/MIT

export * from './todo.controller';
export * from './todo-list.controller';
67 changes: 67 additions & 0 deletions examples/todo-intermediate/src/controllers/todo-list.controller.ts
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();
}
}
1 change: 1 addition & 0 deletions examples/todo-intermediate/src/datasources/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@

export * from './db.datasource';
export * from './geocoder.datasource';
export * from './todo-list-db.datasource';
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "TodoListDb",
"connector": "memory",
"localStorage": "",
"file": ""
}
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);
}
}
1 change: 1 addition & 0 deletions examples/todo-intermediate/src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
// License text available at https://opensource.org/licenses/MIT

export * from './todo.model';
export * from './todo-list.model';
10 changes: 10 additions & 0 deletions examples/todo-intermediate/src/models/todo-list.model.ts
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[];
}
2 changes: 2 additions & 0 deletions examples/todo-intermediate/src/models/todo.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export class Todo extends Entity {
})
remindAtGeo: string; // latitude,longitude

@property() todoListId: number;

getId() {
return this.id;
}
Expand Down
1 change: 1 addition & 0 deletions examples/todo-intermediate/src/repositories/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
// License text available at https://opensource.org/licenses/MIT

export * from './todo.repository';
export * from './todo-list.repository';
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,
);
}
}

0 comments on commit 6fe307d

Please sign in to comment.