EnVisageOnline/Beta/Source/EnVisage/Models/ViewModel.cs

136 lines
4.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 ViewModel : IBaseModel<View>
{
public Guid Id { get; set; }
[Required]
[MaxLength(100)]
[Display(Name="Name")]
public string Name { get; set; }
[Display(Name = "Number of Teams")]
public int TeamCount { get; set; }
public List<CompanyModel> Companies
{
get
{
List<CompanyModel> result = new List<CompanyModel>();
EnVisageEntities DbContext = new EnVisageEntities();
var companies = (from c in DbContext.Companies where c.ParentCompanyId != null select c).ToList();
foreach (var company in companies)
result.Add((CompanyModel)company);
return result.OrderBy(x => x.ParentCompanyName).ToList();
}
set { }
}
public List<CreditDepartmentModel> CostCenters
{
get
{
List<CreditDepartmentModel> result = new List<CreditDepartmentModel>();
EnVisageEntities DbContext = new EnVisageEntities();
var costCenters = (from c in DbContext.CreditDepartments select c).ToList();
foreach (var costCenter in costCenters)
result.Add((CreditDepartmentModel)costCenter);
return result.OrderBy(x => x.Name).ToList();
}
set { }
}
public List<TeamModel> ViewTeams
{
get
{
var dbContext = new EnVisageEntities();
var teams = (from c in dbContext.Teams
join tv in dbContext.Team2View on c.Id equals tv.TeamId
where tv.ViewId == Id
select c).ToList();
var result = teams.Select(team => (TeamModel)team).ToList();
return result.OrderBy(x => x.Name).ToList();
}
set { }
}
public List<TeamModel> AllTeams
{
get
{
var dbContext = new EnVisageEntities();
var teams = (from c in dbContext.Teams select c).ToList();
var result = teams.Select(team => (TeamModel)team).ToList();
return result.OrderBy(x => x.Name).ToList();
}
set { }
}
public IList<Guid> CompanyId { get; set; }
public IList<Guid> UserId { get; set; }
public IList<Guid> CostCenterId { get; set; }
public IList<string> Company { get; set; }
public IList<string> User { get; set; }
public IList<string> CostCenter { get; set; }
public IList<Guid> TeamId { get; set; }
public IEnumerable<AspNetUser> Users
{
get
{
EnVisageEntities DbContext = new EnVisageEntities();
return DbContext.AspNetUsers;
}
}
/// <summary>
/// Casts a <see cref="View"/> obect to the object of type <see cref="ViewModel"/>.
/// </summary>
/// <param name="obj">A <see cref="View"/> object.</param>
/// <returns>A <see cref="GLAccountModel"/> object filled with data from db.</returns>
public static explicit operator ViewModel(View obj)
{
if (obj == null)
return null;
var model = new ViewModel
{
Id = obj.Id,
Name = obj.Name,
TeamCount = obj.Team2View.Count
};
model.TrimStringProperties();
return model;
}
/// <summary>
/// Copies data from model to DAL object.
/// </summary>
/// <param name="dbObj">A target DAL object.</param>
public void CopyTo(View dbObj)
{
if (dbObj == null)
throw new ArgumentNullException();
dbObj.Name = Name;
}
}
/// <summary>
/// An UI representation of client to be displayed as list items
/// </summary>
public class ViewListModel
{
public Guid Id { get; set; }
public string Name { get; set; }
public int TeamCount { get; set; }
public IList<string> Companies { get; set; }
}
}