Skip to content

Commit 1914538

Browse files
committed
CRUD
1 parent 17801bc commit 1914538

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Data;
4+
using System.Data.Entity;
5+
using System.Data.Entity.Infrastructure;
6+
using System.Linq;
7+
using System.Net;
8+
using System.Net.Http;
9+
using System.Threading.Tasks;
10+
using System.Web.Http;
11+
using System.Web.Http.Description;
12+
using Zenergy.Models;
13+
14+
namespace Zenergy.Controllers
15+
{
16+
public class productsController : ApiController
17+
{
18+
private ZenergyContext db = new ZenergyContext();
19+
20+
// GET: api/products
21+
[HttpGet]
22+
[Route("api/product")]
23+
[Authorize(Roles = "User")]
24+
public IQueryable<product> Getproduct()
25+
{
26+
return db.product;
27+
}
28+
29+
// GET: api/products/5
30+
[HttpGet]
31+
[Route("api/products/{productId}")]
32+
[Authorize(Roles = "User")]
33+
[ResponseType(typeof(product))]
34+
public async Task<IHttpActionResult> Getproduct(int productId)
35+
{
36+
product product = await db.product.FindAsync(productId);
37+
if (product == null)
38+
{
39+
return NotFound();
40+
}
41+
42+
return Ok(product);
43+
}
44+
45+
// PUT: api/products/5
46+
[HttpPut]
47+
[Route("api//products/{productId}")]
48+
[Authorize(Roles = "Admin")]
49+
[ResponseType(typeof(void))]
50+
public async Task<IHttpActionResult> Putproduct(int productId, product product)
51+
{
52+
if (!ModelState.IsValid)
53+
{
54+
return BadRequest(ModelState);
55+
}
56+
57+
if (productId != product.productId)
58+
{
59+
return BadRequest();
60+
}
61+
62+
db.Entry(product).State = EntityState.Modified;
63+
64+
try
65+
{
66+
await db.SaveChangesAsync();
67+
}
68+
catch (DbUpdateConcurrencyException)
69+
{
70+
if (!productExists(productId))
71+
{
72+
return NotFound();
73+
}
74+
else
75+
{
76+
throw;
77+
}
78+
}
79+
80+
return StatusCode(HttpStatusCode.NoContent);
81+
}
82+
83+
// POST: api/products
84+
[HttpPost]
85+
[Route("api/product")]
86+
[ResponseType(typeof(product))]
87+
[Authorize(Roles = "Admin")]
88+
public async Task<IHttpActionResult> Postproduct(product product)
89+
{
90+
if (!ModelState.IsValid)
91+
{
92+
return BadRequest(ModelState);
93+
}
94+
95+
db.product.Add(product);
96+
await db.SaveChangesAsync();
97+
98+
return CreatedAtRoute("DefaultApi", new { id = product.productId }, product);
99+
}
100+
101+
// DELETE: api/products/5
102+
[HttpDelete]
103+
[Route("api/products/{productId}")]
104+
[Authorize(Roles = "Admin")]
105+
[ResponseType(typeof(product))]
106+
public async Task<IHttpActionResult> Deleteproduct(int productId)
107+
{
108+
product product = await db.product.FindAsync(productId);
109+
if (product == null)
110+
{
111+
return NotFound();
112+
}
113+
114+
db.product.Remove(product);
115+
await db.SaveChangesAsync();
116+
117+
return Ok(product);
118+
}
119+
120+
protected override void Dispose(bool disposing)
121+
{
122+
if (disposing)
123+
{
124+
db.Dispose();
125+
}
126+
base.Dispose(disposing);
127+
}
128+
129+
private bool productExists(int id)
130+
{
131+
return db.product.Count(e => e.productId == id) > 0;
132+
}
133+
}
134+
}

Diff for: Zenergy/Zenergy/Zenergy.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@
185185
<Compile Include="Controllers\ApiControllers\regularEventsController.cs" />
186186
<Compile Include="Controllers\ApiControllers\usersController.cs" />
187187
<Compile Include="Controllers\HomeController.cs" />
188+
<Compile Include="Controllers\ApiControllers\productsController.cs" />
188189
<Compile Include="Models\accessory.cs">
189190
<DependentUpon>ZenergyModel.tt</DependentUpon>
190191
</Compile>

0 commit comments

Comments
 (0)