Escali License control 1
AgreementElementService.cs
Gå til dokumentasjonen til denne filen.
4using Microsoft.EntityFrameworkCore;
5
7{
8
13{
14
15 private readonly DataContext _db;
16
18 {
19 _db = db;
20 }
21
26 public async Task<List<AgreementElement>> GetAllAgreementElements()
27 {
28 var res = await _db.AgreementElements
29 .Include(ae => ae.Agreement)
30 .ThenInclude(a => a.Currency)
31 .Include(ae => ae.ModuleLevel)
32 .ToListAsync();
33 return res;
34 }
35
42 public async Task<AgreementElement> GetAgreementElementById(int id)
43 {
44 var res = await _db.AgreementElements.FindAsync(id);
45 return res;
46 }
47
55 public async Task<AgreementElement> AddAgreementElement(AgreementElement agreementElement)
56 {
57 if (agreementElement.DateFrom != null && agreementElement.DateTo != null)
58 if (!ServiceUtils.DateFromIsBeforeDateTo(agreementElement.DateFrom.Value, agreementElement.DateTo.Value))
59 throw new InvalidOperationException("DateFrom is not before DateTo.");
60
61 agreementElement.AgreementElementUpdated = DateTime.Now;
62 agreementElement.Discount ??= 0;
63
64 var res = _db.AgreementElements.Add(agreementElement);
65 await _db.SaveChangesAsync();
66 var returned = res.Entity;
67 returned.Agreement = await _db.Agreements.FindAsync(returned.AgreementSeq) ?? new Agreement();
68
69 returned.ModuleLevel = await _db.ModuleLevels.FindAsync(returned.ModuleLevelSeq) ?? new ModuleLevel();
70 return returned;
71 }
72
81 public async Task<AgreementElement> UpdateAgreementElement(AgreementElement agreementElement)
82 {
83 agreementElement.AgreementElementUpdated = DateTime.Now;
84
85 if (agreementElement.DateFrom != null && agreementElement.DateTo != null)
86 if (!ServiceUtils.DateFromIsBeforeDateTo(agreementElement.DateFrom.Value, agreementElement.DateTo.Value))
87 throw new InvalidOperationException("DateFrom is not before DateTo.");
88
89 var res = _db.AgreementElements.Update(agreementElement);
90 await _db.SaveChangesAsync();
91 return res.Entity;
92 }
93
100 public async Task<AgreementElement> DeactivateAgreementElement(int id)
101 {
102 var agreementElement = await _db.AgreementElements.FindAsync(id);
103 if (agreementElement == null)
104 throw new NullReferenceException($"AgreementElement with id {id} doesn't exist.");
105 agreementElement.IsActive = false;
106 var res = await UpdateAgreementElement(agreementElement);
107 return res;
108 }
109
116 public async Task<AgreementElement> ActivateAgreementElement(int id)
117 {
118 var agreementElement = await _db.AgreementElements.FindAsync(id);
119 if (agreementElement == null)
120 throw new NullReferenceException($"AgreementElement with id {id} doesn't exist.");
121 agreementElement.IsActive = true;
122 var res = await UpdateAgreementElement(agreementElement);
123 return res;
124 }
125
126 }
127}
AgreementElementService class inserts and updates AgreementElement in the Database
async Task< AgreementElement > GetAgreementElementById(int id)
Read one AgreementElement with id from database
async Task< AgreementElement > AddAgreementElement(AgreementElement agreementElement)
Adds a AgreementElement to the database
async Task< AgreementElement > ActivateAgreementElement(int id)
Activates a AgreementElement by setting IsActive attribute to true
async Task< AgreementElement > DeactivateAgreementElement(int id)
Deactivates a AgreementElement by setting IsActive attribute to false
async Task< List< AgreementElement > > GetAllAgreementElements()
Read all AgreementElements from database
async Task< AgreementElement > UpdateAgreementElement(AgreementElement agreementElement)
Updates changes on a AgreementElement in the database
static bool DateFromIsBeforeDateTo(DateTime dateFrom, DateTime dateTo)
Compares dateFrom and dateTo
Definition: ServiceUtils.cs:11