Last active
July 21, 2020 18:02
-
-
Save specdrake/62d2cfbdd17ad4121107b59a26ae5724 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE OverloadedStrings #-} | |
module Main where | |
import System.Exit | |
import System.IO | |
import Data.Char | |
import Database.SQLite.Simple | |
import Text.RawString.QQ | |
import Data.Text (Text) | |
import qualified Data.Text as T | |
import Database.SQLite.Simple.Types | |
data User = | |
User { | |
userId :: Integer | |
, username :: Text | |
, shell :: Text | |
, homedirectory :: Text | |
, realName :: Text | |
, phone :: Text | |
} deriving (Eq, Show) | |
instance FromRow User where | |
fromRow = User <$> field | |
<*> field | |
<*> field | |
<*> field | |
<*> field | |
<*> field | |
instance ToRow User where | |
toRow (User id_ username shell homeDir realName phone) = | |
toRow (id_, username, shell, homeDir, realName, phone) | |
updateUser :: Query | |
updateUser = "UPDATE users\ | |
\ SET username = :uname, shell = :shell, homeDirectory = :hd, realName = :rname, phone = :ph WHERE username = :uname;" | |
main :: IO () | |
main = do | |
hSetBuffering stdin LineBuffering | |
conn <- open "finger.db" | |
putStrLn "Enter the username you want to edit or delete : " | |
chknm <- getLine | |
res <- query conn ("SELECT * from users where username=(?);") (Only (T.pack chknm)) :: IO [User] | |
if null res then main else return () | |
putStrLn "Enter 1 to edit, 2 to delete : " | |
choice <- getChar | |
_ <- getChar | |
case (ord choice) - 48 of | |
1 -> return () | |
2 -> do | |
execute conn ("DELETE from users where username=(?);") (Only (T.pack chknm)) | |
rows <- query_ conn ("SELECT * from users;") :: IO [User] | |
mapM_ print (rows :: [User]) | |
Database.SQLite.Simple.close conn | |
exitSuccess | |
_ -> main | |
putStrLn "Enter the shell : " | |
ishell <- getLine | |
putStrLn "Enter home directory : " | |
ihome <- getLine | |
putStrLn "Enter the real name : " | |
irname <- getLine | |
putStrLn "Enter the phone number : " | |
iphone <- getLine | |
executeNamed conn updateUser [":uname" := T.pack chknm, ":shell" := T.pack ishell, ":hd" := T.pack ihome, ":rname" := T.pack irname, ":ph" := T.pack iphone] | |
rows <- query_ conn ("SELECT * from users;") :: IO [User] | |
mapM_ print (rows :: [User]) | |
Database.SQLite.Simple.close conn |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment