-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathTechnologyStackServicesAdmin.cs
More file actions
210 lines (169 loc) · 8.16 KB
/
Copy pathTechnologyStackServicesAdmin.cs
File metadata and controls
210 lines (169 loc) · 8.16 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using ServiceStack;
using ServiceStack.Configuration;
using ServiceStack.OrmLite;
using TechStacks.ServiceInterface.Notifications;
using TechStacks.ServiceModel;
using TechStacks.ServiceModel.Types;
namespace TechStacks.ServiceInterface;
[Authenticate]
public class TechnologyStackServicesAdmin(IConfiguration configuration, IMarkdownProvider markdown) : Service
{
public async Task<CreateTechnologyStackResponse> Post(CreateTechnologyStack request)
{
var slug = request.Slug;
var existingStack = await Db.SingleAsync<TechnologyStack>(q => q.Name == request.Name || q.Slug == slug);
if (existingStack != null)
throw new ArgumentException($"'{slug}' already exists");
if (string.IsNullOrEmpty(request.AppUrl) || request.AppUrl.IndexOf("://", StringComparison.Ordinal) == -1)
throw new ArgumentException("A valid URL to the Website or App is required");
if (string.IsNullOrEmpty(request.Description) || request.Description.Length < 100)
throw new ArgumentException("Summary needs to be a min of 100 chars");
var techStack = request.ConvertTo<TechnologyStack>();
var session = SessionAs<AuthUserSession>();
techStack.CreatedBy = session.UserName;
techStack.LastModifiedBy = session.UserName;
techStack.OwnerId = session.UserAuthId;
techStack.Created = DateTime.UtcNow;
techStack.LastModified = techStack.Created;
techStack.Slug = slug;
techStack.DetailsHtml = markdown.Transform(request.Details);
if (string.IsNullOrEmpty(techStack.ScreenshotUrl) && Request.Files.Length > 0)
{
techStack.ScreenshotUrl = Request.Files[0].UploadToImgur(configuration["oauth.imgur.ClientId"],
nameof(techStack.ScreenshotUrl), minWidth:858, minHeight:689, maxWidth:2600, maxHeight:2600);
}
if (string.IsNullOrEmpty(techStack.ScreenshotUrl))
throw new ArgumentException("Screenshot is Required", nameof(techStack.ScreenshotUrl));
var techIds = (request.TechnologyIds ?? new List<long>()).ToSet();
long id;
using (var trans = Db.OpenTransaction())
{
id = await Db.InsertAsync(techStack, selectIdentity: true);
if (techIds.Count > 0)
{
var techChoices = request.TechnologyIds.Map(x => new TechnologyChoice
{
TechnologyId = x,
TechnologyStackId = id,
CreatedBy = techStack.CreatedBy,
LastModifiedBy = techStack.LastModifiedBy,
OwnerId = techStack.OwnerId,
});
await Db.InsertAllAsync(techChoices);
}
trans.Commit();
}
var createdTechStack = await Db.SingleByIdAsync<TechnologyStack>(id);
var history = createdTechStack.ConvertTo<TechnologyStackHistory>();
history.TechnologyStackId = id;
history.Operation = "INSERT";
history.TechnologyIds = techIds.ToList();
await Db.InsertAsync(history);
await Db.ExecuteSqlAsync(@"update user_activity set
tech_stacks_count = (select count(*) from technology_stack where owner_id = @userIdStr)
where id = @userId",
new { userId = session.GetUserId(), userIdStr = session.UserAuthId });
Cache.FlushAll();
return new CreateTechnologyStackResponse
{
Result = createdTechStack.ConvertTo<TechStackDetails>(),
};
}
public async Task<UpdateTechnologyStackResponse> Put(UpdateTechnologyStack request)
{
var techStack = await Db.SingleByIdAsync<TechnologyStack>(request.Id);
if (techStack == null)
throw HttpError.NotFound("Tech stack not found");
if (string.IsNullOrEmpty(request.AppUrl) || request.AppUrl.IndexOf("://", StringComparison.Ordinal) == -1)
throw new ArgumentException("A valid URL to the Website or App is required");
if (string.IsNullOrEmpty(request.Description) || request.Description.Length < 100)
throw new ArgumentException("Summary needs to be a min of 100 chars");
var session = SessionAs<AuthUserSession>();
var authRepo = HostContext.AppHost.GetAuthRepository(Request);
using (authRepo as IDisposable)
{
if (techStack.IsLocked && !(techStack.OwnerId == session.UserAuthId || session.HasRole(RoleNames.Admin,authRepo)))
throw HttpError.Unauthorized(
"This TechStack is locked and can only be modified by its Owner or Admins.");
}
var techIds = (request.TechnologyIds ?? []).ToSet();
if (techStack.Details != request.Details)
{
techStack.DetailsHtml = markdown.Transform(request.Details);
}
techStack.PopulateWith(request);
techStack.LastModified = DateTime.UtcNow;
techStack.LastModifiedBy = session.UserName;
if (Request.Files.Length > 0)
{
techStack.ScreenshotUrl = Request.Files[0].UploadToImgur(configuration["oauth.imgur.ClientId"],
nameof(techStack.ScreenshotUrl), minWidth:858, minHeight:689, maxWidth:2600, maxHeight:2600);
}
if (string.IsNullOrEmpty(techStack.ScreenshotUrl))
throw new ArgumentException("Screenshot is Required", nameof(techStack.ScreenshotUrl));
using (var trans = Db.OpenTransaction())
{
await Db.SaveAsync(techStack);
var existingTechChoices = await Db.SelectAsync<TechnologyChoice>(q => q.TechnologyStackId == request.Id);
var techIdsToAdd = techIds.Except(existingTechChoices.Select(x => x.TechnologyId)).ToSet();
var techChoices = techIdsToAdd.Map(x => new TechnologyChoice
{
TechnologyId = x,
TechnologyStackId = request.Id,
CreatedBy = techStack.CreatedBy,
LastModifiedBy = techStack.LastModifiedBy,
OwnerId = techStack.OwnerId,
});
var unusedTechChoices = Db.From<TechnologyChoice>().Where(x => x.TechnologyStackId == request.Id);
if (techIds.Count > 0)
unusedTechChoices.And(x => !techIds.Contains(x.TechnologyId));
await Db.DeleteAsync(unusedTechChoices);
await Db.InsertAllAsync(techChoices);
trans.Commit();
}
var history = techStack.ConvertTo<TechnologyStackHistory>();
history.TechnologyStackId = techStack.Id;
history.Operation = "UPDATE";
history.TechnologyIds = techIds.ToList();
await Db.InsertAsync(history);
Cache.FlushAll();
var response = new UpdateTechnologyStackResponse
{
Result = techStack.ConvertTo<TechStackDetails>()
};
return response;
}
public object Delete(DeleteTechnologyStack request)
{
var stack = Db.SingleById<TechnologyStack>(request.Id);
if (stack == null)
throw HttpError.NotFound("TechStack not found");
var session = SessionAs<AuthUserSession>();
var authRepo = HostContext.AppHost.GetAuthRepository(Request);
using (authRepo as IDisposable)
{
if (stack.OwnerId != session.UserAuthId && !session.HasRole(RoleNames.Admin, authRepo))
throw HttpError.Unauthorized("Only the Owner or Admins can delete this TechStack");
}
Db.Delete<UserFavoriteTechnologyStack>(q => q.TechnologyStackId == request.Id);
Db.Delete<TechnologyChoice>(q => q.TechnologyStackId == request.Id);
Db.DeleteById<TechnologyStack>(request.Id);
var history = stack.ConvertTo<TechnologyStackHistory>();
history.TechnologyStackId = stack.Id;
history.LastModified = DateTime.UtcNow;
history.LastModifiedBy = session.UserName;
history.Operation = "DELETE";
Db.Insert(history);
Cache.FlushAll();
return new DeleteTechnologyStackResponse
{
Result = stack.ConvertTo<TechStackDetails>()
};
}
}