diff --git a/yesod-persistent/LICENSE b/yesod-persistent/LICENSE new file mode 100644 index 00000000..8643e5d8 --- /dev/null +++ b/yesod-persistent/LICENSE @@ -0,0 +1,25 @@ +The following license covers this documentation, and the source code, except +where otherwise indicated. + +Copyright 2010, Michael Snoyman. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +* Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +* Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY EXPRESS OR +IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO +EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, +OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE +OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/yesod-persistent/README b/yesod-persistent/README new file mode 100644 index 00000000..e69de29b diff --git a/yesod-persistent/Setup.lhs b/yesod-persistent/Setup.lhs new file mode 100755 index 00000000..06e2708f --- /dev/null +++ b/yesod-persistent/Setup.lhs @@ -0,0 +1,7 @@ +#!/usr/bin/env runhaskell + +> module Main where +> import Distribution.Simple + +> main :: IO () +> main = defaultMain diff --git a/yesod-persistent/Yesod/Persist.hs b/yesod-persistent/Yesod/Persist.hs new file mode 100644 index 00000000..d534a1cb --- /dev/null +++ b/yesod-persistent/Yesod/Persist.hs @@ -0,0 +1,41 @@ +{-# LANGUAGE TypeFamilies #-} +{-# LANGUAGE FlexibleContexts #-} +module Yesod.Persist + ( YesodPersist (..) + , get404 + , getBy404 + , module Database.Persist + , module Database.Persist.TH + ) where + +import Database.Persist +import Database.Persist.TH +import Control.Monad.Trans.Class (MonadTrans (..)) +import Control.Failure (Failure) + +import Yesod.Handler + +class YesodPersist y where + type YesodDB y :: (* -> *) -> * -> * + runDB :: YesodDB y (GGHandler sub y IO) a -> GHandler sub y a + +-- | Get the given entity by ID, or return a 404 not found if it doesn't exist. +get404 :: (PersistBackend (t m), PersistEntity val, Monad (t m), + Failure ErrorResponse m, MonadTrans t) + => Key val -> t m val +get404 key = do + mres <- get key + case mres of + Nothing -> lift notFound + Just res -> return res + +-- | Get the given entity by unique key, or return a 404 not found if it doesn't +-- exist. +getBy404 :: (PersistBackend (t m), PersistEntity val, Monad (t m), + Failure ErrorResponse m, MonadTrans t) + => Unique val -> t m (Key val, val) +getBy404 key = do + mres <- getBy key + case mres of + Nothing -> lift notFound + Just res -> return res diff --git a/yesod-persistent/yesod-persistent.cabal b/yesod-persistent/yesod-persistent.cabal new file mode 100644 index 00000000..54d6748f --- /dev/null +++ b/yesod-persistent/yesod-persistent.cabal @@ -0,0 +1,27 @@ +name: yesod-persistent +version: 0.2.0 +license: BSD3 +license-file: LICENSE +author: Michael Snoyman +maintainer: Michael Snoyman +synopsis: Some helpers for using Persistent from Yesod. +category: Web, Yesod, Database +stability: Stable +cabal-version: >= 1.6 +build-type: Simple +homepage: http://www.yesodweb.com/ + +library + build-depends: base >= 4 && < 5 + , yesod-core >= 0.8 && < 0.10 + , persistent >= 0.6 && < 0.7 + , persistent-template >= 0.6 && < 0.7 + , failure >= 0.1 && < 0.2 + , transformers >= 0.2 && < 0.3 + , web-routes-quasi >= 0.7.1 && < 0.8 + exposed-modules: Yesod.Persist + ghc-options: -Wall + +source-repository head + type: git + location: git://github.com/snoyberg/yesod-persistent.git