Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- open System
- [<AutoOpen>]
- module Common =
- type Result<'TSuccess,'TFailure> =
- | Success of 'TSuccess
- | Failure of 'TFailure
- let validateString min max str =
- String.IsNullOrWhiteSpace(str) || str.Length > max || str.Length < min |> not
- module User =
- let getPasswordHash pass = (hash pass).ToString()
- //value
- type Address = {
- street : string
- city : string
- }
- //Entity not anemic
- type User =
- private
- { id : Guid
- email : string
- passwordHash : string
- address : Address
- isAdmin : bool }
- member x.Id = x.id
- member x.Email = x.email
- member x.Address = x.address
- member x.IsAdmin = x.isAdmin
- static member Create(email : string, password : string, addr : Address, isAdmin : bool) =
- if email.Contains("@") |> not then Failure "Not valid Email"
- else if String.IsNullOrWhiteSpace password then Failure "Not valid password"
- else Success {
- id = Guid.NewGuid()
- email = email
- passwordHash = getPasswordHash password
- address = addr
- isAdmin = isAdmin
- }
- module Channel =
- let validateString min max str =
- String.IsNullOrWhiteSpace(str) || str.Length > max || str.Length < min |> not
- //Entity not anemic
- type Channel =
- private
- { id : Guid
- name : string
- description : string
- sourceUrl : Uri
- showAsAdv : bool}
- member x.Id = x.id
- member x.Name = x.name
- member x.Description = x.description
- member x.SourceUrl = x.sourceUrl
- member x.ShowAsAdv = x.showAsAdv
- static member Create(name : string, description : string, sourceUrl : Uri, showAsAdv : bool) =
- if validateString 0 50 name then Failure "Not valid name"
- else if validateString 0 255 description then Failure "Not valid description"
- else Success {
- id = Guid.NewGuid()
- name = name
- description = description
- sourceUrl = sourceUrl
- showAsAdv = showAsAdv
- }
- open User
- let (Success(u)) = User.Create("hodza@gmail.com", "password", {street = "moskovskaya"; city = "spb"}, false)
- printfn "email %s" u.Email
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement