EnVisageOnline/Main-RMO/Source/EnVisage/Models/ContactModel.cs

89 lines
2.8 KiB
C#

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<Contact>
{
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;
}
/// <summary>
/// Casts a <see cref="Note"/> obect to the object of type <see cref="NoteModel"/>.
/// </summary>
/// <param name="obj">A <see cref="Note"/> object.</param>
/// <returns>A <see cref="NoteModel"/> object filled with data from db.</returns>
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;
}
/// <summary>
/// Copies data from model to DAL object.
/// </summary>
/// <param name="dbObj">A target DAL object.</param>
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.GetID());
}
}
}