Escali License control 1
CustomerService.cs
Gå til dokumentasjonen til denne filen.
5using Microsoft.EntityFrameworkCore;
6
8{
9
13public class CustomerService
14{
15 private readonly DataContext _db;
16
18 {
19 _db = db;
20 }
21
26 public async Task<List<Customer>> GetAllCustomers(int? id = null)
27 {
28 var res = await _db.Customers
29 .Where(c => id == null || c.CustomerSeq == id)
30 .Include(c => c.User)
31 .Include(c => c.Region)
32 .Include(c => c.Priority)
33 .Include(c => c.Segment)
34 .ThenInclude(s => s.MainSegment)
35 .Include(c => c.Agreements)
36 .AsSplitQuery()
37 .ToListAsync();
38 return res;
39 }
40
41 // /// <summary>
42 // /// Retrieves all Customers from database including referenced tables
43 // /// </summary>
44 // /// <returns> A List of Customers</returns>
45 // public async Task<List<Customer>> GetAllCustomerWithChildren(int? id = null)
46 // {
47 // var res = await _db.Customers
48 // .Where(c => id != null && c.CustomerSeq == id)
49 // .Include(c => c.User)
50 // .Include(c => c.Agreements)
51 // .ThenInclude(a => a.AgreementElements)
52 // .ThenInclude(ae => ae.ModuleLevel)
53 // .Include(c => c.Priority)
54 // .Include(c => c.Segment)
55 // .ThenInclude(s => s.MainSegment)
56 // .Include(c => c.Region)
57 // .AsSplitQuery()
58 // .ToListAsync();
59 // return res;
60 //
61 // }
62
69 public async Task<Customer> GetCustomerById(int id)
70 {
71 var res = await _db.Customers.FindAsync(id);
72 return res;
73 }
74
82 public async Task<Customer> AddCustomer(Customer customer)
83 {
84 customer.CustomerSince = DateTime.Now;
85
86 if (_db.Customers.Any(c => c.OrganizationNumber == customer.OrganizationNumber || c.CustomerName == customer.CustomerName))
87 throw new InvalidOperationException($"OrgNr or CustomerName already Exist");
88
89 // CapitalizeFirstLetter(customer.Region?.Area);
90 // CapitalizeFirstLetter(customer.Region?.Country);
91
92 var regionSeq = await GetRegionSeq(customer.Region);
93 if (regionSeq != null)
94 {
95 customer.RegionSeq = regionSeq;
96 customer.Region = null;
97 }
98
99 var res = _db.Customers.Add(customer);
100 await _db.SaveChangesAsync();
101 _db.ChangeTracker.Clear();
102
103 return (await GetAllCustomers(res.Entity.CustomerSeq))[0];
104 }
105
113 public async Task<Customer> UpdateCustomer(Customer customer)
114 {
115
116 if (_db.Customers.Any(c => c.CustomerName == customer.CustomerName
117 && c.CustomerSeq != customer.CustomerSeq))
118 throw new InvalidOperationException($"Kunde med navn '{customer.CustomerName}' finnes allerede.");
119
120 if (_db.Customers.Any(c => c.OrganizationNumber == customer.OrganizationNumber
121 && c.CustomerSeq != customer.CustomerSeq))
122 throw new InvalidOperationException($"Kunde med org.nr. '{customer.OrganizationNumber}' finnes allerede.");
123
124 // CapitalizeFirstLetter(customer.Region?.Area);
125 // CapitalizeFirstLetter(customer.Region?.Country);
126
127 // Region checks
128 // if (customer.RegionSeq != null)
129 // {
130 // if (await CustomerRegionIsChanged(customer.RegionSeq, customer.Region))
131 // {
132 // var regionSeq = await GetRegionSeq(customer.Region);
133 // if (regionSeq != null)
134 // {
135 // customer.RegionSeq = regionSeq;
136 // customer.Region = null;
137 // }
138 // }
139 // }
140 // else
141 // {
142 // var regionSeq = await GetRegionSeq(customer.Region);
143 // if (regionSeq != null)
144 // {
145 // customer.RegionSeq = regionSeq;
146 // customer.Region = null;
147 // }
148 // }
149
150 var isChanged = await CustomerRegionIsChanged(customer.RegionSeq, customer.Region);
151 if (customer.RegionSeq != null && isChanged || customer.RegionSeq == null)
152 {
153 var regionSeq = await GetRegionSeq(customer.Region);
154 if (regionSeq != null)
155 {
156 customer.RegionSeq = regionSeq;
157 customer.Region = null;
158 }
159 }
160
161 _db.Entry(customer).State = EntityState.Modified;
162
163 await _db.SaveChangesAsync();
164 _db.ChangeTracker.Clear();
165 return (await GetAllCustomers(customer.CustomerSeq))[0];
166 }
167
174 public async Task<Customer> DeactivateCustomer(int id)
175 {
176 var customer = await _db.Customers.FindAsync(id);
177 if (customer == null)
178 throw new NullReferenceException($"Customer with id {id} doesn't exist");
179 customer.IsActive = false;
180 var res = await UpdateCustomer(customer);
181 return res;
182 }
183
190 public async Task<Customer> ActivateCustomer(int id)
191 {
192 var customer = await _db.Customers.FindAsync(id);
193 if (customer == null)
194 throw new NullReferenceException($"Customer with id {id} doesn't exist");
195 customer.IsActive = true;
196 var res = await UpdateCustomer(customer);
197 return res;
198 }
199
200
201
202 // private void CapitalizeFirstLetter(string? s)
203 // {
204 // if (!string.IsNullOrEmpty(s)) s.CapitalizeFirstLetter();
205 // }
206
207 private async Task<bool> CustomerRegionIsChanged(int? regionSeq, Region region)
208 {
209 var oldRegion = await _db.Regions.FindAsync(regionSeq);
210 if (oldRegion == null) return false;
211 return !(oldRegion.Area == region.Area && oldRegion.Country == region.Country);
212 }
213
219 public async Task<int> GetRegionSeq(Region region)
220 {
221 // if (!string.IsNullOrEmpty(region.Area))
222 // {
223 // var resAreaCountry = await _db.Regions.FirstOrDefaultAsync(r => r.Area == region.Area && r.Country == region.Country);
224 // return resAreaCountry?.RegionSeq;
225 // }
226 // var resCountry = await _db.Regions.FirstOrDefaultAsync(r => string.IsNullOrEmpty(r.Area) && r.Country == region.Country);
227 // return resCountry?.RegionSeq;
228
229 // if (string.IsNullOrEmpty(region.Area)) region.Area = null;
230
231 if (string.IsNullOrEmpty(region.Area))
232 {
233 try
234 {
235 var res = await _db.Regions.Where(r => string.Equals(r.Country.ToLower(), region.Country.ToLower())).Where(r => r.Area == null).FirstAsync();
236 Console.WriteLine(res.RegionSeq);
237 return res.RegionSeq;
238 }
239 catch (InvalidOperationException e)
240 {
241 var newRegionObj = _db.Regions.Add(region);
242 await _db.SaveChangesAsync();
243 return newRegionObj.Entity.RegionSeq;
244 }
245
246 }
247
248
249 try
250 {
251 var res = await _db.Regions.SingleAsync(r =>
252 string.Equals(r.Area!.ToLower(), region.Area.ToLower()) &&
253 string.Equals(r.Country.ToLower(), region.Country.ToLower()));
254 return res.RegionSeq;
255 }
256 catch (InvalidOperationException e)
257 {
258 var newRegionObj = _db.Regions.Add(region);
259 await _db.SaveChangesAsync();
260 return newRegionObj.Entity.RegionSeq;
261 }
262 }
263
264 }
265}
CustomerService class inserts and updates Customer in the Database
async Task< Customer > DeactivateCustomer(int id)
Deactivated a Customer by setting IsActive attribute to false
async Task< Customer > AddCustomer(Customer customer)
Adds a Customer to the database
async Task< bool > CustomerRegionIsChanged(int? regionSeq, Region region)
async Task< List< Customer > > GetAllCustomers(int? id=null)
Read all Customers from database
async Task< Customer > GetCustomerById(int id)
Read one Customer with id from database
async Task< Customer > ActivateCustomer(int id)
Activates a Customer by setting IsActive attribute to true
async Task< Customer > UpdateCustomer(Customer customer)
Updates changes on a Customer in the database
async Task< int > GetRegionSeq(Region region)
Checks in DB if it already exists a combination of area and country.