EnVisageOnline/Main/Source/EnVisage/Models/NoteModel.cs

79 lines
2.6 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 NoteModel : IBaseModel<Note>
{
public Guid Id { get; set; }
public Guid? ParentId { get; set; }
[Required]
public string Title { get; set; }
public string Details { get; set; }
public DateTime DateAdded { get; set; }
public AspNetUser Author { get; set; }
public int NoteType { get; set; }
public List<NoteModel> ChildNotes { get; set; }
public string CreatedBy { get; set; }
public Guid? DomainId { get; set; }
public NoteModel()
{
}
public NoteModel(Guid parentId)
{
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 NoteModel(Note obj)
{
if (obj == null)
return null;
var model = new NoteModel
{
DateAdded = DateTime.Now,
Id = obj.Id,
ParentId = obj.ParentId,
Title = obj.Title,
Details = obj.NoteDetail,
Author = new EnVisageEntities().AspNetUsers.FirstOrDefault(x => x.Id == obj.UserId.Value.ToString()),
NoteType = obj.NoteType ?? -1,
DomainId = obj.DomainId
};
model.CreatedBy = (model.Author != null) ? model.Author.UserName : String.Empty;
model.TrimStringProperties();
return model;
}
/// <summary>
/// Copies data from model to DAL object.
/// </summary>
/// <param name="dbObj">A target DAL object.</param>
public void CopyTo(Note dbObj)
{
if (dbObj == null)
throw new ArgumentNullException();
if (dbObj.Id == Guid.Empty)
dbObj.Id = Id;
dbObj.ParentId = ParentId;
dbObj.Title = Title;
dbObj.DateAdded = DateTime.Now;
dbObj.NoteDetail = Details;
dbObj.NoteType = NoteType;
dbObj.DomainId = DomainId;
dbObj.UserId = Author != null ? new Guid(Author.Id) : new Guid(HttpContext.Current.User.Identity.GetID());
}
}
}