@@ -2,41 +2,75 @@ package fmt.kotlin.fundamentals
22
33import  java.io.InputStream 
44import  java.nio.file.Path 
5- import  java.util.UUID 
5+ import  java.util.* 
6+ import  kotlin.io.path.* 
67
78class  InventoryFileRepository (
89    val  basePath :  Path 
910) : InventoryRepository {
1011
11-     override  fun  addCellar (cellar :  Cellar ): Entity <Cellar > {
12-         TODO (" Not yet implemented"  )
13-     }
12+     override  fun  addCellar (cellar :  Cellar ) =  Entity (
13+         id =  newId(),
14+         data =  cellar
15+     )
16+         .also  { cellarEntity -> 
17+             basePath.resolve(cellarEntity.id).createDirectory()
18+ 
19+             cellar.bottles.forEach {
20+                 addBottle(it, cellarEntity.id)
21+             }
22+         }
1423
1524    override  fun  addBottle (bottle :  Bottle , cellarId :  String ): Entity <Bottle > {
16-         TODO (" Not yet implemented"  )
17-     }
25+         val  dirPath =  basePath.resolve(cellarId)
26+         if  (dirPath.notExists()) {
27+             throw  IllegalArgumentException (" Cellar does not exists: $cellarId "  )
28+         }
29+         val  bottleEntity =  Entity (
30+             id =  newId(),
31+             data =  bottle
32+         )
33+         dirPath.resolve(bottleEntity.id).writeText(" ${bottle.name} ,${bottle.year} "  )
1834
19-     override  fun  findBottles (cellarId :  String ): List <Entity <Bottle >> {
20-         TODO (" Not yet implemented"  )
35+         return  bottleEntity
2136    }
2237
23-     override  fun  findCellarId (bottleId :  String ): String?  {
24-         TODO (" Not yet implemented"  )
25-     }
38+     override  fun  findBottles (cellarId :  String ) = 
39+         basePath.resolve(cellarId).toFile().listFiles()!! .map {
40+             Entity (
41+                 it.name,
42+                 it.readText().toBottle()
43+             )
44+         }
2645
27-     override  fun  findAllCellarIds (): List <String > {
28-         TODO (" Not yet implemented"  )
29-     }
46+ 
47+     override  fun  findCellarId (bottleId :  String ) = 
48+         basePath.listDirectoryEntries().firstOrNull {
49+             it.toFile().list()?.any { it ==  bottleId } ? :  false 
50+         }?.name
51+ 
52+ 
53+     override  fun  findAllCellarIds () =  basePath.toFile().list()!! .toList()
3054
3155    override  fun  clear (cellarId :  String ) {
32-         TODO ( " Not yet implemented "  )
56+         basePath.resolve(cellarId).toFile().deleteRecursively( )
3357    }
3458
35-     override  fun  importBottle (cellarId :  String , stream :  InputStream ): Bottle  {
36-         TODO (" Not yet implemented"  )
37-     }
59+     override  fun  importBottle (cellarId :  String , stream :  InputStream ) = 
60+         stream.reader()
61+             .use {
62+                 it.readText()
63+             }
64+             .toBottle()
65+             .also  {
66+                 addBottle(it, cellarId)
67+             }
3868
3969    companion  object  {
4070        private  fun  newId () =  UUID .randomUUID().toString()
4171    }
72+ }
73+ 
74+ private  fun  String.toBottle () =  split(" ,"  ).let  {
75+     Bottle (it.first(), it.get(1 ).toInt())
4276}
0 commit comments