1
1
using System ;
2
2
using System . Collections . Generic ;
3
3
using System . Linq ;
4
+ using versioning_manager . api . Controllers ;
4
5
using versioning_manager . contracts . Data ;
5
- using versioning_manager . contracts . Models ;
6
6
using versioning_manager . contracts . Services ;
7
+ using versioning_manager . data . Models ;
7
8
8
9
namespace versioning_manager . api . Services
9
10
{
@@ -15,24 +16,68 @@ public VersionService(IVersionDetailRepository repository)
15
16
_repository = repository ;
16
17
}
17
18
18
- public IEnumerable < IVersionDetail > GetVersions ( IVersionRequest request )
19
+ public IEnumerable < VersionDetail > GetVersions ( IVersionRequest request )
19
20
{
20
- return _repository . GetByProductId ( request . ProductId )
21
+ var listOfVersions = _repository . GetByProductId ( request . ProductId ) ;
22
+ return listOfVersions
21
23
. WithMajorVersion ( request . Major )
22
24
. WithMinorVersion ( request . Minor ) ;
23
25
}
24
26
25
- public IVersionDetail IncrementVersion ( IVersionRequest request )
27
+ public VersionDetail IncrementVersion ( IVersionRequest request )
26
28
{
27
29
var versions = GetVersions ( request ) ;
28
30
29
31
var versionList = versions . OrderBy ( x => x ) . Reverse ( ) ;
30
32
31
- // TODO: Change this to return null instead of exception
33
+ Version version = null ;
32
34
if ( versionList . Count ( ) == 0 )
33
- throw new Exception ( "No Versions available." ) ;
35
+ {
36
+ if ( ! request . Major . HasValue && request . Minor . HasValue )
37
+ throw new ArgumentException ( "Cannot pass a Minor version without Major version" ) ;
34
38
35
- return versionList . First ( ) ;
39
+ if ( request . Major . HasValue && ! request . Minor . HasValue )
40
+ {
41
+ version = new Version ( request . Major . Value , 0 ) ;
42
+ }
43
+ else if ( request . Major . HasValue && request . Minor . HasValue )
44
+ {
45
+ version = new Version ( request . Major . Value , request . Minor . Value ) ;
46
+ }
47
+ else
48
+ {
49
+ version = new Version ( 1 , 0 ) ;
50
+ }
51
+ }
52
+ else {
53
+ version = versionList . First ( ) . Version ;
54
+
55
+ if ( ! request . Major . HasValue && ! request . Minor . HasValue )
56
+ {
57
+ version = version . IncrementBuild ( ) ;
58
+ }
59
+ else if ( ! request . Major . HasValue && request . Minor . HasValue )
60
+ {
61
+ version = version . IncrementMinor ( ) ;
62
+ }
63
+ else {
64
+ version = version . IncrementMajor ( ) ;
65
+ }
66
+ }
67
+
68
+ var versionDetail = new VersionDetail
69
+ {
70
+ Version = version ,
71
+ CreatedDate = DateTime . Now ,
72
+ Product = new Product
73
+ {
74
+ Id = request . ProductId
75
+ }
76
+ } ;
77
+
78
+ var created = _repository . Add ( versionDetail ) ;
79
+
80
+ return created ;
36
81
}
37
82
}
38
83
}
0 commit comments