Advertisement
Guest User

Untitled

a guest
Jun 9th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. open System
  2. [<AutoOpen>]
  3. module Common =
  4. type Result<'TSuccess,'TFailure> =
  5. | Success of 'TSuccess
  6. | Failure of 'TFailure
  7.  
  8. let validateString min max str =
  9. String.IsNullOrWhiteSpace(str) || str.Length > max || str.Length < min |> not
  10.  
  11. module User =
  12. let getPasswordHash pass = (hash pass).ToString()
  13. //value
  14. type Address = {
  15. street : string
  16. city : string
  17. }
  18. //Entity not anemic
  19. type User =
  20. private
  21. { id : Guid
  22. email : string
  23. passwordHash : string
  24. address : Address
  25. isAdmin : bool }
  26. member x.Id = x.id
  27. member x.Email = x.email
  28. member x.Address = x.address
  29. member x.IsAdmin = x.isAdmin
  30. static member Create(email : string, password : string, addr : Address, isAdmin : bool) =
  31. if email.Contains("@") |> not then Failure "Not valid Email"
  32. else if String.IsNullOrWhiteSpace password then Failure "Not valid password"
  33. else Success {
  34. id = Guid.NewGuid()
  35. email = email
  36. passwordHash = getPasswordHash password
  37. address = addr
  38. isAdmin = isAdmin
  39. }
  40.  
  41. module Channel =
  42. let validateString min max str =
  43. String.IsNullOrWhiteSpace(str) || str.Length > max || str.Length < min |> not
  44. //Entity not anemic
  45. type Channel =
  46. private
  47. { id : Guid
  48. name : string
  49. description : string
  50. sourceUrl : Uri
  51. showAsAdv : bool}
  52. member x.Id = x.id
  53. member x.Name = x.name
  54. member x.Description = x.description
  55. member x.SourceUrl = x.sourceUrl
  56. member x.ShowAsAdv = x.showAsAdv
  57. static member Create(name : string, description : string, sourceUrl : Uri, showAsAdv : bool) =
  58. if validateString 0 50 name then Failure "Not valid name"
  59. else if validateString 0 255 description then Failure "Not valid description"
  60. else Success {
  61. id = Guid.NewGuid()
  62. name = name
  63. description = description
  64. sourceUrl = sourceUrl
  65. showAsAdv = showAsAdv
  66. }
  67.  
  68. open User
  69.  
  70. let (Success(u)) = User.Create("hodza@gmail.com", "password", {street = "moskovskaya"; city = "spb"}, false)
  71.  
  72. printfn "email %s" u.Email
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement