-
Notifications
You must be signed in to change notification settings - Fork 0
/
ToDoRepository.cs
65 lines (56 loc) · 1.77 KB
/
ToDoRepository.cs
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
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Threading.Tasks;
using TodoList.Entities;
using TodoList.Infrastructure;
namespace TodoList
{
public interface IToDoRepository
{
List<ToDoDto> GetAll();
int Create(ToDoDto toDoDto);
void Update(int id, ToDoDto toDoDto);
}
public class ToDoRepository : IToDoRepository
{
private class ToDo
{
public int Id { get; }
public string Name { get; set; }
public bool Done { get; set; }
public DateTime creationDate { get; }
public ToDo(int id, string name, bool done)
{
Id = id;
Name = name;
Done = done;
creationDate = DateTime.Now;
}
}
readonly ToDoDbContext _context;
readonly IUnitOfWork _unitOfWork;
public ToDoRepository( ToDoDbContext context, IUnitOfWork unitOfWork )
{
_context = context;
_unitOfWork = unitOfWork;
}
public List<ToDoDto> GetAll() => _context.Set<ToDoEntity>().ToList().Select( item => new ToDoDto( item ) ).ToList();
public int Create( ToDoDto toDoDto )
{
ToDoEntity newEntity = new ToDoEntity();
toDoDto.CopyTo( newEntity );
_context.Set<ToDoEntity>().Add( newEntity );
_unitOfWork.Commit();
return newEntity.Id;
}
public void Update( int id, ToDoDto toDoDto )
{
ToDoEntity foundEntity = _context.Set<ToDoEntity>().SingleOrDefault( item => item.Id == id );
toDoDto.CopyTo( foundEntity );
_unitOfWork.Commit();
}
}
}