-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBundleController.cs
More file actions
179 lines (163 loc) · 6.36 KB
/
BundleController.cs
File metadata and controls
179 lines (163 loc) · 6.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Data;
using Viper.Areas.CTS.Models;
using Viper.Classes;
using Viper.Classes.SQLContext;
using Viper.Models.CTS;
using Web.Authorization;
using Microsoft.Data.SqlClient;
namespace Viper.Areas.CTS.Controllers
{
[Route("/api/cts/bundles/")]
[Permission(Allow = "SVMSecure.CTS")]
public class BundleController : ApiController
{
private readonly VIPERContext context;
public BundleController(VIPERContext context)
{
this.context = context;
}
[HttpGet]
public async Task<ActionResult<List<BundleDto>>> GetBundles(bool? clinical = null, bool? assessment = null, bool? milestone = null,
int? serviceId = null, int? roleId = null)
{
var bundleQuery = context.Bundles
.AsNoTracking()
.Include(b => b.BundleRoles)
.ThenInclude(br => br.Role)
.Include(b => b.BundleCompetencies)
.AsQueryable();
if (clinical != null)
{
bundleQuery = bundleQuery.Where(b => b.Clinical == clinical);
}
if (assessment != null)
{
bundleQuery = bundleQuery.Where(b => b.Assessment == assessment);
}
if (milestone != null)
{
bundleQuery = bundleQuery.Where(b => b.Milestone == milestone);
}
if (serviceId != null)
{
bundleQuery = bundleQuery.Where(b => b.BundleServices.Any(s => s.ServiceId == serviceId));
}
if (roleId != null)
{
bundleQuery = bundleQuery.Where(b => b.BundleRoles.Any(br => br.RoleId == roleId));
}
var bundles = await bundleQuery
.OrderBy(b => b.Name)
.ToListAsync();
return CtsMapper.ToBundleDtos(bundles);
}
[HttpGet("{bundleId}")]
public async Task<ActionResult<BundleDto>> GetBundle(int bundleId)
{
var bundle = await context.Bundles
.Include(b => b.BundleRoles)
.ThenInclude(br => br.Role)
.Where(b => b.BundleId == bundleId)
.FirstOrDefaultAsync();
if (bundle == null)
{
return NotFound();
}
return CtsMapper.ToBundleDto(bundle);
}
[HttpPost]
[Permission(Allow = "SVMSecure.CTS.Manage")]
public async Task<ActionResult<BundleDto>> AddBundle(BundleDto bundleDto)
{
if (string.IsNullOrEmpty(bundleDto.Name))
{
return BadRequest("Bundle Name is required.");
}
var nameCheck = await context.Bundles.Where(b => b.Name == bundleDto.Name).AnyAsync();
if (nameCheck)
{
return BadRequest("Bundle Name must be unique.");
}
Bundle b = CtsMapper.ToBundle(bundleDto);
context.Add(b);
await context.SaveChangesAsync();
return CtsMapper.ToBundleDto(b);
}
[HttpPut("{bundleId}")]
[Permission(Allow = "SVMSecure.CTS.Manage")]
public async Task<ActionResult<BundleDto>> UpdateBundle(int bundleId, BundleDto bundleDto)
{
var exists = await context.Bundles.AnyAsync(b => b.BundleId == bundleId);
if (!exists)
{
return NotFound();
}
if (string.IsNullOrEmpty(bundleDto.Name))
{
return BadRequest("Bundle Name is required.");
}
var nameCheck = await context.Bundles.Where(b => b.Name == bundleDto.Name && b.BundleId != bundleId).AnyAsync();
if (nameCheck)
{
return BadRequest("Bundle Name must be unique.");
}
Bundle b = CtsMapper.ToBundle(bundleDto);
context.Update(b);
await context.SaveChangesAsync();
return CtsMapper.ToBundleDto(b);
}
[HttpDelete("{bundleId}")]
[Permission(Allow = "SVMSecure.CTS.Manage")]
public async Task<ActionResult<BundleDto>> DeleteBundle(int bundleId)
{
var bundle = await context.Bundles.FindAsync(bundleId);
if (bundle == null)
{
return NotFound();
}
try
{
using var trans = await context.Database.BeginTransactionAsync();
var bundleRoles = context.BundleRoles.Where(br => br.BundleId == bundleId);
foreach (var role in bundleRoles)
{
context.Remove(role);
}
context.Entry(bundle).State = EntityState.Deleted;
await context.SaveChangesAsync();
await trans.CommitAsync();
}
catch (Exception ex) when (ex is DbUpdateException or SqlException or InvalidOperationException or OperationCanceledException)
{
return BadRequest("Could not delete bundle. If this bundle has been used, it cannot be deleted.");
}
return CtsMapper.ToBundleDto(bundle);
}
/* Bundle Roles */
[HttpPut("{bundleId}/roles/")]
[Permission(Allow = "SVMSecure.CTS.Manage")]
public async Task<ActionResult<List<RoleDto>>> SetBundleRoles(int bundleId, List<int> bundleRoles)
{
using var trans = await context.Database.BeginTransactionAsync();
var existing = await context.BundleRoles.Where(br => br.BundleId == bundleId).ToListAsync();
foreach (var brId in bundleRoles.Where(brId => !existing.Any(e => e.RoleId == brId)))
{
context.Add(new BundleRole()
{
BundleId = bundleId,
RoleId = brId
});
}
foreach (var e in existing.Where(e => !bundleRoles.Any(brId => brId == e.RoleId)))
{
context.Entry(e).State = EntityState.Deleted;
}
await context.SaveChangesAsync();
await trans.CommitAsync();
var roles = await context.BundleRoles.Where(br => br.BundleId == bundleId).Select(br => br.Role).ToListAsync();
return CtsMapper.ToRoleDtos(roles);
}
}
}