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

78 lines
2.4 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 AttachmentModel : IBaseModel<Attachment>
{
public Guid Id { get; set; }
public string Name { get; set; }
public int Size { get; set; }
public string ContentType { get; set; }
public bool IsNew { get; set; }
public bool IsLink { get; set; }
public string Link { get; set; }
/// <summary>
/// Casts a <see cref="Attachment"/> obect to the object of type <see cref="AttachmentModel"/>.
/// </summary>
/// <param name="obj">An <see cref="Attachment"/> object</param>
/// <returns>A <see cref="AttachmentModel"/> object filled with data from db</returns>
public static explicit operator AttachmentModel(Attachment obj)
{
if (obj == null)
return null;
bool islink = false;
if (!string.IsNullOrEmpty(obj.Link)) {
islink = true;
}
var model = new AttachmentModel
{
Id = obj.Id,
Name = obj.SourceFileName,
Size = obj.FileSize.HasValue ? obj.FileSize.Value : 0,
ContentType = obj.ContentType,
IsNew = false,
IsLink = islink,
Link = obj.Link
};
model.TrimStringProperties();
return model;
}
/// <summary>
/// Copies data from model to DAL object.
/// </summary>
/// <param name="dbObj">A target DAL object</param>
public void CopyTo(Attachment dbObj)
{
if (dbObj == null)
throw new ArgumentNullException();
dbObj.Id = Id;
dbObj.SourceFileName = Name;
dbObj.ContentType = ContentType;
dbObj.FileSize = Size;
dbObj.Link = (Link !=null?Link.ToString():string.Empty);
}
public AttachmentModel Copy()
{
return new AttachmentModel()
{
Id = Guid.NewGuid(),
IsNew = true,
Link = this.Link,
IsLink = this.IsLink,
ContentType = this.ContentType,
Name = this.Name,
Size = this.Size
};
}
}
}