-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExampleItemsController.cs
More file actions
139 lines (118 loc) · 4.23 KB
/
ExampleItemsController.cs
File metadata and controls
139 lines (118 loc) · 4.23 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Keypath.API.DAL;
using Keypath.API.Models;
using Microsoft.AspNetCore.Cors;
namespace Keypath.API.Controllers
{
[Route("api/[controller]")]
[ApiController]
[EnableCors("Policy01")]
public class ExampleItemsController : ControllerBase
{
private readonly KeypathContext _context;
public ExampleItemsController(KeypathContext context)
{
_context = context;
}
// GET: api/ExampleItems
[HttpGet]
public async Task<ActionResult<IEnumerable<ExampleItem>>> GetExampleItems([FromQuery] string wordContains=null)
{
string filter = String.IsNullOrWhiteSpace(wordContains) ? String.Empty : wordContains;
return await _context.ExampleItems
.OrderBy(ei => ei.Id)
.Where(ei => ei.Word.Contains(filter))
.ToListAsync();
}
// GET: api/ExampleItems/5
[HttpGet("{id}")]
public async Task<ActionResult<ExampleItem>> GetExampleItem(long id)
{
var exampleItem = await _context.ExampleItems.FindAsync(id);
if (exampleItem == null)
{
return NotFound();
}
return exampleItem;
}
// PUT: api/ExampleItems/5
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details, see https://go.microsoft.com/fwlink/?linkid=2123754.
[HttpPut("{id}")]
public async Task<IActionResult> PutExampleItem(long id, ExampleItem exampleItem)
{
if (id != exampleItem.Id)
{
return BadRequest();
}
_context.Entry(exampleItem).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!ExampleItemExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
// POST: api/ExampleItems
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details, see https://go.microsoft.com/fwlink/?linkid=2123754.
[HttpPost]
[Route("/api/[controller]")]
[Route("/api/[controller]/json")]
public async Task<ActionResult<ExampleItem>> PostExampleItemJSON(ExampleItem exampleItem)
{
return await PostNewRow(exampleItem);
}
[HttpPost]
[Route("/api/[controller]/query")]
public async Task<ActionResult<ExampleItem>> PostExampleIteQUERY([FromQuery] ExampleItem exampleItem)
{
return await PostNewRow(exampleItem);
}
[HttpPost]
[Route("/api/[controller]/form")]
public async Task<ActionResult<ExampleItem>> PostExampleItemFORM([FromForm] ExampleItem exampleItem)
{
return await PostNewRow(exampleItem);
}
private async Task<CreatedAtActionResult> PostNewRow(ExampleItem exampleItem)
{
_context.ExampleItems.Add(exampleItem);
await _context.SaveChangesAsync();
return CreatedAtAction(nameof(GetExampleItem), new { id = exampleItem.Id }, exampleItem);
}
// DELETE: api/ExampleItems/5
[HttpDelete("{id}")]
public async Task<ActionResult<ExampleItem>> DeleteExampleItem(long id)
{
var exampleItem = await _context.ExampleItems.FindAsync(id);
if (exampleItem == null)
{
return NotFound();
}
_context.ExampleItems.Remove(exampleItem);
await _context.SaveChangesAsync();
return exampleItem;
}
private bool ExampleItemExists(long id)
{
return _context.ExampleItems.Any(e => e.Id == id);
}
}
}