-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathContactsService.cs
More file actions
100 lines (76 loc) · 3.24 KB
/
ContactsService.cs
File metadata and controls
100 lines (76 loc) · 3.24 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
namespace AddressBook;
/// <summary>
/// Manages contacts in an address book.
/// </summary>
public class ContactsService(AddressBookDbContext context, ILogger<ContactsService> logger) : IContactsService
{
public async Task<IEnumerable<Contact>> ReadAllAsync()
{
var result = await ToDtos(context.Contacts).ToListAsync();
logger.LogTrace("Read all contacts");
return result;
}
public async Task<Contact> ReadAsync(string id)
{
var element = await ToDtos(context.Contacts.Where(x => x.Id == id)).SingleOrDefaultAsync()
?? throw new KeyNotFoundException($"Contact '{id}' not found.");
logger.LogTrace("Read contact {Id}", id);
return element;
}
private static IQueryable<Contact> ToDtos(IQueryable<ContactEntity> entities)
=> entities.Select(x => new Contact {Id = x.Id, FirstName = x.FirstName, LastName = x.LastName});
public async Task<Contact> CreateAsync(Contact element)
{
var entity = new ContactEntity
{
FirstName = element.FirstName,
LastName = element.LastName
};
await context.Contacts.AddAsync(entity);
await context.SaveChangesAsync();
logger.LogDebug("Created new contact {Id}", entity.Id);
return new Contact {Id = entity.Id, FirstName = element.FirstName, LastName = element.LastName};
}
public async Task UpdateAsync(Contact element)
{
var entity = await context.Contacts.FindAsync(element.Id)
?? throw new KeyNotFoundException($"Contact '{element.Id}' not found.");
entity.FirstName = element.FirstName;
entity.LastName = element.LastName;
context.Update(entity);
await context.SaveChangesAsync();
logger.LogDebug("Updated contact {Id}", element.Id);
}
public async Task DeleteAsync(string id)
{
var entity = await context.Contacts.FindAsync(id)
?? throw new KeyNotFoundException($"Contact '{id}' not found.");
context.Contacts.Remove(entity);
await context.SaveChangesAsync();
logger.LogDebug("Deleted contact {Id}", id);
}
public async Task<Note> ReadNoteAsync(string id)
{
var note = await context.Contacts.Where(x => x.Id == id).Select(x => new Note {Content = x.Note}).SingleOrDefaultAsync()
?? throw new KeyNotFoundException($"Contact '{id}' not found.");
logger.LogTrace("Read note for contact {Id}", id);
return note;
}
public async Task SetNoteAsync(string id, Note note)
{
var entity = await context.Contacts.FindAsync(id)
?? throw new KeyNotFoundException($"Contact '{id}' not found.");
entity.Note = note.Content;
context.Update(entity);
await context.SaveChangesAsync();
logger.LogDebug("Set note for contact {Id}", id);
}
public async Task PokeAsync(string id)
{
var entity = await context.Contacts.FindAsync(id)
?? throw new KeyNotFoundException($"Contact '{id}' not found.");
entity.Pokes.Add(new PokeEntity {Timestamp = DateTime.UtcNow});
await context.SaveChangesAsync();
logger.LogDebug("Poked contact {Id}", id);
}
}