This repository has been archived by the owner on Apr 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPetCupidRepository.cs
60 lines (50 loc) · 1.52 KB
/
PetCupidRepository.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
using System.Collections.Generic;
using System.Linq;
namespace PetCupid
{
public class PetCupidRepository
{
private PetCupidContext _context;
public PetCupidRepository(PetCupidContext context)
{
_context = context;
}
public IEnumerable<PetCupid> Get()
{
return _context.PetCupids;
}
public PetCupid Get(int id)
{
return _context.PetCupids.SingleOrDefault(pc => pc.Id == id);
}
public void Create(PetCupid petCupid)
{
var newId = _context.PetCupids.Max(pc => pc.Id) + 1;
petCupid.Id = newId;
_context.PetCupids.Add(petCupid);
_context.SaveChanges();
}
public void Update(int id, PetCupid petCupid)
{
var currentPetCupid = this.Get(id);
if (currentPetCupid != null)
{
currentPetCupid.Name = petCupid.Name;
currentPetCupid.Kind = petCupid.Kind;
currentPetCupid.Img = petCupid.Img;
currentPetCupid.ProfileText = petCupid.ProfileText;
currentPetCupid.Popularity = petCupid.Popularity;
}
_context.SaveChanges();
}
public void Delete(int id)
{
var petCupidToDelete = this.Get(id);
if (petCupidToDelete != null)
{
_context.PetCupids.Remove(petCupidToDelete);
_context.SaveChanges();
}
}
}
}