-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCategoriesController.cs
66 lines (55 loc) · 1.7 KB
/
CategoriesController.cs
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
using OnlineStore.Business.Contracts;
using OnlineStore.Entity.Concrete;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
namespace OnlineStore.WebApi.Controllers
{
public class CategoriesController : ApiController
{
private ICategoryService _categoryService;
public CategoriesController(ICategoryService categoryService)
{
_categoryService = categoryService;
}
[Route("api/categories")]
[HttpGet]
public IHttpActionResult CategoryList()
{
return Ok(_categoryService.GetAll());
}
[Route("api/categories/{id}")]
[HttpGet]
public IHttpActionResult Get(int id)
{
var category = _categoryService.Get(_ => _.Id == id);
return Ok(category);
}
[Route("api/categories")]
[HttpPost]
public IHttpActionResult Post([FromBody] Category category)
{
_categoryService.Add(category);
var uri = new Uri(Request.RequestUri + "/" + category.Id);
return Created(uri, category);
}
[Route("api/categories")]
[HttpPut]
public IHttpActionResult Put([FromBody] Category category)
{
_categoryService.Update(category);
return StatusCode(HttpStatusCode.NoContent);
}
[Route("api/categories/{id}")]
[HttpDelete]
public IHttpActionResult Delete(int id)
{
var category = new Category { Id = id };
_categoryService.Delete(category);
return StatusCode(HttpStatusCode.NoContent);
}
}
}