-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSmartDeletersService.cs
121 lines (101 loc) · 4.74 KB
/
SmartDeletersService.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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.SqlClient;
using System.Linq;
using System.Reflection;
using System.Text;
namespace YourProject.Repositories.Services
{
public class SmartDeletersService
{
ApplicationDbContext _dbContext;
public SmartDeletersService(ApplicationDbContext dbContext)
{
_dbContext = dbContext;
}
//This method will check if the entity is used by another entity in the database, usefull when you want to delete an entity
//Please note that you need to pass a Type as second param
public bool IsUsedBy<T1>(T1 entity, Type toCheckIn) where T1 : class
{
var proprityInfos = entity.GetType().GetProperties().Where(p => p.Name.Contains("Id"));
PropertyInfo keyProperty = null;
foreach (var item in proprityInfos)
{
var keyAttr = item
.GetCustomAttributes(typeof(KeyAttribute), false)
.Cast<KeyAttribute>().FirstOrDefault();
if (keyAttr != null)
{
keyProperty = item;
break;
}
}
if (keyProperty != null)
{
var property = toCheckIn.GetProperties().Where(p => p.Name.Contains(keyProperty.Name)).FirstOrDefault();
if (property != null)
{
var t2TableName = _dbContext.Model.FindEntityType(toCheckIn).Relational().TableName;
var t1TableName = _dbContext.Model.FindEntityType(entity.GetType()).Relational().TableName;
int result = 0;
var sqlQuery = $"Select Count(*) from {t2TableName} where {keyProperty.Name} = {((int)keyProperty.GetValue(entity)).ToString()} AND IsDeleted = 0";
var connString = _dbContext.Database.GetDbConnection().ConnectionString;
SqlConnection conn = new SqlConnection("Server=130.117.45.25;Database=ImmoSalesTestDB;User Id=PeakTestUser;Password=%@Peak2018;");
using (SqlCommand cmd = new SqlCommand(sqlQuery, conn))
{
conn.Open();
result = (int)cmd.ExecuteScalar();
conn.Close();
if (result != 0)
return true;
}
}
}
return false;
}
//This method will check if the entity is used by other entities in the database, usefull when you want to delete an entity
public bool IsUsedBy<T1>(T1 entity, List<Type> types) where T1 : class
{
var proprityInfos = entity.GetType().GetProperties().Where(p => p.Name.Contains("Id"));
PropertyInfo keyProperty = null;
foreach (var item in proprityInfos)
{
var keyAttr = item
.GetCustomAttributes(typeof(KeyAttribute), false)
.Cast<KeyAttribute>().FirstOrDefault();
if (keyAttr != null)
{
keyProperty = item;
break;
}
}
if (keyProperty != null)
{
foreach (var toCheckIn in types)
{
var property = toCheckIn.GetProperties().Where(p => p.Name.Contains(keyProperty.Name)).FirstOrDefault();
if (property != null)
{
var t2TableName = _dbContext.Model.FindEntityType(toCheckIn).Relational().TableName;
var t1TableName = _dbContext.Model.FindEntityType(entity.GetType()).Relational().TableName;
int result = 0;
var sqlQuery = $"Select Count(*) from {t2TableName} where {keyProperty.Name} = {((int)keyProperty.GetValue(entity)).ToString()} AND IsDeleted = 0";
var connString = _dbContext.Database.GetDbConnection().ConnectionString;
SqlConnection conn = new SqlConnection("Server=130.117.45.25;Database=ImmoSalesTestDB;User Id=PeakTestUser;Password=%@Peak2018;");
using (SqlCommand cmd = new SqlCommand(sqlQuery, conn))
{
conn.Open();
result = (int)cmd.ExecuteScalar();
conn.Close();
if (result != 0)
return true;
}
}
}
}
return false;
}
}
}