Skip to content

Commit 8aa4bff

Browse files
committed
[Feat] EF Core integrated to App
1 parent 0a20589 commit 8aa4bff

File tree

5 files changed

+93
-82
lines changed

5 files changed

+93
-82
lines changed

AdminCrud/Controllers/UserController.cs

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ public class UserController : Controller
1616

1717
public UserController()
1818
{
19-
this.userList = FakeUser.GetUsers();
19+
// this.userList = FakeUser.GetUsers();
2020
}
2121
// GET: api/User
2222
[HttpGet]
2323
public IEnumerable<User> Get()
2424
{
25-
lock (this.userList.Users)
25+
using (var context = new UserContext())
2626
{
27-
return this.userList.Users;
27+
return context.Users.ToList();
2828
}
2929
}
3030

@@ -34,8 +34,10 @@ public IActionResult Get(int id)
3434
{
3535
try
3636
{
37-
38-
return Ok(this.userList.Users.First(us => us.Id == id));
37+
using (var context = new UserContext())
38+
{
39+
return Ok(context.Users.First(us => us.Id == id));
40+
}
3941
}
4042
catch (Exception)
4143
{
@@ -50,20 +52,22 @@ public IActionResult Get(int id)
5052
public User Post([FromBody]User value)
5153
{
5254

53-
lock (this.userList.Users)
54-
{
5555

56-
this.userList.Users.Add(new Models.User()
56+
57+
using (var context = new UserContext())
58+
{
59+
var newUser = new Models.User()
5760
{
5861
Name = value.Name,
5962
DateOfBirth = value.DateOfBirth,
6063
UserRole = value.UserRole,
6164
EmailId = value.EmailId,
62-
Id = this.userList.Users.Last().Id + 1,
63-
});
65+
};
66+
context.Users.Add(newUser);
67+
context.SaveChanges();
68+
return newUser;
6469
}
6570

66-
return this.userList.Users.Last();
6771

6872
}
6973

@@ -77,11 +81,18 @@ public void Put(int id, [FromBody]string value)
7781
[HttpDelete("{id}")]
7882
public object Delete(int id)
7983
{
80-
var num = this.userList.Users.RemoveAll(usr => usr.Id == id);
81-
if (num == 0)
84+
using (var cxt = new UserContext())
8285
{
83-
return Json(new { StatusCode = 404, Message = "NO User was found." });
86+
var delUser = cxt.Users.Find(id);
87+
if (delUser == null)
88+
{
89+
return Json(new { StatusCode = 404, Message = "NO User was found." });
90+
}
91+
cxt.Users.Remove(delUser);
92+
cxt.SaveChanges();
93+
8494
}
95+
8596
return Json(new { StatusCode = 200, Message = "User was deleted succesfully." });
8697
}
8798
}

AdminCrud/Models/DbContext.cs

Lines changed: 0 additions & 19 deletions
This file was deleted.

AdminCrud/Models/FakeContext.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using Microsoft.EntityFrameworkCore;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
7+
namespace AdminCrud.Models
8+
{
9+
public class FakeUser
10+
{
11+
private static FakeUser _single;
12+
public FakeUser()
13+
{
14+
//int user_seq = 1;
15+
Users = new List<User>();
16+
17+
for (int user_seq = 1; user_seq < 100; user_seq++)
18+
{
19+
20+
Users.Add(new User()
21+
{
22+
Id = user_seq,
23+
Name = "User " + user_seq,
24+
EmailId = String.Format("user{0}@crud.com", user_seq),
25+
DateOfBirth = new DateTimeOffset(1980 + user_seq, user_seq % 12 + 1, user_seq % 28 + 1, 0, 0, 0, TimeSpan.Zero),
26+
UserRole = Role.User
27+
});
28+
}
29+
30+
}
31+
32+
internal static FakeUser GetUsers()
33+
{
34+
var a = new { };
35+
lock (a)
36+
{
37+
if (_single == null)
38+
{
39+
_single = new FakeUser();
40+
}
41+
}
42+
return _single;
43+
}
44+
45+
public List<User> Users { get; set; }
46+
}
47+
}

AdminCrud/Models/UserContext.cs

Lines changed: 19 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,19 @@
1-
using Microsoft.EntityFrameworkCore;
2-
using System;
3-
using System.Collections.Generic;
4-
using System.Linq;
5-
using System.Threading.Tasks;
6-
7-
namespace AdminCrud.Models
8-
{
9-
public class FakeUser
10-
{
11-
private static FakeUser _single;
12-
public FakeUser()
13-
{
14-
//int user_seq = 1;
15-
Users = new List<User>();
16-
17-
for (int user_seq = 1; user_seq < 100; user_seq++)
18-
{
19-
20-
Users.Add(new User()
21-
{
22-
Id = user_seq,
23-
Name = "User " + user_seq,
24-
EmailId = String.Format("user{0}@crud.com", user_seq),
25-
DateOfBirth = new DateTimeOffset(1980 + user_seq, user_seq % 12 + 1, user_seq % 28 + 1, 0, 0, 0, TimeSpan.Zero),
26-
UserRole = Role.User
27-
});
28-
}
29-
30-
}
31-
32-
internal static FakeUser GetUsers()
33-
{
34-
var a = new { };
35-
lock (a)
36-
{
37-
if (_single == null)
38-
{
39-
_single = new FakeUser();
40-
}
41-
}
42-
return _single;
43-
}
44-
45-
public List<User> Users { get; set; }
46-
}
47-
}
1+
using JetBrains.Annotations;
2+
using Microsoft.EntityFrameworkCore;
3+
4+
namespace AdminCrud.Models
5+
{
6+
public class UserContext : DbContext
7+
{
8+
public UserContext() : base()
9+
{
10+
}
11+
protected override void OnConfiguring(DbContextOptionsBuilder opts)
12+
{
13+
opts.UseSqlServer(@"Server=Delli5\SQLEx; Database = master; User id=sa;Password=info123!");
14+
}
15+
16+
17+
public DbSet<User> Users { get; set; }
18+
}
19+
}

AdminCrud/Test/api.http

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ Content-Type: application/json; charset=UTF-8
1313

1414
{
1515
"Id":101,
16-
"Name":"User 101",
16+
"Name":"User 02",
1717
"EmailId": "[email protected]",
1818
"dateOfBirth":"1999-02-02T00:00:00+00:00",
19-
"UserRole":2
19+
"UserRole":3
2020
}
2121

2222
###

0 commit comments

Comments
 (0)