|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using Microsoft.AspNetCore.Http; |
| 6 | +using Microsoft.AspNetCore.Mvc; |
| 7 | +using Microsoft.AspNetCore.Authorization; |
| 8 | +using Microsoft.EntityFrameworkCore; |
| 9 | +using TodoListAPI.Models; |
| 10 | +using System.Security.Claims; |
| 11 | +using Microsoft.Identity.Web.Resource; |
| 12 | + |
| 13 | +namespace TodoListAPI.Controllers |
| 14 | +{ |
| 15 | + [Authorize] |
| 16 | + [Route("api/[controller]")] |
| 17 | + [ApiController] |
| 18 | + public class TodoListController : ControllerBase |
| 19 | + { |
| 20 | + // The Web API will only accept tokens 1) for users, and |
| 21 | + // 2) having the access_as_user scope for this API |
| 22 | + static readonly string[] scopeRequiredByApi = new string[] { "demo.read" }; |
| 23 | + |
| 24 | + private readonly TodoContext _context; |
| 25 | + |
| 26 | + public TodoListController(TodoContext context) |
| 27 | + { |
| 28 | + _context = context; |
| 29 | + } |
| 30 | + |
| 31 | + // GET: api/TodoItems |
| 32 | + [HttpGet] |
| 33 | + public async Task<ActionResult<IEnumerable<TodoItem>>> GetTodoItems() |
| 34 | + { |
| 35 | + HttpContext.VerifyUserHasAnyAcceptedScope(scopeRequiredByApi); |
| 36 | + string owner = User.FindFirst(ClaimTypes.NameIdentifier)?.Value; |
| 37 | + return await _context.TodoItems.Where(item => item.Owner == owner).ToListAsync(); |
| 38 | + } |
| 39 | + |
| 40 | + // GET: api/TodoItems/5 |
| 41 | + [HttpGet("{id}")] |
| 42 | + public async Task<ActionResult<TodoItem>> GetTodoItem(int id) |
| 43 | + { |
| 44 | + HttpContext.VerifyUserHasAnyAcceptedScope(scopeRequiredByApi); |
| 45 | + |
| 46 | + var todoItem = await _context.TodoItems.FindAsync(id); |
| 47 | + |
| 48 | + if (todoItem == null) |
| 49 | + { |
| 50 | + return NotFound(); |
| 51 | + } |
| 52 | + |
| 53 | + return todoItem; |
| 54 | + } |
| 55 | + |
| 56 | + // PUT: api/TodoItems/5 |
| 57 | + // To protect from overposting attacks, please enable the specific properties you want to bind to, for |
| 58 | + // more details see https://aka.ms/RazorPagesCRUD. |
| 59 | + [HttpPut("{id}")] |
| 60 | + public async Task<IActionResult> PutTodoItem(int id, TodoItem todoItem) |
| 61 | + { |
| 62 | + HttpContext.VerifyUserHasAnyAcceptedScope(scopeRequiredByApi); |
| 63 | + |
| 64 | + if (id != todoItem.Id) |
| 65 | + { |
| 66 | + return BadRequest(); |
| 67 | + } |
| 68 | + |
| 69 | + _context.Entry(todoItem).State = EntityState.Modified; |
| 70 | + |
| 71 | + try |
| 72 | + { |
| 73 | + await _context.SaveChangesAsync(); |
| 74 | + } |
| 75 | + catch (DbUpdateConcurrencyException) |
| 76 | + { |
| 77 | + if (!TodoItemExists(id)) |
| 78 | + { |
| 79 | + return NotFound(); |
| 80 | + } |
| 81 | + else |
| 82 | + { |
| 83 | + throw; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return NoContent(); |
| 88 | + } |
| 89 | + |
| 90 | + // POST: api/TodoItems |
| 91 | + // To protect from overposting attacks, please enable the specific properties you want to bind to, for |
| 92 | + // more details see https://aka.ms/RazorPagesCRUD. |
| 93 | + [HttpPost] |
| 94 | + public async Task<ActionResult<TodoItem>> PostTodoItem(TodoItem todoItem) |
| 95 | + { |
| 96 | + HttpContext.VerifyUserHasAnyAcceptedScope(scopeRequiredByApi); |
| 97 | + string owner = User.FindFirst(ClaimTypes.NameIdentifier)?.Value; |
| 98 | + todoItem.Owner = owner; |
| 99 | + todoItem.Status = false; |
| 100 | + |
| 101 | + _context.TodoItems.Add(todoItem); |
| 102 | + await _context.SaveChangesAsync(); |
| 103 | + |
| 104 | + return CreatedAtAction("GetTodoItem", new { id = todoItem.Id }, todoItem); |
| 105 | + } |
| 106 | + |
| 107 | + // DELETE: api/TodoItems/5 |
| 108 | + [HttpDelete("{id}")] |
| 109 | + public async Task<ActionResult<TodoItem>> DeleteTodoItem(int id) |
| 110 | + { |
| 111 | + HttpContext.VerifyUserHasAnyAcceptedScope(scopeRequiredByApi); |
| 112 | + |
| 113 | + var todoItem = await _context.TodoItems.FindAsync(id); |
| 114 | + if (todoItem == null) |
| 115 | + { |
| 116 | + return NotFound(); |
| 117 | + } |
| 118 | + |
| 119 | + _context.TodoItems.Remove(todoItem); |
| 120 | + await _context.SaveChangesAsync(); |
| 121 | + |
| 122 | + return todoItem; |
| 123 | + } |
| 124 | + |
| 125 | + private bool TodoItemExists(int id) |
| 126 | + { |
| 127 | + return _context.TodoItems.Any(e => e.Id == id); |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments