Skip to content

Commit

Permalink
Revert old template per @jongio
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacrlevin committed Dec 1, 2023
1 parent f54b393 commit e806000
Show file tree
Hide file tree
Showing 7 changed files with 370 additions and 199 deletions.
159 changes: 159 additions & 0 deletions templates/todo/api/csharp/ListsController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
using Microsoft.AspNetCore.Mvc;

namespace SimpleTodo.Api;

[ApiController]
[Route("/lists")]
public class ListsController : ControllerBase
{
private readonly ListsRepository _repository;

public ListsController(ListsRepository repository)
{
_repository = repository;
}

[HttpGet]
[ProducesResponseType(200)]
public async Task<ActionResult<IEnumerable<TodoList>>> GetLists([FromQuery] int? skip = null, [FromQuery] int? batchSize = null)
{
return Ok(await _repository.GetListsAsync(skip, batchSize));
}

[HttpPost]
[ProducesResponseType(201)]
public async Task<ActionResult> CreateList([FromBody]CreateUpdateTodoList list)
{
var todoList = new TodoList(list.name)
{
Description = list.description
};

await _repository.AddListAsync(todoList);

return CreatedAtAction(nameof(GetList), new { list_id = todoList.Id }, todoList);
}

[HttpGet("{list_id}")]
[ProducesResponseType(200)]
[ProducesResponseType(404)]
public async Task<ActionResult<IEnumerable<TodoList>>> GetList(string list_id)
{
var list = await _repository.GetListAsync(list_id);

return list == null ? NotFound() : Ok(list);
}

[HttpPut("{list_id}")]
[ProducesResponseType(200)]
public async Task<ActionResult<TodoList>> UpdateList(string list_id, [FromBody]CreateUpdateTodoList list)
{
var existingList = await _repository.GetListAsync(list_id);
if (existingList == null)
{
return NotFound();
}

existingList.Name = list.name;
existingList.Description = list.description;
existingList.UpdatedDate = DateTimeOffset.UtcNow;

await _repository.UpdateList(existingList);

return Ok(existingList);
}

[HttpDelete("{list_id}")]
[ProducesResponseType(204)]
public async Task<ActionResult> DeleteList(string list_id)
{
if (await _repository.GetListAsync(list_id) == null)
{
return NotFound();
}

await _repository.DeleteListAsync(list_id);

return NoContent();
}

[HttpGet("{list_id}/items")]
[ProducesResponseType(200)]
public async Task<ActionResult<IEnumerable<TodoItem>>> GetListItems(string list_id, [FromQuery] int? skip = null, [FromQuery] int? batchSize = null)
{
return Ok(await _repository.GetListItemsAsync(list_id, skip, batchSize));
}

[HttpPost("{list_id}/items")]
[ProducesResponseType(201)]
public async Task<ActionResult<TodoItem>> CreateListItem(string list_id, [FromBody] CreateUpdateTodoItem item)
{
var newItem = new TodoItem(list_id, item.name)
{
Name = item.name,
Description = item.description,
State = item.state,
CreatedDate = DateTimeOffset.UtcNow
};

await _repository.AddListItemAsync(newItem);

return CreatedAtAction(nameof(GetListItem), new { list_id = list_id, item_id = newItem.Id }, newItem);
}

[HttpGet("{list_id}/items/{item_id}")]
[ProducesResponseType(200)]
public async Task<ActionResult<TodoItem>> GetListItem(string list_id, string item_id)
{
var item = await _repository.GetListItemAsync(list_id, item_id);

return item == null ? NotFound() : Ok(item);
}

[HttpPut("{list_id}/items/{item_id}")]
[ProducesResponseType(200)]
public async Task<ActionResult<TodoItem>> UpdateListItem(string list_id, string item_id, [FromBody] CreateUpdateTodoItem item)
{
var existingItem = await _repository.GetListItemAsync(list_id, item_id);
if (existingItem == null)
{
return NotFound();
}

existingItem.Name = item.name;
existingItem.Description = item.description;
existingItem.CompletedDate = item.completedDate;
existingItem.DueDate = item.dueDate;
existingItem.State = item.state;
existingItem.UpdatedDate = DateTimeOffset.UtcNow;

await _repository.UpdateListItem(existingItem);

return Ok(existingItem);
}

[HttpDelete("{list_id}/items/{item_id}")]
[ProducesResponseType(204)]
[ProducesResponseType(404)]
public async Task<ActionResult> DeleteListItem(string list_id, string item_id)
{
if (await _repository.GetListItemAsync(list_id, item_id) == null)
{
return NotFound();
}

await _repository.DeleteListItemAsync(list_id, item_id);

return NoContent();
}

[HttpGet("{list_id}/state/{state}")]
[ProducesResponseType(200)]
public async Task<ActionResult<IEnumerable<TodoItem>>> GetListItemsByState(string list_id, string state, [FromQuery] int? skip = null, [FromQuery] int? batchSize = null)
{
return Ok(await _repository.GetListItemsByStateAsync(list_id, state, skip, batchSize));
}

public record CreateUpdateTodoList(string name, string? description = null);
public record CreateUpdateTodoItem(string name, string state, DateTimeOffset? dueDate, DateTimeOffset? completedDate, string? description = null);
}
Loading

0 comments on commit e806000

Please sign in to comment.