2013-04-30 11 views
6

Sto tentando di utilizzare Yesod e persistente per creare un sito Web. Sono un po 'confuso su come usare l'API persistente.Haskell Inserisce file persistenti se non è già presente nel database

Ecco due delle mie tabelle

Feed 
    url Text 
    UniqueFeed url 

Subscription 
    feed FeedId 
    title Text 
    UniqueSubscription feed 

che sto cercando di creare un feed, se un feed con quella URL non esiste, e quindi aggiungere un abbonamento a questa alimentazione, se l'abbonamento non esiste già .

postFeedR :: Handler RepHtml 
postFeedR = do 
    url <- runInputPost $ ireq urlField "url" 
    title <- runInputPost $ ireq textField "title" 

    runDB $ do 
     feedId <- insertFeed $ UniqueFeed url 
     subscriptionId <- insertSubscription feedId title 
     return 

    defaultLayout [whamlet| <p>done|] 

insertFeed url = do 
    f <- insertBy $ UniqueFeed url 
    case f of 
     Left (Entity uid _) -> uid 
     Right (Key uid) -> do 
      (Key uid) <- insert $ Feed url 
      return uid 

insertSubscription feedId title = do 
    s <- insertBy $ UniqueSubscription feedId 
    case s of 
     Left (Entity uid _) -> uid 
     Right (Key uid) -> do 
      (Key uid) <- insert $ Subscription feedId title 
      return uid 

Ottengo gli errori di seguito. Non capisco perché ghc pensi che il valore di ritorno di insertFeed e insertSubscription dovrebbe essere UniqueFeed e UniqueSubscription. Mi piacerebbe che quelle funzioni restituiscano le chiavi dei record appena creati.

Inoltre, sembra che sto buttando via la chiave che torno in ciascuna delle clausole di destra del caso. Perché restituisce persistentemente quelle chiavi. Nel caso in cui UniqueSubscription non si trova nel database, persistente non dispone di informazioni sufficienti per creare un nuovo record di sottoscrizione, poiché manca il titolo, che non si trova su UniqueSubscription.

Se qualcuno potesse darmi alcuni suggerimenti su come utilizzare l'API persistente, lo apprezzerei molto.

Handler/Home.hs:62:9: 
    Kind incompatibility when matching types: 
     a0 :: * 
     GHandler App App :: * -> * 
    Expected type: (a0 -> t0) 
        -> (t0 -> a0 -> m0 a0) -> YesodDB App App (m0 a0) 
     Actual type: (a0 -> t0) -> (t0 -> a0 -> m0 a0) -> a0 -> m0 a0 
    In a stmt of a 'do' block: feedId <- insertFeed $ UniqueFeed url 
    In the second argument of `($)', namely 
     `do { feedId <- insertFeed $ UniqueFeed url; 
      subscriptionId <- insertSubscription feedId title; 
      return }' 

Handler/Home.hs:62:9: 
    Couldn't match type `YesodPersistBackend App' with `(->)' 
    Expected type: (a0 -> t0) 
        -> (t0 -> a0 -> m0 a0) -> YesodDB App App (m0 a0) 
     Actual type: (a0 -> t0) -> (t0 -> a0 -> m0 a0) -> a0 -> m0 a0 
    In a stmt of a 'do' block: feedId <- insertFeed $ UniqueFeed url 
    In the second argument of `($)', namely 
     `do { feedId <- insertFeed $ UniqueFeed url; 
      subscriptionId <- insertSubscription feedId title; 
      return }' 

Handler/Home.hs:74:20: 
    Couldn't match expected type `Unique Feed' 
       with actual type `Database.Persist.Store.PersistValue' 
    In the first argument of `return', namely `uid' 
    In a stmt of a 'do' block: return uid 
    In the expression: 
     do { (Key uid) <- insert $ Feed url; 
      return uid } 

Handler/Home.hs:83:20: 
    Couldn't match expected type `Unique Subscription' 
       with actual type `Database.Persist.Store.PersistValue' 
    In the first argument of `return', namely `uid' 
    In a stmt of a 'do' block: return uid 
    In the expression: 
     do { (Key uid) <- insert $ Subscription feedId title; 
      return uid } 
+0

Provare a utilizzare "insertBy $ Feed url'. –

risposta

7

insertBy Non ci vuole un vincolo unico come parametro, getBy è più appropriato.

Ma insertUnique è una breve possibilità con il risultato Forse.

postFeedR :: Handler RepHtml 
postFeedR = do 
    url <- runInputPost $ ireq urlField "url" 
    title <- runInputPost $ ireq textField "title" 

    runDB $ do 
     feedId <- insertFeed url 
     _mbSubscriptionId <- insertUnique $ Subscription feedId title 
     return() 

    defaultLayout ... 

insertFeed url = do 
    f <- insertBy $ Feed url 
    case f of 
     Left (Entity uid _) -> return uid 
     Right uid -> return uid 
Problemi correlati