UserDataModel.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.DataAnnotations;
  4. using System.Linq;
  5. using System.Web;
  6. using GreenTree.Nachtragsmanagement.Core.Domain.User;
  7. using FluentValidation.Attributes;
  8. using GreenTree.Nachtragsmanagement.Web.Validation.Admin.User;
  9. namespace GreenTree.Nachtragsmanagement.Web.Models.Admin.User
  10. {
  11. public class UserDataModel
  12. {
  13. public int Id { get; set; }
  14. public string CustomNumber { get; set; }
  15. public string Forename { get; set; }
  16. public string Lastname { get; set; }
  17. public string MailAddress { get; set; }
  18. public string Password { get; set; }
  19. public ICollection<int> RoleValues { get; set; }
  20. public ICollection<string> RoleDescriptions { get; set; }
  21. public string RoleDescription
  22. {
  23. get
  24. {
  25. if (RoleDescriptions == null)
  26. return String.Empty;
  27. else
  28. return String.Join(", ", RoleDescriptions);
  29. }
  30. }
  31. public UserDataModel()
  32. {
  33. RoleValues = new List<int>();
  34. RoleDescriptions = new List<string>();
  35. }
  36. public static UserDataModel FromUser(Core.Domain.User.User userEntity, bool newWhenIsNull)
  37. {
  38. if (userEntity == null && newWhenIsNull)
  39. return new UserDataModel
  40. {
  41. Id = -1
  42. };
  43. if (userEntity == null && !newWhenIsNull)
  44. throw new ArgumentNullException("userEntity", "Cannot create UserDataModel from NULL user entity.");
  45. return new UserDataModel
  46. {
  47. Id = userEntity.Id,
  48. CustomNumber = userEntity.CustomNumber,
  49. Forename = userEntity.Forename,
  50. Lastname = userEntity.Lastname,
  51. MailAddress = userEntity.MailAddress,
  52. RoleValues =
  53. userEntity.Roles
  54. .Select(r => r.Id)
  55. .ToList(),
  56. RoleDescriptions =
  57. userEntity.Roles
  58. .Select(r => r.Description)
  59. .ToList()
  60. };
  61. }
  62. public Core.Domain.User.User ToUser()
  63. {
  64. return new Core.Domain.User.User
  65. {
  66. Id = this.Id,
  67. CustomNumber = this.CustomNumber,
  68. Forename = this.Forename,
  69. Lastname = this.Lastname,
  70. MailAddress = this.MailAddress
  71. };
  72. }
  73. }
  74. }