|
1 |
| -using System.Linq; |
2 |
| -using Microsoft.AspNetCore.JsonPatch; |
3 |
| -using Microsoft.AspNetCore.Mvc; |
4 |
| -using SampleWebApiAspNetCore.Models; |
5 |
| -using SampleWebApiAspNetCore.Services; |
6 |
| - |
7 |
| -namespace SampleWebApiAspNetCore.Controllers |
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using System.Net; |
| 4 | +using Microsoft.AspNet.Mvc; |
| 5 | +using SampleWebApiMVC6.Models; |
| 6 | +using SampleWebApiMVC6.Services; |
| 7 | +using Microsoft.AspNet.JsonPatch; |
| 8 | +using SampleWebApiAspNetCore.Repositories; |
| 9 | + |
| 10 | +namespace SampleWebApiMVC6.Controllers |
8 | 11 | {
|
9 | 12 | [Route("api/[controller]")]
|
10 | 13 | public class HouseController : Controller
|
11 | 14 | {
|
12 | 15 | private readonly IHouseMapper _houseMapper;
|
| 16 | + private readonly IHouseRepository _houseRepository; |
13 | 17 |
|
14 |
| - public HouseController(IHouseMapper houseMapper) |
| 18 | + public HouseController(IHouseMapper houseMapper, IHouseRepository houseRepository) |
15 | 19 | {
|
16 | 20 | _houseMapper = houseMapper;
|
| 21 | + _houseRepository = houseRepository; |
17 | 22 | }
|
18 | 23 |
|
19 | 24 | [HttpGet]
|
20 | 25 | public IActionResult Get()
|
21 | 26 | {
|
22 |
| - return new JsonResult(Singleton.Instance.Houses.Select(x => _houseMapper.MapToDto(x))); |
| 27 | + try |
| 28 | + { |
| 29 | + return Ok(_houseRepository.GetAll().Select(x => _houseMapper.MapToDto(x))); |
| 30 | + } |
| 31 | + catch (Exception exception) |
| 32 | + { |
| 33 | + //logg exception or do anything with it |
| 34 | + return new HttpStatusCodeResult((int) HttpStatusCode.InternalServerError); |
| 35 | + } |
23 | 36 | }
|
24 | 37 |
|
25 | 38 | [HttpGet("{id:int}", Name = "GetSingleHouse")]
|
26 | 39 | public IActionResult GetSingle(int id)
|
27 | 40 | {
|
28 |
| - HouseEntity houseEntity = Singleton.Instance.Houses.FirstOrDefault(x => x.Id == id); |
| 41 | + try |
| 42 | + { |
| 43 | + HouseEntity houseEntity = _houseRepository.GetSingle(id); |
| 44 | + |
| 45 | + if (houseEntity == null) |
| 46 | + { |
| 47 | + return new HttpNotFoundResult(); |
| 48 | + } |
29 | 49 |
|
30 |
| - if (houseEntity == null) |
| 50 | + return Ok(_houseMapper.MapToDto(houseEntity)); |
| 51 | + } |
| 52 | + catch (Exception exception) |
31 | 53 | {
|
32 |
| - return new NotFoundResult(); |
| 54 | + //logg exception or do anything with it |
| 55 | + return new HttpStatusCodeResult((int) HttpStatusCode.InternalServerError); |
33 | 56 | }
|
34 |
| - |
35 |
| - return new JsonResult(_houseMapper.MapToDto(houseEntity)); |
36 | 57 | }
|
37 | 58 |
|
38 |
| - [HttpPatch("{id}")] |
39 |
| - public IActionResult Patch(int id, [FromBody] JsonPatchDocument<HouseEntity> housePatchDocument) |
| 59 | + [HttpPatch] |
| 60 | + public IActionResult Patch(int id, [FromBody] JsonPatchDocument<HouseDto> housePatchDocument) |
40 | 61 | {
|
41 |
| - if (housePatchDocument == null) |
| 62 | + try |
42 | 63 | {
|
43 |
| - return BadRequest(); |
44 |
| - } |
| 64 | + if (housePatchDocument == null) |
| 65 | + { |
| 66 | + return HttpBadRequest(); |
| 67 | + } |
45 | 68 |
|
46 |
| - if (!ModelState.IsValid) |
47 |
| - { |
48 |
| - return BadRequest(ModelState); |
49 |
| - } |
| 69 | + if (!ModelState.IsValid) |
| 70 | + { |
| 71 | + return HttpBadRequest(ModelState); |
| 72 | + } |
50 | 73 |
|
51 |
| - HouseEntity houseEntity = Singleton.Instance.Houses.FirstOrDefault(x => x.Id == id); |
| 74 | + HouseEntity houseEntity = _houseRepository.GetSingle(id); |
52 | 75 |
|
53 |
| - housePatchDocument.ApplyTo(houseEntity, ModelState); |
| 76 | + HouseDto existingHouse = _houseMapper.MapToDto(houseEntity); |
54 | 77 |
|
55 |
| - if (!ModelState.IsValid) |
56 |
| - { |
57 |
| - return BadRequest(ModelState); |
58 |
| - } |
| 78 | + housePatchDocument.ApplyTo(existingHouse, ModelState); |
| 79 | + |
| 80 | + if (!ModelState.IsValid) |
| 81 | + { |
| 82 | + return HttpBadRequest(ModelState); |
| 83 | + } |
59 | 84 |
|
60 |
| - int index = Singleton.Instance.Houses.FindIndex(x => x.Id == id); |
61 |
| - Singleton.Instance.Houses[index] = houseEntity; |
| 85 | + _houseRepository.Update(_houseMapper.MapToEntity(existingHouse)); |
62 | 86 |
|
63 |
| - return new JsonResult(_houseMapper.MapToDto(houseEntity)); |
| 87 | + return Ok(existingHouse); |
| 88 | + } |
| 89 | + catch (Exception exception) |
| 90 | + { |
| 91 | + //logg exception or do anything with it |
| 92 | + return new HttpStatusCodeResult((int) HttpStatusCode.InternalServerError); |
| 93 | + } |
64 | 94 | }
|
65 | 95 |
|
66 | 96 | [HttpPost]
|
67 | 97 | public IActionResult Create([FromBody] HouseDto houseDto)
|
68 | 98 | {
|
69 |
| - if (houseDto == null) |
| 99 | + try |
70 | 100 | {
|
71 |
| - return new BadRequestResult(); |
72 |
| - } |
| 101 | + if (houseDto == null) |
| 102 | + { |
| 103 | + return new BadRequestResult(); |
| 104 | + } |
73 | 105 |
|
74 |
| - if (!ModelState.IsValid) |
75 |
| - { |
76 |
| - return BadRequest(ModelState); |
77 |
| - } |
| 106 | + if (!ModelState.IsValid) |
| 107 | + { |
| 108 | + return HttpBadRequest(ModelState); |
| 109 | + } |
78 | 110 |
|
79 |
| - HouseEntity houseEntity = _houseMapper.MapToEntity(houseDto); |
| 111 | + HouseEntity houseEntity = _houseMapper.MapToEntity(houseDto); |
80 | 112 |
|
81 |
| - Singleton.Instance.Houses.Add(houseEntity); |
| 113 | + _houseRepository.Add(houseEntity); |
82 | 114 |
|
83 |
| - return new CreatedAtRouteResult("GetSingleHouse", new { id = houseEntity.Id }, _houseMapper.MapToDto(houseEntity)); |
| 115 | + return new CreatedAtRouteResult("GetSingleHouse", new { id = houseEntity.Id }, _houseMapper.MapToDto(houseEntity)); |
| 116 | + } |
| 117 | + catch (Exception exception) |
| 118 | + { |
| 119 | + //logg exception or do anything with it |
| 120 | + return new HttpStatusCodeResult((int) HttpStatusCode.InternalServerError); |
| 121 | + } |
84 | 122 | }
|
85 | 123 |
|
86 | 124 | [HttpPut("{id:int}")]
|
87 | 125 | public IActionResult Update(int id, [FromBody] HouseDto houseDto)
|
88 | 126 | {
|
89 |
| - if (houseDto == null) |
| 127 | + try |
90 | 128 | {
|
91 |
| - return new BadRequestResult(); |
92 |
| - } |
| 129 | + if (houseDto == null) |
| 130 | + { |
| 131 | + return new BadRequestResult(); |
| 132 | + } |
93 | 133 |
|
94 |
| - if (!ModelState.IsValid) |
95 |
| - { |
96 |
| - return BadRequest(ModelState); |
97 |
| - } |
| 134 | + if (!ModelState.IsValid) |
| 135 | + { |
| 136 | + return HttpBadRequest(ModelState); |
| 137 | + } |
98 | 138 |
|
99 |
| - HouseEntity houseEntityToUpdate = Singleton.Instance.Houses.FirstOrDefault(x => x.Id == id); |
| 139 | + HouseEntity houseEntityToUpdate = _houseRepository.GetSingle(id); |
100 | 140 |
|
101 |
| - if (houseEntityToUpdate == null) |
102 |
| - { |
103 |
| - return new NotFoundResult(); |
104 |
| - } |
| 141 | + if (houseEntityToUpdate == null) |
| 142 | + { |
| 143 | + return new HttpNotFoundResult(); |
| 144 | + } |
105 | 145 |
|
106 |
| - houseEntityToUpdate.ZipCode = houseDto.ZipCode; |
107 |
| - houseEntityToUpdate.Street = houseDto.Street; |
108 |
| - houseEntityToUpdate.City = houseDto.City; |
| 146 | + houseEntityToUpdate.ZipCode = houseDto.ZipCode; |
| 147 | + houseEntityToUpdate.Street = houseDto.Street; |
| 148 | + houseEntityToUpdate.City = houseDto.City; |
109 | 149 |
|
110 |
| - //Update to Database --> Is singleton in this case.... |
| 150 | + _houseRepository.Update(houseEntityToUpdate); |
111 | 151 |
|
112 |
| - return new JsonResult(_houseMapper.MapToDto(houseEntityToUpdate)); |
| 152 | + return Ok(_houseMapper.MapToDto(houseEntityToUpdate)); |
| 153 | + } |
| 154 | + catch (Exception exception) |
| 155 | + { |
| 156 | + //logg exception or do anything with it |
| 157 | + return new HttpStatusCodeResult((int) HttpStatusCode.InternalServerError); |
| 158 | + } |
113 | 159 | }
|
114 | 160 |
|
115 | 161 | [HttpDelete("{id:int}")]
|
116 | 162 | public IActionResult Delete(int id)
|
117 | 163 | {
|
118 |
| - HouseEntity houseEntityToDelete = Singleton.Instance.Houses.FirstOrDefault(x => x.Id == id); |
119 |
| - |
120 |
| - if (houseEntityToDelete == null) |
| 164 | + try |
121 | 165 | {
|
122 |
| - return new NotFoundResult(); |
123 |
| - } |
| 166 | + HouseEntity houseEntityToDelete = _houseRepository.GetSingle(id); |
124 | 167 |
|
125 |
| - Singleton.Instance.Houses.Remove(houseEntityToDelete); |
| 168 | + if (houseEntityToDelete == null) |
| 169 | + { |
| 170 | + return new HttpNotFoundResult(); |
| 171 | + } |
126 | 172 |
|
127 |
| - return new NoContentResult(); |
| 173 | + _houseRepository.Delete(id); |
| 174 | + |
| 175 | + return new NoContentResult(); |
| 176 | + } |
| 177 | + catch (Exception exception) |
| 178 | + { |
| 179 | + //logg exception or do anything with it |
| 180 | + return new HttpStatusCodeResult((int) HttpStatusCode.InternalServerError); |
| 181 | + } |
128 | 182 | }
|
129 | 183 | }
|
130 | 184 | }
|
0 commit comments