-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathController.cs
More file actions
53 lines (45 loc) · 1.59 KB
/
Controller.cs
File metadata and controls
53 lines (45 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using FreeAwait;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace TodoBackend
{
[Route("api/todos")]
[ApiController]
public class TodoController : ControllerBase
{
[HttpPost]
public async IStep<IActionResult> Post([FromBody] Create create) => Ok(await Locate(create));
[HttpGet("{id}")]
public async IStep<IActionResult> Get([FromRoute] int id) =>
await new Read(id) is { } item
? Ok(await Locate(item))
: NotFound();
[HttpGet]
public async IStep<IActionResult> Get() => Ok(
await (await new ReadAll())
.Select(Locate)
.Sequence());
[HttpDelete("{id}")]
public async IStep<IActionResult> Delete([FromRoute] int id) =>
await new Delete(id) is { } item
? Ok(await Locate(item))
: NotFound();
[HttpDelete]
public async IStep<IActionResult> Delete()
{
await new DeleteAll();
return NoContent();
}
[HttpPatch("{id}")]
public async IStep<IActionResult> Patch([FromRoute] int id, [FromBody] Patch patch) =>
await new Update(id, patch) is { } item
? Ok(await Locate(item))
: NotFound();
private static IStep<Todo> Locate(Todo item) => new Locate(item);
private static async IStep<Todo> Locate(IStep<Todo> step) => await new Locate(await step);
}
}