Skip to content

Commit 5980da0

Browse files
FabianGosebrinkFabianGosebrink
authored andcommitted
Updated to .NET Core RC2
2 parents 080ad15 + 24701f0 commit 5980da0

File tree

6 files changed

+195
-104
lines changed

6 files changed

+195
-104
lines changed
Lines changed: 120 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,130 +1,184 @@
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
811
{
912
[Route("api/[controller]")]
1013
public class HouseController : Controller
1114
{
1215
private readonly IHouseMapper _houseMapper;
16+
private readonly IHouseRepository _houseRepository;
1317

14-
public HouseController(IHouseMapper houseMapper)
18+
public HouseController(IHouseMapper houseMapper, IHouseRepository houseRepository)
1519
{
1620
_houseMapper = houseMapper;
21+
_houseRepository = houseRepository;
1722
}
1823

1924
[HttpGet]
2025
public IActionResult Get()
2126
{
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+
}
2336
}
2437

2538
[HttpGet("{id:int}", Name = "GetSingleHouse")]
2639
public IActionResult GetSingle(int id)
2740
{
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+
}
2949

30-
if (houseEntity == null)
50+
return Ok(_houseMapper.MapToDto(houseEntity));
51+
}
52+
catch (Exception exception)
3153
{
32-
return new NotFoundResult();
54+
//logg exception or do anything with it
55+
return new HttpStatusCodeResult((int) HttpStatusCode.InternalServerError);
3356
}
34-
35-
return new JsonResult(_houseMapper.MapToDto(houseEntity));
3657
}
3758

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)
4061
{
41-
if (housePatchDocument == null)
62+
try
4263
{
43-
return BadRequest();
44-
}
64+
if (housePatchDocument == null)
65+
{
66+
return HttpBadRequest();
67+
}
4568

46-
if (!ModelState.IsValid)
47-
{
48-
return BadRequest(ModelState);
49-
}
69+
if (!ModelState.IsValid)
70+
{
71+
return HttpBadRequest(ModelState);
72+
}
5073

51-
HouseEntity houseEntity = Singleton.Instance.Houses.FirstOrDefault(x => x.Id == id);
74+
HouseEntity houseEntity = _houseRepository.GetSingle(id);
5275

53-
housePatchDocument.ApplyTo(houseEntity, ModelState);
76+
HouseDto existingHouse = _houseMapper.MapToDto(houseEntity);
5477

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+
}
5984

60-
int index = Singleton.Instance.Houses.FindIndex(x => x.Id == id);
61-
Singleton.Instance.Houses[index] = houseEntity;
85+
_houseRepository.Update(_houseMapper.MapToEntity(existingHouse));
6286

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+
}
6494
}
6595

6696
[HttpPost]
6797
public IActionResult Create([FromBody] HouseDto houseDto)
6898
{
69-
if (houseDto == null)
99+
try
70100
{
71-
return new BadRequestResult();
72-
}
101+
if (houseDto == null)
102+
{
103+
return new BadRequestResult();
104+
}
73105

74-
if (!ModelState.IsValid)
75-
{
76-
return BadRequest(ModelState);
77-
}
106+
if (!ModelState.IsValid)
107+
{
108+
return HttpBadRequest(ModelState);
109+
}
78110

79-
HouseEntity houseEntity = _houseMapper.MapToEntity(houseDto);
111+
HouseEntity houseEntity = _houseMapper.MapToEntity(houseDto);
80112

81-
Singleton.Instance.Houses.Add(houseEntity);
113+
_houseRepository.Add(houseEntity);
82114

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+
}
84122
}
85123

86124
[HttpPut("{id:int}")]
87125
public IActionResult Update(int id, [FromBody] HouseDto houseDto)
88126
{
89-
if (houseDto == null)
127+
try
90128
{
91-
return new BadRequestResult();
92-
}
129+
if (houseDto == null)
130+
{
131+
return new BadRequestResult();
132+
}
93133

94-
if (!ModelState.IsValid)
95-
{
96-
return BadRequest(ModelState);
97-
}
134+
if (!ModelState.IsValid)
135+
{
136+
return HttpBadRequest(ModelState);
137+
}
98138

99-
HouseEntity houseEntityToUpdate = Singleton.Instance.Houses.FirstOrDefault(x => x.Id == id);
139+
HouseEntity houseEntityToUpdate = _houseRepository.GetSingle(id);
100140

101-
if (houseEntityToUpdate == null)
102-
{
103-
return new NotFoundResult();
104-
}
141+
if (houseEntityToUpdate == null)
142+
{
143+
return new HttpNotFoundResult();
144+
}
105145

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;
109149

110-
//Update to Database --> Is singleton in this case....
150+
_houseRepository.Update(houseEntityToUpdate);
111151

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+
}
113159
}
114160

115161
[HttpDelete("{id:int}")]
116162
public IActionResult Delete(int id)
117163
{
118-
HouseEntity houseEntityToDelete = Singleton.Instance.Houses.FirstOrDefault(x => x.Id == id);
119-
120-
if (houseEntityToDelete == null)
164+
try
121165
{
122-
return new NotFoundResult();
123-
}
166+
HouseEntity houseEntityToDelete = _houseRepository.GetSingle(id);
124167

125-
Singleton.Instance.Houses.Remove(houseEntityToDelete);
168+
if (houseEntityToDelete == null)
169+
{
170+
return new HttpNotFoundResult();
171+
}
126172

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+
}
128182
}
129183
}
130184
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using SampleWebApiAspNetCore.Models;
4+
5+
namespace SampleWebApiAspNetCore.Repositories
6+
{
7+
public class HouseRepository : IHouseRepository
8+
{
9+
readonly Dictionary<int, HouseEntity> _houses = new Dictionary<int, HouseEntity>();
10+
11+
public HouseRepository()
12+
{
13+
_houses.Add(1, new HouseEntity() { City = "Town1", Id = 1, Street = "Street1", ZipCode = 1234 });
14+
_houses.Add(2, new HouseEntity() { City = "Town2", Id = 2, Street = "Street2", ZipCode = 1234 });
15+
_houses.Add(3, new HouseEntity() { City = "Town3", Id = 3, Street = "Street3", ZipCode = 1234 });
16+
_houses.Add(4, new HouseEntity() { City = "Town4", Id = 4, Street = "Street4", ZipCode = 1234 });
17+
}
18+
19+
public List<HouseEntity> GetAll()
20+
{
21+
return _houses.Select(x => x.Value).ToList();
22+
}
23+
24+
public HouseEntity GetSingle(int id)
25+
{
26+
return _houses.FirstOrDefault(x => x.Key == id).Value;
27+
}
28+
29+
public HouseEntity Add(HouseEntity toAdd)
30+
{
31+
int newId = !GetAll().Any() ? 1 : GetAll().Max(x => x.Id) + 1;
32+
toAdd.Id = newId;
33+
_houses.Add(newId, toAdd);
34+
return toAdd;
35+
}
36+
37+
public HouseEntity Update(HouseEntity toUpdate)
38+
{
39+
HouseEntity single = GetSingle(toUpdate.Id);
40+
41+
if (single == null)
42+
{
43+
return null;
44+
}
45+
46+
_houses[single.Id] = toUpdate;
47+
return toUpdate;
48+
}
49+
50+
public void Delete(int id)
51+
{
52+
_houses.Remove(id);
53+
}
54+
}
55+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.Collections.Generic;
2+
using SampleWebApiAspNetCore.Models;
3+
4+
namespace SampleWebApiAspNetCore.Repositories
5+
{
6+
public interface IHouseRepository
7+
{
8+
List<HouseEntity> GetAll();
9+
HouseEntity GetSingle(int id);
10+
HouseEntity Add(HouseEntity toAdd);
11+
HouseEntity Update(HouseEntity toUpdate);
12+
void Delete(int id);
13+
}
14+
}

src/SampleWebApiAspNetCore/Services/HouseMapper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public HouseEntity MapToEntity(HouseDto houseDto)
2020
{
2121
return new HouseEntity()
2222
{
23-
Id = houseDto.Id == 0 ? Singleton.Instance.Houses.Max(x => x.Id) + 1 : houseDto.Id,
23+
Id = houseDto.Id,
2424
ZipCode = houseDto.ZipCode,
2525
City = houseDto.City,
2626
Street = houseDto.Street

src/SampleWebApiAspNetCore/Services/SingeltonCache.cs

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)