Escali License control 1
MainSegmentServices.cs
Gå til dokumentasjonen til denne filen.
1using System;
2using System.Collections.Generic;
3using System.Linq;
6using Microsoft.EntityFrameworkCore;
7
9{
10
15{
16
17 private readonly DataContext _db;
18
20 {
21 _db = db;
22 }
23
28 public async Task<List<MainSegment>> GetAllMainSegments()
29 {
30 var res = await _db.MainSegments.Include(ms => ms.Segments).ToListAsync();
31 return res;
32 }
33
40 public async Task<MainSegment> GetMainSegmentById(int id)
41 {
42 var res = await _db.MainSegments.FindAsync(id);
43 return res;
44 }
45
53 public async Task<MainSegment> AddMainSegment(MainSegment mainSegment)
54 {
55 if (_db.MainSegments.Any(ms => ms.MainSegmentName == mainSegment.MainSegmentName))
56 throw new InvalidOperationException($"Name already exist");
57
58 var res = _db.MainSegments.Add(mainSegment);
59 await _db.SaveChangesAsync();
60 return res.Entity;
61 }
62
70 public async Task<MainSegment> UpdateMainSegment(MainSegment mainSegment)
71 {
72 if (_db.MainSegments.Any(ms => ms.MainSegmentName == mainSegment.MainSegmentName && ms.MainSegmentSeq != mainSegment.MainSegmentSeq))
73 throw new InvalidOperationException($"Unique name already exist");
74
75 var res = _db.MainSegments.Update(mainSegment);
76 await _db.SaveChangesAsync();
77 return res.Entity;
78 }
79
87 public async Task<MainSegment> DeleteMainSegment(int id)
88 {
89 var found = await _db.MainSegments.FindAsync(id);
90 if (found == null)
91 return null;
92
93 var mainSegment = _db.MainSegments.FindAsync(id);
94 var res = _db.MainSegments.Remove(mainSegment.Result);
95 await _db.SaveChangesAsync();
96 return res.Entity;
97 }
98
99 }
100}
101
MainSegmentService class inserts and updates MainSegment in the Database
async Task< List< MainSegment > > GetAllMainSegments()
Read all Main Segements from database
async Task< MainSegment > DeleteMainSegment(int id)
Deletes a Main Segement in the database
async Task< MainSegment > GetMainSegmentById(int id)
Read one Main Segement with id from database
async Task< MainSegment > AddMainSegment(MainSegment mainSegment)
Adds a Main Segement to the database
async Task< MainSegment > UpdateMainSegment(MainSegment mainSegment)
Updates changes on a Main Segement in the database