-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathPersonRepository.cs
More file actions
120 lines (110 loc) · 4.09 KB
/
Copy pathPersonRepository.cs
File metadata and controls
120 lines (110 loc) · 4.09 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using Blazorcrud.Shared.Data;
using Blazorcrud.Shared.Models;
using Microsoft.EntityFrameworkCore;
namespace Blazorcrud.Server.Models
{
public class PersonRepository:IPersonRepository
{
private readonly AppDbContext _appDbContext;
public PersonRepository(AppDbContext appDbContext)
{
_appDbContext = appDbContext;
}
public async Task<Person> AddPerson(Person person)
{
var result = await _appDbContext.People.AddAsync(person);
await _appDbContext.SaveChangesAsync();
return result.Entity;
}
public async Task<Person?> DeletePerson(int personId)
{
var result = await _appDbContext.People.FirstOrDefaultAsync(p => p.PersonId==personId);
if (result!=null)
{
_appDbContext.People.Remove(result);
await _appDbContext.SaveChangesAsync();
}
else
{
throw new KeyNotFoundException("Person not found");
}
return result;
}
public async Task<Person?> GetPerson(int personId)
{
var result = await _appDbContext.People
.Include(p => p.Addresses)
.FirstOrDefaultAsync(p => p.PersonId==personId);
if (result != null)
{
return result;
}
else
{
throw new KeyNotFoundException("Person not found");
}
}
public PagedResult<Person> GetPeople(string? name, int page)
{
int pageSize = 5;
if (name != null)
{
return _appDbContext.People
.Where(p => p.FirstName.Contains(name) ||
p.LastName.Contains(name))
.OrderBy(p => p.PersonId)
.Include(p => p.Addresses)
.GetPaged(page, pageSize);
}
else
{
return _appDbContext.People
.OrderBy(p => p.PersonId)
.Include(p => p.Addresses)
.GetPaged(page, pageSize);
}
}
public async Task<Person?> UpdatePerson(Person person)
{
var result = await _appDbContext.People.Include("Addresses").FirstOrDefaultAsync(p => p.PersonId==person.PersonId);
if (result!=null)
{
// Update existing person
_appDbContext.Entry(result).CurrentValues.SetValues(person);
// Remove deleted addresses
foreach (var existingAddress in result.Addresses.ToList())
{
if(!person.Addresses.Any(o => o.AddressId == existingAddress.AddressId))
_appDbContext.Addresses.Remove(existingAddress);
}
// Update and Insert Addresses
foreach (var addressModel in person.Addresses)
{
var existingAddress = result.Addresses
.Where(a => a.AddressId == addressModel.AddressId)
.SingleOrDefault();
if (existingAddress != null)
_appDbContext.Entry(existingAddress).CurrentValues.SetValues(addressModel);
else
{
var newAddress = new Address
{
AddressId = addressModel.AddressId,
Street = addressModel.Street,
City = addressModel.City,
State = addressModel.State,
ZipCode = addressModel.ZipCode
};
result.Addresses.Add(newAddress);
}
}
await _appDbContext.SaveChangesAsync();
}
else
{
throw new KeyNotFoundException("Person not found");
}
return result;
}
}
}