4
4
using CoinyProject . Infrastructure . Data ;
5
5
using CoinyProject . Infrastructure . Data . Migrations ;
6
6
using CoinyProject . Infrastructure . Data . Repositories ;
7
+ using Microsoft . AspNetCore . Mvc ;
7
8
using Microsoft . EntityFrameworkCore ;
8
9
using System ;
9
10
using System . Collections . Generic ;
@@ -18,12 +19,12 @@ public class AlbumService : IAlbumService
18
19
{
19
20
private readonly UnitOfWork _unitOfWork ;
20
21
21
- public AlbumService ( UnitOfWork unitOfWork )
22
+ public AlbumService ( ApplicationDBContext dBContext )
22
23
{
23
- _unitOfWork = unitOfWork ;
24
+ _unitOfWork = new UnitOfWork ( dBContext ) ;
24
25
}
25
26
26
- public async Task < int > AddAlbum ( AlbumCreating album )
27
+ public async Task AddAlbum ( AlbumCreating album )
27
28
{
28
29
Album _album = new Album ( ) ;
29
30
@@ -32,10 +33,41 @@ public async Task<int> AddAlbum(AlbumCreating album)
32
33
_album . Description = album . Description ;
33
34
34
35
await _unitOfWork . AlbumRepository . Add ( _album ) ;
35
- await _unitOfWork . Commit ( ) ;
36
- return _album . Id ;
36
+ _unitOfWork . Commit ( ) ;
37
37
}
38
+ public Task AddAlbumElement ( AlbumElementCreating element )
39
+ {
40
+ var album = _unitOfWork . AlbumRepository . Include ( x => x . Elements )
41
+ . Where ( x => x . Elements . Count == 0 )
42
+ . OrderByDescending ( x => x . Id )
43
+ . FirstOrDefault ( ) ;
44
+
45
+ if ( album != null )
46
+ {
47
+ AlbumElement _albumElement = new AlbumElement ( )
48
+ {
49
+ Name = album . Name ,
50
+ Description = album . Description ,
51
+ } ;
38
52
53
+ album . Elements . Add ( _albumElement ) ;
54
+ _unitOfWork . Commit ( ) ;
55
+ }
56
+
57
+ return Task . CompletedTask ;
58
+ }
39
59
60
+ public Task < ( string , string ) > CommitAlbumCreation ( )
61
+ {
62
+ var album = _unitOfWork . AlbumRepository . Include ( x => x . Elements )
63
+ . Where ( x => ! x . Elements . Any ( ) )
64
+ . AsNoTracking ( )
65
+ . FirstOrDefault ( ) ;
66
+
67
+ if ( album != null )
68
+ return Task . FromResult ( ( "error" , "At least one element must be provided" ) ) ;
69
+ else
70
+ return Task . FromResult ( ( "success" , "Album successfule created" ) ) ;
71
+ }
40
72
}
41
73
}
0 commit comments