2
2
using System . Collections . Generic ;
3
3
using System . Linq ;
4
4
using System . Threading . Tasks ;
5
+ using BlazorDiffusion . ServiceInterface . App ;
5
6
using ServiceStack ;
6
7
using ServiceStack . OrmLite ;
7
8
using BlazorDiffusion . ServiceModel ;
9
+ using ServiceStack . Jobs ;
8
10
9
11
namespace BlazorDiffusion . ServiceInterface ;
10
12
11
- public class AlbumServices ( ICrudEvents crudEvents ) : Service
13
+ public class AlbumServices ( IBackgroundJobs jobs , ICrudEvents crudEvents ) : Service
12
14
{
13
- public async Task < object > Any ( CreateAlbum request )
15
+ public object Any ( CreateAlbum request )
14
16
{
15
17
if ( string . IsNullOrEmpty ( request . Name ) )
16
18
throw new ArgumentNullException ( nameof ( request . Name ) ) ;
17
19
18
20
var slug = request . Name . GenerateSlug ( ) ;
19
21
20
- if ( await Db . ExistsAsync < Album > ( x => x . Slug == slug ) )
22
+ if ( Db . Exists < Album > ( x => x . Slug == slug ) )
21
23
throw HttpError . Conflict ( "Album already exists" ) ;
22
24
23
- var session = await SessionAsAsync < CustomUserSession > ( ) ;
25
+ var userId = Request . GetRequiredUserId ( ) ;
24
26
25
27
var album = request . ConvertTo < Album > ( ) ;
26
- album . OwnerId = session . GetUserId ( ) ;
28
+ album . OwnerId = userId ;
27
29
album . OwnerRef = Db . GetUserRef ( album . OwnerId ) ;
28
30
album . RefId = Guid . NewGuid ( ) . ToString ( "D" ) ;
29
31
album . Slug = slug ;
30
- album . WithAudit ( session . UserAuthId ) ;
32
+ album . WithAudit ( userId ) ;
31
33
32
- // TODO CrudEvents.RecordAsync
33
- album . Id = ( int ) await Db . InsertAsync ( album , selectIdentity : true ) ;
34
-
35
- if ( request . ArtifactIds ? . Count > 0 )
34
+ lock ( Locks . AppDb )
36
35
{
37
- var albumArtifacts = request . ArtifactIds . Map ( x => new AlbumArtifact
36
+ album . Id = ( int ) Db . Insert ( album , selectIdentity : true ) ;
37
+
38
+ if ( request . ArtifactIds ? . Count > 0 )
38
39
{
39
- AlbumId = album . Id ,
40
- ArtifactId = x ,
41
- CreatedDate = album . CreatedDate ,
42
- ModifiedDate = album . ModifiedDate ,
43
- } ) ;
44
- await Db . InsertAllAsync ( albumArtifacts ) ;
45
- album . Artifacts = albumArtifacts ;
46
- }
40
+ var albumArtifacts = request . ArtifactIds . Map ( x => new AlbumArtifact
41
+ {
42
+ AlbumId = album . Id ,
43
+ ArtifactId = x ,
44
+ CreatedDate = album . CreatedDate ,
45
+ ModifiedDate = album . ModifiedDate ,
46
+ } ) ;
47
+ Db . InsertAll ( albumArtifacts ) ;
48
+ album . Artifacts = albumArtifacts ;
49
+ }
47
50
48
- var crudContext = CrudContext . Create < Album > ( Request , Db , request , AutoCrudOperation . Create ) ;
49
- await crudEvents . RecordAsync ( crudContext ) ;
51
+ var crudContext = CrudContext . Create < Album > ( Request , Db , request , AutoCrudOperation . Create ) ;
52
+ crudEvents . Record ( crudContext ) ;
53
+ }
50
54
51
55
return album ;
52
56
}
53
57
54
- public async Task < object > Any ( UpdateAlbum request )
58
+ public object Any ( UpdateAlbum request )
55
59
{
56
- var session = await GetSessionAsync ( ) ;
57
-
58
- var album = await Db . LoadSingleByIdAsync < Album > ( request . Id ) ;
60
+ var album = Db . LoadSingleById < Album > ( request . Id ) ;
59
61
if ( album == null )
60
62
throw HttpError . NotFound ( "Album not found" ) ;
61
63
62
- if ( ! await session . IsOwnerOrModerator ( album . OwnerId ) )
64
+ if ( ! Request . IsOwnerOrModerator ( album . OwnerId ) )
63
65
throw HttpError . Forbidden ( "You don't own this Album" ) ;
64
-
65
- using var trans = Db . OpenTransaction ( ) ;
66
- var updateAlbum = request . Name != null || request . Description != null || request . Slug != null || request . Tags ? . Count > 0 ;
67
- if ( updateAlbum )
66
+
67
+ lock ( Locks . AppDb )
68
68
{
69
- if ( album . Name != null )
69
+ using var trans = Db . OpenTransaction ( ) ;
70
+ var updateAlbum = request . Name != null || request . Description != null || request . Slug != null || request . Tags ? . Count > 0 ;
71
+ if ( updateAlbum )
70
72
{
71
- album . Slug = album . Name . GenerateSlug ( ) ;
73
+ if ( album . Name != null )
74
+ {
75
+ album . Slug = album . Name . GenerateSlug ( ) ;
76
+ }
77
+ album . PopulateWithNonDefaultValues ( request ) . WithAudit ( Request ! ) ;
78
+ Db . UpdateNonDefaults ( album , x => x . Id == album . Id ) ;
72
79
}
73
- album . PopulateWithNonDefaultValues ( request ) . WithAudit ( session . UserAuthId ) ;
74
- await Db . UpdateNonDefaultsAsync ( album , x => x . Id == album . Id ) ;
75
- }
76
80
77
- if ( request . RemoveArtifactIds ? . Count > 0 )
78
- {
79
- await Db . DeleteAsync < AlbumArtifact > ( x => x . AlbumId == album . Id && request . RemoveArtifactIds . Contains ( x . ArtifactId ) ) ;
80
- // Delete Album if it no longer contains any Artifacts
81
- if ( ! await Db . ExistsAsync < AlbumArtifact > ( x => x . AlbumId == album . Id ) )
81
+ if ( request . RemoveArtifactIds ? . Count > 0 )
82
82
{
83
- await Db . DeleteByIdAsync < Album > ( album . Id ) ;
83
+ Db . Delete < AlbumArtifact > ( x => x . AlbumId == album . Id && request . RemoveArtifactIds . Contains ( x . ArtifactId ) ) ;
84
+ // Delete Album if it no longer contains any Artifacts
85
+ if ( ! Db . Exists < AlbumArtifact > ( x => x . AlbumId == album . Id ) )
86
+ {
87
+ Db . DeleteById < Album > ( album . Id ) ;
88
+ }
89
+ else if ( album . PrimaryArtifactId != null && request . RemoveArtifactIds . Contains ( album . PrimaryArtifactId . Value ) )
90
+ {
91
+ Db . UpdateOnly ( ( ) => new Album { PrimaryArtifactId = null } , where : x => x . Id == album . Id ) ;
92
+ }
93
+ album . Artifacts . RemoveAll ( x => request . RemoveArtifactIds . Contains ( x . ArtifactId ) ) ; // required so they get added below
94
+ request . RemoveArtifactIds . ForEach ( Updated . ArtifactIds . Add ) ; //rerender artifact .html
84
95
}
85
- else if ( album . PrimaryArtifactId != null && request . RemoveArtifactIds . Contains ( album . PrimaryArtifactId . Value ) )
96
+ if ( request . AddArtifactIds ? . Count > 0 )
86
97
{
87
- await Db . UpdateOnlyAsync ( ( ) => new Album { PrimaryArtifactId = null } , where : x => x . Id == album . Id ) ;
98
+ var albumArtifacts = request . AddArtifactIds . Where ( x => album . Artifacts . OrEmpty ( ) . All ( a => a . ArtifactId != x ) )
99
+ . Map ( x => new AlbumArtifact
100
+ {
101
+ AlbumId = album . Id ,
102
+ ArtifactId = x ,
103
+ CreatedDate = album . CreatedDate ,
104
+ ModifiedDate = album . ModifiedDate ,
105
+ } ) ;
106
+ Db . InsertAll ( albumArtifacts ) ;
107
+ request . AddArtifactIds . ForEach ( Updated . ArtifactIds . Add ) ; //rerender artifact .html
108
+ }
109
+ if ( request . PrimaryArtifactId != null )
110
+ {
111
+ if ( request . UnpinPrimaryArtifact != true )
112
+ Db . UpdateOnly ( ( ) => new Album { PrimaryArtifactId = request . PrimaryArtifactId } , where : x => x . Id == album . Id ) ;
113
+ else
114
+ Db . UpdateOnly ( ( ) => new Album { PrimaryArtifactId = null } , where : x => x . Id == album . Id ) ;
88
115
}
89
- album . Artifacts . RemoveAll ( x => request . RemoveArtifactIds . Contains ( x . ArtifactId ) ) ; // required so they get added below
90
- request . RemoveArtifactIds . ForEach ( Updated . ArtifactIds . Add ) ; //rerender artifact .html
91
- }
92
- if ( request . AddArtifactIds ? . Count > 0 )
93
- {
94
- var albumArtifacts = request . AddArtifactIds . Where ( x => album . Artifacts . OrEmpty ( ) . All ( a => a . ArtifactId != x ) )
95
- . Map ( x => new AlbumArtifact
96
- {
97
- AlbumId = album . Id ,
98
- ArtifactId = x ,
99
- CreatedDate = album . CreatedDate ,
100
- ModifiedDate = album . ModifiedDate ,
101
- } ) ;
102
- await Db . InsertAllAsync ( albumArtifacts ) ;
103
- request . AddArtifactIds . ForEach ( Updated . ArtifactIds . Add ) ; //rerender artifact .html
104
- }
105
- if ( request . PrimaryArtifactId != null )
106
- {
107
- if ( request . UnpinPrimaryArtifact != true )
108
- await Db . UpdateOnlyAsync ( ( ) => new Album { PrimaryArtifactId = request . PrimaryArtifactId } , where : x => x . Id == album . Id ) ;
109
- else
110
- await Db . UpdateOnlyAsync ( ( ) => new Album { PrimaryArtifactId = null } , where : x => x . Id == album . Id ) ;
111
- }
112
116
113
- if ( updateAlbum )
114
- {
115
- var crudContext = CrudContext . Create < Album > ( Request , Db , request , AutoCrudOperation . Patch ) ;
116
- await crudEvents . RecordAsync ( crudContext ) ;
117
- }
117
+ if ( updateAlbum )
118
+ {
119
+ var crudContext = CrudContext . Create < Album > ( Request , Db , request , AutoCrudOperation . Patch ) ;
120
+ crudEvents . Record ( crudContext ) ;
121
+ }
118
122
119
- trans . Commit ( ) ;
123
+ trans . Commit ( ) ;
120
124
121
- PublishMessage ( new BackgroundTasks {
122
- ArtifactIdsAddedToAlbums = request . AddArtifactIds ,
123
- ArtifactIdsRemovedFromAlbums = request . RemoveArtifactIds ,
124
- } ) ;
125
+ jobs . RunCommand < UpdateScoresCommand > ( new UpdateScores {
126
+ ArtifactIdsAddedToAlbums = request . AddArtifactIds ,
127
+ ArtifactIdsRemovedFromAlbums = request . RemoveArtifactIds ,
128
+ } ) ;
129
+ }
125
130
126
131
return album ;
127
132
}
@@ -146,7 +151,7 @@ public async Task<object> Any(GetCreativesInAlbums request)
146
151
artifact . ArtifactId ,
147
152
} ) ;
148
153
149
- var albumRefs = await Db . SelectAsync < AlbumArtifactResult > ( q ) ;
154
+ var albumRefs = Db . Select < AlbumArtifactResult > ( q ) ;
150
155
151
156
var albumMap = new Dictionary < int , AlbumResult > ( ) ;
152
157
foreach ( var albumRef in albumRefs )
@@ -162,8 +167,8 @@ public async Task<object> Any(GetCreativesInAlbums request)
162
167
OwnerRef = albumRef . OwnerRef ,
163
168
Score = albumRef . Score ,
164
169
ArtifactIds = albumRef . PrimaryArtifactId != null
165
- ? new ( ) { albumRef . PrimaryArtifactId . Value }
166
- : new ( ) ,
170
+ ? [ albumRef . PrimaryArtifactId . Value ]
171
+ : [ ] ,
167
172
} ;
168
173
169
174
album . ArtifactIds . AddIfNotExists ( albumRef . ArtifactId ) ;
@@ -175,30 +180,36 @@ public async Task<object> Any(GetCreativesInAlbums request)
175
180
} ;
176
181
}
177
182
178
- public async Task < object > Post ( CreateAlbumLike request )
183
+ public object Post ( CreateAlbumLike request )
179
184
{
180
- var session = await SessionAsAsync < CustomUserSession > ( ) ;
181
- var userId = session . GetUserId ( ) ;
185
+ var userId = Request . GetRequiredUserId ( ) ;
182
186
var row = new AlbumLike
183
187
{
184
188
AppUserId = userId ,
185
189
AlbumId = request . AlbumId ,
186
190
CreatedDate = DateTime . UtcNow ,
187
191
} ;
188
- row . Id = await base . Db . InsertAsync ( row , selectIdentity : true ) ;
192
+ lock ( Locks . AppDb )
193
+ {
194
+ row . Id = base . Db . Insert ( row , selectIdentity : true ) ;
195
+ }
189
196
190
- PublishMessage ( new BackgroundTasks { RecordAlbumLikeId = request . AlbumId } ) ;
197
+ jobs . RunCommand < UpdateScoresCommand > ( new UpdateScores {
198
+ RecordAlbumLikeId = request . AlbumId
199
+ } ) ;
191
200
return row ;
192
201
}
193
202
194
203
public async Task Delete ( DeleteAlbumLike request )
195
204
{
196
- var session = await SessionAsAsync < CustomUserSession > ( ) ;
197
- var userId = session . GetUserId ( ) ;
198
- await Db . DeleteAsync < AlbumLike > ( x => x . AlbumId == request . AlbumId && x . AppUserId == userId ) ;
205
+ var userId = Request . GetRequiredUserId ( ) ;
206
+ lock ( Locks . AppDb )
207
+ {
208
+ Db . Delete < AlbumLike > ( x => x . AlbumId == request . AlbumId && x . AppUserId == userId ) ;
209
+ }
199
210
200
- PublishMessage ( new BackgroundTasks { RecordAlbumUnlikeId = request . AlbumId } ) ;
211
+ jobs . RunCommand < UpdateScoresCommand > ( new UpdateScores {
212
+ RecordAlbumUnlikeId = request . AlbumId
213
+ } ) ;
201
214
}
202
-
203
-
204
215
}
0 commit comments