| | 1 | | using System.Collections.Generic; |
| | 2 | | using System.Linq; |
| | 3 | | using SampleDotNetCore2RestStub.Models; |
| | 4 | |
|
| | 5 | | namespace SampleDotNetCore2RestStub.Repositories |
| | 6 | | { |
| | 7 | | public class PersonRepository : IPersonRepository |
| | 8 | | { |
| 0 | 9 | | private Dictionary<int, Person> _persons = new Dictionary<int, Person>(); |
| | 10 | |
|
| 0 | 11 | | public PersonRepository() |
| | 12 | | { |
| 0 | 13 | | _persons.Add(1, new Person { Id = 1, FirstName = "FN1", LastName = "LN1", Email = "email1@email.na" }); |
| 0 | 14 | | _persons.Add(2, new Person { Id = 2, FirstName = "FN2", LastName = "LN2", Email = "email2@email.na" }); |
| 0 | 15 | | _persons.Add(3, new Person { Id = 3, FirstName = "FN3", LastName = "LN3", Email = "email3@email.na" }); |
| 0 | 16 | | _persons.Add(4, new Person { Id = 4, FirstName = "FN4", LastName = "LN4", Email = "email4@email.na" }); |
| | 17 | | } |
| | 18 | |
|
| | 19 | | public Person GetById(int id) |
| | 20 | | { |
| 0 | 21 | | return _persons[id]; |
| | 22 | | } |
| | 23 | |
|
| | 24 | | public List<Person> GetAll() |
| | 25 | | { |
| 0 | 26 | | return _persons.Values.ToList(); |
| | 27 | | } |
| | 28 | |
|
| | 29 | | public int GetCount() |
| | 30 | | { |
| 0 | 31 | | return _persons.Count(); |
| | 32 | | } |
| | 33 | |
|
| | 34 | | public void Remove() |
| | 35 | | { |
| 0 | 36 | | if (_persons.Keys.Any()) |
| | 37 | | { |
| 0 | 38 | | _persons.Remove(_persons.Keys.Last()); |
| | 39 | | } |
| | 40 | | } |
| | 41 | |
|
| | 42 | | public string Save(Person person) |
| | 43 | | { |
| 0 | 44 | | if (_persons.ContainsKey(person.Id)) |
| | 45 | | { |
| 0 | 46 | | _persons[person.Id] = person; |
| 0 | 47 | | return "Updated Person with id=" + person.Id; |
| | 48 | | } |
| | 49 | | else |
| | 50 | | { |
| 0 | 51 | | _persons.Add(person.Id, person); |
| 0 | 52 | | return "Added Person with id=" + person.Id; |
| | 53 | | } |
| | 54 | | } |
| | 55 | | } |
| | 56 | | } |