Escali License control 1
ModuleLevelService.cs
Gå til dokumentasjonen til denne filen.
3using Microsoft.EntityFrameworkCore;
4
6{
7
12{
13
14 private readonly DataContext _db;
15
17 {
18 _db = db;
19 }
20
25 public async Task<List<ModuleLevel>> GetAllModuleLevels()
26 {
27 var res = await _db.ModuleLevels.ToListAsync();
28 return res;
29 }
30
37 public async Task<ModuleLevel> GetModuleLevelById(int id)
38 {
39 var res = await _db.ModuleLevels.FindAsync(id);
40 return res;
41 }
42
49 public async Task<ModuleLevel> AddModuleLevel(ModuleLevel moduleLevel)
50 {
51 if (_db.ModuleLevels.Any(m => m.ModuleLevelName == moduleLevel.ModuleLevelName && m.ModuleSeq == moduleLevel.ModuleSeq))
52 throw new InvalidOperationException($"Modulelevel with name {moduleLevel.ModuleLevelName} already exists.");
53
54 //if (moduleLevel.InvestmentLimit != null)
55 //{
56 // var roundedInvestmentLimit = Math.Round(moduleLevel.InvestmentLimit.Value, 2);
57 // moduleLevel.InvestmentLimit = roundedInvestmentLimit;
58 //}
59
60 var res = _db.ModuleLevels.Add(moduleLevel);
61 await _db.SaveChangesAsync();
62 return res.Entity;
63 }
64
72 public async Task<ModuleLevel> UpdateModuleLevel(ModuleLevel moduleLevel)
73 {
74 if (_db.ModuleLevels.Any(m => m.ModuleLevelName == moduleLevel.ModuleLevelName && m.ModuleSeq == moduleLevel.ModuleSeq && m.ModuleLevelSeq != moduleLevel.ModuleLevelSeq))
75 throw new InvalidOperationException($"Modulelevel with name {moduleLevel.ModuleLevelName} already exists.");
76
77 //if (moduleLevel.InvestmentLimit != null)
78 //{
79 // var roundedInvestmentLimit = Math.Round(moduleLevel.InvestmentLimit.Value, 2);
80 // moduleLevel.InvestmentLimit = roundedInvestmentLimit;
81 //}
82
83 var res = _db.ModuleLevels.Update(moduleLevel);
84 await _db.SaveChangesAsync();
85 return res.Entity;
86 }
87
94 public async Task<ModuleLevel> DeactivateModuleLevel(int id)
95 {
96 var moduleLevel = await _db.ModuleLevels.FindAsync(id);
97 if (moduleLevel == null) throw new NullReferenceException($"Module with id {id} doesn't exist.");
98 moduleLevel.IsActive = false;
99 var res = await UpdateModuleLevel(moduleLevel);
100 return res;
101 }
102
109 public async Task<ModuleLevel> ActivateModuleLevel(int id)
110 {
111 var moduleLevel = await _db.ModuleLevels.FindAsync(id);
112 if (moduleLevel == null) throw new NullReferenceException($"Module with id {id} doesn't exist.");
113 moduleLevel.IsActive = true;
114 var res = await UpdateModuleLevel(moduleLevel);
115 return res;
116 }
117
118 }
119}
ModuleLevelService class inserts and updates ModuleLevel in the Database
async Task< ModuleLevel > GetModuleLevelById(int id)
Read one ModuleLevel with id from database
async Task< ModuleLevel > DeactivateModuleLevel(int id)
Activates a ModuleLevel by setting IsActive attribute to false
async Task< ModuleLevel > ActivateModuleLevel(int id)
Activates a ModuleLevel by setting IsActive attribute to false
async Task< ModuleLevel > UpdateModuleLevel(ModuleLevel moduleLevel)
Updates changes on a ModuleLevel in the database
async Task< ModuleLevel > AddModuleLevel(ModuleLevel moduleLevel)
Adds a ModuleLevel to the database
async Task< List< ModuleLevel > > GetAllModuleLevels()
Read all ModuleLevels from database