Skip to content

Commit 0d4bf27

Browse files
Small improvements to prepare upgrade
1 parent c7a38a9 commit 0d4bf27

File tree

14 files changed

+221
-120
lines changed

14 files changed

+221
-120
lines changed

src/SampleWebApiAspNetCore/Controllers/HouseController.cs

Lines changed: 72 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,140 +1,170 @@
1-
using System;
2-
using System.Linq;
3-
using System.Net;
1+
using System.Linq;
42
using Microsoft.AspNetCore.JsonPatch;
53
using Microsoft.AspNetCore.Mvc;
64
using SampleWebApiAspNetCore.Models;
75
using SampleWebApiAspNetCore.Repositories;
8-
using SampleWebApiAspNetCore.Services;
6+
using Newtonsoft.Json;
7+
using AutoMapper;
8+
using SampleWebApiAspNetCore.Entities;
9+
using System;
910

1011
namespace SampleWebApiAspNetCore.Controllers
1112
{
1213
[Route("api/[controller]")]
1314
public class HouseController : Controller
1415
{
15-
private readonly IHouseMapper _houseMapper;
1616
private readonly IHouseRepository _houseRepository;
1717

18-
public HouseController(IHouseMapper houseMapper, IHouseRepository houseRepository)
18+
public HouseController(IHouseRepository houseRepository)
1919
{
20-
_houseMapper = houseMapper;
2120
_houseRepository = houseRepository;
2221
}
2322

2423
[HttpGet]
25-
public IActionResult Get()
24+
public IActionResult GetAllHouses()
2625
{
27-
return Ok(_houseRepository.GetAll().Select(x => _houseMapper.MapToDto(x)));
26+
var allHouseEntitys = _houseRepository.GetAll().ToList();
27+
28+
var allHouseEntitysDto = allHouseEntitys.Select(x => Mapper.Map<HouseDto>(x));
29+
30+
Response.Headers.Add("X-Pagination",
31+
JsonConvert.SerializeObject(new { totalCount = _houseRepository.Count() }));
32+
33+
return Ok(allHouseEntitysDto);
2834
}
2935

3036
[HttpGet("{id:int}", Name = "GetSingleHouse")]
3137
public IActionResult GetSingle(int id)
3238
{
33-
HouseEntity houseEntity = _houseRepository.GetSingle(id);
39+
HouseEntity houseEntityFromRepo = _houseRepository.GetSingle(id);
3440

35-
if (houseEntity == null)
41+
if (houseEntityFromRepo == null)
3642
{
3743
return NotFound();
3844
}
3945

40-
return Ok(_houseMapper.MapToDto(houseEntity));
46+
return Ok(Mapper.Map<HouseDto>(houseEntityFromRepo));
4147
}
4248

4349
[HttpPatch("{id:int}")]
44-
public IActionResult Patch(int id, [FromBody] JsonPatchDocument<HouseDto> housePatchDocument)
50+
public IActionResult Patch(int id, [FromBody] JsonPatchDocument<HouseUpdateDto> housePatchDocument)
4551
{
4652
if (housePatchDocument == null)
4753
{
4854
return BadRequest();
4955
}
5056

51-
if (!ModelState.IsValid)
52-
{
53-
return BadRequest(ModelState);
54-
}
55-
56-
HouseEntity houseEntity = _houseRepository.GetSingle(id);
57+
var existingHouse = _houseRepository.GetSingle(id);
5758

58-
if (houseEntity == null)
59+
if (existingHouse == null)
5960
{
6061
return NotFound();
6162
}
6263

63-
HouseDto existingHouse = _houseMapper.MapToDto(houseEntity);
64+
var houseToPatch = Mapper.Map<HouseUpdateDto>(existingHouse);
65+
housePatchDocument.ApplyTo(houseToPatch, ModelState);
6466

65-
housePatchDocument.ApplyTo(existingHouse, ModelState);
67+
TryValidateModel(houseToPatch);
6668

6769
if (!ModelState.IsValid)
6870
{
6971
return BadRequest(ModelState);
7072
}
7173

72-
_houseRepository.Update(_houseMapper.MapToEntity(existingHouse));
74+
Mapper.Map(houseToPatch, existingHouse);
7375

74-
return Ok(existingHouse);
76+
_houseRepository.Update(existingHouse);
77+
78+
bool result = _houseRepository.Save();
79+
80+
if (!result)
81+
{
82+
throw new Exception($"something went wrong when updating the house with id: {id}");
83+
}
84+
85+
return Ok(Mapper.Map<HouseDto>(existingHouse));
7586
}
7687

7788
[HttpPost]
78-
public IActionResult Create([FromBody] HouseDto houseDto)
89+
public IActionResult Create([FromBody] HouseCreateDto houseDto)
7990
{
8091
if (houseDto == null)
8192
{
82-
return BadRequest();
93+
return BadRequest("HouseEntitycreate object was null");
8394
}
8495

8596
if (!ModelState.IsValid)
8697
{
8798
return BadRequest(ModelState);
8899
}
89100

90-
HouseEntity houseEntity = _houseMapper.MapToEntity(houseDto);
101+
HouseEntity toAdd = Mapper.Map<HouseEntity>(houseDto);
91102

92-
_houseRepository.Add(houseEntity);
103+
_houseRepository.Add(toAdd);
93104

94-
return CreatedAtRoute("GetSingleHouse", new { id = houseEntity.Id }, _houseMapper.MapToDto(houseEntity));
105+
bool result = _houseRepository.Save();
106+
107+
if (!result)
108+
{
109+
throw new Exception("something went wrong when adding a new House");
110+
}
111+
112+
return CreatedAtRoute("GetSingleHouse", new { id = toAdd.Id }, Mapper.Map<HouseDto>(toAdd));
95113
}
96114

97115
[HttpPut("{id:int}")]
98-
public IActionResult Update(int id, [FromBody] HouseDto houseDto)
116+
public IActionResult Update(int id, [FromBody] HouseUpdateDto houseDto)
99117
{
100118
if (houseDto == null)
101119
{
102120
return BadRequest();
103121
}
104122

123+
var existingHouseEntity = _houseRepository.GetSingle(id);
124+
125+
if (existingHouseEntity == null)
126+
{
127+
return NotFound();
128+
}
129+
105130
if (!ModelState.IsValid)
106131
{
107132
return BadRequest(ModelState);
108133
}
109134

110-
HouseEntity houseEntityToUpdate = _houseRepository.GetSingle(id);
135+
Mapper.Map(houseDto, existingHouseEntity);
111136

112-
if (houseEntityToUpdate == null)
113-
{
114-
return NotFound();
115-
}
137+
_houseRepository.Update(existingHouseEntity);
116138

117-
houseEntityToUpdate.ZipCode = houseDto.ZipCode;
118-
houseEntityToUpdate.Street = houseDto.Street;
119-
houseEntityToUpdate.City = houseDto.City;
139+
bool result = _houseRepository.Save();
120140

121-
_houseRepository.Update(houseEntityToUpdate);
141+
if (!result)
142+
{
143+
throw new Exception($"something went wrong when updating the house with id: {id}");
144+
}
122145

123-
return Ok(_houseMapper.MapToDto(houseEntityToUpdate));
146+
return Ok(Mapper.Map<HouseDto>(existingHouseEntity));
124147
}
125148

126149
[HttpDelete("{id:int}")]
127150
public IActionResult Delete(int id)
128151
{
129-
HouseEntity houseEntityToDelete = _houseRepository.GetSingle(id);
152+
var existingHouseEntity = _houseRepository.GetSingle(id);
130153

131-
if (houseEntityToDelete == null)
154+
if (existingHouseEntity == null)
132155
{
133156
return NotFound();
134157
}
135158

136159
_houseRepository.Delete(id);
137160

161+
bool result = _houseRepository.Save();
162+
163+
if (!result)
164+
{
165+
throw new Exception($"something went wrong when deleting the House with id: {id}");
166+
}
167+
138168
return NoContent();
139169
}
140170
}

src/SampleWebApiAspNetCore/Models/HouseEntity.cs renamed to src/SampleWebApiAspNetCore/Entities/HouseEntity.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace SampleWebApiAspNetCore.Models
1+
namespace SampleWebApiAspNetCore.Entities
22
{
33
public class HouseEntity
44
{
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using Microsoft.AspNetCore.Builder;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using SampleWebApiAspNetCore.Services;
4+
5+
namespace SampleWebApiAspNetCore.Middleware
6+
{
7+
public static class MiddlewareExtensions
8+
{
9+
public static async void AddSeedData(this IApplicationBuilder app)
10+
{
11+
var seedDataService = app.ApplicationServices.GetRequiredService<ISeedDataService>();
12+
seedDataService.EnsureSeedData();
13+
}
14+
}
15+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel.DataAnnotations;
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
7+
namespace SampleWebApiAspNetCore.Models
8+
{
9+
public class HouseCreateDto
10+
{
11+
[Required, MinLength(3)]
12+
public string Street { get; set; }
13+
14+
[Required, MinLength(3)]
15+
public string City { get; set; }
16+
17+
[Required]
18+
[DataType(DataType.PostalCode)]
19+
public int ZipCode { get; set; }
20+
}
21+
}

src/SampleWebApiAspNetCore/Models/HouseDto.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,8 @@ namespace SampleWebApiAspNetCore.Models
55
public class HouseDto
66
{
77
public int Id { get; set; }
8-
9-
[Required, MinLength(3)]
108
public string Street { get; set; }
11-
12-
[Required, MinLength(3)]
139
public string City { get; set; }
14-
15-
[Required]
16-
[DataType(DataType.PostalCode)]
1710
public int ZipCode { get; set; }
1811
}
1912
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel.DataAnnotations;
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
7+
namespace SampleWebApiAspNetCore.Models
8+
{
9+
public class HouseUpdateDto
10+
{
11+
[Required, MinLength(3)]
12+
public string Street { get; set; }
13+
14+
[Required, MinLength(3)]
15+
public string City { get; set; }
16+
17+
[Required]
18+
[DataType(DataType.PostalCode)]
19+
public int ZipCode { get; set; }
20+
}
21+
}
Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,52 @@
11
using System.Collections.Generic;
22
using System.Linq;
33
using SampleWebApiAspNetCore.Models;
4+
using SampleWebApiAspNetCore.Entities;
45

56
namespace SampleWebApiAspNetCore.Repositories
67
{
78
public class HouseRepository : IHouseRepository
89
{
910
readonly Dictionary<int, HouseEntity> _houses = new Dictionary<int, HouseEntity>();
1011

11-
public HouseRepository()
12+
public IQueryable<HouseEntity> GetAll()
1213
{
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();
14+
return _houses.Select(x => x.Value).AsQueryable();
2215
}
2316

2417
public HouseEntity GetSingle(int id)
2518
{
2619
return _houses.FirstOrDefault(x => x.Key == id).Value;
2720
}
2821

29-
public HouseEntity Add(HouseEntity toAdd)
22+
public void Add(HouseEntity toAdd)
3023
{
3124
int newId = !GetAll().Any() ? 1 : GetAll().Max(x => x.Id) + 1;
3225
toAdd.Id = newId;
3326
_houses.Add(newId, toAdd);
34-
return toAdd;
3527
}
3628

37-
public HouseEntity Update(HouseEntity toUpdate)
29+
public void Update(HouseEntity toUpdate)
3830
{
3931
HouseEntity single = GetSingle(toUpdate.Id);
40-
41-
if (single == null)
42-
{
43-
return null;
44-
}
45-
4632
_houses[single.Id] = toUpdate;
47-
return toUpdate;
4833
}
4934

5035
public void Delete(int id)
5136
{
5237
_houses.Remove(id);
5338
}
39+
40+
public int Count()
41+
{
42+
return _houses.Count();
43+
}
44+
45+
public bool Save()
46+
{
47+
// would call saveChanges on Entity Framework
48+
// always returns true here
49+
return true;
50+
}
5451
}
5552
}
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
using System.Collections.Generic;
2-
using SampleWebApiAspNetCore.Models;
1+
using SampleWebApiAspNetCore.Entities;
2+
using System.Linq;
33

44
namespace SampleWebApiAspNetCore.Repositories
55
{
66
public interface IHouseRepository
77
{
8-
List<HouseEntity> GetAll();
9-
HouseEntity GetSingle(int id);
10-
HouseEntity Add(HouseEntity toAdd);
11-
HouseEntity Update(HouseEntity toUpdate);
8+
void Add(HouseEntity item);
129
void Delete(int id);
10+
IQueryable<HouseEntity> GetAll();
11+
HouseEntity GetSingle(int id);
12+
bool Save();
13+
int Count();
14+
void Update(HouseEntity item);
1315
}
1416
}

0 commit comments

Comments
 (0)