5using Microsoft.EntityFrameworkCore;
29 .Where(c =>
id ==
null || c.CustomerSeq ==
id)
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)
87 throw new InvalidOperationException($
"OrgNr or CustomerName already Exist");
93 if (regionSeq !=
null)
100 await
_db.SaveChangesAsync();
101 _db.ChangeTracker.Clear();
118 throw new InvalidOperationException($
"Kunde med navn '{customer.CustomerName}' finnes allerede.");
122 throw new InvalidOperationException($
"Kunde med org.nr. '{customer.OrganizationNumber}' finnes allerede.");
154 if (regionSeq !=
null)
161 _db.Entry(customer).State = EntityState.Modified;
163 await
_db.SaveChangesAsync();
164 _db.ChangeTracker.Clear();
177 if (customer ==
null)
178 throw new NullReferenceException($
"Customer with id {id} doesn't exist");
179 customer.IsActive =
false;
193 if (customer ==
null)
194 throw new NullReferenceException($
"Customer with id {id} doesn't exist");
195 customer.IsActive =
true;
209 var oldRegion = await
_db.
Regions.FindAsync(regionSeq);
210 if (oldRegion ==
null)
return false;
211 return !(oldRegion.Area == region.
Area && oldRegion.Country == region.
Country);
231 if (
string.IsNullOrEmpty(region.
Area))
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;
239 catch (InvalidOperationException e)
242 await
_db.SaveChangesAsync();
243 return newRegionObj.Entity.RegionSeq;
252 string.Equals(r.Area!.ToLower(), region.
Area.ToLower()) &&
253 string.Equals(r.Country.ToLower(), region.
Country.ToLower()));
254 return res.RegionSeq;
256 catch (InvalidOperationException e)
259 await
_db.SaveChangesAsync();
260 return newRegionObj.Entity.RegionSeq;
DbSet< Customer > Customers
string OrganizationNumber
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
CustomerService(DataContext db)
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.