Repository navigation
Expand file tree
/
Copy pathQuery.hs
More file actions
679 lines (617 loc) · 28 KB
/
Copy pathQuery.hs
File metadata and controls
679 lines (617 loc) · 28 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PartialTypeSignatures #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeSynonymInstances #-}
-- |Module used for the queries in the database
module Query where
import Control.Exception
import Data.ByteString.UTF8 (fromString, toString)
import Data.Int (Int)
import Data.Scientific
import Data.Text
import Data.Time.Calendar
import Database.Beam
import Database.Beam.Backend.SQL (BeamSqlBackend)
import Database.Beam.Postgres
import Database.PostgreSQL.LibPQ (ExecStatus (NonfatalError))
import Schema
-- constants and general functions
-- |Creates a standard uri for connecting to the database with the given username and password
createUri :: String -> String -> String
createUri user pswd = "postgres://" ++ user ++ ":" ++ pswd ++ "@localhost/FabLab"
-- |Given an uri, returns a connection to the database
connectWithUri :: String -> IO Connection
connectWithUri uri = connectPostgreSQL $ fromString uri
-- |Connects to the database with the given characteristics
connectWithInfo :: String -> Integer -> String -> String -> String -> IO Connection
connectWithInfo host port user pswd db = connect $ ConnectInfo host (fromInteger port) user pswd db
-- |Given a connection, close it
closeConnection :: Connection -> IO ()
closeConnection = close
runBeam :: Connection -> Pg a -> IO a
runBeam = runBeamPostgres --Debug putStrLn -- change for debug or production purposes
allElementsOfTable
:: (Table t, BeamSqlBackend be)
=> ( DatabaseSettings be FabLabDB
-> DatabaseEntity be FabLabDB (TableEntity t)
)
-> Q be FabLabDB s (t (QExpr be s))
allElementsOfTable table = all_ (table fabLabDB)
-- |A generic select for list of elements with filters
{-genericSelectList :: (Table t, Generic (t Identity),
Generic (t Database.Beam.Backend.Types.Exposed),
Database.Beam.Backend.SQL.Row.GFromBackendRow
Postgres
(GHC.Generics.Rep (t Database.Beam.Backend.Types.Exposed))
(GHC.Generics.Rep (t Identity))) =>
(DatabaseSettings Postgres FabLabDB
-> DatabaseEntity Postgres FabLabDB (TableEntity t))
-> Maybe
(t (QExpr Postgres QBaseScope) -> QExpr Postgres QBaseScope Bool)
-> Connection
-> IO (Either SomeException [t Identity])-}
genericSelectList table maybeFilter =
let pool = case maybeFilter of
Nothing -> allElementsOfTable table
Just f -> filter_ f $ allElementsOfTable table
in \conn ->
try
$ runBeam conn
$ runSelectReturningList
$ select pool
-- |A generic select for a single element with filters
genericSelectOne table f =
\conn ->
try
$ runBeam conn
$ runSelectReturningOne
$ select
$ filter_ f
$ allElementsOfTable table
-- |Prepares a code to be used as key
prepareCode :: String -> Text
prepareCode = toUpper . pack
-- |Prepares a string to be used as a name in the database
prepareName :: String -> Text
prepareName = toTitle . pack
-- people queries
-- |Select all people in the database
selectAllPeople :: Connection -> IO (Either SqlError [Person])
selectAllPeople = genericSelectList _persone Nothing
-- |Select all partners in the database
selectAllPartners :: Connection -> IO (Either SqlError [Person])
selectAllPartners = genericSelectList _persone $ Just (\p -> _personSocio p ==. (val_ True))
-- |Select all laser cutter operators in the database
selectAllLaserCutterOperators :: Connection -> IO (Either SqlError [Person])
selectAllLaserCutterOperators =
genericSelectList _persone $ Just (\p -> _personOperatoreIntagliatrice p ==. (val_ True))
-- |Select all 3D printer operators in the database
selectAllPrinterOperators :: Connection -> IO (Either SqlError [Person])
selectAllPrinterOperators =
genericSelectList _persone $ Just (\p -> _personOperatoreStampante p ==. (val_ True))
-- |Select all people with the given cf (should be 0 or 1) in the database
selectPersonFromCF :: String -> (Connection -> IO (Either SqlError (Maybe Person)))
selectPersonFromCF cf =
genericSelectOne _persone (\p -> _personCf p ==. (val_ (pack cf)))
-- |Add a person to the database
insertPerson :: String -> String -> String -> (Connection -> IO (Either SqlError ()))
insertPerson cf name surname =
\conn ->
try
$ runBeam conn
$ runInsert
$ insert (_persone fabLabDB)
$ insertValues
[ Person
(prepareCode cf)
(prepareName name)
(prepareName surname)
False
False
False
0.0
]
-- |Modify a person already in the database
modifyPerson :: String -> Bool -> Bool -> Bool -> (Connection -> IO (Either SqlError ()))
modifyPerson cf partner cutter printer =
\conn ->
try
$ runBeam conn
$ runUpdate
$ update (_persone fabLabDB)
( \p ->
mconcat
[ _personSocio p <-. (val_ partner),
_personOperatoreIntagliatrice p <-. (val_ cutter),
_personOperatoreStampante p <-. (val_ printer)
]
)
(\p -> _personCf p ==. (val_ (prepareCode cf)))
-- materials queries
-- |Select all materials in the database
selectAllMaterials :: Connection -> IO (Either SqlError [Material])
selectAllMaterials = genericSelectList _materiali Nothing
-- |Select all classes of materials in the database
selectAllMaterialsClasses :: Connection -> IO (Either SqlError [MaterialsClass])
selectAllMaterialsClasses = genericSelectList _classi_di_materiali Nothing
-- |Select all the materials classes with the given code (should be 1 or 0) in the database
selectMaterialsClassFromCode :: String -> (Connection -> IO (Either SqlError (Maybe MaterialsClass)))
selectMaterialsClassFromCode code =
genericSelectOne _classi_di_materiali (\c -> _materialsclassCodiceClasse c ==. (val_ (prepareCode code)))
-- |Select all materials with the given code (should be 1 or 0) in the database
selectMaterialFromCode :: String -> (Connection -> IO (Either SqlError (Maybe Material)))
selectMaterialFromCode code =
genericSelectOne _materiali (\m -> _materialCodiceMateriale m ==. (val_ (prepareCode code)))
-- |Select all materials of a given class in the database
selectMaterialsByClass :: String -> (Connection -> IO (Either SqlError [Material]))
selectMaterialsByClass classCode =
\conn -> do
selectedClasses <- (selectMaterialsClassFromCode classCode) conn
case selectedClasses of
Left ex -> return $ Left ex
Right Nothing -> return $ Right []
Right (Just mClass) ->
try $ runBeam conn
$ runSelectReturningList
$ select
$ filter_ (\m -> _materialCodiceClasse m ==. val_ (pk mClass))
$ allElementsOfTable _materiali
-- |Add a class of materials to the database in the database
insertMaterialsClass :: String -> String -> (Connection -> IO (Either SqlError ()))
insertMaterialsClass code name =
\conn ->
try
$ runBeam conn
$ runInsert
$ insert (_classi_di_materiali fabLabDB)
$ insertValues
[ MaterialsClass
(prepareCode code)
(prepareName name)
]
-- |Add a material to the database. The code is the id of the material inside the materials class.
insertMaterial :: String -> String -> String -> Double -> String -> (Connection -> IO (Either SqlError ()))
insertMaterial code classCode name width descr =
\conn -> do
selectedClasses <- (selectMaterialsClassFromCode classCode) conn
case selectedClasses of
Left ex -> return $ Left ex
Right Nothing -> return $ Left $ SqlError "" NonfatalError "No class with the given code was present" "" ""
Right (Just mClass) ->
try $ runBeam conn
$ runInsert
$ insert (_materiali fabLabDB)
$ insertValues
[ Material
(pk mClass)
(prepareCode (classCode ++ code ++ show width))
(prepareName name)
width
(pack descr)
]
-- processings queries
-- |Select the processing with the given code in the database
selectProcessingFromCode :: String -> (Connection -> IO (Either SqlError (Maybe Processing)))
selectProcessingFromCode pCode =
genericSelectOne _lavorazioni (\p -> _processingCodiceLavorazione p ==. val_ (prepareCode pCode))
-- |Select all processings in the database
selectAllProcessings :: Connection -> IO (Either SqlError [Processing])
selectAllProcessings = genericSelectList _lavorazioni Nothing
-- |Select all types of processing in the database
selectAllTypes :: Connection -> IO (Either SqlError [Type])
selectAllTypes = genericSelectList _tipi Nothing
-- |Select all types of processing with the given code (should be 1 or 0) in the database
selectTypeFromCode :: String -> (Connection -> IO (Either SqlError (Maybe Type)))
selectTypeFromCode code =
genericSelectOne _tipi (\t -> _typeCodiceTipo t ==. (val_ (prepareCode code)))
-- |Select all processings on a given material in the database
selectProcessingsByMaterials :: String -> (Connection -> IO (Either SqlError [Processing]))
selectProcessingsByMaterials mCode =
\conn -> do
selectedMaterials <- (selectMaterialFromCode mCode) conn
case selectedMaterials of
Left ex -> return $ Left ex
Right Nothing -> return $ Left $ SqlError "" NonfatalError "No material with the given code was present" "" ""
Right (Just material) ->
try $ runBeam conn
$ runSelectReturningList
$ select
$ filter_ (\p -> _processingCodiceMateriale p ==. val_ (pk material))
$ allElementsOfTable _lavorazioni
-- |Add a type of processing to the database
insertType :: String -> String -> String -> (Connection -> IO (Either SqlError ()))
insertType code name descr =
\conn ->
try
$ runBeam conn
$ runInsert
$ insert (_tipi fabLabDB)
$ insertValues
[ Type
(prepareCode code)
(prepareName name)
(pack descr)
]
-- |Add a new processing to the database
insertProcessing :: String -> String -> Int -> Int -> Int -> String -> (Connection -> IO (Either SqlError ()))
insertProcessing typeCode materialCode maxPotency minPotency speed descr =
\conn -> do
selectedTypes <- (selectTypeFromCode typeCode) conn
selectedMaterials <- (selectMaterialFromCode materialCode) conn
case (selectedTypes, selectedMaterials) of
(Left ex, Left ex') -> return $ Left $ (error $ (toString $ sqlErrorMsg ex) ++ (toString $ sqlErrorMsg ex'))
(Left ex, _) -> return $ Left ex
(_, Left ex) -> return $ Left ex
(Right Nothing, _) -> return $ Left $ SqlError "" NonfatalError "No type with the given code was present" "" ""
(_, Right Nothing) -> return $ Left $ SqlError "" NonfatalError "No material with the given code was present" "" ""
(Right (Just pType), Right (Just material)) ->
let code = materialCode ++ (show maxPotency) ++ (show minPotency) ++ (show speed) ++ typeCode
in try $ runBeam conn
$ runInsert
$ insert (_lavorazioni fabLabDB)
$ insertValues
[ Processing
(pk pType)
(prepareCode code)
(pk material)
maxPotency
minPotency
speed
(pack descr)
]
-- plastics and filaments queries
-- |Select all filaments in the database
selectAllFilaments :: Connection -> IO (Either SqlError [Filament])
selectAllFilaments = genericSelectList _filamenti Nothing
-- |Select all plastics in the database
selectAllPlastics :: Connection -> IO (Either SqlError [Plastic])
selectAllPlastics = genericSelectList _plastiche Nothing
-- |Select all the plastics with the given code (should be 1 or 0) in the database
selectPlasticFromCode :: String -> (Connection -> IO (Either SqlError (Maybe Plastic)))
selectPlasticFromCode code =
genericSelectOne _plastiche (\p -> _plasticCodicePlastica p ==. (val_ (prepareCode code)))
-- |Select all the filaments with the given code (should be 1 or 0) in the database
selectFilamentFromCode :: String -> (Connection -> IO (Either SqlError (Maybe Filament)))
selectFilamentFromCode code =
genericSelectOne _filamenti (\f -> _filamentCodiceFilamento f ==. (val_ (prepareCode code)))
-- |Select the filaments made of a given plastic in the database
selectFilamentsByPlastic :: String -> (Connection -> IO (Either SqlError [Filament]))
selectFilamentsByPlastic plasticCode =
\conn -> do
selectedPlastics <- (selectPlasticFromCode plasticCode) conn
case selectedPlastics of
Left ex -> return $ Left ex
Right Nothing -> return $ Left $ SqlError "" NonfatalError "No plastic with the given code was present" "" ""
Right (Just plastic) ->
try $ runBeam conn
$ runSelectReturningList
$ select
$ filter_ (\f -> _filamentCodicePlastica f ==. val_ (pk plastic))
$ allElementsOfTable _filamenti
-- |Add a type of plastic to the database
insertPlastic :: String -> String -> String -> (Connection -> IO (Either SqlError ()))
insertPlastic code name descr =
\conn ->
try
$ runBeam conn
$ runInsert
$ insert (_plastiche fabLabDB)
$ insertValues
[ Plastic
(prepareCode code)
(prepareName name)
(pack descr)
]
-- |Add a filament to the database. The code is the id of the filament inside the type of plastic
insertFilament :: String -> String -> String -> String -> (Connection -> IO (Either SqlError ()))
insertFilament code plasticCode brand color =
\conn -> do
selectedPlastics <- (selectPlasticFromCode plasticCode) conn
case selectedPlastics of
Left ex -> return $ Left ex
Right Nothing -> return $ Left $ SqlError "" NonfatalError "No class with the given code was present" "" ""
Right (Just plastic) ->
try $ runBeam conn
$ runInsert
$ insert (_filamenti fabLabDB)
$ insertValues
[ Filament
(prepareCode $ plasticCode ++ code)
(pk plastic)
(prepareName brand)
(prepareName color)
]
-- printers queries
-- |Select all printers in the database
selectAllPrinters :: Connection -> IO (Either SqlError [Printer])
selectAllPrinters = genericSelectList _stampanti Nothing
-- |Select all the printers with the given code (should be 0 or 1) in the database
selectPrinterFromCode :: String -> (Connection -> IO (Either SqlError (Maybe Printer)))
selectPrinterFromCode code =
genericSelectOne _stampanti (\p -> _printerCodiceStampante p ==. (val_ (prepareCode code)))
-- |Add a printer to the database
insertPrinter :: String -> String -> String -> String -> (Connection -> IO (Either SqlError ()))
insertPrinter code brand model descr =
\conn ->
try
$ runBeam conn
$ runInsert
$ insert (_stampanti fabLabDB)
$ insertValues
[ Printer
(prepareCode code)
(prepareName brand)
(prepareName model)
(pack descr)
]
-- |Assign a printer to a print
assignPrinter :: String -> Int -> (Connection -> IO (Either SqlError ()))
assignPrinter printerCode printCode =
\conn -> do
selectedPrinters <- (selectPrinterFromCode printerCode) conn
case selectedPrinters of
Left ex -> return $ Left ex
Right Nothing -> return $ Left $ SqlError "" NonfatalError "No printer with the given code was present" "" ""
Right (Just printer) ->
try $ runBeam conn
$ runUpdate
$ update (_stampe fabLabDB)
(\s -> _printCodiceStampante s <-. just_ (val_ (pk printer)))
(\s -> _printCodiceStampa s ==. (val_ printCode))
-- prints queries
-- |Select the print with the given code in the database (should be 0 or 1)
selectPrintFromCode :: Int -> (Connection -> IO (Either SqlError (Maybe Print)))
selectPrintFromCode pCode =
genericSelectOne _stampe (\p -> _printCodiceStampa p ==. val_ pCode)
-- |Select all prints in the database
selectAllPrints :: Connection -> IO (Either SqlError [Print])
selectAllPrints = genericSelectList _stampe Nothing
-- |Select all the print that aren't completed in the database
selectAllIncompletePrints :: Connection -> IO (Either SqlError [Print])
selectAllIncompletePrints =
genericSelectList _stampe $ Just (\p -> _printDataConsegna p ==. val_ Nothing)
-- |Select all the completed prints in the database
selectAllCompletePrints :: Connection -> IO (Either SqlError [Print])
selectAllCompletePrints =
genericSelectList _stampe $ Just (\p -> _printDataConsegna p /=. val_ Nothing)
-- |Add a new print to the database
insertPrint :: String -> Day -> String -> (Connection -> IO (Either SqlError ()))
insertPrint cf insertDate descr =
\conn -> do
selectedPeople <- (selectPersonFromCF cf) conn
case selectedPeople of
Left ex -> return $ Left ex
Right Nothing -> return $ Left $ SqlError "" NonfatalError "No person with the given code was present" "" ""
Right (Just person) ->
try $ runBeam conn
$ runInsert
$ insert (_stampe fabLabDB)
$ insertExpressions
[ Print
{ _printCodiceStampa = default_,
_printDataRichiesta = val_ insertDate,
_printDataConsegna = val_ Nothing,
_printTempo = val_ Nothing,
_printCostoMateriali = val_ Nothing,
_printCostoTotale = val_ Nothing,
_printDescrizione = val_ (pack descr),
_printCfRichiedente = val_ (pk person),
_printCfIncaricato = nothing_,
_printCodiceStampante = nothing_
}
]
-- |Assign a print to an operator
assignPrint :: Int -> String -> (Connection -> IO (Either SqlError ()))
assignPrint code cf =
\conn -> do
selectedOperators <- (selectPersonFromCF cf) conn
case selectedOperators of
Left ex -> return $ Left ex
Right Nothing -> return $ Left $ SqlError "" NonfatalError "No person with the given code was present" "" ""
Right (Just operator) ->
try $ runBeam conn
$ runUpdate
$ update (_stampe fabLabDB)
(\s -> _printCfIncaricato s <-. just_ (val_ (pk operator)))
(\s -> _printCodiceStampa s ==. val_ code)
-- |Assign a filament to a print in the database
assignFilament :: Int -> String -> (Connection -> IO (Either SqlError ()))
assignFilament pCode fCode =
\conn -> do
selectedFilaments <- (selectFilamentFromCode fCode) conn
selectedPrints <- (selectPrintFromCode pCode) conn
case (selectedFilaments, selectedPrints) of
(Left ex, Left ex') -> return $ Left $ (error $ (toString $ sqlErrorMsg ex) ++ (toString $ sqlErrorMsg ex'))
(Left ex, _) -> return $ Left ex
(_, Left ex) -> return $ Left ex
(Right Nothing, _) -> return $ Left $ SqlError "" NonfatalError "No filament with the given code was present" "" ""
(_, Right Nothing) -> return $ Left $ SqlError "" NonfatalError "No print with the given code was present" "" ""
(Right (Just filament), Right (Just selectedPrint)) ->
try $ runBeam conn
$ runInsert
$ insert (_usi fabLabDB)
$ insertExpressions
[ Use
{ _useCodiceFilamento = val_ (pk filament),
_useCodiceStampa = val_ (pk selectedPrint)
}
]
-- |Selects the filaments assigned to the given print
assignmentsByPrint :: Int -> (Connection -> IO (Either SqlError [Use]))
assignmentsByPrint pCode =
\conn -> do
selectedPrint <- selectPrintFromCode pCode conn
case selectedPrint of
Left ex -> return $ Left ex
Right Nothing -> return $ Left $ SqlError "" NonfatalError "No print with the given code was present" "" ""
Right (Just p) ->
try
$ runBeam conn
$ runSelectReturningList
$ select
$ filter_ (\u -> _useCodiceStampa u ==. val_ (pk p))
$ allElementsOfTable _usi
-- |Complete a print
completePrint :: Int -> Day -> Double -> Scientific -> Scientific -> (Connection -> IO (Either SqlError ()))
completePrint pCode deliveryDate workTime total materials =
\conn -> do
completeResult <-
try
$ runBeam conn
$ runUpdate
$ update (_stampe fabLabDB)
( \s ->
mconcat
[ _printDataConsegna s <-. val_ (Just deliveryDate),
_printCostoMateriali s <-. val_ (Just materials),
_printCostoTotale s <-. val_ (Just total),
_printTempo s <-. val_ (Just workTime)
]
)
(\s -> _printCodiceStampa s ==. val_ pCode)
case completeResult of
Left ex -> return $ Left ex
Right () -> do
maybePrint <- selectPrintFromCode pCode conn
case maybePrint of
Left ex -> return $ Left ex
Right Nothing -> return $ Left $ SqlError "" NonfatalError "Inconsistent state: a print existed, and then no more!" "" ""
Right (Just print) ->
let (PersonId cf) = _printCfRichiedente print
in try
$ runBeam conn
$ runUpdate
$ update (_persone fabLabDB)
(\p -> _personSpesaTotale p <-. (current_ (_personSpesaTotale p) + (val_ total)))
(\p -> _personCf p ==. val_ cf)
-- cuts queries
-- |Select the cut with the given code in the database (should be 0 or 1)
selectCutFromCode :: Int -> (Connection -> IO (Either SqlError (Maybe Cut)))
selectCutFromCode cCode =
genericSelectOne _intagli (\c -> _cutCodiceIntaglio c ==. val_ cCode)
-- |Select all cuts in the database
selectAllCuts :: Connection -> IO (Either SqlError [Cut])
selectAllCuts = genericSelectList _intagli Nothing
-- |Select all the print that aren't completed in the database
selectAllIncompleteCuts :: Connection -> IO (Either SqlError [Cut])
selectAllIncompleteCuts =
genericSelectList _intagli $ Just (\c -> _cutDataConsegna c ==. val_ Nothing)
-- |Select all the completed prints in the database
selectAllCompleteCuts :: Connection -> IO (Either SqlError [Cut])
selectAllCompleteCuts =
genericSelectList _intagli $ Just (\c -> _cutDataConsegna c /=. val_ Nothing)
-- |Add a new cut to the database
insertCut :: String -> Day -> String -> (Connection -> IO (Either SqlError ()))
insertCut cf insertDate descr =
\conn -> do
selectedPeople <- selectPersonFromCF cf conn
case selectedPeople of
Left ex -> return $ Left ex
Right Nothing -> return $ Left $ SqlError "" NonfatalError "No person with the given code was present" "" ""
Right (Just person) ->
try $ runBeam conn
$ runInsert
$ insert (_intagli fabLabDB)
$ insertExpressions
[ Cut
{ _cutCodiceIntaglio = default_,
_cutDataRichiesta = val_ insertDate,
_cutDataConsegna = val_ Nothing,
_cutTempo = val_ Nothing,
_cutCostoMateriali = val_ Nothing,
_cutCostoTotale = val_ Nothing,
_cutDescrizione = val_ (pack descr),
_cutCfRichiedente = val_ (pk person),
_cutCfIncaricato = nothing_
}
]
-- |Assign a cut to an operator
assignCut :: Int -> String -> (Connection -> IO (Either SqlError ()))
assignCut code cf =
\conn -> do
selectedOperators <- (selectPersonFromCF cf) conn
case selectedOperators of
Left ex -> return $ Left ex
Right Nothing -> return $ Left $ SqlError "" NonfatalError "No person with the given code was present" "" ""
Right (Just operator) ->
try $ runBeam conn
$ runUpdate
$ update (_intagli fabLabDB)
(\c -> _cutCfIncaricato c <-. just_ (val_ (pk operator)))
(\c -> _cutCodiceIntaglio c ==. val_ code)
-- |Assign a processing to a print
assignProcessing :: Int -> String -> (Connection -> IO (Either SqlError ()))
assignProcessing cCode pCode =
\conn -> do
selectedProcessings <- (selectProcessingFromCode pCode) conn
selectedCuts <- (selectCutFromCode cCode) conn
case (selectedProcessings, selectedCuts) of
(Left ex, Left ex') -> return $ Left $ (error $ (toString $ sqlErrorMsg ex) ++ (toString $ sqlErrorMsg ex'))
(Left ex, _) -> return $ Left ex
(_, Left ex) -> return $ Left ex
(Right Nothing, _) -> return $ Left $ SqlError "" NonfatalError "No processing with the given code was present" "" ""
(_, Right Nothing) -> return $ Left $ SqlError "" NonfatalError "No cut with the given code was present" "" ""
(Right (Just processing), Right (Just cut)) -> do
try $ runBeam conn
$ runInsert
$ insert (_composizioni fabLabDB)
$ insertExpressions
[ Composition
{ _compositionCodiceLavorazione = val_ (pk processing),
_compositionCodiceIntaglio = val_ (pk cut)
}
]
-- |Selects the filaments assigned to the given cut
assignmentsByCut :: Int -> (Connection -> IO (Either SqlError [Composition]))
assignmentsByCut cCode =
\conn -> do
selectedCut <- selectCutFromCode cCode conn
case selectedCut of
Left ex -> return $ Left ex
Right Nothing -> return $ Left $ SqlError "" NonfatalError "No cut with the given code was present" "" ""
Right (Just cut) -> try $ runBeam conn
$ runSelectReturningList
$ select
$ filter_ (\c -> _compositionCodiceIntaglio c ==. val_ (pk cut))
$ allElementsOfTable _composizioni
-- |Complete a cut
completeCut :: Int -> Day -> Double -> Scientific -> Scientific -> (Connection -> IO (Either SqlError ()))
completeCut code deliveryDate workTime total materials =
\conn -> do
completeResult <-
try
$ runBeam conn
$ runUpdate
$ update (_intagli fabLabDB)
( \s ->
mconcat
[ _cutDataConsegna s <-. val_ (Just deliveryDate),
_cutCostoMateriali s <-. val_ (Just materials),
_cutCostoTotale s <-. val_ (Just total),
_cutTempo s <-. val_ (Just workTime)
]
)
(\s -> _cutCodiceIntaglio s ==. val_ code)
case completeResult of
Left ex -> return $ Left ex
Right () -> do
maybeCut <- selectCutFromCode code conn
case maybeCut of
Left ex -> return $ Left ex
Right Nothing -> return $ Left $ SqlError "" NonfatalError "Inconsistent state: a cut existed, and then no more!" "" ""
Right (Just cut) ->
let (PersonId cf) = _cutCfRichiedente cut
in try
$ runBeam conn
$ runUpdate
$ update (_persone fabLabDB)
(\p -> _personSpesaTotale p <-. (current_ (_personSpesaTotale p) + (val_ total)))
(\p -> _personCf p ==. val_ cf)