-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDashboardMutations.cs
More file actions
81 lines (67 loc) · 2.49 KB
/
DashboardMutations.cs
File metadata and controls
81 lines (67 loc) · 2.49 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
using HotChocolate;
using HotChocolate.Types;
using PhantomDave.BankTracking.Api.Types.Inputs;
using PhantomDave.BankTracking.Api.Types.ObjectTypes;
using PhantomDave.BankTracking.Data.UnitOfWork;
using PhantomDave.BankTracking.Library.Models;
namespace PhantomDave.BankTracking.Api.Types.Mutations;
[ExtendObjectType(OperationTypeNames.Mutation)]
public class DashboardMutations
{
public async Task<DashboardType> CreateDashboard(
[Service] IUnitOfWork unitOfWork,
[Service] IHttpContextAccessor httpContextAccessor,
CreateDashboardInput input)
{
var accountId = httpContextAccessor.GetAccountIdFromContext();
var dashboard = new Dashboard
{
AccountId = accountId,
Name = input.Name?.Trim() ?? string.Empty
};
await unitOfWork.Dashboards.AddAsync(dashboard);
await unitOfWork.SaveChangesAsync();
return DashboardType.FromDashboard(dashboard);
}
public async Task<DashboardType> UpdateDashboard(
[Service] IUnitOfWork unitOfWork,
[Service] IHttpContextAccessor httpContextAccessor,
UpdateDashboardInput input)
{
var accountId = httpContextAccessor.GetAccountIdFromContext();
var dashboard = await unitOfWork.Dashboards.GetByIdAsync(input.Id);
if (dashboard == null || dashboard.AccountId != accountId)
{
throw new GraphQLException(
ErrorBuilder.New()
.SetMessage("Dashboard not found.")
.SetCode("NOT_FOUND")
.Build());
}
if (input.Name != null)
{
dashboard.Name = input.Name.Trim();
}
await unitOfWork.SaveChangesAsync();
return DashboardType.FromDashboard(dashboard);
}
public async Task<bool> DeleteDashboard(
[Service] IUnitOfWork unitOfWork,
[Service] IHttpContextAccessor httpContextAccessor,
int id)
{
var accountId = httpContextAccessor.GetAccountIdFromContext();
var dashboard = await unitOfWork.Dashboards.GetByIdAsync(id);
if (dashboard == null || dashboard.AccountId != accountId)
{
throw new GraphQLException(
ErrorBuilder.New()
.SetMessage("Dashboard not found.")
.SetCode("NOT_FOUND")
.Build());
}
await unitOfWork.Dashboards.DeleteAsync(id);
await unitOfWork.SaveChangesAsync();
return true;
}
}