-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
244 additions
and
0 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
wizard-public/src/WizardLib/Public/Database/DAO/ExternalLink/ExternalLinkUsageDAO.hs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
module WizardLib.Public.Database.DAO.ExternalLink.ExternalLinkUsageDAO where | ||
|
||
import GHC.Int | ||
|
||
import Shared.Common.Database.DAO.Common | ||
import Shared.Common.Model.Context.AppContext | ||
import WizardLib.Public.Database.Mapping.ExternalLink.ExternalLinkUsage () | ||
import WizardLib.Public.Model.ExternalLink.ExternalLinkUsage | ||
|
||
entityName = "external_link_usage" | ||
|
||
findExternalLinkUsages :: AppContextC s sc m => m [ExternalLinkUsage] | ||
findExternalLinkUsages = createFindEntitiesFn entityName | ||
|
||
insertExternalLinkUsage :: AppContextC s sc m => ExternalLinkUsage -> m Int64 | ||
insertExternalLinkUsage = createInsertFn entityName | ||
|
||
deleteExternalLinkUsages :: AppContextC s sc m => m Int64 | ||
deleteExternalLinkUsages = createDeleteEntitiesFn entityName |
9 changes: 9 additions & 0 deletions
9
wizard-public/src/WizardLib/Public/Database/Mapping/ExternalLink/ExternalLinkUsage.hs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module WizardLib.Public.Database.Mapping.ExternalLink.ExternalLinkUsage where | ||
|
||
import Database.PostgreSQL.Simple | ||
|
||
import WizardLib.Public.Model.ExternalLink.ExternalLinkUsage | ||
|
||
instance ToRow ExternalLinkUsage | ||
|
||
instance FromRow ExternalLinkUsage |
12 changes: 12 additions & 0 deletions
12
...src/WizardLib/Public/Database/Migration/Development/ExternalLink/ExternalLinkMigration.hs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module WizardLib.Public.Database.Migration.Development.ExternalLink.ExternalLinkMigration where | ||
|
||
import Shared.Common.Constant.Component | ||
import Shared.Common.Model.Context.AppContext | ||
import Shared.Common.Util.Logger | ||
import WizardLib.Public.Database.DAO.ExternalLink.ExternalLinkUsageDAO | ||
|
||
runMigration :: AppContextC s sc m => m () | ||
runMigration = do | ||
logInfo _CMP_MIGRATION "(ExternalLink/ExternalLink) started" | ||
deleteExternalLinkUsages | ||
logInfo _CMP_MIGRATION "(ExternalLink/ExternalLink) ended" |
31 changes: 31 additions & 0 deletions
31
...zardLib/Public/Database/Migration/Development/ExternalLink/ExternalLinkSchemaMigration.hs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
module WizardLib.Public.Database.Migration.Development.ExternalLink.ExternalLinkSchemaMigration where | ||
|
||
import Database.PostgreSQL.Simple | ||
import GHC.Int | ||
|
||
import Shared.Common.Database.DAO.Common | ||
import Shared.Common.Model.Context.AppContext | ||
import Shared.Common.Util.Logger | ||
|
||
dropTables :: AppContextC s sc m => m Int64 | ||
dropTables = do | ||
logInfo _CMP_MIGRATION "(Table/ExternalLink) drop tables" | ||
let sql = "DROP TABLE IF EXISTS external_link_usage CASCADE;" | ||
let action conn = execute_ conn sql | ||
runDB action | ||
|
||
createTables :: AppContextC s sc m => m Int64 | ||
createTables = do | ||
logInfo _CMP_MIGRATION "(Table/ExternalLink) create table" | ||
let sql = | ||
"CREATE TABLE external_link_usage \ | ||
\( \ | ||
\ uuid uuid NOT NULL, \ | ||
\ url varchar NOT NULL, \ | ||
\ tenant_uuid uuid NOT NULL, \ | ||
\ created_at timestamptz NOT NULL, \ | ||
\ CONSTRAINT external_link_usage_pk PRIMARY KEY (uuid, tenant_uuid), \ | ||
\ CONSTRAINT external_link_usage_tenant_uuid_fk FOREIGN KEY (tenant_uuid) REFERENCES tenant (uuid) \ | ||
\);" | ||
let action conn = execute_ conn sql | ||
runDB action |
13 changes: 13 additions & 0 deletions
13
wizard-public/src/WizardLib/Public/Model/ExternalLink/ExternalLinkUsage.hs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module WizardLib.Public.Model.ExternalLink.ExternalLinkUsage where | ||
|
||
import Data.Time | ||
import qualified Data.UUID as U | ||
import GHC.Generics | ||
|
||
data ExternalLinkUsage = ExternalLinkUsage | ||
{ uuid :: U.UUID | ||
, url :: String | ||
, tenantUuid :: U.UUID | ||
, createdAt :: UTCTime | ||
} | ||
deriving (Show, Eq, Generic) |
22 changes: 22 additions & 0 deletions
22
wizard-public/src/WizardLib/Public/Service/ExternalLink/ExternalLinkUsageService.hs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module WizardLib.Public.Service.ExternalLink.ExternalLinkUsageService where | ||
|
||
import Control.Monad (void) | ||
import Control.Monad.Reader (asks, liftIO) | ||
import Data.Time | ||
import Prelude hiding (id) | ||
|
||
import Shared.Common.Database.DAO.Common | ||
import Shared.Common.Model.Context.AppContext | ||
import Shared.Common.Util.Logger | ||
import Shared.Common.Util.Uuid | ||
import WizardLib.Public.Database.DAO.ExternalLink.ExternalLinkUsageDAO | ||
import WizardLib.Public.Model.ExternalLink.ExternalLinkUsage | ||
|
||
createExternalLinkUsage :: AppContextC s sc m => String -> m () | ||
createExternalLinkUsage url = | ||
runInTransaction logInfoI logWarnI $ do | ||
uuid <- liftIO generateUuid | ||
tenantUuid <- asks (.tenantUuid') | ||
now <- liftIO getCurrentTime | ||
let externalLinkUsage = ExternalLinkUsage uuid url tenantUuid now | ||
void $ insertExternalLinkUsage externalLinkUsage |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module Wizard.Api.Handler.ExternalLink.Api where | ||
|
||
import Servant | ||
import Servant.Swagger.Tags | ||
|
||
import Wizard.Api.Handler.ExternalLink.List_GET | ||
import Wizard.Model.Context.BaseContext | ||
|
||
type ExternalLinkAPI = | ||
Tags "ExternalLink" | ||
:> List_GET | ||
|
||
externalLinkApi :: Proxy ExternalLinkAPI | ||
externalLinkApi = Proxy | ||
|
||
externalLinkServer :: ServerT ExternalLinkAPI BaseContextM | ||
externalLinkServer = list_GET |
23 changes: 23 additions & 0 deletions
23
wizard-server/src/Wizard/Api/Handler/ExternalLink/List_GET.hs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module Wizard.Api.Handler.ExternalLink.List_GET where | ||
|
||
import Servant | ||
|
||
import Shared.Common.Api.Handler.Common | ||
import Shared.Common.Model.Context.TransactionState | ||
import Shared.Common.Model.Error.Error | ||
import Wizard.Api.Handler.Common | ||
import Wizard.Model.Context.BaseContext | ||
import WizardLib.Public.Service.ExternalLink.ExternalLinkUsageService | ||
|
||
type List_GET = | ||
Header "Host" String | ||
:> "external-link" | ||
:> QueryParam' '[Required] "url" String | ||
:> Get '[SafeJSON] (Headers '[Header "x-trace-uuid" String] NoContent) | ||
|
||
list_GET :: Maybe String -> String -> BaseContextM (Headers '[Header "x-trace-uuid" String] NoContent) | ||
list_GET mServerUrl url = | ||
runInUnauthService mServerUrl Transactional $ | ||
addTraceUuidHeader =<< do | ||
createExternalLinkUsage url | ||
throwError $ FoundError url |
This file contains 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
This file contains 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
This file contains 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
12 changes: 12 additions & 0 deletions
12
wizard-server/test/Wizard/Specs/API/ExternalLink/APISpec.hs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module Wizard.Specs.API.ExternalLink.APISpec where | ||
|
||
import Test.Hspec | ||
import Test.Hspec.Wai hiding (shouldRespondWith) | ||
|
||
import Wizard.Specs.API.Common | ||
import Wizard.Specs.API.ExternalLink.List_GET | ||
|
||
externalLinkAPI baseContext appContext = | ||
with (startWebApp baseContext appContext) $ | ||
describe "EXTERNAL LINK API Spec" $ do | ||
list_GET appContext |
55 changes: 55 additions & 0 deletions
55
wizard-server/test/Wizard/Specs/API/ExternalLink/List_GET.hs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
module Wizard.Specs.API.ExternalLink.List_GET ( | ||
list_GET, | ||
) where | ||
|
||
import Data.Aeson (encode) | ||
import Network.HTTP.Types | ||
import Network.Wai (Application) | ||
import Test.Hspec | ||
import Test.Hspec.Wai hiding (shouldRespondWith) | ||
import Test.Hspec.Wai.Matcher | ||
|
||
import Shared.Common.Api.Resource.Error.ErrorJM () | ||
import Shared.Common.Model.Error.Error | ||
import Wizard.Model.Context.AppContext | ||
import WizardLib.Public.Database.DAO.ExternalLink.ExternalLinkUsageDAO | ||
|
||
import SharedTest.Specs.API.Common | ||
import Wizard.Specs.API.Common | ||
|
||
-- ------------------------------------------------------------------------ | ||
-- GET /wizard-api/external-link?url=http://example.com/my-link | ||
-- ------------------------------------------------------------------------ | ||
list_GET :: AppContext -> SpecWith ((), Application) | ||
list_GET appContext = describe "GET /wizard-api/external-link?url=http://example.com/my-link" $ test_302 appContext | ||
|
||
-- ---------------------------------------------------- | ||
-- ---------------------------------------------------- | ||
-- ---------------------------------------------------- | ||
reqMethod = methodGet | ||
|
||
reqUrl = "/wizard-api/external-link?url=http://example.com/my-link" | ||
|
||
reqHeaders = [] | ||
|
||
reqBody = "" | ||
|
||
-- ---------------------------------------------------- | ||
-- ---------------------------------------------------- | ||
-- ---------------------------------------------------- | ||
test_302 appContext = | ||
it "HTTP 302 FOUND" $ | ||
-- GIVEN: Prepare expectation | ||
do | ||
let expStatus = 302 | ||
let expHeaders = resCtHeader : resCorsHeaders | ||
let expDto = FoundError "http://example.com/my-link" | ||
let expBody = encode expDto | ||
-- WHEN: Call API | ||
response <- request reqMethod reqUrl reqHeaders reqBody | ||
-- THEN: Compare response with expectation | ||
let responseMatcher = | ||
ResponseMatcher {matchHeaders = expHeaders, matchStatus = expStatus, matchBody = bodyEquals expBody} | ||
response `shouldRespondWith` responseMatcher | ||
-- THEN: Compare DB with expectation | ||
assertCountInDB findExternalLinkUsages appContext 1 |
This file contains 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