using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Web; using EnVisage.Code; namespace EnVisage.Models { public class ContactModel : IBaseModel { public Guid Id { get; set; } [Required] [MaxLength(100)] [Display(Name="First Name")] public string FirstName { get; set; } [MaxLength(100)] [Required] [Display(Name="Last Name")] public string LastName { get; set; } [Required] [EmailAddress] [MaxLength(100)] public string Email { get; set; } [MaxLength(100)] public string Phone { get; set; } public ContactType Type { get; set; } [Display(Name="Contact Classification")] public InternalContactClassification ContactClassification { get; set; } public Guid ParentId { get; set; } public int ProjectLinksCount { get; set; } public ContactModel() { Id = new Guid(); } public ContactModel(Guid parentId) { Id = new Guid(); ParentId = parentId; } /// /// Casts a obect to the object of type . /// /// A object. /// A object filled with data from db. public static explicit operator ContactModel(Contact obj) { if (obj == null) return null; var model = new ContactModel { Id = obj.Id, Type = (ContactType)Enum.Parse(typeof(ContactType), obj.Type.ToString(), true), FirstName = obj.FirstName, LastName = obj.LastName, Email = obj.Email, Phone = obj.Phone, ParentId = obj.ParentId.HasValue ? obj.ParentId.Value : Guid.Empty, ProjectLinksCount = obj.Contact2Project.Count, ContactClassification = (InternalContactClassification)obj.ContactClassification }; model.TrimStringProperties(); return model; } /// /// Copies data from model to DAL object. /// /// A target DAL object. public void CopyTo(Contact dbObj) { if (dbObj == null) throw new ArgumentNullException(); dbObj.Type = Type.GetHashCode(); dbObj.FirstName = FirstName; dbObj.LastName = LastName; dbObj.Phone = Phone; dbObj.Email = Email; dbObj.ParentId = ParentId; dbObj.ContactClassification = (int)ContactClassification; // dbObj.UserId = (Author != null) ? new Guid(Author.Id) : new Guid(User.Identity.GetUserId()); } } }